Question
I need the UML class and sequence diagram for the following: The code is at the bottom. 1. Shopping Cart In this exercise, you will
I need the UML class and sequence diagram for the following:
The code is at the bottom.
1. Shopping Cart In this exercise, you will implement a shopping card program using an array. 1. Write a class named Item as follows. a. The class has attributes of a name, price, and quantity (the quantity purchased) which are initialized in the constructor by the corresponding parameters of the constructor. b. The class has toString for printing an item as a string of price, quantity, and sub-total price (price*quantity) and getters for name, price, and quanitity. 2. Write a class named ShoppingCart as follows. a. The class has attributes of itemCount, totalPrice, and capacity which are initialized to 0, 0, and 5 respectively in the constructor. b. The class has a cart implemented as an array of items. cart is instantiated in the constructor with the capacity number of items. c. The class has a method named increaseSize increasing the size of the array by 3 elements. Hint: It is similar to the increaseSize() method in Listing 8.8 in the text. d. The class has a method named addToCart for adding the item to the cart and updating totalPrice (note that this should consider the quantity). e. The class has toString for printing a cart as a string of all the items in the cart with the grand total price. 3. Write the driver class Shopping that simulates shopping. The class should have a loop that continues as long as the user wants to shop up to two times of size increase of capacity. Each time through the loop read in the name, price, and quantity of the item the user wants to add to the cart. After adding an item to the cart, the cart contents should be printed. After the loop print a "Please pay ..." message with the total price of the items in the cart.
1.
public class Item {
private String name; private double price; private int quantity; public Item (String iName, double iPrice,int iPurchased) { name = iName; price = iPrice; quantity = iPurchased; } public String toString () { NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t" + fmt.format(price*quantity)); } public double getPrice() { return price; } public String getName() { return name; } public int getQuantity() { return quantity; } }
2.
public class ShoppingCart { private int itemCount; private double totalPrice; private int capacity; private Item[] cart;
public ShoppingCart() { capacity = 5; itemCount = 0; totalPrice = 0.0; cart=new Item[capacity]; }
public void addToCart(String itemName, double price, int quantity) { cart[itemCount]=new Item(itemName,price,quantity); totalPrice+=price; itemCount++; if(itemCount==capacity) { increaseSize(); } }
public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance();
String contents = " Shopping Cart "; contents += " Item\t\tUnit Price\tQuantity\tTotal ";
for (int i = 0; i < itemCount; i++) contents += cart[i].toString() + " ";
contents += " Total Price: " + fmt.format(totalPrice); contents += " ";
return contents; }
private void increaseSize() { Item[] temp=new Item[capacity+3]; System.arraycopy(cart, 0, temp, 0, capacity); cart=temp;
} }
3.
import java.util.Scanner; import java.text.NumberFormat; public class SimulateShopping {
static Scanner scn = new Scanner(System.in); static NumberFormat fmt = NumberFormat.getCurrencyInstance(); static ShoppingCart cart = new ShoppingCart();
static int totalQuantity = 0; static double totalPrice = 0; static int itemCount = 0; public static void main(String[] args) { displayChoice(); int ch = scn.nextInt(); while (ch != 0) { displayResult(ch); displayChoice(); ch = scn.nextInt(); } System.out.println("Please pay $" + totalPrice); }
public static void displayResult(int ch) { switch (ch) { case 0: System.out.println("Please pay $" + totalPrice); break; case 1: System.out.print("Item Name: "); String itemName = scn.next(); System.out.print("Price: $"); double price = scn.nextDouble(); System.out.print("Quantity: "); int quantity = scn.nextInt(); totalQuantity = +quantity; totalPrice += (quantity * price); itemCount += 1; cart.addToCart(itemName, price, quantity); break; default: System.out.println("Sorry, invalid choice"); } } public static void displayChoice() { System.out.println("Chooce option:"); System.out.println("0: Check Details"); System.out.println("1: Take a new item"); System.out.println(cart.toString()); System.out.print(" Enter your choice: "); } }
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