eclipse 用于销售收据的 Java 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26023421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:00:49  来源:igfitidea点击:

Java program for a sales receipt

javasqleclipse

提问by Joe Eastman

The purpose of the program is to print to greet the customer and then provide them with information on what is for sale along with the prices and then to ask how much of each item the customer wants.

该程序的目的是打印以迎接客户,然后向他们提供有关待售商品和价格的信息,然后询问客户需要多少每件商品。

After the program should be able to display the total and ask how much the customer is paying with and finally display a receipt containing the item, item price, quantity, the cost for the product, subtotal, discount, tax, total, payment, and the customer's change.

程序应该能够显示总计并询问客户支付了多少,最后显示包含项目、项目价格、数量、产品成本、小计、折扣、税收、总计、付款和客户的变化。

The list prices for the items at the store are: $18.95 for T-shirt, $1.79 for a bag of potato chips, and $2.99 for a 12-pack Coke ($1.20 deposit as well).

店内商品的标价为:T 恤 18.95 美元,一袋薯片 1.79 美元,12 包可乐 2.99 美元(押金 1.20 美元)。

All merchandise except Coke is on sale with 15% off the list price. T-shirt is also charged a 6% Michigan sales tax.

除可乐外的所有商品均以低于标价 15% 的价格出售。T 恤还征收 6% 的密歇根销售税。

I'm fairly new at writing programs and using java so all of the help would be appreciated. Here is what I have written so far for the code:

我在编写程序和使用 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     .95    15% off");
        System.out.println("Chips       .79     15% off");
        System.out.println("Coke        .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);

}

}

Right now when I type the inputs in I'm getting 0 for my total and it says I'm not using any of my variables. The inputs for the number of products can can be anything, it just has to correctly calculate the total and change the customer should receive.

现在,当我输入输入时,我的总数为 0,它表示我没有使用任何变量。产品数量的输入可以是任何东西,它只需要正确计算客户应该收到的总数和更改。

Here is an example of what the output should look like when the program is finished and ran:

以下是程序完成并运行时的输出示例:

What's your name? John
Welcome to Denny's Market, John!  We have the following items for sale:
T-shirt .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 .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!

回答by But I'm Not A Wrapper Class

You seem to already have some of it (which is good). Let's take this a step at a time

你似乎已经拥有了一些(这很好)。让我们一步一步来

$18.95 for T-shirt

T 恤 $18.95

double tshirtTotal = TSHIRT_PRICE * numberShirts;

$1.79 for a bag of potato chips

一袋薯片 $1.79

double chipsTotal = CHIPS_PRICE * numberChips;

$2.99 for a 12-pack Coke ($1.20 deposit as well)

12 件装可乐 2.99 美元(押金 1.20 美元)

double cokeTotal = (COKE_PRICE + 1.20) * numberCoke ;

All merchandise except Coke is on sale with 15% off the list price.

除可乐外的所有商品均以低于标价 15% 的价格出售。

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

T-shirt is also charged a 6% Michigan sales tax.

T 恤还征收 6% 的密歇根销售税。

tshirtTotal = tshirtTotal * 1.06;

Finally let's do the total:

最后让我们做一下总数:

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

So your mainmethod should look as follows:

所以你的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     .95    15% off");
System.out.println("Chips       .79     15% off");
System.out.println("Coke        .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);

Then ask for money. For change, just do money-finalTotal(assuming user always inputs enough money). Everything else you need to do is formatting of the output. I'll leave that to you.

然后要钱。对于零钱,就去做money-finalTotal(假设用户总是投入足够的钱)。您需要做的其他一切就是格式化输出。我会把它留给你。

If you're worried about formatting the doublevariables into a money-like value, then check this post out.

如果您担心将double变量格式化为类似货币的值,请查看这篇文章