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 computeSalePricel0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

}

}

Add a comment
Know the answer?
Add Answer to:
JAVA i need write java program Object Classes: designing Shopping Cart 1.Shopping Cart , build a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).

    Ch 7 Program: Online shopping cart (continued) (Java)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...

  • 4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping...

    4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (solution from previous lab assignment is provided in Canvas). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal PrintItemDescription() -...

  • DESCRIPTION You have to design an e-commerce shopping cart. These require classes Item, Electronics, Food, Dress,...

    DESCRIPTION You have to design an e-commerce shopping cart. These require classes Item, Electronics, Food, Dress, Cart and Main. ITEM CLASS Your Item class should contain: Attributes (protected) String name - name of the Item double price - price of the item Methods (public) void setName(String n) - sets the name of Item void setPrice(double p) - sets the price of Item String getName() - retrieves name double getPrice() - retrieves price String formattedOutput() returns a string containing detail of...

  • ​I have to create two classes one class that accepts an object, stores the object in...

    ​I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList {    private ShoppingItem [] list;    private int amtItems = 0;       public ShoppingList()    {       list=new ShoppingItem[8];    }       public void add(ShoppingItem item)    {       if(amtItems<8)       {          list[amtItems] = ShoppingItem(item);...

  • I need help with this assignment, can someone HELP ? This is the assignment: Online shopping...

    I need help with this assignment, can someone HELP ? This is the assignment: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by...

  • 11.12 LAB*: Program: Online shopping cart (continued) This program extends the earlier "Online shopping cart" pr...

    11.12 LAB*: Program: Online shopping cart (continued)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class to contain a new attribute. (2 pts)item_description (string) - Set to "none" in default constructorImplement the following method for the ItemToPurchase class.print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter.Ex. of print_item_description() output:Bottled Water: Deer Park, 12 oz.(2) Build the ShoppingCart class with the following data attributes and related methods. Note: Some can be method stubs...

  • 8.7 LAB*: Program: Online shopping cart (Part 2)

    8.7 LAB*: Program: Online shopping cart (Part 2)Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized...

  • Can the folllowing be done in Java, can code for all classes and code for the...

    Can the folllowing be done in Java, can code for all classes and code for the interface be shown please. Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometriObject class for finding the larger of two GeometricObject objects. Write a test program that uses the max method to find the larger of two circles, the larger of two rectangles. The GeometricObject class is provided below: public abstract class GeometricObject { private...

  • Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this...

    Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this is in Zybooks Existing Code # Type code for classes here class ItemToPurchase: def __init__(self, item_name="none", item_price=0, item_quantity=0): self.item_name = item_name self.item_price = item_price self.item_quantity = item_quantity # def __mul__(self): # print_item_cost = (self.item_quantity * self.item_price) # return '{} {} @ ${} = ${}' .format(self_item_name, self.item_quantity, self.item_price, print_item_cost) def print_item_cost(self): self.print_cost = (self.item_quantity * self.item_price) print(('{} {} @ ${} = ${}') .format(self.item_name, self.item_quantity, self.item_price,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT