3 Replies - 230 Views - Last Post: 13 February 2013 - 07:53 PM Rate Topic: -----

#1 indigrace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-February 13

Question with Array Output with my Program

Posted 13 February 2013 - 02:09 AM

Hello! For class, we have been assigned to build a java project for our final. I have built a quote calculator for a t-shirt company. However, part of the requirements is to include an array. We have not come to that subject yet in class so I'm reading ahead and trying to find examples so I can customize them to fit my project but I'm not having luck.

What works: My project as it is now works great for quoting with applicable discounts.
What doesn't: The array code I tried to include. ALSO, I can't seem to figure out how to make it go to the array list display with the user enters "n" when asked if they want to get another quote, it just ends the program.
What I'm looking for: When the user chooses "n" when asked if they would like another quote, I would like to have a rectangle array list of the multiple quotes that shows them:

Shirt Cost Setup Fee Discount Total

I am brand new to Java so I hope I haven't bitten off more than I can chew. I tried reading ahead in our text and have gotten overwhelmed. Thank you for your help!!

import java.text.NumberFormat;
import java.util.Scanner;

public class HighOctaneApp {

     public static void main(String[] args)
    {
        // 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";

            // print the results
            System.out.println();
            System.out.println("INITIAL QUOTE");
            System.out.println(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();
        }
    }
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.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;
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Question with Array Output with my Program

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: Question with Array Output with my Program

Posted 13 February 2013 - 03:07 AM

Quote

What doesn't: The array code I tried to include.
There's no sign of even an attempt in the code you posted. What's more, it's difficult to see how you would use one without (properly) refactoring your code to be object-oriented. The result, otherwise, would probably be a mess of parallel arrays which is a no-no on several different levels.

This post has been edited by g00se: 13 February 2013 - 03:07 AM

Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Question with Array Output with my Program

Posted 13 February 2013 - 10:38 AM

A correctly developed Java application should, most of the case, have a single static method and it is the main() method.

All other method shouldn't be static as they apply to an object of the class

The main() method should only have a few lines (1-5) where an object of the class is instantiate and after that everything happens in the class methods themselve

:^:
Was This Post Helpful? 1
  • +
  • -

#4 indigrace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-February 13

Re: Question with Array Output with my Program

Posted 13 February 2013 - 07:53 PM

View Postpbl, on 13 February 2013 - 10:38 AM, said:

A correctly developed Java application should, most of the case, have a single static method and it is the main() method.

All other method shouldn't be static as they apply to an object of the class

The main() method should only have a few lines (1-5) where an object of the class is instantiate and after that everything happens in the class methods themselve

:^:/>/>


Ok, here is the code that I've been working on all day. Again, please remember I've only been doing Java for 3 weeks so I'm stumbling through some things. The code I posted earlier was my pre-Arrays attempts. The program still compiles and runs correctly but I still can't get it to advance when you choose "n" for another quote so I can't see if the information has been collected by the array. Or if what I'm even saying makes sense.

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Scanner;

public class NewClass {

     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;
}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1