天天看點

連結清單實戰之超市購物車 | 帶你學《Java面向對象程式設計》之九十六

上一篇:連結清單實戰之寵物商店 | 帶你學《Java面向對象程式設計》之九十五

【本節目标】

通過閱讀本節内容,你将借助連結清單這一工具,實作對更加複雜的現實情景的資料處理,進一步掌握連結清單的各類定義、實作以及運用手段。

綜合實戰:超市購物車

使用面向對象的概念表示出下面的生活場景:小明去超市買東西,所有買到的東西都放在了購物車,最後到收銀員結賬。

連結清單實戰之超市購物車 | 帶你學《Java面向對象程式設計》之九十六

圖一 超市購物設計實作

步驟:

1、定義商品标準

2、定義購物車标準

3、定義一個購物車的實作類

4、定義收銀台

5、定義商品資訊

圖書:

書包:

6、進行代碼測試的編寫

interface ILink<E> {         //設定泛型避免安全隐患
       public void add(E e) ;   //增加資料
       public int size() ;    //擷取資料的個數
       public boolean isEmpty() ;   //判斷是否空集合
       public Object[] toArray() ;     //将集合元素以數組的形式傳回
       public E get(int index) ;   //根據索引擷取資料
       public void set(int index,E data) ;    //修改索引資料
       public boolean contains(E data) ; //判斷資料是否存在
       public void remove(E e) ;        //資料删除
       public void clean() ;    //清空集合
}
class LinkImpl<E> implements ILink<E> {
      private class Node {         //儲存節點的資料關系
              private E data ;      //儲存資料
              private Node next ;       //儲存下一個引用
              public Node(E data) {          //有資料的情況下才有意義
                     this.data = data ;
              }
                //第一次調用:this = LinkImpl.root ;
                //第二次調用:this = LinkImpl.root.next ;
                 //第三次調用:this = LinkImpl.root.next.next ;
              public void addNode(Node newNode){      //儲存新的Node資料
                      if (this.next == null) {   //目前節點的下一個節點為null
                           this.next = newNode;      //儲存目前節點
                      }else {
                           this.next.addNode(newNode);
                      }
               }
                //第一次調用:this = LinkImpl.root
                //第二次調用:this = LinkImpl.root.next
                /第三次調用:this = LinkImpl.root.next.next
               public void toArrayNode() {
                     LinkImpl.this.returnData [LinkImpl.this.foot ++] = this.data ;
                     if (this.next != null) {     //還有下一個資料
                          this.next.toArrayNode() ;
                    }
               }
               public E getNode(int index) {
                      if (LinkImpl.this.foot ++ == index) {       //索引相同
                           return this.data ;    //傳回目前資料
                      }else {
                           return this.next.getNode(index) ;
                      }
               }
               public void setNode(int index,E data) {
                      if (LinkImpl.this.foot ++ == index) {       //索引相同
                           this.data = data ;    //修改資料
                      }else {
                            this.next.setNode(index,data) ;
                      }
               }
               public boolean containsNode(E data) {
                      if (data.equals(this.data)) {    //對象比較
                           return true ;
                     }else {
                           if (this.next == null) {         //沒有後續節點
                                  return false ;   //找不到
                           }else {
                                  return this.next.containsNode(data) ;   //向後繼續判斷 
                           }
                     }
               }
               public void removeNode(Node<E> previous,E date) {
                      if (this.date.equals(date)) {
                           previous.next = this.next ;
                      }else {
                          if (this.next!=null) {
                                this.next.removeNode(this,date) ;
                          }
                      }
               }
               public void removeNode (Node previous,E data) {
                      if (this.data.equals(data)) {
                            previous.next = this.next ;    //空出目前節點
                      }else {
                            if (this.next != null) {       //有後續節點
                                 this.next.removeNode(this, data) ;    //向後繼續删除
                           }
                     }
               }
      }
      //------------以下為Link類中定義的成員-----------------
      private Node root ;       //儲存根元素
      private int count ;     //儲存資料的個數
      private int foot ;     //描述的是操作數組的腳标
      private Object[] returnData ;   //傳回的資料儲存
      //------------以下為Link類中定義的方法-----------------
      public void add(E e){
         if(e == null){
             return ;
         }
        //資料本身是不具有關聯特性的,隻有Node類有,要想關聯處理就必須将資料包裝在Node類中
         Node newNode = new Node(e);    //建立一個新的節點
         if (this.root == null){            //現在沒有根節點
            this.root = newNode;       //第一個節點作為根節點
         }else{                          //根節點存在
            this.root.addNode(newNode);       //将新節點儲存在合适的位置
         }   
         this.count++ ;  
     }
     public int size() {
            return this.count ;
     }
     public boolean isEmpty() {
             //return this.root == null ;
             return this.count == 0 ;
     }
     public Object[] toArray() {
            if (this.isEmpty()) {           //空集合
                return null ;      //現在沒有資料
            }
            this.foot = 0 ;  //腳标清零
            this.returnData = new Object[this.count] ;   //根據已有的長度開辟數組
            this.root.toArrayNode() ; //利用Node類進行遞歸資料擷取
            return this.returnData ;
     }
     public E get(int index) {
           if (index >= this.count) {    //索引應該在指定的範圍之内
                return null ;
           }    //索引資料的擷取應該由Node類完成
           this.foot = 0 ;   //重置索引的下标
           return this.root.getNode(index) ;
     }
     public void set(int index,E data) {
             if (index >= this.count) {    //索引應該在指定的範圍之内
                return  ;     //方法結束
           }    //索引資料的擷取應該由Node類完成
           this.foot = 0 ;   //重置索引的下标
           this.root.setNode(index,data) ;  //修改資料
     }  
     public boolean contains(E data) {
            if (data == null) {
                 return false ;     //沒有資料
            }
            return this.root.containsNode(data) ;    //交給Node類判斷
     }
     public void remove(E data) {
            if (this.contains(data)) {     //判斷資料是否存在
                  if (this.root.data.equals(data)) {       //根節點為要删除節點
                      this.root = this.root.next ;    //根的下一個節點  
                  }else {         //交由Node類進行删除
                       this.root.next.removeNode(this.root , data) ;
                  }
                  this.count -- ;
            }
     }
     public void clean() {
           this.root = null ;  //後續的所有節點都沒了
           this.count = 0 ;   //個數清零
     }
}
interface IGoods{    //定義商品标準
    public String getName();
    public double getPrice();
}
interface IShopCar{       //購物車
    public void add(IGoods goods);   //添加商品資訊
    public void delete(IGoods goods); //删除商品
    public Object [] getAll();   //獲得購物車中全部商品資訊
}
interface ICashier{
    public int getNumber();
    public double getPrice(); 
}
class ShopCarImpl implements IShopCar{   //購物車
    ILink<IGoods> allGoodseses = new LinkImpl<IGoods>() ;
    public void add(IGoods goods) {
        this.allGoodseses.add(goods);
    }
    public void delete(IGoods goods) {
        this.allGoodses.remove(goods);
    }
    public Object [] getAll() {
        return this.allGoodses.toArray();
    }
}
class Cashier {     //收銀台
private IShopCar shopcar;
public Cashier(IShopCar shopcar) {
        this.shopcar = shopcar;
    }
public double allPrice() {    //計算總價
        double all =0.0;
    Object result [] = this.shopcar.getAll() ;
        for(Object obj : result) {
            IGoods goods = (IGoods)obj;
            all += goods.getPrice();
        }
        return all ;
}
public int allCount() {    //商品數量
       return this.shopcar.getAll().length;
}
 }
class Bag implements IGoods{
    private String name;
    private double price;
    public Bag(String name,double price) {
        this.name = name;
        this.price = price;
    }
    public String getName() {
        return this.name;
    }
    public double getPrice() {
        return this.price;
    }
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(this == obj){
            return true;
        }
        if(!(obj instanceof Bag)) {
            return false;
        }
        Bag bag = (Bag) obj;
        return this.name.equals(bag.name) && this.price == bag.price;
    }
    public String toString() {
        return "【背包資訊】名稱:"+this.name + "、價格:"+this.price;
    }
}
class Book implements IGoods {
    private String name ;
    private double price ;
    public Book(String name,double price) {
        this.name = name ;
        this.price = price ;
    }
    public String getName() {
        return this.name;
    }
    public double getPrice() {
        return this.price;
    }
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(this == obj){
            return true;
        }
        if(!(obj instanceof Book)) {
            return false;
        }
        Book book = (Book) obj;
        return this.name.equals(book.name) && this.price == book.price;
    }
    public String toString() {
        return "【圖書資訊】名稱:"+this.name + "、價格:"+this.price;
    }
}
public class JavaDemo{
    public static void main(String args[]) {
        IShopcar car = new ShopCarImpl();
        car.add(new Book("Java開發",79.8));
        car.add(new Book("Oracle ",89.8));
        car.add(new Bag("小強背包",889.8));
        Cashier cas = new Cashier(car);
        System.out.println("總價格:"+cas.allPrice ()+"、購買總數量:"+cas.allCount ());
    }
}           
連結清單實戰之超市購物車 | 帶你學《Java面向對象程式設計》之九十六

圖二 執行結果圖

整體代碼都是基于連結清單的功能實作的。

想學習更多的Java的課程嗎?從小白到大神,從入門到精通,更多精彩不容錯過!免費為您提供更多的學習資源。

本内容視訊來源于

阿裡雲大學 下一篇:認識開發利器-Eclipse | 帶你學《Java面向對象程式設計》之九十七 更多Java面向對象程式設計文章檢視此處

繼續閱讀