Virtual Shopping
// Lớp trừu tượng ShopItem
package virtualshopping;
abstract class ShopItem {
protected String iName; // khai bao ten hang hoa
protected double iPrice; // khai bao gia hang hoa
// xay dung constructor khong doi
public ShopItem() {
}
// xay dung constructor 2 doi
public ShopItem(String iName, double iPrice){
this.iName = iName;
this.iPrice = iPrice;
}
// ham lay ten hang
public String getName(){
return this.iName;
}
// ham lay gia hang
public double getPrice(){
return this.iPrice;
}
public abstract String toString();
// ham lay khoi luong
public abstract double getWeight();
// ham lay so dia CD
public abstract int getNoCD();
}
//Lớp Book thừa kế từ Lớp ShopItem
package virtualshopping;
class Book extends ShopItem{
private double bWeight; // khai bao thuoc tinh khoi luong cua book
// xay dung mot constructor khong doi
public Book(){
super();
}
// xay dung mot constructor 3 doi
public Book(String bName, double bPrice, double bWeight){
super(bName,bPrice);
this.bWeight = bWeight;
}
public String toString(){
return "Book Title: \""+this.iName+"\", Price: $"+this.iPrice+", Weight: "+this.bWeight;
}
// ham lay khoi luong cua book
public double getWeight(){
return this.bWeight;
}
// ham lay so dia CD
public int getNoCD(){
return 0;
}
}
//Lớp Software thừa kế từ lớp ShopItem
package virtualshopping;
class Software extends ShopItem{
private int numOfCD; // khao bao so dia CD
// khai bao constructor khong doi
public Software() {
super();
}
// khai bao constructor 3 doi
public Software(String sName, double sPrice, int numOfCD){
super(sName,sPrice);
this.numOfCD = numOfCD;
}
public String toString(){
return "Software Title: \""+this.iName+"\", Price: $"+this.iPrice+ ", Quantity: "+this.numOfCD;
}
// ham lay khoi luong
public double getWeight(){
return 0;
}
// ham lay so dia CD
public int getNoCD(){
return this.numOfCD;
}
}
//package Shop;
package virtualshopping;
import java.util.Vector;
class Basket implements Cost{
// khai bao vector
private Vector<ShopItem> cart;
// condtructor khong doi, khoi tao vector
public Basket(){
cart = new Vector<ShopItem>(10,5);
}
// them mot mon hang vao gio hang
public void add(ShopItem I){
this.cart.add(I);
}
// bo bot mot mon hang ra khoi gio hang
public void remove(int index){
this.cart.removeElementAt(index);
}
// ham tinh tong chi phi
public double totalPrice(){
double totalPrice = 0.0;
for(int i = 0;i < this.cart.size();i++)
totalPrice +=this.cart.elementAt(i).getPrice();
return totalPrice;
}
// ham tinh tong trong luong
public double totalWeight(){
double totalWeight = 0.0;
for(int i = 0;i < this.cart.size();i++)
totalWeight +=this.cart.elementAt(i).getWeight();
return totalWeight;
}
// ham tinh tong so luong CD
public int totalCD(){
int totalCD = 0;
for(int i = 0;i < this.cart.size();i++)
totalCD +=this.cart.elementAt(i).getNoCD();
return totalCD;
}
// ham tinh toan chi phi dong goi - van chuyen cua book và software
public double bookPP(){
double totalWeight = this.totalWeight();
double bookPP = 0.0;
if(totalWeight > 0 && totalWeight < BOOK_WEIGHT1){
bookPP = BOOK_PP1;
}
if(totalWeight >= BOOK_WEIGHT1 && totalWeight < BOOK_WEIGHT2){
bookPP = BOOK_PP2;
}
if(totalWeight >= BOOK_WEIGHT2 && totalWeight< BOOK_WEIGHT2 + BOOK_WEIGHT_INCREASE){
bookPP = BOOK_PP2 + BOOK_PP_INCREASE;
}
if(totalWeight >= BOOK_WEIGHT2+BOOK_WEIGHT_INCREASE){
bookPP = BOOK_PP2+(totalWeight/BOOK_WEIGHT_INCREASE)*BOOK_PP_INCREASE;
}
return bookPP;
}
//chi phi van chuyen dong goi cua Software
public double softwarePP(){
int totalCD = this.totalCD();
return (totalCD)<=3? SOFTWARE_PP*totalCD:SOFTWARE_PP*3+(totalCD-
SOFTWARE_LEVEL)*SOFTWARE_PP_INCREASE;
}
public String toString(){
String basket ="";
for(int i = 0;i < this.cart.size();i++)
basket += i+1 +"--"+ this.cart.get(i).toString()+"\n";
return basket;
}
//in hoa don
public void printInvoice(){
System.out.println(" Total price of selection: $"+this.totalPrice());
System.out.println(" Book total weight: "+this.totalWeight()+"g. Postage and packing: $"+this.bookPP());
System.out.println(" Number of Sofware Titles: "+this.totalCD()+". Postage and packing: $"+this.softwarePP());
System.out.println(" Total basket cost <include postage and packing>: $"+(this.totalPrice()+this.bookPP()
+this.softwarePP()));
}
}
//Tính chi phí
package virtualshopping;
interface Cost {
int SOFTWARE_LEVEL = 3; // mua 3 software
int SOFTWARE_INCREASE = 1; //moi software them vao gio hang
double SOFTWARE_PP = 3.25; // chi phi mua tu 1 den 3 software
double SOFTWARE_PP_INCREASE = 1.50; // chi phi moi software them vao gio hang
double BOOK_PP1 = 5.00; //chi phi cua book co khoi luong < 500g
double BOOK_PP2 = 9.50; // chi phi cua book co khoi luong 500g den 1000g
double BOOK_PP_INCREASE = 7.00; // chi phi cua moi book them vao gio hang
int BOOK_WEIGHT1 = 500; // khoi luong book < 500g
int BOOK_WEIGHT2 = 1000; // khoi luong book tu 500g den 1000g
int BOOK_WEIGHT_INCREASE = 1000; // khoi luong moi 1000g book them vao gio hang
}
// class doc mot ki tu nhap tu ban phim
package virtualshopping;
import java.io.*;
class Keyboard {
private BufferedReader br;
public Keyboard(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public int readInt(String msg){
System.out.print(msg);
try{
return Integer.parseInt(br.readLine());
}catch(IOException e){
System.out.println("Error: IOException");
}catch(NumberFormatException nf){
System.out.println("Error: Invalid choice (NumberFormatException - Integer)");
}
return 1;
}
public char readChar(String msg){
System.out.print(msg);
try{
return br.readLine().charAt(0);
}catch(IOException e){
System.out.println("Error: IOException");
}
return '1';
}
}
//Lớp Shop chứa hàm main
package virtualshopping;
import java.util.Vector;
public class Shop {
// khai bao vecto
private Vector<ShopItem> listItem;
private Basket basket;
private Keyboard keyboard;
public Shop() {
listItem = new Vector<ShopItem>(10,5);
basket = new Basket();