Java 【食品库存管理】
一.食品类
//食品类 class Food{ private String coding; //编码 private String name; //名称 private double price; //价格 Food(String coding,String name,double price){ this.coding = coding; this.name = name; this.price = price; } public String getCoding(){ return coding; } public void setCoding(String coding){ this.coding = coding; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public double getPrice(){ return price; } public void setPrice(double price){ this.price = price; } }
二.管理类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//管理类 class Manage{ private ArrayList<Food> obj=null; Manage(){ //构造方法 obj = new ArrayList<Food>(); } public void addFood(Food food){ //普通方法 obj.add(food); System.out.println("添加食品成功!"); } public void spInfo(String coding){ int i; for(i=0;i<obj.size();i++){ Food sp = (Food)obj.get(i); if(sp.getCoding().equals(coding)){ System.out.println("该食品的信息:"); System.out.println("食品名称:"+sp.getName()); System.out.println("食品编号:"+coding); System.out.println("食品价格:"+sp.getPrice()); break; } } if(i == obj.size()){ System.out.println("对不起,没有此商品!"); } } public void foodFix(String coding,double price){ int i; for(i=0;i<obj.size();i++){ Food sp = (Food)obj.get(i); if(sp.getCoding().equals(coding)){ sp.setPrice(price); System.out.println("修改食品价格成功!"); break; } } if(i == obj.size()){ System.out.println("没有找到相应的商品,修改失败!"); } } public void foodDel(String coding){ int i; if(obj.size()==0){ System.out.println("没有库存,请库存!"); } for(i=0;i<obj.size();i++){ Food sp = (Food)obj.get(i); if(sp.getCoding().equals(coding)){ obj.remove(i); System.out.println("删除食品成功!"); break; } } if((i==obj.size()) && (obj.size()!=0)){ System.out.println("对不起,没有此商品!"); } } }
三.逻辑实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.*; import java.io.*; public class Jar{ public static void main(String[] args) throws Exception{ Manage foods = new Manage(); //将键盘作为数据流接收 //BufferedReader data = new BufferedReader(new InputStreamReader(System.in)); Scanner data = new Scanner(System.in); while(true){ System.out.println("请按照提示选择:"); System.out.println("添加食品:1"); System.out.println("查找食品:2"); System.out.println("修改食品价格:3"); System.out.println("删除食品:4"); System.out.println("退出:0"); String str = data.next(); //读取一行 if(str.equals("1")){ System.out.println("请输入商品编号"); String coding = data.next(); System.out.println("请输入商品名称"); String name = data.next(); System.out.println("请输入商品价格"); double price = data.nextDouble(); Food food = new Food(coding,name,price); foods.addFood(food); } else if(str.equals("2")){ System.out.println("请输入商品编号"); String coding = data.next(); foods.spInfo(coding); } else if(str.equals("3")){ System.out.println("请输入商品编号"); String coding = data.next(); System.out.println("请输入商品价格"); double price = data.nextDouble(); foods.foodFix(coding,price); } else if(str.equals("4")){ System.out.println("请输入商品编号"); String coding = data.next(); foods.foodDel(coding); } else if(str.equals("0")){ System.out.println("感谢使用,下次再见!"); System.exit(0); }else{ System.out.println("输入有误"); } } } }
M54