-1

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).

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

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:

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);

}

}

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.

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 $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!
dds
  • 2,335
  • 1
  • 31
  • 45
Joe Eastman
  • 69
  • 2
  • 5
  • 10

1 Answers1

1

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

double tshirtTotal = TSHIRT_PRICE * numberShirts;

$1.79 for a bag of potato chips

double chipsTotal = CHIPS_PRICE * numberChips;

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

double cokeTotal = (COKE_PRICE + 1.20) * numberCoke ;

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

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

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

tshirtTotal = tshirtTotal * 1.06;

Finally let's do the total:

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

So your main method should look as follows:

//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);

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.

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

Community
  • 1
  • 1
  • I posted an edit with the code I now have and I've found myself stuck again, the total keeps coming up with 0 and it says my variables aren't being used. Help! – Joe Eastman Sep 24 '14 at 21:59
  • @JoeEastman It's because you're calculating the variables at the top (outside the `main`) and as `static`. Calculate it all as you get the values from the user. And don't use `static` for these variables. – But I'm Not A Wrapper Class Sep 25 '14 at 01:13