Results 1 to 4 of 4
  1. #1
    Indigrace is offline Member
    Join Date
    Feb 2013
    Location
    Scenic Flint, MI
    Posts
    6
    Rep Power
    0

    Default Stuck on how to print ArrayList in Project I've Created

    Please forgive me but I've made this program by hand and I think I'm close to finishing my final project, I just have to add the use of Arrays, which we HAVE NOT covered yet in class. Our text isn't that helpful with examples (I'm not even sure if I have to add another class to the project!!) so I'll try to explain what I'm trying to do.
    -I'm trying to have the customer run multiple quotes, have the arraylist store the calculations and once they are finished and the program ends, it will print a summary of the calculations to the console.
    -I also don't know how to make it go to displaying the quotes from entering "No" when asking if they'd like to do another quote.
    -I apologize but I am a noob and I've been up for 28 hours trying to put the finishing touches when I hit the array requirement. All the rest of the code should be working perfectly, I'm just stuck now so I appreciate any and all help!!

    Java Code:
    import java.text.NumberFormat;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class HighOctaneApp {
    
         public static void main(String[] args)
        {
            ArrayList<String> code =  new ArrayList<>();
            
            // display a welcome message
            System.out.println("Welcome to the High Octane Quote Calculator");
            System.out.println();
    
            // perform 1 or more calculations
            Scanner sc = new Scanner(System.in);
            String choice = "y";
            while (choice.equalsIgnoreCase("y"))
            {
    
                // get the input from the user
                System.out.println("Please complete the following fields");
                System.out.print("Enter customer type= New or Existing (n/e): ");
                String customerType = sc.next();
                double shirtQuantity = getDoubleWithinRange(sc,
                    "Enter Shirt Quantity: ", 0, 1000);
                int colors = getColorWithinRange(sc,
                    "Enter number of Colors in Design ($20/color): ", 0, 8);
                
                // get the discount percent
                double discountPercent = getDiscountPercent(customerType, shirtQuantity);
    
                // calculate the discount amount and total
                double shirtCost = shirtQuantity * 7;
                double discountAmount = shirtCost * discountPercent;
                double subtotal = shirtCost - discountAmount;
                int setupColor = colors * 20;
                double Total = subtotal + setupColor;
    
                // get the currency and percent formatters
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                NumberFormat percent = NumberFormat.getPercentInstance();
                percent.setMinimumFractionDigits(1);
    
                // format the result as a single string
                String results =
                      "Shirt Cost:\t"
                          + currency.format(shirtCost) + "\n"
                    + "Color Setup Fee:\t"
                          + currency.format(setupColor) + "\n"
                    + "Discount Amount:\t"
                          + currency.format(discountAmount) +  "\n"
                    + "Current Total (includes applicable discounts):\t\t"
                          + currency.format(Total) + "\n";
    
                // save the String in the ArrayList
                code.add(results);
                 // see if the user wants to calculate another quote
                System.out.print("Would you like to quote another project? (y/n): ");
                choice = sc.next();
                System.out.println();
            }   
                // print the results
                System.out.println();
                System.out.println("INITIAL QUOTE");
                System.out.println("Shirt\tCost\tSetup\tFee\t\t\tDiscount\t\t\tTotal");
                System.out.println("------.\t----\t\t\t-----\t\t\t------");
    
           // prints the Strings stored in the ArrayList
               for(int i = 0; i < code.size(); ++i) {
                System.out.println(code.get(i));
            }
        }
         
            
    public static double getDouble(Scanner sc, String prompt)
        {
            boolean isValid = false;
            double d = 0;
            while (isValid == false)
            {
                System.out.print(prompt);
                if (sc.hasNextDouble())
                {
                    d = sc.nextDouble();
                    isValid = true;
                }
                else
                {
                    System.out.println("Error! Invalid decimal value. Try again.");
                }
                sc.nextLine();  // discard any other data entered on the line
            }
            return d;
        }
    
        public static double getDoubleWithinRange(Scanner sc, String prompt,
        double min, double max)
        {
            double d = 0;
            boolean isValid = false;
            while (isValid == false)
            {
                d = getDouble(sc, prompt);
                if (d <= min) {
                    System.out.println(
                        "Error! Number must be greater than " + min + ".");
                }
                else if (d >= max) {
                    System.out.println(
                        "Error! Number must be less than " + max + ".");
                }
                else {
                    isValid = true;
                }
            }
            return d;
        }
    
        public static int getInt(Scanner sc, String prompt)
        {
            boolean isValidInt = false;
            int i = 0;
            while (isValidInt == false)
            {
                System.out.print(prompt);
                if (sc.hasNextInt())
                {
                    i = sc.nextInt();
                    isValidInt = true;
                }
                else
                {
                    System.out.println("Error! Invalid entry. Y/N only. Try again.");
                }
                sc.nextLine();  // discard any other data entered on the line
            }
            return i;
        }
    
        public static int getColorWithinRange(Scanner sc, String prompt,
        int min, int max)
        {
            int i = 0;
            boolean isValid = false;
            while (isValid == false)
            {
                i = getInt(sc, prompt);
                if (i <= min) {
                    System.out.println(
                        "Error! Number must be greater than " + min + ".");
                }
                else if (i >= max) {
                    System.out.println(
                        "Error! Number must be less than " + max + ".");
                }
                else {
                    isValid = true;
                }
            }
            return i;
        }   
            
         public static double calculateTotal(double subtotal, int setupColor){
             double Total;
             { 
                 Total = (subtotal + setupColor);
             }
             return Total;
         }
         
            // a static method that that has two parameters: customer type and subtotal.
        /**
         *
         * @param customerType
         * @param shirtQuantity
         * @return discountPercent
         */
        public static double getDiscountPercent(String customerType, double shirtQuantity) {
               double discountPercent = 0;
               if (customerType != null) {
                if (customerType.equalsIgnoreCase("E"))
                {
                    if (shirtQuantity < 40) {
                        discountPercent = 0;
                    }
                    else if (shirtQuantity >= 40 && shirtQuantity < 100) {
                        discountPercent = .2;
                    }
                    else if (shirtQuantity >= 100) {
                        discountPercent = .3;
                    }
                }
                else if (customerType.equalsIgnoreCase("N"))
                {
                    if (shirtQuantity < 40) {
                        discountPercent = 0.5;
                    }
                    else if (shirtQuantity >= 40 && shirtQuantity < 100) {
                        discountPercent = .25;
                    }
                    else if (shirtQuantity >= 100) {
                        discountPercent = .35;
                    }
                }
            }
     
                return discountPercent;
    }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Stuck on how to print ArrayList in Project I've Created

    What is your actual question? What do you get and what do you expect? Do you get compiler errors or runtime errors?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Indigrace is offline Member
    Join Date
    Feb 2013
    Location
    Scenic Flint, MI
    Posts
    6
    Rep Power
    0

    Default Re: Stuck on how to print ArrayList in Project I've Created

    Quote Originally Posted by PhHein View Post
    What is your actual question? What do you get and what do you expect? Do you get compiler errors or runtime errors?
    No, I get zero errors, which is great for the rest of the code BUT I am missing something when it comes to using an array for my final project requirement. What I would like it to do is when the person chooses "n" when asked if they'd like another quote, is for the program to print a summary of the multiple quote calculations that were made in a rectangle. I'm flying somewhat blind since we haven't covered it in class yet but I needed to declare variables at the beginning of the main method for a rectangular array (which I believe I've done correctly), add code that stores the formatted values as strings (I think I may have done or am close), and then add code to display elements in the array at the console when the user indicates the program should end. I've tried to piece together what I could but even reading ahead in our text, I could not find any comparable examples or explanations. I just need to finish what I have for the arrays part, then I can check my final project off as done. Thank you so much for your patience.

    **What I do get is a correct output for the rest of the application but when you enter "n" when it asks if you'd like to do another quote, it just ends the application. That's when I need it to print out the array.
    Last edited by Indigrace; 02-12-2013 at 02:46 PM.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Stuck on how to print ArrayList in Project I've Created

    Quote Originally Posted by Indigrace View Post
    **What I do get is a correct output for the rest of the application but when you enter "n" when it asks if you'd like to do another quote, it just ends the application. That's when I need it to print out the array.
    Java Code:
    // prints the Strings stored in the ArrayList
               for(int i = 0; i < code.size(); ++i) {
                System.out.println(code.get(i));
    }
    That part of your code prints out stuff if you have entered at least one quote. This is what I get:
    Java Code:
    Welcome to the High Octane Quote Calculator
    
    Please complete the following fields
    Enter customer type= New or Existing (n/e): n
    Enter Shirt Quantity: 1
    Enter number of Colors in Design ($20/color): 2
    Would you like to quote another project? (y/n): n
    
    
    INITIAL QUOTE
    Shirt	Cost	Setup	Fee			Discount			Total
    ------.	----			-----			------
    Shirt Cost:	7,00 €
    Color Setup Fee:	40,00 €
    Discount Amount:	3,50 €
    Current Total (includes applicable discounts):		43,50 €
    So again, what do you get and what do you expect?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Replies: 3
    Last Post: 03-12-2012, 03:03 PM
  2. I stuck my project from any piece
    By papyon in forum Java 2D
    Replies: 3
    Last Post: 12-05-2011, 09:06 PM
  3. Replies: 1
    Last Post: 12-18-2009, 07:18 PM
  4. Replies: 0
    Last Post: 03-24-2009, 04:23 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Tags for this Thread

Posting Permissions

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