点餐系统(Java小项目)
1.项目介绍
该项目能够实现简单的餐厅点菜,查看已点菜品,删除菜品,结账等基本操作。 顾客进行点餐时首先进入主菜单中,在主菜单中可以选择相应的操作,用户可以选择每项操作前的序号即可进入到相应操作的子菜单,在子菜单中也可以选择相应的操作。
2.项目设计规划
该项目的实现主要依靠三个类来实现:
1.Dish类,主要作用用于封装菜品,以及初始化后的菜单
2.DishApp(程序入口方法)主类,点菜的主程序
3.DishInterface类,定义多个方法,显示各种操作对应的菜单
3.运行效果
4.代码如下
4.1 Dish类
package com.hao;
import java.util.ArrayList;
import java.util.List;
public class Dish{
/**
* 编号
*/
private int id;
/**
* 菜名
*/
private String name;
/**
* 价格
*/
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 Dish() {
}
public Dish(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
/**
* 初始化菜单
*
* @return 初始化后的菜单
*/
public List<Dish> initMenu() {
List<Dish> dishList = new ArrayList<>();
dishList.add(new Dish(1, "爆火牛肉", 58));
dishList.add(new Dish(2, "鲜甲鱼汤", 68));
dishList.add(new Dish(3, "铁板卤蛋", 38));
dishList.add(new Dish(4, "泡椒炒肉", 48));
dishList.add(new Dish(5, "麻婆豆腐", 18));
dishList.add(new Dish(6, "干煸豆角", 28));
dishList.add(new Dish(7, "清炒时蔬", 25));
dishList.add(new Dish(8, "凉拌黄瓜", 10));
dishList.add(new Dish(9, "胡椒皮蛋", 15));
dishList.add(new Dish(10, "红烧茄子", 20));
return dishList;
}
/**
* 展示菜单
*
* @param dishList 菜品列表
*/
public void showMenu(List<Dish> dishList) {
System.out.println("------------本店菜单------------");
System.out.println("序号\t\t菜名\t\t\t\t单价");
for (int i = 0; i < dishList.size(); i++) {
System.out.format("%d\t\t%s\t\t\t%.2f\n", dishList.get(i).getId(), dishList.get(i).getName(), dishList.get(i).getPrice());
}
System.out.println("---------------------------");
}
/**
* 删除对应菜品
*
* @param id 菜品对应序号
* @param dishList 菜品列表
* @param orderedList 已点菜品列表
*/
public void deleteDish(int id, List<Dish> dishList, List<Dish> orderedList) {
if (!orderedList.isEmpty()) {
orderedList.remove(dishList.get(id - 1));
}
}
/**
* 查看已点的菜
*/
public void showOrderedMenu(List<Dish> orderedList) {
if (orderedList.isEmpty()) {
System.out.println("当前您还未点餐,请重新输入选择");
} else {
System.out.println("当前您点的菜品如下");
for (int i = 0; i < orderedList.size(); i++) {
System.out.println(orderedList.get(i).getName());
}
}
}
/**
* 结账
*/
public void checkout(List<Dish> orderedList) {
double money = 0.0d;
if (orderedList.isEmpty()) {
System.out.println("当前您还未点餐,请重新输入选择");
} else {
System.out.println("请稍等,正在结算中……");
for (int i = 0; i < orderedList.size(); i++) {
money += orderedList.get(i).getPrice();
}
System.out.format("你此次总共消费:¥ %.2f\n", money);
}
}
}
4.2 DishApp类
package com.hao;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DishApp {
public static void main(String[] args) {
Dish dish = new Dish();
// 初始化菜单
List<Dish> dishList = dish.initMenu();
Scanner scanner = new Scanner(System.in);
List<Dish> orderedList = new ArrayList<>();
while (true) {
System.out.println("----------欢迎使用点餐系统--------");
System.out.println("----------【1】点菜--------------");
System.out.println("----------【2】查看已点菜品-------");
System.out.println("----------【3】删除菜品-----------");
System.out.println("----------【4】结账--------------");
System.out.println("----------【0】返回上一级/退出-----");
System.out.println("输入你的选择");
String choice = scanner.next();
switch (choice) {
case "1":
while (true) {
dish.showMenu(dishList);
System.out.println("请输入菜品序号进行点菜,输入 0 则返回上一级菜单");
int id = scanner.nextInt();
if (id == 0) {
break;
}
System.out.println("输入菜品序号:" + id);
System.out.println("你点的菜是:" + dishList.get(id - 1).getName());
// 把已点的菜加入已点菜单
orderedList.add(dishList.get(id - 1));
}
break;
case "2":
dish.showOrderedMenu(orderedList);
break;
case "3":
if (orderedList.isEmpty()) {
System.out.println("当前还未点菜,请重新输入你的选择");
} else {
System.out.println("输入要删除菜品的序号");
int id = scanner.nextInt();
dish.deleteDish(id, dishList, orderedList);
}
break;
case "4":
dish.checkout(orderedList);
break;
case "0":
System.exit(0);
default:
break;
}
}
}
}
4.3 DishInterface类
package com.hao;
import java.util.List;
public interface DishInterface {
/**
* 初始化菜单
*
* @return 返回餐厅当前所提供的菜品列表
*/
public List<Dish> initMenu();
/**
* 显示菜单
*
* @param dishList 菜品列表
*/
public void showMenu(List<Dish> dishList);
/**
* 显示已经点过的菜品
*
* @param orderedList 当前已点菜品
*/
public void showOrderedMenu(List<Dish> orderedList);
/**
* 移除菜品
*
* @param id 菜品对应序号
* @param dishList 菜品列表
* @param orderedList 已点菜品列表
*/
public void deleteDish(int id, List<Dish> dishList, List<Dish> orderedList);
/**
* 结账
*
* @param orderedList
*/
public void checkout(List<Dish> orderedList);
}