JAVA i need write java program Object Classes: designing Shopping Cart 1.Shoppin
ID: 3708359 • Letter: J
Question
JAVA
i need write java program
Object Classes: designing Shopping Cart
1.Shopping Cart , build a checkout system for a shop which sells items (i.e., products say Bread, Milk, and Bananas). A shopping cart that can have multiples. Costs of the products are : Bread - $1, Milk - $0.60 and Banana - $0.40. A system should displays the order total.
2.The heart of a shopping cart can be represented in three classes: a cart class an order class, and an item class. Item objects are added to an array stored within the cart object.
3.Think first what are the nouns in the specification? (ex: Item) These are likely your Objects or possibly key data in an object
4.Which responsibilities belong to which nouns? These are likely to become their What are the relationships between nouns (Objects)? Draw it out that will help you visualize how they are interconnected
interace -oProductinterface o +double computeSalePriced + double getRegularPricep o+void setRegularPrice(double regularPrice) impleents interTace Product intertace Bookinterface Electronicsintertace -double regularPrice +Sinng gettianufacturer) +String getPublishet vold setPublishenString publisherj +Product(double regularPrice + double computeSalePriceQ o+int getfearPublisheco +void setYearP ublished(int?arPublished) o+double getRegula Price) ?+void setRegularPrice(double regularPrice) implements» -String manufacturer Electronicsidouble regularPrice, String manufacturer) o 3tring getManufacturer0 o tvoid setklanutacturerSbing manufacturer) Book Sting publisher -int year Published Bookdoule regularPrice, Sring publisher, ini rearPublishec) o double computealePrice0 O Sbing getPublisher MP3Player vold setPublisher(Sting publisher) int geYearPublishedo int size -String color ?-void serre arPublished(int yearPublished TVidouble regularPfice, String manufacturer, int seMP3Playe double regularPrice, String manufacturer, String colorn o+double computesalePrice0 o+double computesalePrice0 +String getColor0 o void setColor String color) Cartoon ChildrenBook Main 4-String characterName -int age +Maino ?+Cartoon[double regularFnce, string publisher, int yearPublished, string characterName) ChildrenBookidouble regularPrice, String publisher, int yearPublish double computeSalePriceo double computeSalePricel0Explanation / Answer
Product.java
package collections.shoppingcart;
import java.util.Objects;
class Product {
private Integer pid;
private String name;
private Double price;
private Integer stock;
public Product () {
}
public Product (Integer pid, String name, Double price, Integer stock) {
this.pid = pid;
this.name = name;
this.price = price;
this.stock = stock;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
public Double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(Double price) {
this.price = price;
}
/**
* @return the stock
*/
public Integer getStock() {
return stock;
}
/**
* @param stock the stock to set
*/
public void setStock(Integer stock) {
this.stock = stock;
}
/**
* @return the pid
*/
public Integer getPid() {
return pid;
}
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + Objects.hashCode(this.pid);
hash = 29 * hash + Objects.hashCode(this.name);
hash = 29 * hash + Objects.hashCode(this.price);
hash = 29 * hash + Objects.hashCode(this.stock);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Product other = (Product) obj;
if (!Objects.equals(this.name, other.name)) {
return false;
}
if (!Objects.equals(this.pid, other.pid)) {
return false;
}
if (!Objects.equals(this.price, other.price)) {
return false;
}
if (!Objects.equals(this.stock, other.stock)) {
return false;
}
return true;
}
/**
* @param pid the pid to set
*/
public void setPid(Integer pid) {
this.pid = pid;
}
}
Products.java
package collections.shoppingcart;
import java.util.ArrayList;
import java.util.List;
public class Products {
private final List<Product> products = new ArrayList<Product>();
public Products () {
this.initStoreItems();
}
public List<Product> getProducts() {
return products;
}
public void initStoreItems() {
String [] productNames = {"Lux Soap", "Fair n Lovely", "MTR"};
Double [] productPrice = {40.00d, 60.00d, 30.00d};
Integer [] stock = {10, 6, 10};
for (int i=0; i < productNames.length; i++) {
this.products.add(new Product(i+1, productNames[i], productPrice[i], stock[i]));
}
}
}
Cart.java
package collections.shoppingcart;
import java.util.ArrayList;
import java.util.List;
class Cart {
List<Product> cartItems = new ArrayList<Product>();
public void addProductToCartByPID(int pid) {
Product product = getProductByProductID(pid);
addToCart(product);
}
private Product getProductByProductID(int pid) {
Product product = null;
List<Product> products = new Products().getProducts();
for (Product prod: products) {
if (prod.getPid() == pid) {
product = prod;
break;
}
}
return product;
}
private void addToCart(Product product) {
cartItems.add(product);
}
public void removeProductByPID(int pid) {
Product prod = getProductByProductID(pid);
cartItems.remove(prod);
}
void printCartItems() {
for (Product prod: cartItems) {
System.out.println(prod.getName());
}
}
}
UI.java
package collections.shoppingcart;
import java.util.List;
import java.util.Scanner;
public class UI {
Cart cart = new Cart();
private int ch = 0;
public UI () {
menu();
}
public void startScreen () {
System.out.println("1. Display Store Products");
System.out.println("2. Display Cart");
System.out.println("0. Exit");
}
public void storeProductsMenu() {
System.out.println("1. Add to Cart");
System.out.println("2. Remove From Cart");
System.out.println("0. Exit");
}
public void menu () {
do {
startScreen();
getUserInput();
switch (ch) {
case 1:
displayStoreProducts();
storeProductsMenu();
getUserInput();
innerChoice1();
break;
case 2:
showCart();
break;
case 0:
System.exit(0);
break;
default:
break;
}
} while (ch != 0);
}
private void innerChoice1() {
switch (ch) {
case 1:
addProductToCart();
showCart();
break;
case 2:
removeProductFromCart();
break;
default:
break;
}
}
private int getUserInput() throws NumberFormatException {
Scanner in = new Scanner (System.in);
ch = Integer.parseInt(in.nextLine());
return ch;
}
private void displayStoreProducts() {
List<Product> products = new Products().getProducts();
for (Product prod: products) {
System.out.println(
prod.getPid() + "- " +
prod.getName() + " " +
prod.getPrice() + " " +
prod.getStock()
);
}
}
private void addProductToCart() {
int pid = getUserInput();
cart.addProductToCartByPID(pid);
}
private void showCart() {
cart.printCartItems();
}
private void removeProductFromCart() {
int pid = getUserInput();
cart.removeProductByPID(pid);
}
}
Main.java
/*
* Create a fully functional program to store and delete objects from the cart
*/
package collections.shoppingcart;
public class Main {
public static void main (String [] args) {
new UI();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.