YOMEDIA
ADSENSE
Virtual Shopping
382
lượt xem 12
download
lượt xem 12
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
Lớp trừu tượng ShopItem
AMBIENT/
Chủ đề:
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Virtual Shopping
- 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 cart; // condtructor khong doi, khoi tao vector
- public Basket(){ cart = new Vector(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)
- // 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 listItem; private Basket basket; private Keyboard keyboard;
- public Shop() { listItem = new Vector(10,5); basket = new Basket(); keyboard = new Keyboard(); } void addItem(ShopItem I){ this.listItem.add(I); } public String toString(){ String list =""; for(int i = 0;i < this.listItem.size();i++) list += i+1 + "--"+ this.listItem.get(i).toString()+"\n"; return list; } public void showCatalog(){ System.out.println("-----------------------------------"); System.out.println("\t\t VIRTUAL SHOP CATALOGUE"); System.out.println("\t7 items in shop"); } public char chooseAction(){ this.showCatalog(); System.out.println(this); System.out.println(" THE VIRTUAL SHOP (Internet Shoping Centre)\n"); System.out.println(" Q -Quit"); System.out.println(" A -Add to shopping basket"); System.out.println(" R -Remove from basket"); System.out.println(" P -Print invoice"); char c = Character.toUpperCase(keyboard.readChar("Enter selection: ")); return c; } public void run(){ char c = this.chooseAction(); if(c=='A'){ addToBasket(); System.out.println("Adding item to basket successfull"); this.run(); } if(c=='R'){ removeFromBasket(); System.out.println("Remove item from basket successfull"); this.run(); } if(c=='P'){ System.out.println(basket); basket.printInvoice(); this.run();
- } if(c=='Q'){ System.exit(0); } } public void addToBasket(){ int i = keyboard.readInt("Select item by number: "); basket.add(listItem.get(i-1)); } public void removeFromBasket(){ int i = keyboard.readInt("Select item to remove by number: "); basket.remove(i-1); } public static void main(String[] arg){ Shop shop = new Shop(); shop.addItem(new Book("C++ Primer",74.95, 600)); shop.addItem(new Book("C++ for Java Programmers",64.95, 200)); shop.addItem(new Book("Linux in a nutshell",85, 950)); shop.addItem(new Software("Borland C++",145, 1)); shop.addItem(new Software("Adobe photoshop",650, 2)); shop.addItem(new Software("Linux RedHat 7.2",34.5,2)); shop.addItem(new Software("QuickTax2000", 49.95, 1)); shop.run(); } }
ADSENSE
CÓ THỂ BẠN MUỐN DOWNLOAD
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
AANETWORK
TRỢ GIÚP
HỖ TRỢ KHÁCH HÀNG
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn