菜品上架与展示
1、功能简介
1、实现菜品的上架
2、实现菜品的展示2、代码展示
2.1 food类
package com.ham.pkg.ArryList;
public class Food {
private String name;
private double price;
private String desc;
public Food(String name, double price, String desc) {
this.name = name;
this.price = price;
this.desc = desc;
}
//无参构造器
public Food() {
}
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;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
2.1 foodOpertor类
package com.ham.pkg.ArryList;
import java.util.ArrayList;
import java.util.Scanner;
public class FoodOpertor {
//菜品操作类
//定义一个ArryList存储菜品信息
ArrayList<Food> foods=new ArrayList<>();
//添加菜品
public void addfood(){
Food f=new Food(); //创建菜品类
Scanner sc =new Scanner(System.in);
System.out.println("输入菜品名称");
String name=sc.next();
f.setName(name);
System.out.println("输入菜品价格");
Double price=sc.nextDouble();
f.setPrice(price);
System.out.println("输入菜品描述");
String desc=sc.next();
f.setDesc(desc);
foods.add(f); //将数据添加到list中去
System.out.println("上架成功");
}
//浏览菜品
public void LookFood(){
if (foods.size()==0){
return;
}
for (int i=0;i<foods.size();i++){
Food f=foods.get(i);
System.out.println(f.getName());
System.out.println(f.getPrice());
System.out.println(f.getDesc());
}
}
//负责展示菜名
public void start(){
while (true) {
System.out.println("请选择功能");
System.out.println("1.上架菜品");
System.out.println("2.展示菜品");
System.out.println("3.退出");
Scanner sc=new Scanner(System.in);
System.out.println("请选择");
String index =sc.next();
switch (index){
case "1":
addfood();
break;
case "2":
LookFood();
break;
case "3":
return;
default:
System.out.println("输入不合法");
}
}
}
}
2.1 foodCase类
package com.ham.pkg.ArryList;
public class ArryListCase1 {
public static void main(String[] args) {
FoodOpertor opertor =new FoodOpertor();
opertor.start();
}
}