So i wrote this program and basically, I want it to add up the different devices purchased and list the amount of devices, total price(price of all the devices before tax),sales tax, and final price. Everything is working except the total price. For example if when prompted to select an item from the menu I press 10, which is the same is choosing nothing this works correctly. If prompted to select and item and I just choose one item it will calculate it and display it properly. However when i try to have the program calculate multiple purchases it only adds the last purchase thus giving me an inaccurate total price and sales tax, i'm not sure where i want.
Edit: Sorry I just figured out how to edit it properly.
package grapefruitelectronicreceipt; import java.util.Scanner; /** *This program prompts user to select items to purchase and displays a receipt * @author * @version 9/30/2013 */ public class GrapeFruitElectronicReceipt { /** * This method accepts prompts user for total devices purchased then * calculates and displays the information on a receipt * @param args the command line arguments */ public static void main(String[] args) { //Declare variables String sCustomerName; //Customer's name int nTotal=0; //Total number of devices ordered int nTPrice=0; //Total price double dTaxRate=0; //Tax rate int nItem=0; //The item the user selects int nPrice=0; //value associated with item double dFinalTotal = 0; //Total price plus tax final int GPodShuffle = 49; //value associated with item final int GPodTouch = 299; //value associated with item final int GPadMini = 329; //value associated with item final int GPad2 = 399; //value associated with item final int GPhone = 199; //value associated with item final int GMac = 1299; //value associated with item final int MacNovelPro = 1199; //value associated with item final int MacNovelAir = 999; //value associated with item final int Minimac = 599; //value associated with item Scanner input = new Scanner (System.in); //Used to read input from the console //Declare constants final double TAX = .065; final int SENTINEL = 10; //Prompt user to enter name System.out.print("Please enter your name:"); //Record person's name sCustomerName = input.nextLine(); //Blank line System.out.println(); //Display title GRAPEFRUIT PRODUCT System.out.println("GRAPEFRUIT PRODUCT"); //Display first product System.out.println("1." + " gPod shuffle " + " $49 " ); //Display second product System.out.println("2." + " gPod Touch " + " $299 "); //Display third produc System.out.println("3." + " gPad Mini " + " $329" ); //Display fourth product System.out.println("4." + " gPad 2 " + " $399 "); //Display fifth product System.out.println("5." + " gPhone " + " $199 " ); //Display sixth product System.out.println("6." + " gMac " + " $1299 " ); //Display seventh product System.out.println("7." + " MacNovel Pro " + " $1199 "); //Display eight product System.out.println("8." + " MacNovel Air " + " $999 "); //Display ninth product System.out.println("9." + " Minimac " + " $599 "); //Complete order System.out.println("10." + " Complete my order" ); //Prompt user to pick an item System.out.print("Please select an item from the menu above:"); //Record their answer nItem = input.nextInt(); //assign value to products if (nItem == 1){ //assign price to gpod nPrice = GPodShuffle; } //assign value to second product else if (nItem == 2){ //assign price to gpod touch nPrice = GPodTouch; } //assign value to third product else if (nItem == 3) { //assign prodcut to gpad mini nPrice = GPadMini; } //assign value to fourth product else if (nItem == 4){ //assign prodcut to gpad 2 nPrice = GPad2; } //assign value to fifth product else if (nItem == 5){ //assign product to gphone nPrice = GPhone; } //assign value to sixth product else if (nItem == 6){ //assign product to gmac nPrice = GMac; } //assign value to seventh product else if (nItem == 7) { //assign value to macnovel pro nPrice = MacNovelPro; } //assign value to eigth product else if (nItem == 8 ) { //assign value to macnovel air nPrice = MacNovelAir; } //assign value to ninth product else if (nItem == 9) { nPrice = Minimac; } //Price of Items ordered nTPrice = nTPrice + nPrice; //Declare loop while (nItem != SENTINEL){ //Statement System.out.print("Please select another item from the menu above:"); nTotal++; //Record item nItem = input.nextInt(); //assign value to products if (nItem == 1){ //assign price to gpod nPrice = GPodShuffle; } //assign value to second product else if (nItem == 2){ //assign price to gpod touch nPrice = GPodTouch; } //assign value to third product else if (nItem == 3) { //assign prodcut to gpad mini nPrice = GPadMini; } //assign value to fourth product else if (nItem == 4){ //assign prodcut to gpad 2 nPrice = GPad2; } //assign value to fifth product else if (nItem == 5){ //assign product to gphone nPrice = GPhone; } //assign value to sixth product else if (nItem == 6){ //assign product to gmac nPrice = GMac; } //assign value to seventh product else if (nItem == 7) { //assign value to macnovel pro nPrice = MacNovelPro; } //assign value to eigth product else if (nItem == 8 ) { //assign value to macnovel air nPrice = MacNovelAir; } //assign value to ninth product else if (nItem == 9) { nPrice = Minimac; //Price of Items ordered nTPrice = nTPrice + nPrice; } } //formula for tax rate dTaxRate = TAX *nTPrice; //formula for total price plus tax dFinalTotal = dTaxRate + nTPrice; //Display thank you message System.out.println("Thank you for odering with Grapefruit Company:" + sCustomerName); //Display items ordered System.out.println("Total items ordered:" + nTotal ); //Display Price of items ordered System.out.println("Price of items ordered:" + "$" + nTPrice ); //Display sales tax System.out.println("Sales tax:" + "$" + dTaxRate ); //Total amount due System.out.println("Total amound due:" + "$" + dFinalTotal ); } }
Just choosing one device works:
1. gPod shuffle $49
2. gPod Touch $299
3. gPad Mini $329
4. gPad 2 $399
5. gPhone $199
6. gMac $1299
7. MacNovel Pro $1199
8. MacNovel Air $999
9. Minimac $599
10. Complete my order
Please select an item from the menu above:10
Thank you for odering with Grapefruit Company:zach
Total items ordered:0
Price of items ordered:$0
Sales tax:$0.0
Total amound due:$0.0
BUILD SUCCESSFUL (total time: 4 seconds)
No devices works
GRAPEFRUIT PRODUCT
GRAPEFRUIT PRODUCT
1. gPod shuffle $49
2. gPod Touch $299
3. gPad Mini $329
4. gPad 2 $399
5. gPhone $199
6. gMac $1299
7. MacNovel Pro $1199
8. MacNovel Air $999
9. Minimac $599
10. Complete my order
Please select an item from the menu above:1
Please select another item from the menu above:10
Thank you for odering with Grapefruit Company:zach
Total items ordered:1
Price of items ordered:$49
Sales tax:$3.185
Total amound due:$52.185
BUILD SUCCESSFUL (total time: 6 seconds)
This is what happenss when i try to add multiple devices
Please enter your name:zach
GRAPEFRUIT PRODUCT
1. gPod shuffle $49
2. gPod Touch $299
3. gPad Mini $329
4. gPad 2 $399
5. gPhone $199
6. gMac $1299
7. MacNovel Pro $1199
8. MacNovel Air $999
9. Minimac $599
10. Complete my order
Please select an item from the menu above:1
Please select another item from the menu above:2
Please select another item from the menu above:10
Thank you for odering with Grapefruit Company:zach
Total items ordered:2
Price of items ordered:$49
Sales tax:$3.185
Total amound due:$52.185 It only calculates the first item and leave out the second, I cant figure out why.