ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 2 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 05 May 2017 My Price 9.00

validate the input, display an error message

I need to modify the Continue? (y/n): code so when a user enters two or more values the application will still work correctly. Meaning it should work even if the user makes tow or more entries for a single prompt. I believe I need to validate the input, display an error message until either Y, y, N, or n is entered and discard any extra entries.

import java.text.NumberFormat;

import java.util.InputMismatchException;

import java.util.Scanner;

 

public class InvoiceApp

{

 

public static String getValidCustomerType(Scanner sc)

{

String customerType="";

while(true)

{

System.out.print("Enter customer type (r/c): ");

customerType= sc.next();

if(customerType.equalsIgnoreCase("c") ||customerType.equalsIgnoreCase("r"))

break;

 

else

System.out.println("invalid code,");

}

 

return customerType;

 

}

 

 

public static double getValidSubtotal (Scanner sc)

{

double ;

String garbagedata;

while(true)

{

System.out.print("Enter subtotal: ");

if( sc.hasNextDouble())

{

.nextDouble();

if(subTotal>0 && subTotal<1000)

break;

}

else

{

.next();

 

}

 

System.out.println("invalid subTotal, please enter a double value greater than 0 and less than 1000");

}

 

return subTotal;

 

 

 

}

 

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

String choice = "y";

double discountPercent = 0;

double ;

 

 

while (choice.charAt(0)=|| choice.charAt(0)=)

{

try

{

// get the input from the user

String customerType (sc);

 

(sc);

// get the discount percent

if (customerType.equalsIgnoreCase("R"))

{

if (subtotal < 100)

discountPercent = 0;

else if (subtotal >= 100 && subtotal < 250)

discountPercent = .1;

else if (subtotal >= 250)

discountPercent = .2;

}

else if (customerType.equalsIgnoreCase("C"))

{

if (subtotal < 250)

discountPercent = .2;

else

discountPercent = .3;

}

else

{

discountPercent = .1;

}

}

{

catch(InputMismatchException e)

}

// 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();

}

}

}

Answers

(11)
Status NEW Posted 05 May 2017 02:05 AM My Price 9.00

-----------

Not Rated(0)