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 13 of 13

Thread: need help with writing this program

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help with writing this program

    1 Create a java file in NetBeans, name it Catalog.java.
    2 Create one String array to store 3 products for a catalogue
    3 Create the appropriate variables to store the product name, product code and product price.
    4 At the start of the program display the catalogue for the user by looping through the array and outputting it to the screen with a number listing for each product, as shown above
    5 Create an infinite loop to enter orders; to stop the loop the user should enter 0.
    6 Keep a running total (accumulator) of all products amount total and sub-total (multiple accumulators)
    7 Write a method to calculate the taxes and return a grand total
    8 Write another method to print out the order as listed above

    hadda look like this

    Our catalogue [Product Codes in brackets]:

    (1) Condensed Milk [M3487], $9.50 per can.
    (2) Distilled Water [W3876], $3.00 a bottle.
    (3) Pack Rice [R9983], $12.75 for 5lbs.
    Buy something!

    Enter Order Number (0 to stop): M3487
    Enter Quantity: 2
    Enter Order Number (0 to stop): W3876
    Enter Quantity: 3
    Enter Order Number (0 to stop): R9983
    Enter Quantity: 3
    Enter Order Number (0 to stop): 0

    --- Update ---

    this is the code i have so far

    its not entering the if statement...can anyone help??
    package catalog;
     
    import java.util.*;
     
    public class Catalog {
     
       static String products[] = new String[3];
       static int answer;
     
        public static void main(String[] args) {
     
                 System.out.println("------------------");
                 System.out.println("Shopping Catalog");
                 System.out.println("------------------");
     
                  String[] pCode = new String[3];
                  float pPrice[] = new float[3];
                  int orderNum = 0;
                  int quantity=0;
     
                 Scanner s = new Scanner(System.in);
                 System.out.println("------------------------------------------");
                    System.out.println("condensed milk [M3487], $9.50 per can.");
                        System.out.println("");
                    System.out.println("Distilled Water [W3876], $3.00 a bottle.");
                        System.out.println("");
                    System.out.println("Pack Rice [R9983], $12.75 for 5lbs.");
                 System.out.println("------------------------------------------");
     
     
     
     
                    do{
     
                             System.out.println("Please enter order number (0 to stop)");
                                        pCode[orderNum] = s.nextLine();
     
                                                if(pCode[orderNum] == "M3487"){
                                                    System.out.println("condensed milk $9.50");
                                                        System.out.println("Enter Quantity");
                                                            quantity = s.nextInt();
                                                }//close if statement
                                                orderNum++;
                         if(answer == 0){
                             break;
                         }//close if
                    }while(true);//close while loop
     
        }//close main method
     
    }//close class
    Last edited by nickans; May 5th, 2014 at 07:42 AM.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: need help with writing this program

    I think if(pCode[orderNum] == "M3487") is the if statement you are talking about.
    The reason is pCode[0] - pCode[n] has a value of "null" (String "null" not null pointer).
    why? because you never declare the value of each element. you just declared an array of String.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: need help with writing this program

    nickans, you've been asked: Please post your code correctly. If uncertain how, please review this link.

  4. #4
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with writing this program

    [QUOTE=nickans;147057]1 Create a java file in NetBeans, name it Catalog.java.
    2 Create one String array to store 3 products for a catalogue
    3 Create the appropriate variables to store the product name, product code and product price.
    4 At the start of the program display the catalogue for the user by looping through the array and outputting it to the screen with a number listing for each product, as shown above
    5 Create an infinite loop to enter orders; to stop the loop the user should enter 0.
    6 Keep a running total (accumulator) of all products amount total and sub-total (multiple accumulators)
    7 Write a method to calculate the taxes and return a grand total
    8 Write another method to print out the order as listed above

    hadda look like this

    Our catalogue [Product Codes in brackets]:

    (1) Condensed Milk [M3487], $9.50 per can.
    (2) Distilled Water [W3876], $3.00 a bottle.
    (3) Pack Rice [R9983], $12.75 for 5lbs.
    Buy something!

    Enter Order Number (0 to stop): M3487
    Enter Quantity: 2
    Enter Order Number (0 to stop): W3876
    Enter Quantity: 3
    Enter Order Number (0 to stop): R9983
    Enter Quantity: 3
    Enter Order Number (0 to stop): 0

    --- Update ---

    this is the code i have so far

    its not entering the if statement...can anyone help??
    package catalog;
     
    import java.util.*;
     
    public class Catalog {
     
       static String products[] = new String[3];
       static int answer;
     
        public static void main(String[] args) {
     
                 System.out.println("------------------");
                 System.out.println("Shopping Catalog");
                 System.out.println("------------------");
     
                  String[] pCode = new String[3];
                  float pPrice[] = new float[3];
                  int orderNum = 0;
                  int quantity=0;
     
                 Scanner s = new Scanner(System.in);
                 System.out.println("------------------------------------------");
                    System.out.println("condensed milk [M3487], $9.50 per can.");
                        System.out.println("");
                    System.out.println("Distilled Water [W3876], $3.00 a bottle.");
                        System.out.println("");
                    System.out.println("Pack Rice [R9983], $12.75 for 5lbs.");
                 System.out.println("------------------------------------------");
     
     
     
     
                    do{
     
                             System.out.println("Please enter order number (0 to stop)");
                                        pCode[orderNum] = s.nextLine();
     
                                                if(pCode[orderNum] == "M3487"){
                                                    System.out.println("condensed milk $9.50");
                                                        System.out.println("Enter Quantity");
                                                            quantity = s.nextInt();
                                                }//close if statement
                                                orderNum++;
                         if(answer == 0){
                             break;
                         }//close if
                    }while(true);//close while loop
     
        }//close main method
     
    }//close class

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: need help with writing this program

    Wasn't this answer provided in your other thread? Have you lost track, or am I remembering incorrectly? Multiple threads on the same topic end up wasting time and causing confusion so are discouraged.

  6. #6
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with writing this program

    yes it was, the other thread was solved. i'm confused as to where should i go from here. That's why i posted it here with additional information

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with writing this program

    so i've gotten this far, but i cant get a grand total. can anyone help with it??

     
    package catalog;
     
     
    import java.util.*;
    public class Catalog1 {
        private static final String[] products = new String[3];
       private static float total=0.0f; 
       private static final float tax = 15/100;
       private static final float[] subTotal = new float[3];
       private static final float[] itemTotal = new float[3];
       private static final double[]pPrice = new double[3];
       private static String []pCode = new String[10];
        public static void main(String[] args) {
                products[0] = "Condensed Milk"; 
                 products[1] = "Distilled Water";
                 products[2] = "Packed rice";
                 pPrice[0] = (9.50);
                 pPrice[1] = (3.00);
                 pPrice[2] = (12.75);
                int quantity = 0;
                int orderNum = 0;
             Scanner s = new Scanner(System.in);  
     
     
     
                 do{ 
                        System.out.println("------------------------------------------");
                        System.out.println("condensed Milk[M3487], $9.50 per can.");
                        System.out.println("");
                        System.out.println("Distilled Water [W3876], $3.00 a bottle.");
                        System.out.println("");
                        System.out.println("packed Rice [R9983], $12.75 for 5lbs.");
                        System.out.println("------------------------------------------");
     
                             System.out.println("Please enter order number (0 to stop)");
                                        pCode[orderNum] = s.nextLine();
     
                                                if(pCode[orderNum].equals("M3487")){
                                                    System.out.println("condensed Milk $9.50");
                                                        System.out.println("Enter Quantity");
                                                            quantity = s.nextInt();
                                                               itemTotal[0] = (float) (pPrice[0]*quantity); 
                                         System.out.println(quantity + " condensed Milk @ $9.50 = $" + itemTotal[0]);
     
                                                }else if(pCode[orderNum].equals("W3876")){
                                                     System.out.println("Distilled Water $3.00");
                                                        System.out.println("Enter Quantity");
                                                            quantity = s.nextInt();
                                                                itemTotal[1] = (float) (pPrice[1] * quantity);
                                            System.out.println(quantity +" Distilled Water @ 3.00 = $" + pPrice[1]*quantity);
     
                                                }else if(pCode[orderNum].equals("R9983")){
                                                     System.out.println("packed Rice $12.75");
                                                       System.out.println("Enter Quantity");
                                                           quantity = s.nextInt();
                                                                itemTotal[2] = (float) (pPrice[2] * quantity);
                                             System.out.println(quantity +" Distilled Water @ 3.00 = $" + pPrice[2]*quantity);
     
                                                    }else if(pCode[orderNum].equals("0")){
                                                        System.out.println("your total bill is $" + total);
                                                        break;
                                                    }//close if statement
                                                orderNum++;
                                                quantity++;
     
     
                    }while(true);//close while loop
     
        }
        public static float totalBill() {
     
     
                   subTotal[0] = itemTotal[0] * tax;
                   total = subTotal[0] + subTotal[1] + subTotal[2] + itemTotal[0] + itemTotal[1] + itemTotal[2];
    		return total ; 
     
    	}//close bill method
    }

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: need help with writing this program

    What terms are included in the grand total? Once you've determined the answer to that, add those terms together and print them in a statement as the Grand Total = result.

  9. #9
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with writing this program

    i got it.. problem was that i wasn't calling it in main. but a new problem arrived, after i enter the product code and quantity, the menu appears twice.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: need help with writing this program

    So the logic to leave whatever loop the code is in at the time isn't working. Review that logic and fix it.

    There's probably a better way than a do/while( true ) loop to present the menu. I'm mostly prejudiced agains while ( true ) loops of any kind, so if it's working for you, ignore me. But there's a better way.

  11. #11
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with writing this program

    instead of the while(true)..what would u use??

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: need help with writing this program

    A deterministic approach that applies logic rather than accepting a default result that must be broken out of.

  13. #13
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with writing this program

    having it in an infinite loop was within the specification of the assignment.

Similar Threads

  1. Writing an encryption program, and keep getting this exception.
    By Jdafforn1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 11th, 2013, 07:33 AM
  2. help with writing a letters encoding program
    By Samedge20 in forum Object Oriented Programming
    Replies: 8
    Last Post: July 14th, 2013, 09:18 AM
  3. Need help writing a Java program
    By goose05 in forum Loops & Control Statements
    Replies: 9
    Last Post: April 3rd, 2012, 07:00 AM
  4. writing a program to interface with other programs
    By gib65 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: November 4th, 2011, 04:16 PM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM

Tags for this Thread