首先 创建一个Food类
public class Food {
private String name;
private int price;
public Food() {//无参构造方法
}
public Food(String name, int price) { //有参构造方法
this.setName(name);
this.setPrice(price);
}
//设置getter/setter方法
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price; }
}
其次 创建一个Serve类 把订餐系统的功能设计成一个个方法以及设计 餐品数据的存储与打印功能
import java.util.Scanner;
public class Serve {
static String[] userx = new String[4];//可存储4个用户信息...
Food[] food = new Food[3];//存放食物的对象数组
String[] list = new String[4];//可存储4个用户的菜单
int[] praise = new int[3]; //点赞
int[] state = new int[]{0, 0, 0, 0}; //状态
String[] isSign = new String[]{"未签收", "已签收"};
int i = 0;
Scanner sc = new Scanner(System.in);
public void printFood() {//打印菜品
food[0] = new Food("蛋炒饭", 10);
food[1] = new Food("炒米粉", 10);
food[2] = new Food("炒刀削", 10);
System.out.println("序号\t菜名\t价格(元)");
for (int i = 0; i < food.length; i++) {
System.out.println((i + 1) + "\t" + food[i].getName() + "\t" + food[i].getPrice());
}
}
public void orderFood() { //订餐方法
if (list[list.length - 1] != null) {
System.out.println("您的餐袋已满,无法点餐!");
return;
} else if (list[i] == null) {
String all = " ";
int total = 0;
int xuhao;
do {
printFood();
System.out.println("4.退出");
System.out.println("************");
System.out.println("请选择餐品序号或退出:");
xuhao = sc.nextInt();
if (xuhao < 4 && xuhao > 0) {
System.out.println("请输入份数:");
int fenshu = sc.nextInt();
all += food[xuhao - 1].getName() + fenshu + "份" + " ";
total += fenshu * food[xuhao - 1].getPrice();
}
System.out.println("已加入菜单!");
} while (xuhao != 4);
list[i] = all;
System.out.println("总价:" + total + "元");
}
System.out.println("请输入你的姓名:");
String name = sc.next();
System.out.println("请输入你的电话:");
long tel = sc.nextLong();
System.out.println("请输入你的地址:");
String addr = sc.next();
System.out.print("请输入你的送餐时间 (8-22)整点送餐):");
int time = sc.nextInt();
if (time < 8 | time > 22) {
System.out.print("超出送餐时间,请重新输入:");
time = sc.nextInt();
}
//String xinXi = name + " " + "电话:" + tel + "\t" + "地址:" + addr + " " + "时间:" + time + "点";
userx[i] =name + " " + "电话:" + tel + "\t" + "地址:" + addr + " " + "时间:" + time + "点";;
System.out.println("订餐成功!");
i++;
}
public void show() { //查看订单
System.out.println("序号 \t餐品信息");
for (int i = 0; i < list.length; i++) {
if (list[i] != null) {
System.out.println((i + 1) + " \t" + list[i] + "\t状态:" + isSign[state[i]]);
System.out.println("订餐人信息:" + userx[i]);
}else {
break;
}
}
}
public void sign() { //签收订单
System.out.print("请输入你要签收的订单序号:");
int qian = sc.nextInt();
if (list[qian - 1] != null && state[qian - 1] == 0) {
state[qian - 1] = 1;
System.out.println("订单签收成功!");
} else if (list[qian - 1] != null && state[qian - 1] == 1){
System.out.println("订单已完成签收,请不要重复操作!");
}else {
System.out.println("订单不存在!");
}
}
public void delete() { //删除订单
System.out.print("请输入你要删除的订单序号:");
int shan = sc.nextInt();
if (list[shan - 1] != null && state[shan - 1] == 1) {
state[shan - 1] = 0;//0未签收 1已签收
for (int i = 0; i < list.length - 1; i++) {//s.length-1:指倒数第1个
list[i] = list[i + 1];
}
list[list.length - 1] = null;
System.out.println("订单删除成功!");
} else if (list[shan - 1] != null && state[shan - 1] == 0) {
System.out.println("订单未签收,无法删除!");
} else {
System.out.println("订单不存在");
}
}
public void praise() { //点赞
System.out.println("序号" + "\t" + "菜名" + "\t" + "点赞数");
for (int i = 0; i < food.length; i++) {
System.out.println((i + 1) + "\t" + food[i].getName() + "\t" + praise[i]);
}
System.out.print("请输入你要点赞的序号:");
int zan = sc.nextInt();
while(zan>3){
System.out.println("您的输入有误,请重新输入:");
zan = sc.nextInt();
}
praise[zan - 1]++;
System.out.println("点赞成功!");
}
}
最后 创建一个Test类进行测试 使用while循环+switch case贯穿之前设计的方法 然后根据实际情况自行完善,使代码更加贴近现实生活!
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Serve dc=new Serve();
int num=0;
System.out.println("*****欢迎使用“吃货联盟订餐系统”******");
while(num==0) {
printMenu();
System.out.print("请输入你要进行的操作:");
int caozuo=input.nextInt();
switch(caozuo) {
case 1:
System.out.println("****我要订餐****");
dc.orderFood();
break;
case 2:
System.out.println("****查看订单****");
dc.show();
break;
case 3:
System.out.println("****签收订单****");
dc.sign();
break;
case 4:
System.out.println("****删除订单****");
dc.delete();
break;
case 5:
System.out.println("****我要点赞****");
dc.praise();
break;
case 6:
System.out.println("****退出系统****");
num = 1;
System.out.println("退出系统,谢谢您的使用!");
break;
default:
break;
}
}
}
public static void printMenu() {
System.out.println("********************");
System.out.println("1、我要订餐");
System.out.println("2、查看订单");
System.out.println("3、签收订单");
System.out.println("4、删除订单");
System.out.println("5、我要点赞");
System.out.println("6、退出系统");
System.out.println("********************");
}
}
初次发帖,还望大家多多关照。
代码不足之处希望互相讨论交流,天天向上!