NEED THE JAVA CODE You need the source code to the Murach\'s Java Programming bo
ID: 3549779 • Letter: N
Question
NEED THE JAVA CODE
You need the source code to the Murach's Java Programming book found here:
http://www.murach.com/downloads/javp.htm
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. Open the project named ch07_ex3_Invoice 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. 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. 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. Test this application to make sure that it works the way it did in step 1.Explanation / Answer
// OUTPUT
Welcome to the Invoice Total Calculator
Enter customer type (r/c): r
Enter subtotal: 4500
Subtotal: $4,500.00
Customer type: Retail
Discount percent: 20%
Discount amount: $900.00
Total: $3,600.00
Continue? (y/n): y
Enter customer type (r/c): c
Enter subtotal: 2100
Subtotal: $2,100.00
Customer type: College
Discount percent: 20%
Discount amount: $420.00
Total: $1,680.00
Continue? (y/n): n
//
import java.text.NumberFormat;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ashok
*/
public class Invoice {
private String customer_Type;
private double subtotal;
public Invoice(String c_t,double s)
{
customer_Type = c_t;
subtotal = s;
}
/**
* @return the subtotal
*/
public double getSubtotal() {
return subtotal;
}
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 double getdiscountAmount()
{
return subtotal * getDiscountPercent();
}
public double gettotal()
{
return (subtotal - getdiscountAmount());
}
public String getsCustomer_Type()
{
if (customer_Type.equalsIgnoreCase("r"))
return "Retail";
else if (customer_Type.equalsIgnoreCase("c"))
return "College";
return "";
}
public String getsSubtotal()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
return currency.format(subtotal);
}
public String getsDiscountPercent()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
return percent.format(getDiscountPercent());
}
public String getsDiscountAmount()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
return currency.format(getdiscountAmount());
}
public String getsTotal()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
return currency.format(gettotal());
}
}
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);
//Create Invoice Object.
Invoice new_invoice = new Invoice(customerType,subtotal);
// format and display the results
String message = "Subtotal: " + new_invoice.getsSubtotal() + " "
+ "Customer type: " + new_invoice.getsCustomer_Type() + " "
+ "Discount percent: " + new_invoice.getsDiscountPercent() + " "
+ "Discount amount: " + new_invoice.getsDiscountAmount() + " "
+ "Total: " + new_invoice.getsTotal() + " ";
System.out.println();
System.out.println(message);
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.