Need help with my java assignment.. Thanks in advance! Project name: JavaH5NBL I
ID: 3566329 • Letter: N
Question
Need help with my java assignment.. Thanks in advance!
Project name: JavaH5NBL
In this exercise, you'll create an Invoice class and construct objects from it as you convert the Invoice application to an object-oriented application. 1. Open the project named ch07_ex3_hwoice that's in the ex_starts directory. Then; review the code for the InvoiceApp and Validator classes, and run the project to see how this application works. 2. 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, copying the code from the InvoiceApp class whenever that makes sense: . Include two private fields for the customer type and subtotal entered by the user. . Include a single constructor that accepts the customer type and subtotal as parameters. . Include 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. .Include get methods that return formatted string values for the subtotal, discount percent, discount amount, and total. These methods should call the other get methods to get the values to be formatted. . Include a get method that returns a string that contains the full name for a customer type. 3. Modify the code in the InvoiceApp class so it creates an Invoice object. Then, call the methods of the Invoice object to display the formatted values for the Invoice, and delete any code that is no longer needed. That should simplify the InvoiceApp class considerably. 4. Test this application to make sure that it works the way it did in Step 1.Explanation / Answer
Code for Invoice.java
import java.text.NumberFormat;
import java.util.Scanner;
public class Invoice {
String type;
double subtotal;
public Invoice(String customerType,double subTotal)
{
type=customerType;
subtotal=subTotal;
}
public double getDiscountPercent()
{
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;
}
public double getDiscountAmount()
{
return subtotal * getDiscountPercent();
}
public double getTotal()
{
return subtotal - getDiscountAmount();
}
public double getSubTotal()
{
return subtotal;
}
public String GetSubTotal()
{
return String.valueOf(getSubTotal());
}
public String GetDiscountAmount()
{
return String.valueOf(getDiscountAmount());
}
public String GetDiscountPercent()
{
return String.valueOf(getDiscountPercent());
}
public String GetTotal()
{
return String.valueOf(getTotal());
}
}
Code for InvoiceApp.java
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);
Invoice invoiceobj = new Invoice(customerType, subtotal);
// format the values
String sCustomerType = "Unknown";
if (customerType.equalsIgnoreCase("r"))
sCustomerType = "Retail";
else if (customerType.equalsIgnoreCase("c"))
sCustomerType = "College";
// format and display the results
String message = "Subtotal: " + invoiceobj.GetSubTotal() + " "
+ "Customer type: " + sCustomerType + " "
+ "Discount percent: " + invoiceobj.GetDiscountPercent() + " "
+ "Discount amount: " + invoiceobj.GetDiscountAmount() + " "
+ "Total: " + invoiceobj.GetTotal() + " ";
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;
}
}
OUTPUT:
Welcome to the Invoice Total Calculator
Enter customer type (r/c): c
Enter subtotal: 100
Subtotal: 100.0
Customer type: College
Discount percent: 0.2
Discount amount: 20.0
Total: 80.0
Continue? (y/n): y
Enter customer type (r/c): r
Enter subtotal: 100
Subtotal: 100.0
Customer type: Retail
Discount percent: 0.1
Discount amount: 10.0
Total: 90.0
Continue? (y/n): n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.