Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
  1. #21
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try using switch cases. I may have to help you later when I have a computer, I have some helpful ideas for you.

  2. #22
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    where was the code missing braces?

  3. #23
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    No where. They are just formatted poorly. I'll have access to a computer in about 2 hours, so be around then if you can.

  4. #24
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    alright thank you

  5. #25
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, Im around a computer to help you out now. The first thing I need to do is get a better understanding of what can be done. There are some much better ways to do this, Im aware you cannot use extra classes, but are you allowed to use oop programming at all? Or is this all tended to be static methods working on info declared in main?

    Next, for each method tell me what you want them to return. How are they supposed to work? When you setup shop you just store items in parallel arrays? Using parallel arrays is usually not good and can lead to weird code. generally is you have items that have multiple items it helps to create a new class, but if you cannot we can work around this, it just makes the code a bit more challenging.

    One of my ideas is to make just a single class(which has a main method at the bottom.


    Java Code:
    public class TheStore{
      //the arrays as instance variable
      //methods to print everything, add items and do other interesting things
      //main
    }
    Inside main here you would declare an object of the class, but you will not initialize it. When you call the setup shop method it will initialize the object and add items to it. Perhaps the setup method could look like this
    Java Code:
    public TheStore createStore(){
      //create object
      //prompt for items and add to object
      //return object
    }

  6. #26
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I actually did almost this whole assignment in about 100 lines to get a better understanding. I did, however; use an extra class to make thinks a bit easier, but here is a method outline for you to view.

    Java Code:
    public class Shop{
      //instance variables //parallel arrays
      public static Shop createShop();  //basically a static factory method
      //I had this method prompt for items to fill the store with and it
      //returns a shop, therefore you can in main(after someone clicks 1)
      //set a shop variable to the return value of this method
    
      public String toString(); //this method is fairly basic //prints out all the items.
     
      public static void main(String[] args);  //do stuff
      
      //I didn't add a buy method but that's the only thing I believe
      //would be needed
    }

  7. #27
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    yes we are allowed to use oop programming, the return for each of the 4 steps should be as follows: setup shop should decided first if the user entered 1, if the user entered anything besides 1, then prompt the user to reenter a value until its 1, each time it should reprint the entire menu until the user enters 1, then it should ask the user to enter the number of items, then the name of the product/s, the price/s of the product/s, then to enter the amount to qualify for discount, then the discount rate(decimal), end of method 1, the program should then reprint the menu, again the user must input the correct number corresponding to the correct next chronological step, this time the user must input 2, if they input 1 then they should be referred to the previos step, essentially re-doing step1/method 1, if they enter anything besides 2 and not 1, then they should be prompted to reenter the correct value, if they enter 2 then they will be asked how many of each product/s, again the menu is shown to user, the user must once again also choose the correct next step, this time the user must enter 3, again the program must control for any other number that the user might enter, in method 3 it will simply display how many of each product, the product's name and the price for each product and the total cost of each product(amount of product * price of product). The menu is again reprinted, user must select 4 to see the checkout display which will list the total price, the amount of the discount, and the total after the discount, also the discount is only applied if the total is more than or equal to the value the user entered as the minimum purchase amount in order to qualify for the discount. Then the program is done, my only problem is that I am unable to reprint the menu if the user selects a number other than what they are supposed to input, my code only works if the user inputs 1,2,3,4. My program needs to be able to encounter inputs such as 3,2,3,4 and still be able to keep reprinting the menu until the user selects the correct number.

  8. #28
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default finished code

    For all those in need here is the code that I submitted:
    Java Code:
    import java.util.Scanner;
    public class shop3 {
    
        public static double setup(int max, String[] names, double[]prices){
            Scanner input=new Scanner(System.in);
    
            int i=0;
    
            do{
                System.out.print("Enter name of product " + i + ": ");
                names[i]=input.next();
                System.out.print("Enter price of product " + i + ": ");
                prices[i]=input.nextDouble();
                i++;
            }while(i<max);
            return max;
        }
    
        public static int Buy(String[] names,int[] amounts){
            Scanner input=new Scanner(System.in);
            int i = 0;
            do{
                System.out.print("Enter the amount of " + names[i] + " :");
                amounts[i]=input.nextInt();
                i++;
            } while(i<names.length);
            return amounts[i-1];
        }
    
        public static double listItems(String[] names,int[] amounts, double[]prices,double subtotal){
            int i = 0;
            do{
                System.out.println(amounts[i]+ " count of " + names[i] + " @ " + prices[i] + " = $" + (prices[i]*amounts[i]));
                subtotal+=(prices[i]*amounts[i]);
                i++;
            }while (i<names.length);
            return subtotal;
    
                      }
    
        public static double checkout(double total, double discountminimum, double discount, double subtotal){
    
            System.out.println("Thanks for coming!");
            if (subtotal>discountminimum){
                System.out.println("Sub Total: $" + subtotal);
                System.out.println("-Discount: $" + (discount*subtotal));
                total=(subtotal-(discount*subtotal));
                System.out.println("Total\t : $" + total);
            } 
            
            
            
            else if (subtotal < discountminimum) {
                discount = 0;
                System.out.println("Sub Total: $" + subtotal);
                System.out.println("-Discount: $" + (discount));
                total=(subtotal-(discount*subtotal));
                System.out.println("Total\t : $" + total);
                    }
                           return total;
        }
    
        public static void main(String[] args) {
    
            Scanner input=new Scanner(System.in);
    
            String[] names;
            double[] prices;
            int[] amounts;
    
            int max=0;
            int func;
            names = new String[max];
            prices = new double[max];
            amounts = new int[max];
            int total=0;
            double subtotal=0;
            double discount=0;
            double discountminimum = 0;
    
                      boolean seenZero = false;
            boolean seenOne = false;
    
            while(true){
                System.out.println("This program supports 4 functions:");
                System.out.println("\t1. Setup Shop");
                System.out.println("\t2. Buy");
                System.out.println("\t3. List Items");
                System.out.println("\t4. Checkout");
                System.out.print("Please choose the function you want:");
                             func=input.nextInt();    
    
                if (func==1){
                    seenZero = true;
                    System.out.print("Please enter the number of items:");
                              max=input.nextInt();
    
                    names = new String[max];
                    prices = new double[max];
                    amounts = new int[max];
    
                    setup(max, names, prices);
    
                    System.out.print("Please enter the amount to qualify for discount: ");
                           discountminimum=input.nextDouble();
                    System.out.print("Please enter the discount rate(0.1 for 10%):");
                            discount=input.nextDouble();
                    System.out.println("");
                }
                else if (func==2 && seenZero == true){
                    seenOne = true;
                    System.out.println("");
                    Buy(names,amounts);
                    System.out.println("");
                } 
                       else if (seenZero == false && func == 2) {
                       System.out.println("");
                    System.out.println("Shop is not setup yet!");
                    System.out.println("");
                }
                       else if (seenZero == false && func == 3) {
                    System.out.println("");
                    System.out.println("Shop is not setup yet!");
                    System.out.println("");
                }
                       else if (func==3 && seenZero == true && seenOne == true){
                    System.out.println("");
                    listItems(names,amounts,prices,subtotal);
                    System.out.println("");
                }
                else if (func == 3 && seenOne == false){
                    System.out.println("");
                    System.out.println("Try again: You have not bought anything");
                    System.out.println("");
                }
                else if (func==4 && seenZero == true && seenOne == true){
                    System.out.println("");
                    checkout(total,discountminimum,discount,listItems(names, amounts, prices, subtotal));
                    System.out.println("");
                    break;
                }
                else if (func==4 && seenZero == false){
                    System.out.println("");
                    System.out.println("Shop is not setup yet!");
                    System.out.println("");
                }
                else if (func==4 && seenOne == false){
                    System.out.println("");
                    System.out.println("Try again: You have not bought anything");
                    System.out.println("");
                }
                else{
                    System.out.println("");
                    System.out.println("Error: do not know " + func);
                    System.out.println("");
                }
            }
             }
    }

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: 02-03-2011, 10:19 PM
  2. I can only print string literals
    By leeavital in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 01:20 AM
  3. println doesn't print from inside for loop, et.al.
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 01:19 PM
  4. Loop System print or println statements
    By Singing Boyo in forum New To Java
    Replies: 6
    Last Post: 03-22-2009, 03:52 AM
  5. how to print output on same line in 'while loop'?
    By acidblue in forum New To Java
    Replies: 5
    Last Post: 12-13-2007, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •