For our school assignment we had to write a program that would display a menu of items and allow the user to select from them, and then the program calculates all the items prices, tax, total, and prints a receipt. The new assignment which is based off the previous one, is to now store the items ordered in an array and then print a method that calls the array. I know that an array needs to be declared in the variables like int [] naNum = new int [9], and then i would need a loop, but i''m not really sure where to start and how to properly write a loop using an array.
package electronicreceiptarray; import java.util.Scanner; /** * * @author Zach */ public class ElectronicReceiptArray { /** * 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 double 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 Scanner input = new Scanner (System.in); //Used to read input from the console //Declare constants final double TAX = .065; final int SENTINEL = 10; 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 //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; } else{ //Price of Items ordered nTPrice = nTPrice + nPrice; //formula for tax rate dTaxRate = TAX *nTPrice; //formula for total price plus tax dFinalTotal = dTaxRate + nTPrice; } //Declare loop while (nItem != SENTINEL){ //Statement System.out.print("Please select another item from the menu above:"); //Record item nItem = input.nextInt(); //calculate sum nTPrice = nTPrice + nPrice; //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; } nTotal++; } //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 amount due:" + "$" + dFinalTotal ); }//end Main Method } // end class GrapeFruitElectronicReceipt