2012-12-01 71 views
0

你能幫我在Java中做到這一點嗎?Java中的銷售點

一個命令行銷售點系統,可以計算折扣和總計。該系統應該是賣書的,它應該包含以下產品:如果您購買的兩份

1.相距作者1人,定價在$ 20
2.祕境由author2,定價在$ 40

一本書,你會得到8%的折扣。如果您購買三份或更多,您可以享受所有優惠20%的折扣。

系統接受指定項目數量和書本ID的輸入,然後按Enter鍵。完成後,鍵入「結束」並按Enter鍵。

示例輸入:

輸出示例:一個人的
5份分開:$ 80.00祕境的
1複製:$ 40.00
總價格:$ 120.00

示例輸入2:
結束

輸出示例2:一個人的
2份分開:$ 36.80
總價格:$ 36.80

package pos; 

import java.util.Scanner; 


public class Pos { 


    public static void main(String[] args) { 
     System.out.print("Please enter the quantity along with a book book code (1,2, etc) " 
       + "from the list.Please space you entry.\n "); 
     System.out.print("1.A man apart by author1 - Price: $ 20\n " 
       + "2.Uncharted by author2 - Price: $ 40\n Quantity and Book Code:\n"); 

      int bkprice1 = 20; 
      int bkprice2 = 40; 

      String book1 = "Hunger Games"; 
      String book2 = "Clean Code by Robert C. Martin"; 
      float Totalbk1 = 0; 
      float Totalbk2 = 0; 
      float Totalbk1L1 = 0; 
      float Totalbk2L1 = 0; 
      float Totalbk2L2 = 0; 
      float Totalbk3 = 0; 
      float Total; 
      float Disc; 

     try (Scanner in = new Scanner(System.in)) { 

      int qty = in.nextInt(); 
      int code = in.nextInt(); 
      int qty2 = in.nextInt(); 
      int code2 = in.nextInt(); 
      String tt = in.next(); 
      int qty3; 



      if (qty == 2 && code == 1) { 
       Disc = (float) ((float) (qty*bkprice1)*(0.08)); 
       Totalbk1 = (float) (qty*bkprice1) - Disc; 
       System.out.println(qty+" copies of "+book1+": GHC "+Totalbk1); 
       Totalbk1L1 = Totalbk1; 

      } else if (qty > 2 && code == 1) { 
       Disc = (float) ((float) (qty*bkprice1)*(0.2)); 
       Totalbk1 = (float) (qty*bkprice1) - Disc; 
       System.out.println(qty+" copies of "+book1+": GHC "+Totalbk1); 
       Totalbk1L1 = Totalbk1; 

      } else if (qty <= 1 && code == 1) { 
       Totalbk1 = (float) (qty*bkprice1); 
       System.out.println(qty+" copy of "+book1+": GHC "+Totalbk1); 
       Totalbk1L1 = Totalbk1; 

      } 

      if (qty2 == 2 && code2 == 1) { 
       Disc = (float) ((float) (qty2*bkprice1)*(0.08)); 
       Totalbk1 = (float) (qty2*bkprice1) - Disc; 
       System.out.println(qty2+" copies of "+book1+": GHC "+Totalbk1); 
       Total = Totalbk1L1 + Totalbk1; 
       System.out.println("Total price: "+Total); 
      } else if (qty2 > 2 && code2 == 1) { 
       Disc = (float) ((float) (qty2*bkprice1)*(0.2)); 
       Totalbk1 = (float) (qty2*bkprice1) - Disc; 
       System.out.println(qty2+" copies of "+book1+": GHC "+Totalbk1); 
       Total = Totalbk1L1 + Totalbk1; 
       System.out.println("Total price: "+Total); 
      } else if (qty2 <= 1 && code2 == 1) { 
       Totalbk1 = (float) (qty2*bkprice1); 
       System.out.println(qty2+" copy of "+book1+": GHC "+Totalbk1); 
       Total = Totalbk1L1 + Totalbk1; 
       System.out.println("Total price: "+Total); 
      } 




      if (qty == 2 && code == 2) { 
       Disc = (float) ((float) (qty*bkprice2)*(0.08)); 
       Totalbk2 = (float) (qty*bkprice2) - Disc; 
       System.out.println(qty+" copies of "+book2+": GHC "+Totalbk2); 
       Totalbk2L1 = Totalbk2; 

      } else if (qty > 2 && code == 2) { 
       Disc = (float) ((float) (qty*bkprice2)*(0.2)); 
       Totalbk2 = (float) (qty*bkprice2) - Disc; 
       System.out.println(qty+" copies of "+book2+": GHC "+Totalbk2); 
       Totalbk2L1 = Totalbk2; 

      } else if (qty <= 1 && code == 2) { 
       Totalbk2 = (float) (qty*bkprice2); 
       System.out.println(qty+" copy of "+book2+": GHC "+Totalbk2); 
       Totalbk2L1 = Totalbk2; 

      } 


      if (qty2 == 2 && code2 == 2) { 
       Disc = (float) ((float) (qty2*bkprice2)*(0.08)); 
       Totalbk2 = (float) (qty2*bkprice2) - Disc; 
       System.out.println(qty2+" copies of "+book2+": GHC "+Totalbk2); 
       Total = Totalbk2L1 + Totalbk2; 
       System.out.println("Total price: "+Total); 
      } else if (qty2 > 2 && code2 == 2) { 
       Disc = (float) ((float) (qty2*bkprice2)*(0.2)); 
       Totalbk2 = (float) (qty2*bkprice2) - Disc; 
       System.out.println(qty2+" copies of "+book2+": GHC "+Totalbk2); 
       Total = Totalbk2L1 + Totalbk2; 
       System.out.println("Total price: "+Total); 
      } else if (qty2 <= 1 && code2 == 2) { 
       Totalbk2 = (float) (qty2*bkprice2); 
       System.out.println(qty2+" copy of "+book2+": GHC "+Totalbk2); 
       Total = Totalbk2L1 + Totalbk2; 
       System.out.println("Total price: "+Total); 
      } 



      if (qty == 2 && code == 1) { 
       Disc = (float) ((float) (qty*bkprice1)*(0.08)); 
       Totalbk1 = (float) (qty*bkprice1) - Disc; 
       System.out.println(qty+" copies of "+book1+": GHC "+Totalbk1); 
       Totalbk1L1 = Totalbk1; 

      } else if (qty > 2 && code == 1) { 
       Disc = (float) ((float) (qty*bkprice1)*(0.2)); 
       Totalbk1 = (float) (qty*bkprice1) - Disc; 
       System.out.println(qty+" copies of "+book1+": GHC "+Totalbk1); 
       Totalbk1L1 = Totalbk1; 

      } else if (qty <= 1 && code == 1) { 
       Totalbk1 = (float) (qty*bkprice1); 
       System.out.println(qty+" copy of "+book1+": GHC "+Totalbk1); 
       Totalbk1L1 = Totalbk1; 

      } 


      if (qty2 == 2 && code2 == 2) { 
       Disc = (float) ((float) (qty2*bkprice2)*(0.08)); 
       Totalbk2 = (float) (qty2*bkprice2) - Disc; 
       System.out.println(qty2+" copies of "+book2+": GHC "+Totalbk2); 
       Total = Totalbk2L1 + Totalbk2; 
       System.out.println("Total price: "+Total); 
      } else if (qty2 > 2 && code2 == 2) { 
       Disc = (float) ((float) (qty2*bkprice2)*(0.2)); 
       Totalbk2 = (float) (qty2*bkprice2) - Disc; 
       System.out.println(qty2+" copies of "+book2+": GHC "+Totalbk2); 
       Total = Totalbk2L1 + Totalbk2; 
       System.out.println("Total price: "+Total); 
      } else if (qty2 <= 1 && code2 == 2) { 
       Totalbk2 = (float) (qty2*bkprice2); 
       System.out.println(qty2+" copy of "+book2+": GHC "+Totalbk2); 
       Total = Totalbk2L1 + Totalbk2; 
       System.out.println("Total price: "+Total); 
      } 



     } 
    } 
} 

請讓我知道,如果我的邏輯是正確的。我希望我的代碼儘可能簡潔。

+0

作爲提醒,java約定是字段名稱應該以小寫字母開頭 – luiges90

+1

您要求實現,但已經提供了可能的解決方案。那麼你的問題到底是什麼? – home

+0

你好,謝謝你的評論。當你運行這段代碼時,它不會生成期望的輸出。這是因爲我必須爲用戶輸入的所有可能性創建if語句。這會使代碼太多。我的問題是, 有一個更好的方法嗎? –

回答

0

你需要學會使用,而不是試圖把一切都在獨立的變量

有書一類的類,有標題和價格

有一類訂單

使用集合類保持訂單

這應該讓你開始列表...

+2

由於某種原因,我們不再喜歡homework標籤:-) http://stackoverflow.com/tags/homework/info – home

0

嘗試使用地圖的書的d價格,因此if-else的數量會減少。

+0

您好,請解釋一下在這種情況下「地圖」是什麼。謝謝 –