Question

In: Computer Science

I created a shoppingcart program but I'm getting a few compilation failed errors: 1) Tests that...

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);
}
}

Solutions

Expert Solution

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


Related Solutions

(C++) I'm getting a few errors that prevents the program from running what is causing this?...
(C++) I'm getting a few errors that prevents the program from running what is causing this? " routes3.cpp:202:9: warning: add explicit braces to avoid dangling else [-Wdangling-else] else { ^ " #include <iostream> #include <vector> #include <string> #include <set> #include <cstring> using namespace std; class Leg{ const char* const startCity; const char* const endCity; const int dist; public: Leg(const char* const, const char* const, int); Leg& operator= (const Leg&); int getDist() const {return dist;} void output(ostream&) const; friend class Route;...
I working on this program in C++ and I keep getting 20 errors of the same...
I working on this program in C++ and I keep getting 20 errors of the same type again.cpp:36:11: error: use of undeclared identifier 'Polynomial' int main() { // create a list of polinomials vector<Polynomial> polynomials; // welcome message cout << "Welcome to Polynomial Calculator" << endl; int option = 0; while (option != 6) { // display menu displayMenu(); // get user input; cin >> option; if (option == 1) { cout << "Enter a polynomial :" << endl; string...
I'm getting a conversion issue with this program. When I ran this program using the Windows...
I'm getting a conversion issue with this program. When I ran this program using the Windows command prompt it compiles and runs without issues. Although, when I run it using the zyBooks compiler it keeps giving me these conversion errors in the following lines: main.c: In function ‘main’: main.c:53:24: error: conversion to ‘float’ from ‘long int’ may alter its value [-Werror=conversion] averageCpi += cpi[i] * instructionCount[i] * Million; main.c:57:29: error: conversion to ‘float’ from ‘long int’ may alter its value...
I am writing a matlab program where I created a figure that has a few different...
I am writing a matlab program where I created a figure that has a few different elements to it and now I need to move that figure into a specific excel file into a specific set of boxes in the excel file. With numbers and text I have always used the xlswrite function for this in order to put data into specific boxes. How do I do the same with this figure? The figure I have is called like this:...
I need a few unit tests done on my SelectionSort program in Visual Studio 2019 with...
I need a few unit tests done on my SelectionSort program in Visual Studio 2019 with MSTest. My program is below. I just need screen shots of the unit test code in Visual Studio 2019 with MSTest. Please and thank you. so easentially I need someone to write some unit tests with my program in MSTest visual studio 2019. Then I need the unit tests supplied as answers. using System; namespace SelectionSortAlgorithm { public class SelectionSort { public static void...
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
I keep getting minor errors I can't figure out and I don't know how to convert...
I keep getting minor errors I can't figure out and I don't know how to convert decimal .10 to percentage 10% either.   With these functions defined now expand the program for a company who gives discounts on items bought in bulk. Create a main function and inside of it ask the user how many different items they are buying. For each item have the user input a price and quantity, validating them with the functions that you wrote. Use your...
In java, I keep getting the error below and I can't figure out what i'm doing...
In java, I keep getting the error below and I can't figure out what i'm doing wrong. Any help would be appreciated. 207: error: not a statement allocationMatrix[i][j];
One part of this question I am getting wrong. I'm assuming it's the test statistic? I...
One part of this question I am getting wrong. I'm assuming it's the test statistic? I got 1 for Question C, and I got 5.432 for question b, and I got B for question C. An undergraduate student performed a survey on the perceived physical and mental health of UBC students for her term project. She collected information by asking students whether they are satisfied with their physical and mental health status. 129 male and 157 female UBC students were...
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint....
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'test_ibfk_5' in the referenced table 'appointment', can you please tell me what is wrong with my code: -- Table III: Appointment = (site_name [fk7], date, time) -- fk7: site_name -> Site.site_name DROP TABLE IF EXISTS appointment; CREATE TABLE appointment (    appt_site VARCHAR(100) NOT NULL, appt_date DATE NOT NULL, appt_time TIME NOT NULL, PRIMARY KEY (appt_date, appt_time), FOREIGN KEY (appt_site)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT