Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sopping cart design in java. Hi i want to create a shopping cart in java, but im

ID: 3773886 • Letter: S

Question

Sopping cart design in java.

Hi i want to create a shopping cart in java, but im not sure where to start. This is a project for an intro to programming course, so i cant have fancy imports or hasmaps for the arrays, also it needs to display on the console. And i need three or four classes.

The idea is like this, i present a menu

1 dairy- select one for dairy. if you do select one a new prompt that has a options for dairy items such as eggs, cheese, milk. and then you can choose to add to your cart. Also there should be a price for each item.

I want to create thre categories, dairy, meats, vegies. with the same functionality described above.

once the user is done and selects to exit the program, a display with the total and the items chosen would come up.

Im not sure how this website works, so im just writing a general idea. I dont know if i can be responded to, or an answer or suggestion will be posted. I'll wait and see.

Explanation / Answer


1.So let’s start by writing an IOrder interface. This interface is having method exposed to client.
public interface IOrder {
   boolean addProduct(Product d);
   boolean removeProduct(String did) throws ProductNotFoundException;
   Collection getCartDetails();
   Product getProductFromCart(String did) throws ProductNotFoundException;
   int productCount();
double getCartPrice();
}
2.Here is over customized exception class ProductNotFoundException, used to throw when any product is not found in cart while retrieving from Cart.
public class ProductNotFoundException extends Exception {
   public ProductNotFoundException(){}
   public ProductNotFoundException(String msg){
       super(msg);
   }
}

3.In a shopping cart, Product is a base entity, so we need a class representing Product.
public String did;
public String dname;
public int qty;
public double price;

public Product(){}
public Product(String did, String dname, int qty, double price) {
this.pid = did;
this.pname = dname;
this.qty = qty;
this.price = price;
}
4.Now lets create a class which implements IOrder interface and do all the core work for us.


public class Order implements IOrder {

   public String uid;
   private Map map;

   public Order(){}
   public Order(String uid){
       this.uid=uid;
       map = new HashMap();
   }//Order

   public boolean addProduct(Product d){
       if(map.containsKey(d.getdid())){
           Product d1 = map.get(d.getdid());
           d1.setPrice(d1.getPrice()+p.getPrice());
           d1.setQty(d1.getQty()+p.getQty());
           return true;
       }
       map.put(d.getdid(),d);
       return false;
   }//addProduct

   public boolean removeProduct(String did)
throws ProductNotFoundException {
       if(map.containsKey(did)){
           map.remove(did);
           return true;
       }else throw new ProductNotFoundException(
"Product with ID "+did+" is not Found.");
   }

   public Collection getCartDetails(){
       return map.values();
   }

   public Product getProductFromCart(String did)
throws ProductNotFoundException {
       if(map.containsKey(did)){
           return map.get(did);
       }else {
           throw new ProductNotFoundException(
"Product with ID "+did+" is not Found.");
       }
   }

   public int productCount(){
       return map.size();
   }

public double getCartPrice() {
double price = 0.0d;
Iterator iterator = getCartDetails().iterator();
while(iterator.hasNext()){
price+= iterator.next().getPrice();
}
return price;
}
}

public class Order implements IOrder {

public String uid;
private Map map;

public Order(){}
public Order(String uid){
this.uid=uid;
map = new HashMap();
}//Order

public boolean addProduct(Product d){
if(map.containsKey(d.getdid())){
Product d1 = map.get(d.getdid());
d1.setPrice(d1.getPrice()+d.getPrice());
d1.setQty(d1.getQty()+d.getQty());
return true;
}
map.put(d.getdid(),d);
return false;
}//addProduct

public boolean removeProduct(String did)
throws ProductNotFoundException {
if(map.containsKey(did)){
map.remove(did);
return true;
}else throw new ProductNotFoundException(
"Product with ID "+did+" is not Found.");
}

public Collection getCartDetails(){
return map.values();
}

public Product getProductFromCart(String did)
throws ProductNotFoundException {
if(map.containsKey(did)){
return map.get(did);
}else {
throw new ProductNotFoundException(
"Product with ID "+did+" is not Found.");
}
}

public int productCount(){
return map.size();
}

public double getCartPrice() {
double price = 0.0d;
Iterator iterator = getCartDetails().iterator();
while(iterator.hasNext()){
price+= iterator.next().getPrice();
}
return price;
}

//getter/setter abbreviated.
}//class
}
5.Now time to test what we wrote. Create a class which Create get the order from customer, add product, get cart price etc.


public class OrderTest {

   public static void main (String[] s) throws Exception {

       Order o = new Order("Dairy");
       o.addProduct(new Product("d101","Dairy",12,120));
       o.addProduct(new Product("d102","meet",4,140));
o.addProduct(new Product("d103","vegies",7,140));
      
System.out.println(" No. of Product : "+o.productCount());
       System.out.println ("Order Places By: "+o.uid);

// code to remove product from Cart
// o.removeProduct("d103");
// Get Product By its Id
// Product d = o.getProductFromCart("d102");

System.out.println (o.getCartPrice());
       final Iterator it = o.getCartDetails().iterator();
       while(it.hasNext()){
           System.out.println (it.next());
       }
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote