要求:
测试类:
java
package Y;
public class 模仿外卖系统中的商家系统 {
public static void main(String[] args) {
//1、设计一个菜品类Food
//2、设计一个菜品操作类,FoodOperator 负责创建菜品对象,封装菜品数据
FoodOperator e = new FoodOperator();
e.start();
}
}
菜品类
java
package Y;
/*
* 菜品类
* */
public class Food {
private String foodName;
private double foodPrice;
private String desc;
public Food() {
}
public Food(String foodName, double foodPrice, String desc) {
this.foodName = foodName;
this.foodPrice = foodPrice;
this.desc = desc;
}
public String getFoodName() {
return foodName;
}
public void setFoodName(String foodName) {
this.foodName = foodName;
}
public double getFoodPrice() {
return foodPrice;
}
public void setFoodPrice(double foodPrice) {
this.foodPrice = foodPrice;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
菜品操作类:负责上架和浏览功能的实现
java
package Y;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 菜品操作类:负责上架和浏览功能的实现
* */
public class FoodOperator {
//1、定义一个ArrayList集合对象,负责存储菜品对象信息
private ArrayList<Food> foods = new ArrayList<>() ;
//foods = []
//2、开发更能,上架菜品功能
public void addFood(){
//3、创建一个菜品对象,封装商家的菜品信息
Food f = new Food();
//4、录入菜品信息进去
Scanner sc = new Scanner(System.in);
System.out.println("请您输入该菜品名称");
String name = sc.nextLine();
f.setFoodName(name);
System.out.println("请您输入该菜品价格");
double price = sc.nextDouble();
f.setFoodPrice(price);
System.out.println("请您输入该菜品描述");
String desc = sc.next();
f.setDesc(desc);
//5、把菜品对象存入到集合中去
foods.add(f);
System.out.println("上架成功");
}
//6、展示菜品
public void showFoods(){
if (foods.size()== 0){
System.out.println("什么菜品您都没有,请去上架");
return;//拦截,底层的for不进行
}
for(int i = 0; i<foods.size(); i++){
//把菜品对象交给Food类的变量记一下
Food f = foods.get(i);//foods.get(i)是菜品对象
System.out.println("菜品名:"+f.getFoodName());
System.out.println("价格:"+f.getFoodPrice());
System.out.println("详细信息:"+f.getDesc());
System.out.println("--------------");
}
}
//负责展示操作界面
public void start() {
while (true){
System.out.println("请选择功能");
System.out.println("1.上架菜品");
System.out.println("2.展示菜品");
System.out.println("3.退出");
Scanner sc1 = new Scanner(System.in);
System.out.println("请输入您的选择");
int choice = sc1.nextInt();
switch(choice){
case 1:
addFood();
break;
case 2:
showFoods();
break;
case 3:
System.out.println("欢迎下次光顾!");
return;//return表示结束这个方法
default:
System.out.println("您输入的命令不存在!,请重新输入");
}
}
}
}