Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project 5-2: Data Validation Add data validation to the solution to code below in java. Make sure that r c and t are the only

Project 5-2: Data Validation Add data validation to the solution to code below in java. 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:

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

public class InvoiceApp {

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/t): "); 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; } else if (subtotal >= 250 && subtotal < 500) { discountPercent = .25; } else if (subtotal > 500) { discountPercent = .3; } break; case "c": case "C": discountPercent = .20; // if (subtotal < 250) { // discountPercent = .2; // } else if (subtotal >= 250) { // discountPercent = .3; // } break; case "t": case "T": if (subtotal < 500) { discountPercent = .4; } else { discountPercent = .5; } 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions