package com.ArrayList; //菜品管理 public class Demo2 { public static void main(String[] args) { Demo22 manager=new Demo22(); manager.start(); } }
package com.ArrayList; //构造器 public class Demo21 { private String name; private double originalPrice; private double specialPrice; private String info; //无参构造器 public Demo21() { } //有参构造器 public Demo21(String name, double originalPrice, double specialPrice, String info) { this.name = name; this.originalPrice = originalPrice; this.specialPrice = specialPrice; this.info = info; } //get和set public String getName() { return name; } public void setName(String name) { this.name = name; } public double getOriginalPrice() { return originalPrice; } public void setOriginalPrice(double originalPrice) { this.originalPrice = originalPrice; } public double getSpecialPrice() { return specialPrice; } public void setSpecialPrice(double specialPrice) { this.specialPrice = specialPrice; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } }
package com.ArrayList; import java.util.ArrayList; import java.util.Scanner; public class Demo22 { private ArrayList<Demo21> foods=new ArrayList<>(); private Scanner sc=new Scanner(System.in); //商家上架菜品 public void add(){ System.out.println("==菜品上架=="); //创建一个才能对象 Demo21 food=new Demo21(); System.out.println("请您输入上架菜品的名称:"); String name=sc.next(); food.setName(name); System.out.println("请您输入上架菜品的原价:"); double originalPrice=sc.nextDouble(); food.setOriginalPrice(originalPrice); System.out.println("请您输入上架菜品的优惠价:"); double specialPrice=sc.nextDouble(); food.setSpecialPrice(specialPrice); System.out.println("请您输入上架菜品的其他信息:"); String info=sc.next(); food.setInfo(info); //把菜品对象添加到集合容器中去 foods.add(food); System.out.println("恭喜您上架成功!"); } //菜品浏览功能 public void printALLFoods(){ System.out.println("当前菜品信息如下:"); for (int i = 0; i < foods.size(); i++) { Demo21 food=foods.get(i); System.out.println("菜品名称:"+food.getName()); System.out.println("菜品原价:"+food.getOriginalPrice()); System.out.println("菜品优惠价:"+food.getSpecialPrice()); System.out.println("其他描述信息:"+food.getInfo()); System.out.println("---------------------------------------------"); } } //专门负责展示系统界面 public void start(){ while (true) { System.out.println("欢迎进入商家后台管理系统"); System.out.println("1.上架菜品(add)"); System.out.println("2.浏览菜品(query)"); System.out.println("3.退出系统(exit)"); System.out.println("请您选择操作指令:"); String command=sc.next(); switch (command){ case "add": add(); break; case "query" : printALLFoods(); break; case "exit": return;//结束当前方法 default: System.out.println("您输入的操作命令有误"); } } } }
标签:String,管理,System,案例,菜品,println,public,out From: https://www.cnblogs.com/Karl-hut/p/17437955.html