In: Computer Science
Write a shoppingcartmanager.java that contains a main method for this code in java
Itemtopurchase.java
public class ItemToPurchase {
// instance variables
private String itemName;
private String itemDescription;
private int itemPrice;
private int itemQuantity;
// default constructor
public ItemToPurchase() {
this.itemName = "none";
this.itemDescription =
"none";
this.itemPrice = 0;
this.itemQuantity = 0;
}
public ItemToPurchase(String itemName, int itemPrice,
int itemQuantity,String itemDescription) {
this.itemName = itemName;
this.itemDescription =
itemDescription;
this.itemPrice = itemPrice;
this.itemQuantity =
itemQuantity;
}
// method to set name of the item
public void setName(String name) {
itemName = name;
}
// method to set price of the item
public void setPrice(int price) {
itemPrice = price;
}
// method to set quantity of the item
public void setQuantity(int quantity) {
itemQuantity = quantity;
}
public void setDescription(String description) {
itemDescription =
description;
}
// method to get name of the item
public String getName() {
return itemName;
}
// method to get price of the item
public int getPrice() {
return itemPrice;
}
// method to get quantity of the item
public int getQuantity() {
return itemQuantity;
}
public String getDescription() {
return itemDescription;
}
public void printItemPurchase() {
System.out.println(itemName + " " +
itemQuantity + " @ $" + itemPrice + " = $" + (itemPrice *
itemQuantity));
}
public void printItemDescription() {
System.out.println(itemName+":
"+itemDescription);
}
}
*******************
shoppingcart.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ShoppingCart {
private String customerName;
private String currentDate;
private List<ItemToPurchase> cartItems=new
ArrayList<>();
ShoppingCart() {
customerName = "none";
currentDate = "January 1,
2016";
}
ShoppingCart(String customerName, String currentDate)
{
super();
this.customerName =
customerName;
this.currentDate =
currentDate;
}
public String getCustomerName() {
return customerName;
}
public String getDate() {
return currentDate;
}
public void addItem(ItemToPurchase item) {
cartItems.add(item);
}
public void removeItem(String itemName) {
boolean check = false;
for (ItemToPurchase item :
cartItems) {
if(item.getName().equalsIgnoreCase(itemName)) {
cartItems.remove(item);
check=true;
}
}
if(!check)
System.out.println("Item not found in cart. Nothing
removed.");
}
public void modifyItem(ItemToPurchase item) {
boolean check = false;
for (ItemToPurchase itemToPurchase
: cartItems) {
if(itemToPurchase.getName().equalsIgnoreCase(item.getName()))
{
cartItems.remove(itemToPurchase);
cartItems.add(item);
check=true;
}
}
if(!check)
System.out.println("Item not found in cart. Nothing
modified.");
}
public int getNumItemsInCart() {
int total=0;
for (ItemToPurchase item :
cartItems) {
total+=item.getQuantity();
}
return total;
}
public int getCostOfCart() {
int cost=0;
for (ItemToPurchase item :
cartItems) {
cost+=item.getPrice()*item.getQuantity();
}
return cost;
}
public void printTotal() {
System.out.println(customerName+"'s
Shopping Cart - "+currentDate);
System.out.println("Number of
Items: "+getNumItemsInCart());
System.out.println();
for (ItemToPurchase item :
cartItems) {
System.out.println(item.getName()+" "+item.getQuantity()+" @
$"+item.getPrice()+" =
$"+(item.getQuantity()*item.getPrice()));
}
System.out.println();
System.out.println("Total:
$"+getCostOfCart());
}
public void printDesciptions() {
System.out.println(customerName+"'s
Shopping Cart - "+currentDate);
System.out.println();
System.out.println("Item
Descriptions");
for (ItemToPurchase item :
cartItems)
System.out.println(item.getName()+":
"+item.getDescription());
}
public static void main(String[] args) {
Scanner sc=new
Scanner(System.in);
System.out.println("Enter
Customer's Name:");
String name=sc.nextLine();
System.out.println("Enter Today's
Date:");
String date=sc.nextLine();
ShoppingCart cart=new
ShoppingCart(name,date);
System.out.println();
System.out.println();
}
}
Here I am providing the answer for the above question:
Code:
import java.util.Scanner;
public class ShoppingCartManager
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Customer's Name:");
String name=sc.nextLine();
System.out.println("Enter Today's Date:");
String date=sc.nextLine();
ShoppingCart cart=new ShoppingCart(name,date);
System.out.println("Enter total number of
items:");
int n=sc.nextInt();
sc.nextLine();
for(int i=1;i<=n;i++)
{
System.out.println("Enter
item"+i+"name:");
String item=sc.nextLine();
System.out.println("Enter
item"+i+"description:");
String
description=sc.nextLine();
System.out.println("Enter
item"+i+"price:");
int price=sc.nextInt();
System.out.println("Enter
item"+i+"quantity:");
int quantity=sc.nextInt();
sc.nextLine();
cart.addItem(new
ItemToPurchase(item,price,quantity,description));
}
cart.printTotal();
cart.printDesciptions();
}
}
Screenshot of code:
Output:
Hoping that the above answer will help you...Thank you...