1. 程式人生 > >用JAVA基礎知識制作簡易《訂單系統》(適用於初學者學習)

用JAVA基礎知識制作簡易《訂單系統》(適用於初學者學習)

double 北京 count enum ise 生成 imp 如果 系統

import java.util.Scanner;
public class OrderingMsg {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
String[] names = new String[6]; // 訂餐人姓名
String[] dishMsgs = new String[6];// 選擇菜品
int[] numbers = new int[6];// 菜品份數
int[] times = new int[6]; // 送餐時間
String[] addresses = new String[6];// 送餐地址
int[] states = new int[6];// 訂單狀態 0:已經預定 1:已經完成
double[] sumPrices = new double[6];// 總金額
int[] praiseNums = new int[6];
String[] dishNames = { "北京烤鴨", "麻婆豆腐", "松鼠魚", "紅燒帶魚", "時令蔬菜", "辣土豆絲" };
double[] prices = { 88, 24, 38, 35, 15, 12 };
System.out.println("歡迎進入surper訂餐系統");
int num = -1; // 輸入0 默認返回主菜單
boolean isExit = false;// 默認不退出
do {
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("**********************");
System.out.println("請選擇:");
int choice = scanner.nextInt(); //選擇你所需要的服務
switch (choice) {
case 1:
System.out.println("************我要訂餐************");
boolean empty = false;//作一個標記
for (int i = 0; i < names.length; i++) { // 訂餐人的集合
if (names[i] == null) {
empty = true;//訂單如果是空的,便可以往裏加數據
System.out.println("請輸入訂餐人的姓名:");
names[i] = scanner.next(); // 獲取訂餐人的姓名
System.out.println("您可以選擇下列的菜品:");
System.out.println("序號\t\t\t菜名\t\t\t單價\t\t\t點贊數");
for (int j = 0; j < dishNames.length; j++) { // 遍歷菜品
String praiseNum = (praiseNums[j] > 0) ? praiseNums[j]
+ "個贊"
: "";
System.out.println(j + 1 + "\t\t\t" + dishNames[j]
+ "\t\t\t" + prices[j] + "\t\t\t"
+ praiseNum);
}
// 用戶開始點菜,並收集信息
System.out.println("請您選擇菜品的編號:");
int choiceName = scanner.nextInt();
System.out.println("請您輸入點餐的份數:");
int number = scanner.nextInt();
System.out.println("請您輸入送餐的地址:");
String address = scanner.next();
System.out.println("請您輸入送餐的時間:");
System.out.println("本店整點送餐:8點至20點");
int time = scanner.nextInt();
double sumPrice = prices[choiceName - 1] * number;
double price = (sumPrice >=50) ? 0 : 5;//判斷是否收取運費
System.out.println("訂單已生成!");
System.out.println("您定的菜品是:"+ dishNames[choiceName - 1] + "\t" + number+ "份");
System.out.print("餐費:" + sumPrice);
System.out.print("\t送餐費:" + price);
System.out.println("\t總費用:" + (price + sumPrice));
// 把輸入的信息塞進數組中
times[i] = time; // 時間
addresses[i] = address; // 地址
sumPrices[i] = (sumPrice + price); // 總金額
dishMsgs[i] = dishNames[choiceName - 1]; // 定的菜品
states[i] = 0; // 訂餐的狀態
numbers[i] = number; // 份數
break;
}
}
if (!empty) {// 餐袋已經滿了
System.out.println("餐袋已經滿了");
}
break;
case 2:
System.out.println("************查看餐袋************");
System.out.println("編號\t\t訂餐人\t\t菜品(份數)\t\t送餐時間 \t\t送餐地址\t\t總金額\t\t狀態\t\t點贊數量");
for (int i = 0; i < names.length; i++) {
if (names[i] != null) { // 有人訂餐 的顯示
String state = (states[i] == 0) ? "已經預定" : "已經完成"; // 獲取狀態
int time = times[i]; // 時間
String sumPrice = sumPrices[i] + "元"; // 總金額
String address = addresses[i]; // 地址
String dishMsg = dishMsgs[i]; // 菜品
int count = numbers[i];//份數
System.out.println((i + 1) + "\t\t" + names[i] + "\t\t"+ dishMsg + "(" + count + ")" + "\t\t" + time+ " \t\t" + address + "\t\t" + sumPrice+ "\t\t" + state + "\t\t" + praiseNums[i]);
}
}
break;
case 3:
System.out.println("************簽收訂單************");
System.out.println("請您輸入需要簽收的訂單編號:");
int sign = scanner.nextInt();
int flag = 0;// 作一個標記,來判斷訂單是否被簽收
for (int i = 0; i < names.length; i++) {
if (names[i] != null && states[i] == 0) {//有訂單並且可以簽收
states[i] = 1;// 改變狀態
flag = 1;
break;
} else if (names[i] != null && states[i] == 1) {//有訂單,並且已被簽收
flag = 2;
}
}
if (flag == 1) {
System.out.println("訂單已經簽收!");
} else if (flag == 2) {
System.out.println("訂單不能重復簽收!");
} else {
System.out.println("沒有找到您的訂單!請您核對編號!");
}


break;
case 4:
System.out.println("************刪除訂單************");
System.out.println("請您輸入需要刪除的訂單編號:");
int dealNum = scanner.nextInt();
int deal = 0; // 作一個標記 來判斷訂單是否被簽收以及是否存在
for (int i = 0; i < names.length; i++) {
if (names[i] != null && states[dealNum - 1] == 1) { //有訂單,並且已簽收,可以刪除
// 刪除一條信息,將之後的所有信息,依次向前移
for (int j = dealNum - 1; j < names.length - 1; j++) {
names[j] = names[j + 1]; // 訂餐人
dishMsgs[j] = dishMsgs[j + 1];// 菜品
times[j] = times[j + 1];// 時間
addresses[j] = addresses[j + 1];// 地址
states[j] = states[j + 1];// 狀態
numbers[j] = numbers[j + 1];// 份數
}
deal = 1;
break;
} else if (names[i] != null && states[i] == 0) { // 右訂單並且訂單還沒有簽收,便不能刪除
deal = 2;
}
}
if (deal == 1) {
System.out.println("訂單已經被刪除");
} else if (deal == 2) {
System.out.println("訂單還沒有簽收 不允許刪除!");
} else {
System.out.println("沒有找到指定的訂單!");
}
break;
case 5:
System.out.println("************我要點贊***************");
System.out.println("編號\t\t\t菜名\t\t\t價格");
for (int i = 0; i < dishNames.length; i++) { // 遍歷所有的菜名
String price = prices[i] + "元"; //價格
String name = dishNames[i];//菜名
System.out.println((i + 1) + "\t\t\t" + name + "\t\t\t"+ price);
}
System.out.println("請您輸入點贊的菜品編號:");
int choiceNum = scanner.nextInt();
praiseNums[choiceNum - 1]++;// 點贊數累加
System.out.println("謝謝參與!點贊成功!");
break;
case 6:
// 退出系統
isExit = true;
break;
default:
// 退出系統
isExit = true;
break;
}
if (!isExit) { // 默認這是不退出
System.out.println("輸入0返回主菜單!");
num = scanner.nextInt();
} else {
break; // 退出系統
}
} while (num == 0);
System.out.println("謝謝您的光臨!!!!!");
}
}

用JAVA基礎知識制作簡易《訂單系統》(適用於初學者學習)