Program Description: This assignment will give you practice writing Java classes
ID: 3662768 • Letter: P
Question
Program Description: This assignment will give you practice writing Java classes to a provided API and using Collections. You are to write a set of supporting classes for a simple shopping cart. The instructor has provided the Graphical User Interface (GUI) that will provide the "front end" or "view" to your program. You are to write the back end (what is often referred to as the "domain specific code" or the "model") Here is a screen shot of what the program could look like when the user has selected various items to order (note that the exact appearance may be platform dependent) Prices are expressed as real numbers and quantities are expressed as integers (i.e., you can't buy 2.345 units of something). Notice that some of the items have a discount when you buy more. For example, Silly Putty normally costs $4.41 per unit, but you can buy 6 for $10.04. These items have, in effect, two prices: a single item price and a bulk item price for a bulk quantity. The bulk quantity discount is only used if the membership checkbox at the bottom of the GUI is selected. When computing the total with the bulk item discount, apply as many of the bulk quantity as you can and then use the single item price for any leftovers. For example, the user is ordering 12 buttons that cost $0.95 each but can be bought in bulk at 10 for $5.00. The first 10 are sold at that bulk price (S5.00) and the two extras are charged at 1G4 inch Plasma HDrV, 51,297sG the single item price ($0.95 each) for a total of $6.90. If the user were ordering 22 buttons, the total would be $11.90 (two bulk quantities plus two extras) Shopping Cert order total $1,374.87 7 Silly Putty, $4.41 (6 for $10.04) Java Rules! button, 50.95 (10 for $5.00) Java Rules! bumper sticker, $0.99 (20 for $B.95) mputer science pen, $2.00 1 Rubik's Cube, $9.99 2 Mp3, Mp4, FMI Music Player, $18.01 Arduino Uno Ultimate Starter Kit, $54.99 LEGO Mindstorms EV3, $349.95 ox One, $399.00 Play Station 4, $399.00 1 64 inch Plasma HDTV, $1,297.99 Clear customer has store membershi At the bottom of the GUI there is a checkbox for a discount for customers who have a store membership. If this box is checked, the bulk pricing discount is applied to any items with bulk pricing as described above ShappingLat order total $1,358.45 TSIlly Putty, $4.41 (6 tor $10.04) Java Rules! button, $0.95 (10 tor $5.00) For example, if we select that checkbox, the GUI looks like this Java Rules! bumper sticker, $0.99 [20 for $8.95) science pen, $2.00 Implementation Guidelines: 1Rubik's Cube, $9.99 3, Mp4, FM Music Player, $18.01 Your task is to implement the three classes that are used to make this code work: Arduino Uno Ultimate Starter Kit, $54.99 EGO Mindstorms EV3, $349.95 *Item Xbox One, $399.00 ItemOrder yStation 4, $399.00 * ShoppingCart 4 Inch Plasma HDTV-$1,297.99 Clear customer has store membershipExplanation / Answer
IOrder Java Class:
import java.util.Collection;
import org.techzoo.shopping.exception.ProductNotFoundException;
import org.techzoo.shopping.bean.Product;
public interface IOrder {
boolean addProduct(Product p);
boolean removeProduct(String pid) throws ProductNotFoundException;
Collection<Product> getCartDetails();
Product getProductFromCart(String pid) throws ProductNotFoundException;
int productCount();
double getCartPrice();
}
Order Java Class:
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Collection;
import org.techzoo.shopping.exception.ProductNotFoundException;
import org.techzoo.shopping.bean.Product;
public class Order implements IOrder {
public String uid;
private Map<String, Product> map;
public Order(){}
public Order(String uid){
this.uid=uid;
map = new HashMap<String, Product>();
}//Order
public boolean addProduct(Product p){
if(map.containsKey(p.getPid())){
Product p1 = map.get(p.getPid());
p1.setPrice(p1.getPrice()+p.getPrice());
p1.setQty(p1.getQty()+p.getQty());
return true;
}
map.put(p.getPid(),p);
return false;
}//addProduct
public boolean removeProduct(String pid)
throws ProductNotFoundException {
if(map.containsKey(pid)){
map.remove(pid);
return true;
}else throw new ProductNotFoundException(
"Product with ID "+pid+" is not Found.");
}
public Collection<Product> getCartDetails(){
return map.values();
}
public Product getProductFromCart(String pid)
throws ProductNotFoundException {
if(map.containsKey(pid)){
return map.get(pid);
}else {
throw new ProductNotFoundException(
"Product with ID "+pid+" is not Found.");
}
}
public int productCount(){
return map.size();
}
public double getCartPrice() {
double price = 0.0d;
Iterator<Product> iterator = getCartDetails().iterator();
while(iterator.hasNext()){
price+= iterator.next().getPrice();
}
return price;
}
}
Order Test Java Class:
import java.util.Collection;
import java.util.Iterator;
import org.techzoo.shopping.exception.ProductNotFoundException;
import org.techzoo.shopping.bean.Product;
import org.techzoo.shopping.order.Order;
public class OrderTest {
public static void main (String[] s) throws Exception {
Order o = new Order("Tousif Khan");
o.addProduct(new Product("p101","Lux Soap",12,120));
o.addProduct(new Product("p102","Olive Oil",4,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("p103");
// Get Product By its Id
// Product p = o.getProductFromCart("p102");
System.out.println ("Cart Total : "+o.getCartPrice());
final Iterator<Product> it = o.getCartDetails().iterator();
while(it.hasNext()){
System.out.println (it.next());
}
}
}
Product Java Class:
public class Product implements java.io.Serializable {
public String pid;
public String pname;
public int qty;
public double price;
public Product(){}
public Product(String pid, String pname, int qty, double price) {
this.pid = pid;
this.pname = pname;
this.qty = qty;
this.price = price;
}
public void setPid(String pid) {
this.pid = pid;
}
public void setPname(String pname) {
this.pname = pname;
}
public void setQty(int qty) {
this.qty = qty;
}
public void setPrice(double price) {
this.price = price;
}
public String getPid() {
return (this.pid);
}
public String getPname() {
return (this.pname);
}
public int getQty() {
return (this.qty);
}
public double getPrice() {
return (this.price);
}
public String toString() {
String sep = System.getProperty("line.separator");
StringBuffer buffer = new StringBuffer();
buffer.append(sep);
buffer.append("----- Product Detail ----- ");
buffer.append(sep);
buffer.append(" pid = ");
buffer.append(pid);
buffer.append(sep);
buffer.append(" pname = ");
buffer.append(pname);
buffer.append(sep);
buffer.append(" qty = ");
buffer.append(qty);
buffer.append(sep);
buffer.append(" price = ");
buffer.append(price);
buffer.append(sep);
return buffer.toString();
}//toString
}//class
Product not found exception Java Class:
public class ProductNotFoundException extends Exception {
public ProductNotFoundException(){}
public ProductNotFoundException(String msg){
super(msg);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.