Results 1 to 3 of 3
  1. #1
    kgstandoolot is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Post Help me with the code

    hello guys, i would really appreciate it if you helped me with the code.. How can i change the code so it just accepts letters r and c. and give error message for other character.

    here is the code

    import java.text.NumberFormat;
    import java.util.*;

    public class ValidatedInvoiceApp
    {
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    String choice = "y";

    while (!choice.equalsIgnoreCase("n"))
    {


    // get the input from the user
    System.out.print("Enter customer type (R/C): ");
    String customerType = sc.next();
    System.out.print("Enter subtotal: ");
    double subtotal = sc.nextDouble();

    double discountPercent = getDiscountPercent(customerType, subtotal);



    // calculate the discount amount and total
    double discountAmount = subtotal * discountPercent;
    double total = subtotal - discountAmount;

    // format and display the results
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    NumberFormat percent = NumberFormat.getPercentInstance();
    System.out.println(
    "Discount percent: " + percent.format(discountPercent) + "\n" +
    "Discount amount: " + currency.format(discountAmount) + "\n" +
    "Total: " + currency.format(total) + "\n");

    // see if the user wants to continue
    System.out.print("Continue? (y/n): ");
    choice = sc.next();
    System.out.println();
    }
    }

    private static double getDiscountPercent(String customerType, double subtotal)
    {
    // get the discount percent




    // double discountPercent = .0;

    if (customerType.equalsIgnoreCase("R"))
    {
    if (subtotal < 100)
    discountPercent = 0;
    else if (subtotal >= 100 && subtotal < 250)
    discountPercent = .1;
    else if (subtotal >= 250 && subtotal < 500)
    discountPercent = .25;
    else
    discountPercent = .3;
    }
    else if (customerType.equalsIgnoreCase("C"))

    discountPercent = .2;




    return discountPercent;


    }

    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    You will need to use an if statement and calls to equals method. Just like you did when checking choice is "y" or "n".

  3. #3
    acmohan is offline Member
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    just check 'R/C' using if condition in main method only. It will me more appropriate.

Similar Threads

  1. look @ code and give ideas! Install new code....
    By turbowhat in forum New To Java
    Replies: 2
    Last Post: 04-27-2011, 03:08 AM
  2. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  3. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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