Question
Java Programming question: Create a shopping cart application with three classes (in three files) as follows: class CartItem defines these private data members: String item,
Java Programming question:
Create a shopping cart application with three classes (in three files) as follows:
class CartItem
- defines these private data members:
- String item, double price, int quantity
- defines a constructor that sets all data members when a CartItem is instantiated.
- defines setters for all data members.
- defines a subtotal method that returns the item price * quantity.
- defines a toString method that summarizes the data for an item.
class Cart
- defines these data members:
- a private ArrayList of CartItem objects .
- a public static double to hold the total for the cart, initial value zero.
- defines a constructor that instantiates the ArrayList of cart items.
- codes the method with the signature below to add a cart item and update the total:
- public void add(String item, double price, int quantity)
- codes a showCart method to print and number the arraylist cart items. This method also prints the cart total.
- codes the following method to remove an item based on its number in the cart.
- public void removeItem(int number)
class CartTest
- creates a new empty Cart and then adds five or six items to it.
- displays the cart.
- runs a loop at least once to ask if an item should be deleted, and if so, prompts for the item number, deletes that item, and redisplays the cart.
Sample Output
1 Cart item: Echo Dot, price=29.99, quantity=3
2 Cart item: Wemo plug, price=39.99, quantity=2
3 Cart item: Ear buds, price=49.99, quantity=2
4 Cart item: Google Home, price=79.0, quantity=1
Cart total $348.93
Do you wish to remove an item before checkout (y/n)?
y
Enter item # to delete
3
1 Cart item: Echo Dot, price=29.99, quantity=3
2 Cart item: Wemo plug, price=39.99, quantity=2
3 Cart item: Google Home, price=79.0, quantity=1
Cart total $248.95
Do you wish to remove an item before checkout (y/n)?
n
1 Cart item: Echo Dot, price=29.99, quantity=3
2 Cart item: Wemo plug, price=39.99, quantity=2
3 Cart item: Google Home, price=79.0, quantity=1
Cart total $248.95
_________________________________________________________________________________________
I'm not getting the correct output
Here is my code:
package chapter10;
public class CartItem {
private String item; private double price; private int quantity; public CartItem(String i, double p, int q) { setItem(i); setPrice(p); setQuantity(q); } public void setItem(String value) { this.item = value; } public void setPrice(double value) { this.price = value; } public void setQuantity(int value) { this.quantity = value; }
public double subTotal() { return price * quantity; } public String toString() { return "Item: "+ item +", Cost: "+ price; } }
package chapter10;
import java.util.ArrayList;
public class Cart {
private ArrayList
}
package chapter10;
public class CartTest {
static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String[] args) { //Create a new cart Cart cart = new Cart(); cart.add("Echo Dot", 29.99, 3); cart.add("Wemo plug", 39.99, 2); cart.add("Ear buds", 49.99, 2); cart.add("Google Home", 79.00, 1); //Display the cart cart.showCart(); String line = ""; char choice = 0; while(choice != 'n') { line = prompt("Do you wish to remove an item before checkout (y/n)?"); choice = line.toLowerCase().charAt(0); if (choice == 'y') { line = prompt("Enter item number to remove:"); cart.removeItem(Integer.parseInt(line)); } } cart.showCart(); } private static String prompt(String prompt) { while(true) { System.out.println(prompt); try { return scanner.nextLine(); } catch (Exception e) {} } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started