2014-09-24 40 views
-1

該計劃的目的是印刷迎接客戶,然後向他們提供有關銷售價格和價格的信息,然後詢問客戶需要的每件商品的數量。銷售收據的Java程序

該程序應該能夠顯示總數並詢問客戶付多少錢,並最終顯示包含物品,物品價格,數量,產品成本,小計,折扣,稅收,總計的收據,付款和客戶的變化。

商店的物品價格爲:T恤18.95美元,一包薯片1.79美元,12包可樂(2.20美元),2.99美元。

除可口可樂之外的所有商品都以低於標價的15%的價格出售。 T恤也收取6%的密歇根州銷售稅。

我在寫程序和使用java相當新,所以所有的幫助將不勝感激。以下是我迄今爲止編寫的代碼:

import java.util.Scanner; 

//The purpose of this program is to simulate the shopping process by calculating the costs of the items 
//and producing a receipt for the shopping trip. 
//CPS 180 
//Joseph Eastman 
//September 24, 2014 
public class StoreReceipt { 
    static final double TSHIRT_PRICE = 16.1075; 
    static final double CHIPS_PRICE = 1.5215; 
    static final double COKE_PRICE = 2.99; 
    String a; 
    static int numberShirts; 
    static int numberChips; 
    static int numberCoke; 
    static double tshirtTotal = TSHIRT_PRICE * numberShirts; 
    static double chipsTotal = CHIPS_PRICE * numberChips; 
    static double cokeTotal = (COKE_PRICE + 1.20) * numberCoke; 
    static double finalTotal = tshirtTotal + chipsTotal + cokeTotal; 
    { 
    } 

    private static Scanner input; 

    public static void main(String[] args) { 
     input = new Scanner(System.in); 
     System.out.println("What's your name?"); 
     String a = input.nextLine(); 
     System.out.println("Welcome to Denny's Market, " + a + "! We have the following items for sale:"); 
     System.out.println("T-shirt  $18.95 15% off"); 
     System.out.println("Chips  $1.79  15% off"); 
     System.out.println("Coke  $2.99"); 
     System.out.println("How many T-shirts do you want?"); 
     String numberShirts = input.nextLine(); 
     System.out.println("How many bags of potato chips?"); 
     String numberChips = input.nextLine(); 
     System.out.println("What about 12-pack coke?"); 
     String numberCoke = input.nextLine(); 
     tshirtTotal = tshirtTotal * .85; 
     chipsTotal = chipsTotal * .85; 
     tshirtTotal = tshirtTotal * 1.06; 
     System.out.println("Your total is: " + finalTotal); 

} 

} 

現在當我輸入的輸入中我得到0的總我和它說我沒有使用任何我的變量。產品數量的輸入可以是任何東西,只需要正確計算總數並改變客戶應該收到的數量。

這裏的輸出應該是什麼時候計劃完成,跑起來像一個例子:

What’s your name? John 
Welcome to Denny’s Market, John! We have the following items for sale: 
T-shirt $18.95 15% off 
Chips $ 1.79 15% off 
Coke $ 2.99 
How many T-shirts do you want? 3 
How many bags of potato chips? 4 
What about 12-pack Coke? 2 
Your total is $65.69. 
Please enter your payment: 70 
John, here is your receipt: 
item  unit price how many cost 
-------------------------------------------------------- 
T-shirt  18.95  3  56.85 
Chips  1.79  4  7.16 
Coke   2.99  2  5.98 
Deposit      2.40 

Subtotal      72.39 
Discount      -9.60 
Tax       2.90 
          ---------- 
Total       65.69 

Payment      70.00 
Your Change     4.31 

Thank you. Come Again! 
+0

你能給我們提供樣品輸入,預期產量和實際產量嗎?你的問題有點過於寬泛,似乎你已經打印出總數。 – 2014-09-24 18:01:48

+0

我編輯了這篇文章,並添加了程序運行後的輸出結果。 – 2014-09-24 18:50:07

+0

您當前的輸出是什麼?你得到什麼錯誤? – APerson 2014-09-24 19:24:43

回答

0

你似乎已經有一些吧(這是很好)。讓我們藉此一步步來18.95

$ T-恤

double tshirtTotal = TSHIRT_PRICE * numberShirts; 

1.79 $用於薯片

double chipsTotal = CHIPS_PRICE * numberChips; 

$ 2.99一包12罐可樂($ 1.20存款)

double cokeTotal = (COKE_PRICE + 1.20) * numberCoke ; 

除了可樂的所有商品在售15折的標價。

tshirtTotal = tshirtTotal * .85; 
chipsTotal = chipsTotal * .85; 

T恤也被收取6%,密歇根州的銷售稅。

tshirtTotal = tshirtTotal * 1.06; 

最後讓我們做總:

double finalTotal = tshirtTotal + chipsTotal + cokeTotal; 
System.out.println("Your total is: " + finalTotal); 

所以你main方法應該如下所示:

//part of your original code 
input = new Scanner(System.in); 
System.out.println("What's your name?"); 
String a = input.nextLine(); 
System.out.println("Welcome to Denny's Market, " + a + "! We have the following items for sale:"); 
System.out.println("T-shirt  $18.95 15% off"); 
System.out.println("Chips  $1.79  15% off"); 
System.out.println("Coke  $2.99"); 
System.out.println("How many T-shirts do you want?"); 
String numberShirts = input.nextLine(); 
System.out.println("How many bags of potato chips?"); 
String numberChips = input.nextLine(); 
System.out.println("What about 12-pack coke?"); 
String numberCoke = input.nextLine(); 

//new code below 
double tshirtTotal = TSHIRT_PRICE * numberShirts; 
double chipsTotal = CHIPS_PRICE * numberChips; 
double cokeTotal = (COKE_PRICE + 1.20) * numberCoke ; 
tshirtTotal = tshirtTotal * .85; 
chipsTotal = chipsTotal * .85; 
tshirtTotal = tshirtTotal * 1.06; 
double finalTotal = tshirtTotal + chipsTotal + cokeTotal; 
System.out.println("Your total is: " + finalTotal); 

然後索要錢財。對於更改,只需要money-finalTotal(假設用戶總是輸入足夠的錢)。其他所有你需要做的是格式化輸出。我會把它留給你。

如果您擔心將double變量格式化爲類似貨幣的值,那麼check this post out

+0

我發佈了一個帶有現在代碼的編輯,並且我發現自己再次卡住了,總計不斷出現0,並且表示我的變量沒有被使用。幫幫我! – 2014-09-24 21:59:31

+0

@JoeEastman這是因爲你正在計算頂部的變量(在'main'之外)和'static'。計算一切,因爲你從用戶獲得的值。並且不要對這些變量使用'static'。 – 2014-09-25 01:13:19