1. 程式人生 > >Java作業 第十七章

Java作業 第十七章

吃貨聯盟訂餐系統


import java.util.*;
public class OrderingUtil {     //點餐類
    Scanner sc = new Scanner (System.in);
    OrderingSet oSet = new OrderingSet();
    String[] dishNames = { "紅燒魚", "油豆腐炒肉", "土豆絲" }; // 菜品名稱
    double[] prices = new double[] { 38.0, 15.0, 12.0 }; // 菜品價格
    int[] prainseNums = new int
[3]; // 點贊數 public void initial() { // 初始化第一條訂單資訊 oSet.names[0] = "張晴"; oSet.dishMegs[0] = "紅燒魚2份"; oSet.times[0] = 12; oSet.addresses[0] = "楊柳路207號"; oSet.sumPrinces[0] = 76.0; oSet.states[0] = 1; // 初始化第二條訂單資訊 oSet.names[0] = "張晴"; oSet.dishMegs[0
] = "油豆腐炒肉2份"; oSet.times[0] = 18; oSet.addresses[0] = "楊柳路207號"; oSet.sumPrinces[0] = 30; oSet.states[0] = 0; } public boolean add(){ //點餐方法 boolean isAdd = false;// 記錄是否可以點餐 for (int j = 0; j < oSet.names.length; j++) { if (oSet.names[j]==null) { //找到第一個位置,可新增訂單資訊
isAdd = true; //置標誌位,可以訂餐 System.out.print("請輸入訂餐人姓名:"); String name = sc.next(); //顯示供選擇的菜品資訊 System.out.println("序號"+"\t"+"菜品"+"\t"+"單價"+"\t"+"點贊數"); for (int i = 0; i < dishNames.length; i++) { String price = prices[i]+"元"; String priaiseNum = (prainseNums[i])>0? prainseNums[i]+"贊":"0"; System.out.println((i+1)+"\t"+dishNames[i]+"\t"+price+"\t"+priaiseNum); } //使用者點餐 System.out.print("請選擇您要點的菜品編號:"); int chooseDish = sc.nextInt(); System.out.print("請選擇您需要的份數:"); int number = sc.nextInt(); String dishMeg = dishNames[chooseDish-1]+" "+number+"份"; double sumPrice = prices[chooseDish-1]*number; //滿費50元,免送餐費5元 double deliCharge = (sumPrice>=50)?0:5; System.out.print("請輸入送餐時間(送餐時間是10點至12點間整點送餐):"); int time = sc.nextInt(); while (time<10 || time>20) { System.out.println("您的輸入有誤,請輸入10~20之間的整數!"); time=sc.nextInt(); } System.out.print("請輸入送餐地址:"); String address = sc.next(); //無需新增狀態,預設是0,即已預定狀態 System.out.println("訂餐成功!"); System.out.println("您訂的是:"+dishMeg); System.out.println("送餐時間:"+time+"點"); System.out.println("餐費:"+sumPrice+"元,送餐費"+deliCharge+"元,總計:"+(sumPrice+deliCharge)+"元。"); //新增資料 oSet.names[j] = name; oSet.dishMegs[j] = dishMeg; oSet.times[j] =time; oSet.addresses[j] = address; oSet.sumPrinces[j]=sumPrice+deliCharge; break; } } if(!isAdd){ System.out.println("對不起,您的餐袋已滿!"); return false; }else return true; } public void display(){ System.out.println("序號\t訂餐人\t餐品資訊\t\t送餐時間\t送餐地址\t\t總金額\t訂單狀態"); for (int i = 0; i < oSet.names.length; i++) { if (oSet.names[i]!=null) { String state =(oSet.states[i]==0)?"已預定":"已完成"; String date = oSet.times[i]+"點"; String sumPrice = oSet.sumPrinces[i]+"元"; System.out.println((i+1)+"\t"+oSet.names[i]+"\t"+oSet.dishMegs[i]+"\t"+date+"\t" +oSet.addresses[i]+"\t"+sumPrice+"\t"+state); } } } public void sigh(){ boolean isSignFind = false; //找到要簽收的訂單 System.out.print("請選擇要簽收的訂單序號:"); int signOrderId = sc.nextInt(); for (int i = 0; i <oSet.names.length; i++) { if (oSet.names[i]!=null&&oSet.states[i]==0&&signOrderId == i+1) { oSet.states[i]=1;//將狀態值置為已完成 System.out.println("訂單簽收成功!"); isSignFind = true; //標記已找到訂單 }else if(oSet.names[i]!=null&&oSet.states[i]==1&&signOrderId ==i+1){ System.out.println("請選擇的訂單已完成簽收,不能再次簽收!"); isSignFind = true; //標記已找到訂單 } } if(!isSignFind){ System.out.println("您選擇的訂單不存在!"); } } public void delete(){ boolean isDelFind = false; //標記是否找到要刪除的訂單 System.out.print("請輸入要刪除的訂單序號:"); int delId = sc.nextInt(); for (int i = 0; i < oSet.names.length; i++) { if (oSet.names[i]!=null&&oSet.states[i]==1&&delId == i+1) { isDelFind = true; //標記已找到次訂單 //執行刪除操作:刪除位置後的元素依次前移 for (int j = delId-1; j < oSet.names.length-1; j++) { oSet.names[j] = oSet.names[j+1]; oSet.dishMegs[j]= oSet.dishMegs[j+1]; oSet.times[j] = oSet.times[j+1]; oSet.addresses[j] = oSet.addresses[j+1]; oSet.states[j] = oSet.states[j+1]; oSet.sumPrinces[j] = oSet.sumPrinces[j+1]; } //最後一位清空 int endIndex = oSet.names.length-1; oSet.names[endIndex] =null; oSet.dishMegs[endIndex] = null; oSet.times[endIndex] = 0; oSet.addresses[endIndex]=null; oSet.states[endIndex] = 0; oSet.sumPrinces[endIndex]=0; System.out.println("刪除訂單成功!"); break; }else if(oSet.names[i]!=null && oSet.states[i]==0&&delId==i+1){ System.out.println("您選擇的訂單未簽收,不能刪除!"); isDelFind = true; //標記找到此訂單 break; } } //未找到該序列號的訂單:不能刪除 if (!isDelFind) { System.out.println("您要刪除的訂單不存在!"); } } public void praise(){ //顯示菜品資訊 System.out.println("序號"+"\t"+"菜名"+"\t"+"單價"); for (int i = 0; i < dishNames.length; i++) { String price = prices[i]+"元"; String priaiseNum = (prainseNums[i])>0?prainseNums[i]+"贊":""; System.out.println((i + 1) + "\t" + dishNames[i] + "\t" + prices[i] + "元" + priaiseNum); } System.out.print("請選擇您要點讚的菜品序號:"); int number = sc.nextInt(); prainseNums[number - 1]++; System.out.println("點贊成功"); } public void startMenu() { int num = -1; //使用者輸入0返回主選單,否則退出系統 boolean isExit = false; //標誌使用者是否退出系統,ture為退出系統 System.out.println("\n歡迎使用 “吃貨聯盟訂餐系統” "); //迴圈: 顯示選單,根據使用者選擇的數字執行相應功能 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.print("請選擇:"); int choose = sc.nextInt(); //記錄使用者選擇的功能編號 switch (choose) { case 1: System.out.println("***我要訂餐***"); add(); break; case 2: System.out.println("***檢視餐袋***"); display(); break; case 3: System.out.println("****簽收訂單***"); sigh(); break; case 4: System.out.println("***刪除訂單***"); delete(); break; case 5: System.out.println("***我要點贊***"); praise(); break; case 6: System.out.println("系統退出"); isExit = true; break; default: System.out.println("請輸入正確的數字!"); isExit = true; break; } if(!isExit){ System.out.print("輸入0返回:"); num = sc.nextInt(); }else{ break; } } while (num == 0); } }
public class OrderingSet {
    //資料主體:一組訂單資訊
    String [] names = new String [4];       //儲存訂餐人名稱
    String [] dishMegs = new String [4];    //儲存所選資訊,包括菜品名及份數
    int []  times = new int [4];            //儲存送餐時間
    String [] addresses = new String [4];   //儲存送餐地址
    int [] states = new int [4];            //儲存訂單狀態:0表示預定,表示已完成
    double [] sumPrinces  = new double [4]; //儲存訂單的總金額
}
import java.util.*;
public class OrderingMgr {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        OrderingUtil om = new OrderingUtil();
        om.initial();
        om.startMenu();
        }
    }