MikeCat商城

这是最后的效果:

java做小成果

  • MikeCat商城
  • 一、建Goods类
  • MikeCat商城大概
  • 1、先是主方法
    • 2、一系列方法
      • (1)打印出商品
      • (2) 添加
      • (3)更新
      • (4)删除
      • (5)防止id重复
  • 总结

一、建Goods类

/* MikeCat商城 * */
public class Goods { 

    private String id; // 序号

    private String brand; // 品牌名

    private String name; // 物品名

    private String price; //价格

    private String term; // 保质期,时间

    private String birth; // 生产日期

    public Goods() { 

    }
// 由于需要设置信息,所以以下代码是必要的
    public Goods(String id, String brand, String name, String price, String term, String birth) { 
        this.id = id;
        this.brand = brand;
        this.name = name;
        this.price = price;
        this.term = term;
        this.birth = birth;
    }
    public String getId() { 
        return id;
    }

    public void setId(String id) { 
        this.id = id;
    }

    public String getBrand() { 
        return brand;
    }

    public void setBrand(String brand) { 
        this.brand = brand;
    }

    public String getName() { 
        return name;
    }

    public void setName(String name) { 
        this.name = name;
    }

    public String getPrice() { 
        return price;
    }

    public void setPrice(String price) { 
        this.price = price;
    }

    public String getTerm() { 
        return term;
    }

    public void setTerm(String term) { 
        this.term = term;
    }

    public String getBirth() { 
        return birth;
    }

    public void setBirth(String birth) { 
        this.birth = birth;
    }
}

MikeCat商城大概

1、先是主方法

public static void main(String[] args) throws IOException { 
        ArrayList<Goods> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true){ 
            System.out.println("---------------欢迎来到MikeCat商城----------------");
            System.out.println("--在这里你可以买到你想要的一切,也可以将你的物品按照你心仪的价格出售--");
            System.out.println("1.添加商品");
            System.out.println("2.更新商品");
            System.out.println("3.处理过期");
            System.out.println("4.查看所有商品");
            System.out.println("5.退出");
            System.out.println("联系电话:124 8941 8121*");
            System.out.println("请输入你的选择:");

            int choose = sc.nextInt();
            switch (choose){ 
                case 1:

                    add(list);
                    System.out.println("---------------------------------------");
                    break;

                case 2:

                    upGoods(list);
                    System.out.println("---------------------------------------");
                    break;

                case 3:

                    deleteGoods(list);
                    System.out.println("---------------------------------------");
                    break;

                case 4:

                    print(list);
                    System.out.println("---------------------------------------");
                    break;

                case 5:

                    System.out.println("感谢使用!");
                    System.out.println("---------------------------------------");

                    System.exit(0);
                default :
                    System.out.println("操作失败,请重试!");
                    System.out.println("---------------------------------------");

                    break;
            }
        }
    }

throws IOException 因为下面的方法中有用到异常的方法,所以就先抛出了。(不抛出也可以 因为之后idea会提醒的)

2、一系列方法

(1)打印出商品

public static void print(ArrayList<Goods> list) { 
        Scanner sc = new Scanner(System.in);
        System.out.println("1.查看刚添加的商品。 2.返回");
        System.out.println("请选择:");
        int choose = sc.nextInt();
        switch (choose){ 
            case 1:
                if (list.size() == 0){ 
                    System.out.println("请添加商品!");
                    return;
                }
                System.out.println("序号\t\t品牌\t\t\t\t物品\t\t\t\t价格\t\t\t\t保质期\t\t\t\t生产期");//(id,brand,name,price,term,birth);
                for (int i = 0; i < list.size(); i++) { 
                    Goods g = list.get(i);
                    System.out.println(g.getId() + "\t\t" + g.getBrand()+ "\t\t\t\t" + g.getName() + "\t\t\t\t" + g.getPrice() + "\t\t\t\t" + g.getTerm() + "\t\t\t\t" + g.getBirth());
                }
                break;

            case 2:
               return;

            default:
                System.out.println("输入错误!,将返回首页!");
                break;
        }
    }

(2) 添加

 public static void add(ArrayList<Goods> list)throws IOException { 

        Scanner sc = new Scanner(System.in);
        String id;

        while (true){ 
            System.out.println("请输入商品序号:");
            id = sc.nextLine();
            boolean result = idUsed(list,id);
            if (!result){ 
                break;
            }
            System.out.println("此位置已有物品,请重选序号");

        }

        System.out.println("请输入品牌:");
        String brand = sc.nextLine();
        System.out.println("请输入物品名:");
        String name = sc.nextLine();

        System.out.println("请输入价格(元):");
        String price = sc.nextLine();
        System.out.println("请输入保质期时间(年):");
        String term = sc.nextLine();
        System.out.println("请输入生产日期(xxxx-xx-xx):");
        String birth = sc.nextLine();
        Goods g = new Goods(id,brand,name,price,term,birth);
        list.add(g);
        FileWriter bw = new FileWriter("D:\\MikeCat.txt",true);//这里的文件看自己的喜好,添加到需要的位置。 加上 true 防止以前内容被删除。

        for (Goods goods : list) { 
            bw.write(goods.getId() + "\t\t\t\t\t\t" + goods.getBrand()+ "\t\t\t\t\t" + goods.getName() + "\t\t\t\t\t" + goods.getPrice() + "\t\t\t\t\t\t\t" + goods.getTerm() + "\t\t\t\t\t\t" + goods.getBirth()+"\r\t");
            // \t 按照自己的喜好添加。因为这只是为了打印出来好看
        }

        bw.flush();
        bw.close();//记得关闭,否则商品不可被添加入MikeCat商城

        System.out.println("申请成功!");
        System.out.println("经过管理员审核即可添加商品。");

    }

在添加商品的时候想了很多,想要打印,添加到表格中。由于自己的技术问题,只能换种方式。
添加到 .txt 中,这个还是学过些的。
奥对了,记得抛出异常。(ALT + INS)

(3)更新

public static void upGoods(ArrayList<Goods> list) throws IOException { 

        Scanner sc = new Scanner(System.in);
        System.out.println("输入要替换的商品序号:");
        String id = sc.nextLine();
        boolean result = idUsed(list,id);
        if (!result){ 
            System.out.println("输入错误,请重试:");
            return;
        }
        System.out.println("请输入新品牌:");
        String b = sc.nextLine();
        System.out.println("请输入新物品名:");
        String n = sc.nextLine();
        System.out.println("请输入价格:");
        String p = sc.nextLine();
        System.out.println("请输入保质期时间:");
        String t = sc.nextLine();
        System.out.println("请输入生产日期:");
        String bi= sc.nextLine();
        Goods g = new Goods(id,b,n,p,t,bi);

        for (int i = 0; i < list.size(); i++) { 
            Goods older = list.get(i);
            if (id.equals(older.getId())){ 
                list.set(i,g);
                System.out.println("替换成功!");
                break;
            }
        }

        FileWriter h = new FileWriter("D:\\JAVA\\project05\\MikeCat.txt",true);

        for (Goods goods : list) { 
            h.write("*替换* :"+ goods.getId() + "\t\t\t\t" + goods.getBrand()+ "\t\t\t\t\t" + goods.getName() + "\t\t\t\t\t" + goods.getPrice() + "\t\t\t\t\t\t\t" + goods.getTerm() + "\t\t\t\t\t\t" + goods.getBirth()+"\r\t");
        }

        h.flush();
        h.close();

        System.out.println("申请成功!");
        System.out.println("经过管理员审核即可替换商品。");


    }

(4)删除

 public static void deleteGoods(ArrayList<Goods> list) throws IOException { 
        Scanner c = new Scanner(System.in);
        System.out.println("是否运用管理员身份?(1.是。2.否。3.测试功能)");

        int choose = c.nextInt();

        switch (choose){ 
            case 1:
                /* try { RandomAccessFile raf = new RandomAccessFile("D:\\JAVA\\project05\\MikeCat.txt","rw"); String line; //逐行读取文件,判断是否包含需要替换的内容; while (null != (line = raf.readLine())){ if (line.contains(id)){ //查找到需要替换的内容后,使用split方法,将该行分隔; String[] split = line.split(id); raf.seek(split[0].length()); //先写入分隔前部分的内容,再写入替换的内容,最后写入分隔后部分的内容。 raf.writeBytes("已撤回"); raf.writeBytes(split[1]); } } raf.close(); break; }catch (IOException e){ e.printStackTrace(); }*/

                FileWriter fw = new FileWriter("D:\\JAVA\\project05\\MikeCat.txt",true);
                System.out.println("请输入商品 *序号,品牌*(核对后执行):");
                String id = c.next();
                System.out.println();

                fw.write("撤销 " +id +"\r\t");

                System.out.println("撤销信息已通知! 将返回首页");

                fw.flush();
                fw.close();
                break;
            case 2:
                break;

            case 3:
                System.out.println("请输入密码(一次机会):");
                int mm = 123456;
                int m = c.nextInt();
                if (mm == m){ 

                    if (list.isEmpty()){ 
                        System.out.println("当前无商品,请添加:");
                        return;
                    }
                    Scanner sc = new Scanner(System.in);
                    System.out.println("请输入要处理的商品序号:");
                    String delete = sc.nextLine();
                    if (!idUsed(list,delete)){ 
                        System.out.println("查询失败,请重试!");
                        return;
                    }
                    for (int i = 0; i < list.size(); i++) { 
                        Goods g = list.get(i);
                        if (g.getId().equals(delete)){ 
                            list.remove(i);
                            System.out.println("已处理!" );
                            return;
                        }
                    }
                    System.out.println("输入错误! 将返回首界面。");
                    break;
                }else { 
                    System.out.println("输入错误! 将返回首界面。");
                    break;
                }
            default:
                System.out.println("输入错误! 将返回首界面。");
                break;

        }
    }

中间的被注释了的是原本想用的方法,但是我想用有把握的,所以那个高效的方法就先被替换了

(5)防止id重复

 public static boolean idUsed(ArrayList<Goods> list,String id){ 
        for (int i = 0; i < list.size(); i++) { 
            Goods g = list.get(i);
            if (g.getId().equals(id)){ 
                return true;
            }
        }
        return false;

    }

总结

内容并没有想太多,只是自己想做一下。
弄了这个发现,自己做的在运行的时候总会有些想不到的bug。
结果不是很满意。
希望在以后学会一些知识后,再次做一个MikeCat商城。

  • 作者:麦克猫Cat
  • 本文版权归作者和CSDN共有,欢迎交流

本文地址:https://blog.csdn.net/weixin_52309367/article/details/114274135