创建Fruit.java文件:
package com.fruit; //定义水果属性类 public class Fruit { int ID; String name; String price; int num; double money; }
创建Fruitshop.java文件:
package com.fruit; import java.util.ArrayList; import java.util.Scanner; public class Fruitshop { public static void main(String[] args) { //创建集合 ArrayList<Fruit> array = new ArrayList<Fruit>(); //调用方法,在集合里添加数据 add(array); //进入循环 while(true){ //调用主菜单 mainMenu(); //获取输入数字 int choose = chooseFunction(); switch(choose){ //调用展示商品信息方法 case 1:showFruit(array); break; //调用添加商品信息方法 case 2:addFruit(array); break; //调用删除商品信息方法 case 3:delFruit(array); break; //调用修改商品信息方法 case 4:updateFruit(array); break; //退出循环 case 5: return; //输入超出范围数字显示“按键无效” default: System.out.println("按键无效"); break; } } } //主菜单 public static void mainMenu() { System.out.println(); System.out.println("=====================欢迎光临xx水果超市========================="); System.out.println("1 查看清单 2:添加水果 3:删除水果 4:修改水果 5:退出"); System.out.println("请选择功能序号"); } //商品信息 public static void showFruit(ArrayList<Fruit> array) { System.out.println(); System.out.println("========欢迎光临xx水果超市============"); System.out.println("编号 水果名 水果单价"); for(int i = 0;i<array.size();i++){ Fruit goods = array.get(i); System.out.println(goods.ID+" "+goods.name+" "+goods.price+" "); } } //添加商品 public static void addFruit(ArrayList<Fruit> array) { System.out.println("您选择的是添加功能"); System.out.println("请输入水果编号"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); System.out.println("请输入水果名"); String text = sc.next(); System.out.println("请输入价格"); String price = sc.next(); Fruit goods = new Fruit(); goods.ID = num; goods.name= text; goods.price = price; array.add(goods); System.out.println("添加成功"); } //删除商品 public static void delFruit(ArrayList<Fruit> array) { System.out.println("您选择的是删除功能"); System.out.println("请输入要删除水果的编号"); Scanner sc = new Scanner(System.in); int ID = sc.nextInt(); for(int i = 0;i < array.size();i++){ Fruit goods = array.get(i); if (goods.ID==ID){ array.remove(i); System.out.println("删除成功"); return; } } System.out.println("您输入的水果编号不存在"); } //修改商品 public static void updateFruit(ArrayList<Fruit> array){ System.out.println("您选择的是修改功能"); System.out.println("请输入您要修改的水果编号"); Scanner sc = new Scanner(System.in); int ID = sc.nextInt(); for(int i = 0;i<array.size();i++){ Fruit goods = array.get(i); if(goods.ID==ID){ System.out.println("请输入新的水果编号"); goods.ID= sc.nextInt(); System.out.println("请输入新的水果名字"); String name =sc.next(); goods.name = name; System.out.println("请输入新的价格"); goods.price= sc.next(); return; } } System.out.println("编号不存在"); } //返回循环 public static int chooseFunction(){ Scanner sc = new Scanner(System.in); return sc.nextInt(); } //添加初始商品信息 public static void add(ArrayList<Fruit> array) { Fruit s = new Fruit(); s.ID = 01; s.name = "苹果"; s.price = "4.8元/千克"; array.add(s); Fruit s1 = new Fruit(); s1.ID = 02; s1.name = "红提"; s1.price = "4.9元/千克"; array.add(s1); Fruit s2 = new Fruit(); s2.ID = 03; s2.name = "金钱橘"; s2.price = "4.1元/千克"; array.add(s2); Fruit s3 = new Fruit(); s3.ID = 04; s3.name = "皇冠梨"; s3.price = "5.5元/千克"; array.add(s3); Fruit s4 = new Fruit(); s4.ID = 05; s4.name = "香蕉"; s4.price = "4.3元/千克"; array.add(s4); Fruit s5 = new Fruit(); s5.ID = 06; s5.name = "火龙果"; s5.price = "6.8元/千克"; array.add(s5); } }