I Have no idea where to start on this I need to modify this code and make these
ID: 3539199 • Letter: I
Question
I Have no idea where to start on this I need to modify this code and make these changes.
-Start a new class named Invoice and save it in the same package as the other classes. Then write the code for this class as described here.
- Include two private fields for the customer type and subtotal entered by the user
-Include a single construct that accepts the customer type and subtotal as parameters
i inclide a get method that returns the subtotal as a double value. In addition, include get methods that calculate and return double values for the discount percent, discount amount, and total
-Inclide get methods that return formatted string values for the subtaotal, discoount percent, discoint amount, and total. These methods should call to the other get methods to get the values formatted.
- inclide a get method that returns a string that contains the full name for the customer type.
- Modify the code in the invoiceApp class so it creates an Invoice object. Then, call methods of the Invoice objects to disply formatted values for the invoice, and delete any code that is no longer needed. That should simplify the invoiceApp class considerably.
here is the code
import java.text.NumberFormat;
import java.util.Scanner;
public class InvoiceApp
{
public static void main(String[] args)
{
// display a welcome message
System.out.println("Welcome to the Invoice Total Calculator");
System.out.println(); // print a blank line
Scanner sc = new Scanner(System.in);
String choice = "y";
while(choice.equalsIgnoreCase("y"))
{
// get user entries
String customerType = Validator.getString(sc,
"Enter customer type (r/c): ");
double subtotal = Validator.getDouble(sc,
"Enter subtotal: ", 0, 10000);
// calculate the discount amount and total
double discountPercent = getDiscountPercent(subtotal, customerType);
double discountAmount = subtotal * discountPercent;
double total = subtotal - discountAmount;
// format the values
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
String sSubtotal = currency.format(subtotal);
String sCustomerType = "Unknown";
if (customerType.equalsIgnoreCase("r"))
sCustomerType = "Retail";
else if (customerType.equalsIgnoreCase("c"))
sCustomerType = "College";
String sDiscountPercent = percent.format(discountPercent);
String sDiscountAmount = currency.format(discountAmount);
String sTotal = currency.format(total);
// format and display the results
String message = "Subtotal: " + sSubtotal + " "
+ "Customer type: " + sCustomerType + " "
+ "Discount percent: " + sDiscountPercent + " "
+ "Discount amount: " + sDiscountAmount + " "
+ "Total: " + sTotal + " ";
System.out.println();
System.out.println(message);
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
static double getDiscountPercent(double subtotal, String type)
{
double discountPercent = 0.0;
if (type.equalsIgnoreCase("r"))
{
if (subtotal >= 500)
discountPercent = .2;
else if (subtotal >= 250 && subtotal < 500)
discountPercent =.15;
else if (subtotal >= 100 && subtotal < 250)
discountPercent =.1;
else if (subtotal < 100)
discountPercent =.0;
}
else if (type.equalsIgnoreCase("c"))
{
discountPercent = .2;
}
else
discountPercent = .05;
return discountPercent;
}
}
Explanation / Answer
import java.text.NumberFormat;
import java.util.Scanner;
class Invoice
{
private String customer_type;
private double subtotal;
public Invoice(String str,double tot)
{
customer_type = str;
subtotal = tot;
}
public double getsubtotal()
{
return subtotal;
}
public double getDiscAmount()
{
return subtotal*getDiscountPercent();
}
public double total()
{
return (subtotal - getDiscAmount());
}
public double getDiscountPercent()
{
double discountPercent = 0.0;
if (customer_type.equalsIgnoreCase("r"))
{
if (subtotal >= 500)
discountPercent = .2;
else if (subtotal >= 250 && subtotal < 500)
discountPercent =.15;
else if (subtotal >= 100 && subtotal < 250)
discountPercent =.1;
else if (subtotal < 100)
discountPercent =.0;
}
else if (customer_type.equalsIgnoreCase("c"))
{
discountPercent = .2;
}
else
discountPercent = .05;
return discountPercent;
}
public String get_total()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(total());
}
public String get_DiscAmount()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(getDiscAmount());
}
public String get_DiscPercent()
{
NumberFormat percent = NumberFormat.getPercentInstance();
return percent.format(getDiscountPercent());
}
public String get_subtotal()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(subtotal);
}
public String get_customer_type()
{
if (customer_type.equalsIgnoreCase("r"))
return "Retail";
else if (customer_type.equalsIgnoreCase("c"))
return "College";
return "UnKnown";
}
public String toString()
{
// format and display the results
String message = "Subtotal: " + get_subtotal() + " "
+ "Customer type: " + get_customer_type() + " "
+ "Discount percent: " + get_DiscPercent() + " "
+ "Discount amount: " + get_DiscAmount() + " "
+ "Total: " + get_total() + " ";
return message;
}
}
public class InvoiceApp
{
public static void main(String[] args)
{
// display a welcome message
System.out.println("Welcome to the Invoice Total Calculator");
System.out.println(); // print a blank line
Scanner sc = new Scanner(System.in);
String choice = "y";
while(choice.equalsIgnoreCase("y"))
{
// get user entries
System.out.println("Enter customer type (r/c): ");
String customerType = sc.next();
System.out.println("Enter subtotal: (0, 10000);");
double subtotal = sc.nextDouble();
Invoice new_Invoice= new Invoice(customerType,subtotal);
// calculate the discount amount and total
double discountPercent = new_Invoice.getDiscountPercent();
double discountAmount = new_Invoice.getDiscAmount();
double total = new_Invoice.total();
System.out.println();
System.out.println(new_Invoice);
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.