Question
1. Modify the Invoice Application that: Validates the input of the subtotal using the Scanner class method hasNextDouble o This validation must be done using
1. Modify the Invoice Application that:
Validates the input of the subtotal using the Scanner class method hasNextDouble
o This validation must be done using a static method called getSubtotalInput. The method will return the input from the user only when it is a valid double. Upon error, the user will be prompt with an error message and the user will not be able to move forward in the application workflow until a valid entry is provided.
o Valid entry for the Subtotal is a positive double value.
Refactor the calculation of the discount percentage in a static method called calculateDiscountPercentage
Refactor all user inputs to static methods. Validate all user inputs. Add try catch blocks to trap any errors inputted by the user. Return types are based off the required user input.
Refactor the final print to the console to a static method called prtingFormattedResults
Invoice App:
package invoiceapp;
import java.text.NumberFormat; import java.util.Scanner;
public class InvoiceApp {
public static void main(String[] args) { printLogo(); Scanner input = new Scanner(System.in);
String choice = "y"; while (choice.equalsIgnoreCase("y")) { final double SALES_TAX_PCT =0.14; System.out.print("System ready...");
System.out.println("Enter a subtotal: "); double discountPercent = 0.0; //store the calculated discount double subTotal=input.nextDouble(); System.out.println("Enter your customer type"); String customerType = input.nextLine();
switch(customerType){ case "R": case "r": if(subTotal>250){ discountPercent=.2; } else if(subTotal>=100 && subTotal <= 250){ discountPercent=.1; } else{ discountPercent=0; }; break; case "C": case "c": if(subTotal<250){ discountPercent=.2; } else if(subTotal<=250){ discountPercent=.3; } break; default: discountPercent = 0.1; break; }
double discountAmount=subTotal*discountPercent; double totalBeforeTax = subTotal-discountAmount; double salesTax=totalBeforeTax*SALES_TAX_PCT; double total = totalBeforeTax+salesTax; NumberFormat percent = NumberFormat.getPercentInstance(); NumberFormat currency=NumberFormat.getCurrencyInstance(); String message = "Discount percent: "+percent.format(discountPercent) + " "; message += "Discount amount: "+ currency.format(discountAmount)+" "; message+="Total before tax: "+currency.format(totalBeforeTax)+" "; message+="Sales tax: "+currency.format(salesTax)+" "; message+="Invoice total: "+currency.format(total)+" "; System.out.println(message); String debugMessage = " UNFORMATTED RESULTS " +"Discont percent: "+discountPercent+" " +"Discount amount: "+ discountAmount+" " +"Total before tax: "+totalBeforeTax+" " +"Sales tax: "+salesTax+" " +"Invoice total: "+total+" " +" FORMATTED RESULTS"; System.out.println(debugMessage); System.out.print("Continue? (y/n): "); choice = input.next(); System.out.println(); } } public static void printLogo(){ System.out.println(" ,_ . ._. _. ."); System.out.println(" , _-\\','|~\\~ ~/ ;-'_ _-' ,;_;_, ~~-"); System.out.println(" /~~-\\_/-'~'--' \\~~| ', ,' / / ~|-_\\_/~/~ ~~--~~~~'--_"); System.out.println(" / ,/'-/~ '\\ ,' _ , '|,'|~ ._/-, /~"); System.out.println(" ~/-'~\\_, '-,| '|. ' ~ ,\\ /'~ / /_ /~"); System.out.println(".-~ '| '',\\~|\\ _\\~ ,_ , /|"); System.out.println(" '\\ /'~ |_/~\\\\,-,~ \\ \" ,_,/ |"); System.out.println(" | / ._-~'\\_ _~| \\ ) /"); System.out.println(" \\ __-\\ '/ ~ |\\ \\_ / "); System.out.println(" ., '\\ |, ~-_ - | \\\\_' ~| /\\ \\~ ,"); System.out.println(" ~-_' _; '\\ '-, \\,' /\\/ |"); System.out.println(" '\\_,~'\\_ \\_ _, /' ' |, /|'"); System.out.println(" / \\_ ~ | / \\ ~'; -,_."); System.out.println(" | ~\\ | | , '-_, ,; ~ ~\\"); System.out.println(" \\, / \\ / /| ,-, , -,"); System.out.println(" | ,/ | |' |/ ,- ~ \\ '."); System.out.println(" ,| ,/ \\ ,/ \\ |"); System.out.println(" / | ~ -~~-, / _"); System.out.println(" | ,-' ~ /"); System.out.println(" / ,' ~"); System.out.println(" ',| ~"); System.out.println(" ~'"); System.out.println(" "); System.out.println("/****************************************************************************/"); System.out.println("* WORLD BANK INVOICE CALCULATOR *"); System.out.println("/****************************************************************************/"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started