Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with school assignment/arrays

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with school assignment/arrays

    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


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Help with school assignment/arrays



    What are you planning to do with this array? How are you populating it?


    As for printing it out, using a for loop seems like a good idea. Array indexes start at 0 and for an array of size n, goes up to index (n-1)

    int[] array = new int[9];

    array[8] refers to the value at the final index in the array. array[1] refers to the value at the second index.

    You can set the value at an index like this

    array[1] = 5;


    There is a variable called length. Using array.length will return the length of the array. Use this in your for loop to print it out.

    As for passing it to a method, you can just pass it as an array.


    public void print(int[] array)
    {



    }


    You call the method that prints the array, say like this


    int[] myarray = new int[9];

    // populate array


    print(myarray);


    If you are using arrays of two or more []s then you are using an array of arrays.

    int[][] 2DArray = new int[3][5];

    You would do the for loop for the first dimension like with a 1D but for the second dimension, then you'd refer to the array at index i (or whatever variable you used) for your outer for loop and call the .length on that to refer to the length of the array at that index.


    2DArray[1] would refer to an array of type int that has size 5.


    Does that help any? You aren't very specific on what you're asking help on other than needing to print out an array.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with school assignment/arrays

    Sorry if I wasn't specific enough, what a I need is an array that stores the items ordered by the user and then a method that would call said array, but I think you already described how to call it.

Similar Threads

  1. Help with Coding School Assignment
    By mattmattmatt in forum Loops & Control Statements
    Replies: 12
    Last Post: October 8th, 2013, 09:11 PM
  2. Java assignment school
    By Ur Nerd in forum Paid Java Projects
    Replies: 3
    Last Post: September 7th, 2013, 03:22 AM
  3. Problem with School Assignment.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 13th, 2012, 08:22 AM
  4. School Assignment AHH!
    By Europa in forum Loops & Control Statements
    Replies: 8
    Last Post: January 20th, 2012, 09:19 AM
  5. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM