import java.io.Serializable; public abstract class Product implements Serializable{ private String name; private String
Posted: Sat Nov 27, 2021 2:31 pm
import java.io.Serializable;
public abstract
class Product
implements Serializable{
private
String name;
private
String description;
private
float originalPrice;
public
Product(String name, String description, float
price){
this.name = name;
this.description = description;
originalPrice = price;
}
public
String getName(){return name;}
public
void setName(String
name){this.name = name;}
public
String getDescription(){return description;}
public
void setDescription(String d) {description =
d;}
public
float getOriginalPrice() {return
originalPrice;}
public
void setOriginalPrice(float p)
{originalPrice = p;}
}
//-----------------------------------------------------------------------------------
public interface
HasQuantityPrice{
// calculates the price per
item if a quantity of product type changes
float
getQuantityPrice(int quantity);
}
//-----------------------------------------------------------------------------------
public class
Software extends
Product{
//Notice that this class
doesn't implement HasQuantityPrice
private
int version;
public
Software(String name, String desc, float price,
int version){
super(name, desc, price);
this.version= version;
}
public
int getVersion() {return
version;}
public
void setVersion(int version)
{this.version = version;}
}
//-----------------------------------------------------------------------------------
public class
Book extends
Product implements
HasQuantityPrice{
private
String author;
public
Book(String name, String description, float p,
String author){
super(name, description, p);
this.author = author;
}
public
String getAuthor() {return author;}
public
void setAuthor(String author)
{this.author = author;}
}
//-----------------------------------------------------------------------------------
public class
Toy extends
Product implements
HasQuantityPrice{
private
int minAge;
public
Toy(String name, String description, float price,
int minAge){
super(name, description, price);
this.minAge = minAge;
}
public
int getMinAge() {return
minAge;}
public
void setMinAge(int minAge)
{this.minAge = minAge;}
@Override
public
float getQuantityPrice(int
quantity){
if(quantity == 1) return
getOriginalPrice();
return getOriginalPrice() * 0.9f;
}
}
//-----------------------------------------------------------------------------------
public class
CartItem{
private
Product product; // to be bought
private
float price; // might be different from the
original price when
// the quantity of the product
type changes
public
CartItem(Product product, float price){
this.product = product;
this.price = price;
}
public
Product getProduct(){return product;}
public
float getPrice(){return
price;}
public
void setProduct(Product
product){this.product = product;}
public
void setPrice(float
price){this.price = price;}
}
//-----------------------------------------------------------------------------------
import java.util.ArrayList;
public class
ShoppingCart{
//includes items that user
is going to buy
private
ArrayList<CartItem> cartItems = new
ArrayList<CartItem>();
}
public abstract
class Product
implements Serializable{
private
String name;
private
String description;
private
float originalPrice;
public
Product(String name, String description, float
price){
this.name = name;
this.description = description;
originalPrice = price;
}
public
String getName(){return name;}
public
void setName(String
name){this.name = name;}
public
String getDescription(){return description;}
public
void setDescription(String d) {description =
d;}
public
float getOriginalPrice() {return
originalPrice;}
public
void setOriginalPrice(float p)
{originalPrice = p;}
}
//-----------------------------------------------------------------------------------
public interface
HasQuantityPrice{
// calculates the price per
item if a quantity of product type changes
float
getQuantityPrice(int quantity);
}
//-----------------------------------------------------------------------------------
public class
Software extends
Product{
//Notice that this class
doesn't implement HasQuantityPrice
private
int version;
public
Software(String name, String desc, float price,
int version){
super(name, desc, price);
this.version= version;
}
public
int getVersion() {return
version;}
public
void setVersion(int version)
{this.version = version;}
}
//-----------------------------------------------------------------------------------
public class
Book extends
Product implements
HasQuantityPrice{
private
String author;
public
Book(String name, String description, float p,
String author){
super(name, description, p);
this.author = author;
}
public
String getAuthor() {return author;}
public
void setAuthor(String author)
{this.author = author;}
}
//-----------------------------------------------------------------------------------
public class
Toy extends
Product implements
HasQuantityPrice{
private
int minAge;
public
Toy(String name, String description, float price,
int minAge){
super(name, description, price);
this.minAge = minAge;
}
public
int getMinAge() {return
minAge;}
public
void setMinAge(int minAge)
{this.minAge = minAge;}
@Override
public
float getQuantityPrice(int
quantity){
if(quantity == 1) return
getOriginalPrice();
return getOriginalPrice() * 0.9f;
}
}
//-----------------------------------------------------------------------------------
public class
CartItem{
private
Product product; // to be bought
private
float price; // might be different from the
original price when
// the quantity of the product
type changes
public
CartItem(Product product, float price){
this.product = product;
this.price = price;
}
public
Product getProduct(){return product;}
public
float getPrice(){return
price;}
public
void setProduct(Product
product){this.product = product;}
public
void setPrice(float
price){this.price = price;}
}
//-----------------------------------------------------------------------------------
import java.util.ArrayList;
public class
ShoppingCart{
//includes items that user
is going to buy
private
ArrayList<CartItem> cartItems = new
ArrayList<CartItem>();
}