Project Overview: Make a copy of your previous project. BlueJ Project called: Ch
ID: 3600002 • Letter: P
Question
Project Overview:
Make a copy of your previous project. BlueJ Project called: CheckoutDriver
You have 2 .java/class files from your previous programming assignment.
Same as the previous assignment, your program will simulate a purchase of an item in a shopping cart. Using your choice of JOptionPane or Scanner, you will prompt and enter the:
Customer’s First name,
Customer’s Last name, the
number of items purchased,
and the cost for each item.
New in this assignment, the program will calculate a discount and shipping charges on the purchase based on the number of items and state the purchase is being shipped. It will also continue to calculate the state tax and total cost including tax. The output will be displayed as shown at bottom of page.
First File: Cart.java
In the Cart class, make the following changes to your previous assignment:
Change the STATE TAX constant to 8% (.08)
Add a new Cart class attribute called shipToState. Use String data type.
Declare shipToState Access Modifier as public! No accessor or mutator needed to set/get this attribute. It is publicly available.
Add new method called calculateDiscount() to the Cart class.
NOTE: Use if, if-else, if-else-if statements to determine the Discount.
if the SubTotal is $0.00 to $25.00 then there is no discount.
if the SubTotal is $25.01 to $50.00 then the discount will be $2.00.
if the SubTotal is $50.01 to $100.00 then the discount will be $10.00.
if the SubTotal is $100.01 or more then the discount will be $25.00.
add new method called calculateShippingCost to Cart class.
NOTE: Use a switch statement to determine the Shipping Cost.
if shipping to NJ, DE, or PA then there is NO shipping cost
if shipping to AK then the shipping cost is $9.00.
if shipping to HI then the shipping cost is $10.00.
if shipping to any other state, the shipping cost is $5.00
change the getTotalPurchase() method in Cart class to use calculateShippingCost and calculateDiscount().
NOTE: the calculateTaxes() method should NOT change and continue to calculate taxes on the subtotal before the discount.
In the CheckoutDriver class, make the following changes to your previous assignment printf() statements. Make sure to use Cart instance methods and instance attribute when printing:
add prompt to ask user for Ship To State, “Enter state to ship item to: “.
assign the “ship to state” to the Cart instance attribute. Do not need to call method. Set attribute directly. The attribute’s access modifier is public.
change the output to display the Discount and Shipping Costs as follows:
last name uppercase then comma, then space, then first name as entered on keyboard.
Name: REA, jim
Number of items: 5
Monetary amounts have dollar sign and 5 numbers to the left of decimal and 2 positions after the decimal.
Unit Cost: $ 10.00
Subtotal: $ 50.00
+ NJ State Tax (8pct): $ 4.00
- Discount: $ 2.00
+ Shipping: $ 2.50 (ship to: NJ)
= Total: $ 54.50
Test Your Program: Run your program using different inputs. Check to make sure program runs with different inputs.
Try this test data and compare with expected output below. Run your program 4 times with different input:
Ship to NJ; 1 item; unit cost $100.00
Ship to HI; 3 items; unit cost $40.00
Ship to CA: 25 items; unit cost $1.00
Ship to AK; 2 items; unit cost $25.00
Name: SMITH, John
Number of items: 1
Unit Cost: $ 100.00
Subtotal: $ 100.00
+ NJ State Tax (8pct): $ 8.00
- Discount: $ 10.00
+ Shipping: $ 0.00 (ship to: NJ)
= Total: $ 98.00
Name: SMITH, John
Number of items: 3
Unit Cost: $ 40.00
Subtotal: $ 120.00
+ NJ State Tax (8pct): $ 9.60
- Discount: $ 25.00
+ Shipping: $ 10.00 (ship to: HI)
= Total: $ 114.60
Name: SMITH, John
Number of items: 25
Unit Cost: $ 1.00
Subtotal: $ 25.00
+ NJ State Tax (8pct): $ 2.00
- Discount: $ 0.00
+ Shipping: $ 5.00 (ship to: CA)
= Total: $ 32.00
Name: SMITH, John
Number of items: 2
Unit Cost: $ 25.00
Subtotal: $ 50.00
+ NJ State Tax (8pct): $ 4.00
- Discount: $ 2.00
+ Shipping: $ 9.00 (ship to: AK)
= Total: $ 61.00
Previous Project to edit:
package checkoutdriver;
public class Cart {
private String firstName;
private String lastName;
private int numItems;
private double unitCost;
public final double STATE_TAX_RATE = 0.07d;
/**
* @param firstName
* @param lastName
*/
public Cart(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.numItems = 0;
this.unitCost = 0;
}
/**
* @param unitCost
* the unitCost to set
*/
public void setUnitCost(double unitCost) {
this.unitCost = unitCost;
}
/**
* @param numItems
* the numItems to set
*/
public void setNumItems(int numItems) {
this.numItems = numItems;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @return the numItems
*/
public double getSubTotal() {
return numItems * unitCost;
}
/**
* @return the numItems
*/
public int getNumItems() {
return numItems;
}
/**
* @return the unitCost
*/
public double getUnitCost() {
return unitCost;
}
/**
* @param firstName
* the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @param lastName
* the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the unitCost
*/
public double getTaxes() {
return getSubTotal() * STATE_TAX_RATE;
}
/**
* @return the unitCost
*/
public double getTotalPurchase() {
return (getSubTotal() + getTaxes());
}
}
package checkoutdriver;
import javax.swing.JOptionPane;
public class CheckoutDriver {
public static void main(String[] args) {
String firstName;
String lastName;
int numItems;
double unitCost;
firstName = JOptionPane.showInputDialog("Enter the First Name:");
lastName = JOptionPane.showInputDialog("Enter the Last Name:");
Cart cart = new Cart(firstName, lastName);
numItems = Integer.parseInt(JOptionPane
.showInputDialog("Enter the number of items being purchased:"));
cart.setNumItems(numItems);
unitCost = Double.parseDouble(JOptionPane
.showInputDialog("Enter the unit cost for the item:"));
cart.setUnitCost(unitCost);
System.out.println("YOUR PURCHASE");
System.out
.println("Name: " + cart.getLastName().toUpperCase()
+ ", " + cart.getFirstName());
System.out.println("Number of items: " + cart.getNumItems());
System.out.printf("Unit Cost: $%.2f ", cart.getUnitCost());
System.out.printf("Subtotal: $%.2f ", cart.getSubTotal());
System.out.printf("+ NJ State Tax: $%.2f ", cart.getTaxes());
System.out.printf("= Total: $%.2f ", cart.getTotalPurchase());
}
}
last name uppercase then comma, then space, then first name as entered on keyboard.
Explanation / Answer
package checkoutdriver;
public class Cart {
private String firstName;
private String lastName;
private int numItems;
private double unitCost;
public final double STATE_TAX_RATE = 0.08d;
public String shipToState;
/**
*
* @param firstName
*
* @param lastName
*
*/
public double calculateDiscount() {
double subT = getSubTotal();
if (subT >= 0.0 && subT <= 25.00) {
return 0;
} else if (subT >= 25.01 && subT <= 50.00) {
return 2.0;
} else if (subT >= 50.01 && subT <= 100.00) {
return 10.0;
} else {
return 25.0;
}
}
public double calculateShippingCost() {
double shipCost = 0;
switch (shipToState) {
case "NJ":
shipCost = 0;
break;
case "DE":
shipCost = 0;
break;
case "PA":
shipCost = 0;
break;
case "AK":
shipCost = 9.0;
break;
case "HI":
shipCost = 10.0;
break;
default:
shipCost = 5.0;
}
return shipCost;
}
public Cart(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.numItems = 0;
this.unitCost = 0;
}
/**
*
* @param unitCost
*
* the unitCost to set
*
*/
public void setUnitCost(double unitCost) {
this.unitCost = unitCost;
}
/**
*
* @param numItems
*
* the numItems to set
*
*/
public void setNumItems(int numItems) {
this.numItems = numItems;
}
/**
*
* @return the firstName
*
*/
public String getFirstName() {
return firstName;
}
/**
*
* @return the lastName
*
*/
public String getLastName() {
return lastName;
}
/**
*
* @return the numItems
*
*/
public double getSubTotal() {
return numItems * unitCost;
}
/**
*
* @return the numItems
*
*/
public int getNumItems() {
return numItems;
}
/**
*
* @return the unitCost
*
*/
public double getUnitCost() {
return unitCost;
}
/**
*
* @param firstName
*
* the firstName to set
*
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
*
* @param lastName
*
* the lastName to set
*
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
*
* @return the unitCost
*
*/
public double getTaxes() {
return getSubTotal() * STATE_TAX_RATE;
}
/**
*
* @return the unitCost
*
*/
public double getTotalPurchase() {
return (getSubTotal() + getTaxes() - calculateDiscount() + calculateShippingCost());
}
}
=================
package checkoutdriver;
import javax.swing.JOptionPane;
public class CheckoutDriver {
public static void main(String[] args) {
String firstName;
String lastName;
int numItems;
double unitCost;
firstName = JOptionPane.showInputDialog("Enter the First Name:");
lastName = JOptionPane.showInputDialog("Enter the Last Name:");
Cart cart = new Cart(firstName, lastName);
numItems = Integer.parseInt(JOptionPane
.showInputDialog("Enter the number of items being purchased:"));
cart.setNumItems(numItems);
unitCost = Double.parseDouble(JOptionPane
.showInputDialog("Enter the unit cost for the item:"));
cart.setUnitCost(unitCost);
cart.shipToState = JOptionPane.showInputDialog("Enter state to ship item to:");
System.out.println("YOUR PURCHASE");
System.out
.println("Name: " + cart.getLastName().toUpperCase()
+ ", " + cart.getFirstName());
System.out.println("Number of items: " + cart.getNumItems());
System.out.printf("Unit Cost: $%.2f ", cart.getUnitCost());
System.out.printf("Subtotal: $%.2f ", cart.getSubTotal());
System.out.printf("+ NJ State Tax: $%.2f ", cart.getTaxes());
System.out.printf("- Discount: $%.2f ", cart.calculateDiscount());
System.out.printf("+ Shipping: $%.2f ", cart.calculateShippingCost());
System.out.printf("= Total: $%.2f ", cart.getTotalPurchase());
}
}
===========
YOUR PURCHASE
Name: JOHN, SMITH
Number of items: 1
Unit Cost: $100.00
Subtotal: $100.00
+ NJ State Tax: $8.00
- Discount: $10.00
+ Shipping: $0.00
= Total: $98.00
=========
YOUR PURCHASE
Name: JOHN, smith
Number of items: 2
Unit Cost: $25.00
Subtotal: $50.00
+ NJ State Tax: $4.00
- Discount: $2.00
+ Shipping: $9.00
= Total: $61.00
==
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.