Question
Add data validation to the solution to code below . Make sure that r c and t are the only values that can be entered
Add data validation to the solution to code below . Make sure that r c and t are the only values that can be entered subtotal can only be double and between 0 and 1000 You have to enter a y or n to end or continue. Ensure y or n only no other values. Specifications The application requires a numeric value, the application should continue prompting the user until the user enters a valid number within range. The application requires a string value, it should continue prompting the user until the user enters a valid string value. (r c or t) (y or n) Use BookApp ch05_FutureValueValidation as a template The code thats used to validate data should be stored in separate methods. For example: public static double getDoubleWithinRange(Scanner sc, String prompt,double min, double max) public static int getIntWithinRange(Scanner sc, String prompt,int min, int max)
Code :
package javaprojectv;
import java.text.NumberFormat; import java.util.Scanner;
/** * * */ public class JavaProjectV {
/** * @param args the command line arguments */ 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(); // get the discount percent double discountPercent = 0.0; switch(customerType) { case "r": case "R": if (subtotal < 100) { discountPercent = 0.0; } else if (subtotal >= 100 && subtotal < 250) { discountPercent = .1; } else if (subtotal >= 250) { discountPercent = .2; } break; case "c": case "C": if (subtotal < 250) { discountPercent = .2; } else if (subtotal >= 250) { discountPercent = .3; } break; default: discountPercent = .1; break; } // calculate the discount amount and round to 2 decimals double discountAmount = subtotal * discountPercent; discountAmount = Math.ceil(discountAmount * 100) / 100;
// calculate the total 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) + " " + "Discount amount: " + currency.format(discountAmount) + " " + "Total: " + currency.format(total) + " ");
// see if the user wants to continue System.out.print("Continue? (y/n): "); choice = sc.next(); 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