Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class called ItemToPurchase . This class has the following attributes:

ID: 3712320 • Letter: D

Question

Design a class called ItemToPurchase.

This class has the following attributes: item_name, item_price, and item_quanity

The ItemToPurchase class should have methods to return the current values of these attributes. In addition, this class has a method get_item_cost to return the cost of this item (which is the product of item_price and item_quantity)

Design a class called Customer.

This class has the following attributes: customer_name, shopping_date, and shopping_cart. The shopping_cart attribute is a list of ItemToPurchase objects.

The Customer class should have methods to get values of the name and date attributes, to add items to the shopping, and to remove items from the shopping cart. Item removal should be done based on item name; that is, the item_remove method takes item_name as a parameter, searches an item using the given item_name, and removes the found item from the shopping cart.

The Customer class also has a method for checking out, which should display the following information:

customer name, shopping date, a list of purchased items with item_name, item_quanity, and item_price, and the total cost of all the items in the shopping cart.

In the main function, the program should create a Customer object, then ask the user to enter how many items the customer will purchase and provide the name, price, and quantity for each item. The program adds all the items to the shopping cart. Then the program asks the user to enter the number of items to remove from the shopping cart and provide names for these items. The program removes these items from the shopping cart. Then the program performs check out function for the customer to display the customer name, shopping date, a list of purchased items, and the total cost.

The following is a sample output of the program:

Enter Customer Name: Mark Smith

Enter Shopping Date: November 2016

How many items does the customer want add into shopping cart? 5

Item #1

Item Name: Bottle Water

Item Price: 1.5

Item Quantity: 20

Item #2

Item Name: Chips

Item Price: 3.5

Item Quantity: 6

Item #3

Item Name: Soda

Item Price: 4.0

Item Quantity: 10

Item #4

Item Name: Coffe

Item Price: 6.5

Item Quantity: 3

Item #5

Item Name: Tea

Item Price: 5.5

Item Quantity: 4

How many items does the customer want to remove from the shopping cart? 2

Item name for removing: Soda

Soda has been removed from shopping cart.

Item name for removing: Tea

Tea has been removed from shopping cart.

Checking out

Customer Name: Mark Smith

Shopping Date: November 2016

20 Bottle Water @ $1.5

6 Chips @ $3.5

3 Coffe @ $6.5

Total Amount: $70.5

Explanation / Answer

public class ItemToPurchase {

private String itemName;

private double itemPrice;

private int itemQuanity;

/**

* @param itemName

* @param itemPrice

* @param itemQuanity

*/

public ItemToPurchase(String itemName, double itemPrice, int itemQuanity) {

this.itemName = itemName;

this.itemPrice = itemPrice;

this.itemQuanity = itemQuanity;

}

/**

* @return the itemName

*/

public String getItemName() {

return itemName;

}

/**

* @return the itemPrice

*/

public double getItemPrice() {

return itemPrice;

}

/**

* @return the itemQuanity

*/

public int getItemQuanity() {

return itemQuanity;

}

/**

* @param itemName

* the itemName to set

*/

public void setItemName(String itemName) {

this.itemName = itemName;

}

/**

* @param itemPrice

* the itemPrice to set

*/

public void setItemPrice(double itemPrice) {

this.itemPrice = itemPrice;

}

/**

* @param itemQuanity

* the itemQuanity to set

*/

public void setItemQuanity(int itemQuanity) {

this.itemQuanity = itemQuanity;

}

public double getItemCost() {

return getItemPrice() * getItemQuanity();

}

}

import java.util.ArrayList;

import java.util.List;

public class Customer {

private String customerName;

private String shopping_date;

private List shopping_cart = new ArrayList();

/**

* @return the customerName

*/

public String getCustomerName() {

return customerName;

}

/**

* @return the shopping_date

*/

public String getShopping_date(){

return shopping_date;

}

/**

* @return the shopping_cart

*/

public List getShopping_cart() {

return shopping_cart;

}

public void addItem(ItemToPurchase itemToPurchase) {

shopping_cart.add(itemToPurchase);

}

public boolean removeItem(String itemName) {

for (int j = 0; j < shopping_cart.size(); j++) {

if (shopping_cart.get(j).getItemName().equalsIgnoreCase(itemName)) {

shopping_cart.remove(j);

return true;

}

}

return false;

}

/**

* @param customerName

* the customerName to set

*/

public void setCustomerName(String customerName) {

this.customerName = customerName;

}

/**

* @param shopping_date

* the shopping_date to set

*/

public void setShopping_date(String shopping_date) {

this.shopping_date = shopping_date;

}

public void checkOut() {

System.out.println("Checking out");

System.out.println("Customer Name: " + getCustomerName());

System.out.println("Shopping Date: " + getShopping_date());

double totalAmt = 0;

for (int j = 0; j < shopping_cart.size(); j++) {

ItemToPurchase itemToPurchase = shopping_cart.get(j); System.out.println(itemToPurchase.getItemQuanity()

+ " " + itemToPurchase.getItemName() + " @ $"

+ itemToPurchase.getItemCost());

totalAmt += itemToPurchase.getItemCost();

}

System.out.println("Total Amount: $" + totalAmt);

}

}

import java.util.Scanner;

public class TestCustomer {

public static void main(String[] args) {

Scanner scanner = null;

try {

scanner = new Scanner(System.in);

System.out.print("Enter Customer Name: ");

String customerName = scanner.nextLine();

System.out.print("Enter Shopping Date: ");

String shopping_date = scanner.nextLine();

System.out.print("How many items does the customer want add into shopping cart? ");

int noOfItems = scanner.nextInt();

Customer customer = new Customer();

customer.setCustomerName(customerName);

customer.setShopping_date(shopping_date);

String itemName;

double itemPrice;

int itemQuanity;

for (int i = 1; i <= noOfItems; i++) {

System.out.println("Item #" + i);

System.out.print("Item Name: ");

// scanner.next();

itemName = scanner.next();

System.out.print("Item Price: ");

itemPrice = scanner.nextDouble();

System.out.print("Item Quantity: ");

itemQuanity = scanner.nextInt();

customer.addItem(new ItemToPurchase(itemName, itemPrice,itemQuanity));

}

System.out.print("How many items does the customer want to remove from the shopping cart? ");

int noOfItemsRemove = scanner.nextInt();

for (int i = 1; i <= noOfItemsRemove; i++) {

System.out.print("Item name for removing: ");

// scanner.next();

itemName = scanner.next();

if (customer.removeItem(itemName))

System.out.println(itemName + " has been removed from shopping cart.");

}

customer.checkOut();

} catch (Exception e) {

// TODO: handle exception

}

}

}

OUTPUT:

Enter Customer Name: Srinivas Palli

Enter Shopping Date: November 2012

How many items does the customer want add into shopping cart? 3

Item #1

Item Name: Tea

Item Price: 4

Item Quantity: 5

Item #2

Item Name: Apple

Item Price: 4

Item Quantity: 5

Item #3

Item Name: Watter

Item Price: 3

Item Quantity: 5

How many items does the customer want to remove from the shopping cart? 2

Item name for removing: Watter

Watter has been removed from shopping cart.

Item name for removing: Tea

Tea has been removed from shopping cart.

Checking out

Customer Name: Srinivas Palli

Shopping Date: November 2012

5 Apple @ $20.0

Total Amount: $20.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote