ArrayList的综合案例-模仿外卖系统中的商家系统

要求:

测试类:

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("您输入的命令不存在!,请重新输入");
          }
      }

    }
}
相关推荐
Chester_19996 小时前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
EXI-小洲8 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员8 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导8 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan8 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
m0_695251498 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
2601_961901709 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
何以解忧,唯有..9 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
大衛說10 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习
QQ_216962909610 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战
java·spring boot·后端