首先创建一个Food实体类,用来存储菜品信息;然后创建一个FoodManager类,来实现我们这个简单后台管理系统的”增加“,“查看”,“退出系统”,三个简单的操作:
Food实体类:
package ArrayDemo;
public class Food {
private String name;
private Double originalPrice;
private Double specialPrice;
public Food() {
}
public Food(String name, Double originalPrice, Double specialPrice, String info) {
this.name = name;
this.originalPrice = originalPrice;
this.specialPrice = specialPrice;
this.info = info;
}
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;
}
private String info;
}
FoodManager类:
package ArrayDemo;
import java.util.ArrayList;
import java.util.Scanner;
public class FoodManager {
private ArrayList<Food> foods=new ArrayList<>();
private Scanner sc=new Scanner(System.in);
public void add(){
System.out.println("请添加菜品信息:");
Food food=new Food();
System.out.println("请输入菜品的名称:");
String name=sc.next();
food.setName(name);
System.out.println("请输入菜品的原价:");
Double price=sc.nextDouble();
food.setOriginalPrice(price);
System.out.println("请输入菜品的优惠价:");
Double specialPrice=sc.nextDouble();
food.setSpecialPrice(specialPrice);
System.out.println("请输入菜品的其他信息:");
String info=sc.next();
food.setInfo(info);
System.out.println("---------------------------");
foods.add(food);
System.out.println("恭喜你上架成功---------------");
}
public void printFoods(){
if (!foods.isEmpty()) {
for (int i = 0; i < foods.size(); i++) {
System.out.println("菜品名称:"+foods.get(i).getName());
System.out.println("菜品原价:"+foods.get(i).getOriginalPrice());
System.out.println("菜品优惠价:"+foods.get(i).getSpecialPrice());
System.out.println("菜品的其他信息:"+foods.get(i).getInfo());
}
}else {
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)");
String command=sc.next();
switch (command){
case "add":
add();
break;
case "query":
printFoods();
break;
case "exit":
return;
default:
System.out.println("您输入的操做有问题,请重新输入——————");
}
}
}
}
测试类:
package ArrayDemo;
public class ArrayDemo {
public static void main(String[] args) {
FoodManager foodManager=new FoodManager();
foodManager.start();
}
}