In: Computer Science
I created a shoppingcart program but I'm getting a few compilation failed errors:
1) Tests that ItemToPurchase("Bottled Water", "Deer Park, 12 oz.", 1, 10) correctly initializes item compilation failed
2) Tests default constructor and accessors for ShoppingCart Compilation failed
3)Tests ShoppingCart("John Doe", "February 1, 2016") correctly initializes cart Compilation failed
4) Tests that getNumItemsInCart() returns 6 (ShoppingCart) compilation failed
5) Test that getCostOfCart() returns 9 (ShoppingCart) compilation failed
Complete program:
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;
public class ShoppingCart {
//instance variables
private String customerName;
private String currentDate;
private ArrayList<ItemToPurchase> cartItems = new
ArrayList<ItemToPurchase>();
//Default constructor
public ShoppingCart() {
customerName = "";
currentDate = "";
}
//parameterized constructor
public ShoppingCart(String customerName, String currentDate)
{
this.customerName = customerName;
this.currentDate = currentDate;
}
//getter methods
public String getCustomerName() {
return customerName;
}
public String getCurrentDate() {
return currentDate;
}
//add items
public void addItem(ItemToPurchase item) {
cartItems.add(item);
}
//remove item
public void removeItem(String name) {
for (ItemToPurchase i : cartItems) {
if (i.getName().equalsIgnoreCase(name)) {
cartItems.remove(i);
return;
}
}
System.out.println("Item not found in cart");
}
//modify the item
public void modifyItem(ItemToPurchase item) {
for (int i = 0; i < cartItems.size(); i++) {
if (cartItems.get(i).getName().equalsIgnoreCase(item.getName()))
{
if (!item.getDescription().equals("none") &&
!(item.getPrice() == 0) && !(item.getQuantity() == 0))
{
cartItems.add(i, item);
return;
}
}
}
System.out.println("Item not found in cart. Nothing modified");
}
//get total number of items in the cart
public int getNumItemsIncart() {
int sum = 0;
for (ItemToPurchase i : cartItems) {
sum += i.getQuantity();
}
return sum;
}
//get total cost of items in the cart
public int getCostOfcart() {
int sum = 0;
for (ItemToPurchase i : cartItems) {
sum += i.getPrice();
}
return sum;
}
//printing total of each items
public void printTotal() {
System.out.println(customerName + "'s Shopping Cart - " +
currentDate);
if (cartItems.isEmpty()) {
System.out.println("Shopping Cart is Empty");
return;
}
System.out.println("Number of Items: " +
cartItems.size());
System.out.println();
for (ItemToPurchase i : cartItems) {
i.printItemPurchase();
}
System.out.println();
System.out.println("Total: $" + getCostOfcart());
}
//printing description of all items
public void printDescription() {
System.out.println(customerName + "'s Shopping Cart - " +
currentDate);
System.out.println();
if (cartItems.isEmpty()) {
System.out.println("Shopping Cart is Empty");
return;
}
System.out.println("Item Descriptions: ");
for (ItemToPurchase i : cartItems) {
i.printItemDescription();
}
}
}
shoppingcartmanager.java
import java.util.Scanner;
public class ShoppingCartManager {
// method to add an item in the cart
public static void addCartItem(Scanner scan, ShoppingCart cart)
{
String itemName, itemDescription;
int itemPrice, itemQuantity;
System.out.println("ADD ITEM TO CART");
System.out.print("Enter the item name : ");
itemName = scan.nextLine();
System.out.print("Enter the item description : ");
itemDescription = scan.nextLine();
System.out.print("Enter the item price : ");
itemPrice = scan.nextInt();
System.out.print("Enter the item quantity : ");
itemQuantity = scan.nextInt();
scan.nextLine();
cart.addItem(new ItemToPurchase(itemName, itemPrice, itemQuantity, itemDescription));
}
// method to remove an item from the cart
public static void removeCartItem(Scanner scan, ShoppingCart cart)
{
String itemName;
System.out.println("REMOVE ITEM FROM CART");
System.out.print("Enter the name of item to remove : ");
itemName = scan.nextLine();
cart.removeItem(itemName);
}
// method to change quantity of the cart
public static void changeItemQuantity(Scanner scan, ShoppingCart
cart) {
String itemName;
int itemQuantity;
System.out.println("CHANGE ITEM QUANTITY");
System.out.print("Enter the item name : ");
itemName = scan.nextLine();
System.out.print("Enter the new quantity : ");
itemQuantity = scan.nextInt();
scan.nextLine();
cart.modifyItem(new ItemToPurchase(itemName, 0, itemQuantity,
"none"));
}
// method to display menu choices and perform the operations on
the cart until
// the user quits
public static void printMenu(ShoppingCart cart) {
String choice;
Scanner scan = new Scanner(System.in);
do {
System.out.println("\nMENU");
System.out.println("a - Add item to cart");
System.out.println("d - Remove item from cart");
System.out.println("c - Change item quantity");
System.out.println("i - Output items' description");
System.out.println("o - Output shopping cart");
System.out.println("q - Quit");
System.out.print("\nChoose an option : ");
choice = scan.nextLine();
if (choice.equalsIgnoreCase("a")) {
addCartItem(scan, cart);
} else if (choice.equalsIgnoreCase("d")) {
removeCartItem(scan, cart);
} else if (choice.equalsIgnoreCase("c")) {
changeItemQuantity(scan, cart);
} else if (choice.equalsIgnoreCase("o")) {
System.out.println("OUTPUT SHOPPING CART");
cart.printTotal();
} else if (choice.equalsIgnoreCase("i")) {
System.out.println("OUTPUT ITEMS' DESCRIPTION");
cart.printDescription();
} else if (!choice.equalsIgnoreCase("q"))
System.out.println("Invalid choice");
} while (!choice.equalsIgnoreCase("q"));
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// taking user input
System.out.println("Enter Customer's Name:");
String name = sc.nextLine();
System.out.println("Enter Today's Date:");
String date = sc.nextLine();
// printing user input
System.out.println();
System.out.println("Customer Name: " + name);
System.out.println("Today's Date: " + date);
ShoppingCart cart = new ShoppingCart(name, date);// created
shopping cart object
printMenu(cart);
}
}
ANSWER::
// 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,String itemDescription,
int itemPrice, int itemQuantity) {
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;
public class ShoppingCart {
// instance variables
private String customerName;
private String currentDate;
private ArrayList<ItemToPurchase> cartItems = new
ArrayList<ItemToPurchase>();
// Default constructor
public ShoppingCart() {
customerName = "";
currentDate = "";
}
// parameterized constructor
public ShoppingCart(String customerName, String currentDate)
{
this.customerName = customerName;
this.currentDate = currentDate;
}
// getter methods
public String getCustomerName() {
return customerName;
}
public String getCurrentDate() {
return currentDate;
}
// add items
public void addItem(ItemToPurchase item) {
cartItems.add(item);
}
// remove item
public void removeItem(String name) {
for (ItemToPurchase i : cartItems) {
if (i.getName().equalsIgnoreCase(name)) {
cartItems.remove(i);
return;
}
}
System.out.println("Item not found in cart");
}
// modify the item
public void modifyItem(ItemToPurchase item) {
int flag=0;
for (int i = 0; i < cartItems.size(); i++) {
if (cartItems.get(i).getName().equalsIgnoreCase(item.getName()))
{
cartItems.get(i).setQuantity(item.getQuantity());
flag=1;
}
}
if(flag==0)
System.out.println("Item not found in cart. Nothing modified");
}
// get total number of items in the cart
public int getNumItemsIncart() {
int sum = 0;
for (ItemToPurchase i : cartItems) {
sum += i.getQuantity();
}
return sum;
}
// get total cost of items in the cart
public int getCostOfcart() {
int sum = 0;
for (ItemToPurchase i : cartItems) {
sum += i.getPrice()*i.getQuantity();
}
return sum;
}
// printing total of each items
public void printTotal() {
System.out.println(customerName + "'s Shopping Cart - " +
currentDate);
if (cartItems.isEmpty()) {
System.out.println("Shopping Cart is Empty");
return;
}
System.out.println("Number of Items: " +
getNumItemsIncart());
System.out.println();
for (ItemToPurchase i : cartItems) {
i.printItemPurchase();
}
System.out.println();
System.out.println("Total: $" + getCostOfcart());
}
// printing description of all items
public void printDescription() {
System.out.println(customerName + "'s Shopping Cart - " +
currentDate);
System.out.println();
if (cartItems.isEmpty()) {
System.out.println("Shopping Cart is Empty");
return;
}
System.out.println("Item Descriptions: ");
for (ItemToPurchase i : cartItems) {
i.printItemDescription();
}
}
}
___________
// ShoppingCartManager.java
import java.util.Scanner;
public class ShoppingCartManager {
// method to add an item in the cart
public static void addCartItem(Scanner scan, ShoppingCart cart)
{
String itemName, itemDescription;
int itemPrice, itemQuantity;
System.out.println("ADD ITEM TO CART");
System.out.print("Enter the item name : ");
itemName = scan.nextLine();
System.out.print("Enter the item description : ");
itemDescription = scan.nextLine();
System.out.print("Enter the item price : ");
itemPrice = scan.nextInt();
System.out.print("Enter the item quantity : ");
itemQuantity = scan.nextInt();
scan.nextLine();
cart.addItem(new ItemToPurchase(itemName,itemDescription, itemPrice, itemQuantity));
}
// method to remove an item from the cart
public static void removeCartItem(Scanner scan, ShoppingCart cart)
{
String itemName;
System.out.println("REMOVE ITEM FROM CART");
System.out.print("Enter the name of item to remove : ");
itemName = scan.nextLine();
cart.removeItem(itemName);
}
// method to change quantity of the cart
public static void changeItemQuantity(Scanner scan, ShoppingCart
cart) {
String itemName;
int itemQuantity;
System.out.println("CHANGE ITEM QUANTITY");
System.out.print("Enter the item name : ");
itemName = scan.nextLine();
System.out.print("Enter the new quantity : ");
itemQuantity = scan.nextInt();
scan.nextLine();
cart.modifyItem(new
ItemToPurchase(itemName,"none",0,itemQuantity));
}
// method to display menu choices and perform the operations on
the cart
// until
// the user quits
public static void printMenu(ShoppingCart cart) {
String choice;
Scanner scan = new Scanner(System.in);
do {
System.out.println("\nMENU");
System.out.println("a - Add item to cart");
System.out.println("d - Remove item from cart");
System.out.println("c - Change item quantity");
System.out.println("i - Output items' description");
System.out.println("o - Output shopping cart");
System.out.println("q - Quit");
System.out.print("\nChoose an option : ");
choice = scan.nextLine();
if (choice.equalsIgnoreCase("a")) {
addCartItem(scan, cart);
} else if (choice.equalsIgnoreCase("d")) {
removeCartItem(scan, cart);
} else if (choice.equalsIgnoreCase("c")) {
changeItemQuantity(scan, cart);
} else if (choice.equalsIgnoreCase("o")) {
System.out.println("OUTPUT SHOPPING CART");
cart.printTotal();
} else if (choice.equalsIgnoreCase("i")) {
System.out.println("OUTPUT ITEMS' DESCRIPTION");
cart.printDescription();
} else if (!choice.equalsIgnoreCase("q"))
System.out.println("Invalid choice");
} while (!choice.equalsIgnoreCase("q"));
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// taking user input
System.out.println("Enter Customer's Name:");
String name = sc.nextLine();
System.out.println("Enter Today's Date:");
String date = sc.nextLine();
// printing user input
System.out.println();
System.out.println("Customer Name: " + name);
System.out.println("Today's Date: " + date);
ShoppingCart cart = new ShoppingCart(name, date);// created
shopping
// cart object
printMenu(cart);
}
}
___________
Output
Enter Customer's Name:
John Doe
Enter Today's Date:
February 1, 2016
Customer Name: John Doe
Today's Date: February 1, 2016
MENU
a - Add item to cart
d - Remove item from cart
c - Change item quantity
i - Output items' description
o - Output shopping cart
q - Quit
Choose an option : a
ADD ITEM TO CART
Enter the item name : Nike Romaleos
Enter the item description : Volt color, Weightlifting shoes
Enter the item price : 189
Enter the item quantity : 2
MENU
a - Add item to cart
d - Remove item from cart
c - Change item quantity
i - Output items' description
o - Output shopping cart
q - Quit
Choose an option : a
ADD ITEM TO CART
Enter the item name : Chocolate Chips
Enter the item description : Semi-sweet
Enter the item price : 3
Enter the item quantity : 5
MENU
a - Add item to cart
d - Remove item from cart
c - Change item quantity
i - Output items' description
o - Output shopping cart
q - Quit
Choose an option : a
ADD ITEM TO CART
Enter the item name : Powerbeats 2 Headphones
Enter the item description : Bluetooth headphones
Enter the item price : 128
Enter the item quantity : 1
MENU
a - Add item to cart
d - Remove item from cart
c - Change item quantity
i - Output items' description
o - Output shopping cart
q - Quit
Choose an option : o
OUTPUT SHOPPING CART
John Doe's Shopping Cart - February 1, 2016
Number of Items: 8
Nike Romaleos 2 @ $189 = $378
Chocolate Chips 5 @ $3 = $15
Powerbeats 2 Headphones 1 @ $128 = $128
Total: $521
MENU
a - Add item to cart
d - Remove item from cart
c - Change item quantity
i - Output items' description
o - Output shopping cart
q - Quit
Choose an option : i
OUTPUT ITEMS' DESCRIPTION
John Doe's Shopping Cart - February 1, 2016
Item Descriptions:
Nike Romaleos: Volt color, Weightlifting shoes
Chocolate Chips: Semi-sweet
Powerbeats 2 Headphones: Bluetooth headphones
MENU
a - Add item to cart
d - Remove item from cart
c - Change item quantity
i - Output items' description
o - Output shopping cart
q - Quit
Choose an option : c
CHANGE ITEM QUANTITY
Enter the item name : Powerbeats 2 Headphones
Enter the new quantity : 5
MENU
a - Add item to cart
d - Remove item from cart
c - Change item quantity
i - Output items' description
o - Output shopping cart
q - Quit
Choose an option : o
OUTPUT SHOPPING CART
John Doe's Shopping Cart - February 1, 2016
Number of Items: 12
Nike Romaleos 2 @ $189 = $378
Chocolate Chips 5 @ $3 = $15
Powerbeats 2 Headphones 5 @ $128 = $640
Total: $1033
MENU
a - Add item to cart, d - Remove item from cart, c - Change item
quantity, i - Output items' description, o - Output shopping cart,
q - Quit
Choose an option : d
REMOVE ITEM FROM CART
Enter the name of item to remove : Powerbeats 2 Headphones
MENU
a - Add item to cart, d - Remove item from cart, c - Change item
quantity, i - Output items' description, o - Output shopping cart,
q - Quit
Choose an option : o
OUTPUT SHOPPING CART
John Doe's Shopping Cart - February 1, 2016
Number of Items: 7
Nike Romaleos 2 @ $189 = $378
Chocolate Chips 5 @ $3 = $15
Total: $393
MENU
a - Add item to cart, d - Remove item from cart, c - Change item
quantity, i - Output items' description, o -
Output shopping cart, q - Quit
Choose an option : q