同學們應該都去麥當勞或肯德基吃過快餐吧?請同學們參考肯德基官網的資訊模擬肯德基快餐店的收銀系統,使用java語言進行編寫。
package KFC;
import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Date; import java.util.HashMap; import java.util.Scanner; import java.util.Set;
public class Text1 { public static void main(String[] args) throws IOException { Foods k1=new Foods(“豪華至尊全家桶”,1,(float)(79.9)); Foods k2=new Foods(“黃金奧爾良雞腿堡”,1,(float)(19.9)); Foods k3=new Foods(“印度熱辣雞腿堡”,1,(float)(18.9)); Foods k4=new Foods(“帕尼尼早餐套餐”,1,(float)(29.9)); Foods k5=new Foods(“冰可樂”,1,(float)(9.9)); Foods k6=new Foods(“招牌雞米花”,1,(float)(9.9)); Foods k7=new Foods(“薯條”,1,(float)(8.8)); Foods k8=new Foods(“皇家熱辣雞翅”,1,(float)(11.9)); Foods k9=new Foods(“冰淇淋甜筒”,1,(float)(9.9));
HashMap<Foods , String > hm= new HashMap<Foods , String >(); System.out.println("**********************************"); System.out.println("**********歡迎光臨肯德基************"); System.out.println(" 【 選單 】 "); System.out.println("1:"+k1.toString()); System.out.println("2:"+k2.toString()); System.out.println("3:"+k3.toString()); System.out.println("4:"+k4.toString()); System.out.println("5:"+k5.toString()); System.out.println("6:"+k6.toString()); System.out.println("7:"+k7.toString()); System.out.println("8:"+k8.toString()); System.out.println("9:"+k9.toString()); boolean z=true ; int count=1;//計數器 float total=0; while(z) { System.out.println("點餐請輸入編號:"); Scanner sc = new Scanner (System.in ); int i = sc.nextInt(); switch (i) { case 1: System.out.println(count+k1.getFname()); System.out.println("請輸入數量:"); Scanner sc1= new Scanner (System.in ); int n = sc1.nextInt(); k1.setFnumb(n);//輸入數量 total=total+k1.getfPrice();//計算入總價 hm.put(k1,"數量:"+String.valueOf( k1.getFnumb()) );//新增進樹 break; case 2: System.out.println(count+k2.getFname()); System.out.println("請輸入數量:"); Scanner sc2= new Scanner (System.in ); int n2 = sc2.nextInt(); k2.setFnumb(n2); total=total+k2.getfPrice();//計算入總價 hm.put(k2,"數量:"+String.valueOf( k2.getFnumb()) );//新增進樹 break; case 3: System.out.println(count+k3.getFname()); System.out.println("請輸入數量:"); Scanner sc3= new Scanner (System.in ); int n3 = sc3.nextInt(); k3.setFnumb(n3); total=total+k3.getfPrice();//計算入總價 hm.put(k3,"數量:"+String.valueOf( k3.getFnumb()) );//新增進樹 break; case 4: System.out.println(count+k4.getFname()); System.out.println("請輸入數量:"); Scanner sc4= new Scanner (System.in ); int n4 = sc4.nextInt(); k4.setFnumb(n4); total=total+k4.getfPrice();//計算入總價 hm.put(k4,"數量:"+String.valueOf( k4.getFnumb()) );//新增進樹 break; case 5: System.out.println(count+k5.getFname()); System.out.println("請輸入數量:"); Scanner sc5= new Scanner (System.in ); int n5 = sc5.nextInt(); k5.setFnumb(n5); total=total+k5.getfPrice();//計算入總價 hm.put(k5,"數量:"+String.valueOf( k5.getFnumb()) );//新增進樹 break; case 6: System.out.println(count+k6.getFname()); System.out.println("請輸入數量:"); Scanner sc6= new Scanner (System.in ); int n6 = sc6.nextInt(); k6.setFnumb(n6); total=total+k6.getfPrice();//計算入總價 hm.put(k6,"數量:"+String.valueOf( k6.getFnumb()) );//新增進樹 break; case 7: System.out.println(count+k7.getFname()); System.out.println("請輸入數量:"); Scanner sc7= new Scanner (System.in ); int n7 = sc7.nextInt(); k7.setFnumb(n7); total=total+k7.getfPrice();//計算入總價 hm.put(k7,"數量:"+String.valueOf( k7.getFnumb()) );//新增進樹 break; case 8: System.out.println(count+k8.getFname()); System.out.println("請輸入數量:"); Scanner sc8= new Scanner (System.in ); int n8 = sc8.nextInt(); k8.setFnumb(n8); total=total+k8.getfPrice();//計算入總價 hm.put(k8,"數量:"+String.valueOf( k8.getFnumb()) );//新增進樹 break; case 9: System.out.println(count+k9.getFname()); System.out.println("請輸入數量:"); Scanner sc9= new Scanner (System.in ); int n9 = sc9.nextInt(); k9.setFnumb(n9); total=total+k9.getfPrice();//計算入總價 hm.put(k9,"數量:"+String.valueOf( k9.getFnumb()) );//新增進樹 break; default:System.out.println("請輸入正確的號碼!"); break; } //遍歷樹 Set<Foods> ks1 = hm.keySet();//遍歷Map for(Foods s2:ks1) { System.out.println("("+s2+","+hm.get(s2)+")"); } System.out.println("1 —— 繼續點餐 2 —— 重新點餐 3 —— 結束點餐 "); Scanner sca= new Scanner (System.in ); int n = sca.nextInt(); switch(n) { case 1: count++; break ; case 2 : System.out.println("重新點餐!"); hm.clear(); break; case 3 : System.out.println("總價"+total); System.out.println("結束點餐"); z=false; break; default:System.out.println("請輸入正確的號碼!"); break; }
} System.out.println("***************************"); System.out.println("歡迎光臨肯德基"); System.out.println(" 您的選單為: ");
//遍歷樹 Set<Foods> ks1 = hm.keySet();//遍歷Map for(Foods s2:ks1) { System.out.println(" "+s2+", "+hm.get(s2)+" "); } System.out.println("總價"+total); System.out.println("訂單時間: "+new Date()); System.err.println(" 歡迎下次光臨!"); System.out.println("**********************************"); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("xiaopiao.txt")));//字元緩衝輸出流 bw.write("********************************");bw.newLine(); bw.write("*********歡迎光臨肯德基!*************");bw.newLine(); bw.write(" 您的選單為: ");bw.newLine(); Set<Foods> ks2 = hm.keySet();//遍歷Map for(Foods s2:ks2) { // System.out.println(" "+s2+", "+hm.get(s2)+" "); bw.write(s2+", "+hm.get(s2));bw.newLine(); } //System.out.println("總價"+total); bw.write("訂單時間: "+new Date());bw.newLine(); bw.write(" 歡迎下次光臨!");bw.newLine(); bw.write("**********************************");bw.newLine(); bw.flush(); bw.close(); }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package KFC;
public class Foods { private String fname ; private int fnumb=1; private float fPrice;
public Foods() {
super();
}
public Foods(String fname, int fnumb, float fPrice) {
super();
this.fname = fname;
this.fnumb = fnumb;
this.fPrice = fPrice;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public int getFnumb() {
return fnumb;
}
public void setFnumb(int fnumb) {
this.fnumb = fnumb;
}
public float getfPrice() {
return fPrice*fnumb;
}
public void setfPrice(float fPrice) {
this.fPrice = fPrice;
}
@Override
public String toString() {
return fname + " 價格 [" + fPrice + "]";
}
}