一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - JavaWeb后臺(tái)購(gòu)物車(chē)類(lèi)實(shí)現(xiàn)代碼詳解

JavaWeb后臺(tái)購(gòu)物車(chē)類(lèi)實(shí)現(xiàn)代碼詳解

2020-07-26 00:13MrDong先生 Java教程

這篇文章主要介紹了JavaWeb后臺(tái)購(gòu)物車(chē)類(lèi)實(shí)現(xiàn)代碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

相信大家肯定都在電商網(wǎng)站買(mǎi)過(guò)東西,當(dāng)我們看中一件喜歡又想買(mǎi)的東西時(shí),這時(shí)候你又不想這么快結(jié)賬,這時(shí)候你就可以放入購(gòu)物車(chē)

就像我們平時(shí)去超市買(mǎi)東西一樣,會(huì)推著購(gòu)物車(chē)去買(mǎi)東西;

那么我們接下來(lái)看看java怎么實(shí)現(xiàn)購(gòu)物車(chē)的功能,其實(shí)原理很簡(jiǎn)單,java的特點(diǎn)就是面向?qū)ο螅⑶矣兄庋b繼承多態(tài)三大特性;

java實(shí)現(xiàn)這個(gè)購(gòu)物車(chē)功能是通過(guò)內(nèi)存來(lái)實(shí)現(xiàn)的而不是將數(shù)據(jù)添加到數(shù)據(jù)庫(kù)中

首先是Item類(lèi),一個(gè)Item就代表購(gòu)物車(chē)?yán)锩娴囊恍袛?shù)據(jù)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.wxd.shopping;
 
public class Item {
  private int id; //商品id
  private String name;//商品名稱(chēng)
  private String city;//商品產(chǎn)地
  private double price;//商品價(jià)格
  private int number;//商品數(shù)量
  private String picture;//商品圖片地址
 
  public int getId() {
    return id;
  }
 
  public void setId(int id) {
    this.id = id;
  }
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
 
  public String getCity() {
    return city;
  }
 
  public void setCity(String city) {
    this.city = city;
  }
 
  public double getPrice() {
    return price;
  }
 
  public void setPrice(double price) {
    this.price = price;
  }
 
  public int getNumber() {
    return number;
  }
 
  public void setNumber(int number) {
    this.number = number;
  }
 
  public String getPicture() {
    return picture;
  }
 
  public void setPicture(String picture) {
    this.picture = picture;
  }
 
  /**
   * 重寫(xiě)hashCode方法,使得在購(gòu)物車(chē)添加商品的時(shí)候,如果id和名稱(chēng)相同就判定為同一件商品
   * @return
   */
  @Override
  public int hashCode() {
    return (this.getId()+this.getName()).hashCode();
  }
 
  /**
   * 重寫(xiě)equals方法,判斷是否為同一個(gè)對(duì)象
   * @param obj
   * @return
   */
  @Override
  public boolean equals(Object obj) {
    if(this==obj){
      return true;
    }
    if(obj instanceof Item){
      Item i= (Item) obj;
      if(this.getId()==i.getId()&&this.getName().equals(i.getName())){
        return true;
      }else{
        return false;
      }
    }else{
      return false;
    }
  }
 
  @Override
  public String toString() {
    return "Item{" +
        "id=" + id +
        ", name='" + name + '\'' +
        ", city='" + city + '\'' +
        ", price=" + price +
        ", number=" + number +
        ", picture='" + picture + '\'' +
        '}';
  }
 
  //有參構(gòu)造
  public Item(int id, String name, String city, double price, int number, String picture) {
    this.id = id;
    this.name = name;
    this.city = city;
    this.price = price;
    this.number = number;
    this.picture = picture;
  }
  //無(wú)參構(gòu)造
  public Item() {
  }
}

購(gòu)物車(chē)類(lèi)分裝了Item和數(shù)量的一個(gè)集合,還有商品的總金額

下面是購(gòu)物車(chē)類(lèi)的詳細(xì)代碼以及測(cè)試方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.wxd.shopping;
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
//購(gòu)物車(chē)
public class Cart {
  //購(gòu)買(mǎi)商品的集合
  private HashMap<Item,Integer> goods;
  //購(gòu)物車(chē)的總金額
  private double totalPrice;
 
  //構(gòu)造方法初始化數(shù)據(jù)
  public Cart(){
    goods=new HashMap<Item,Integer>();
    totalPrice=0.0;
  }
  public HashMap<Item, Integer> getGoods() {
    return goods;
  }
 
  public void setGoods(HashMap<Item, Integer> goods) {
    this.goods = goods;
  }
 
  public double getTotalPrice() {
    return totalPrice;
  }
 
  public void setTotalPrice(double totalPrice) {
    this.totalPrice = totalPrice;
  }
  //添加商品進(jìn)購(gòu)物車(chē)的方法
  public boolean addGoodsInCart(Item item,int number){
    if(goods.containsKey(item)){
      goods.put(item,goods.get(item)+number);
    }else{
      goods.put(item,number);
    }
    calTotalPrice();//重新計(jì)算購(gòu)物車(chē)的總金額
    return true;
  }
  //刪除商品的方法
  public boolean removeGoodsFromCart(Item item){
    goods.remove(item);
    calTotalPrice();//重新計(jì)算購(gòu)物車(chē)的總金額
    return true;
  }
  //統(tǒng)計(jì)購(gòu)物車(chē)的總金額
  public double calTotalPrice(){
    double sum=0.0;
    Set<Item> keys=goods.keySet();
    Iterator<Item> iterator = keys.iterator();
    while (iterator.hasNext()){
      Item i = iterator.next();
      sum+=i.getPrice()*goods.get(i);
    }
    this.setTotalPrice(sum);//設(shè)置購(gòu)物車(chē)的總金額
    return this.getTotalPrice();
  }
 
  //測(cè)試的main方法
  public static void main(String[] args) {
    //先創(chuàng)建兩個(gè)商品對(duì)象
    Item i1=new Item(1,"耐克","溫州",300.0,500,"001.jpg");
    Item i2=new Item(2,"阿迪","廣州",500.0,500,"001.jpg");
    Item i3=new Item(1,"耐克","溫州",300.0,500,"001.jpg");
    Cart c=new Cart();
    c.addGoodsInCart(i1,1);
    c.addGoodsInCart(i2,2);
    //再次購(gòu)買(mǎi)耐克鞋
    c.addGoodsInCart(i3,3);
    //遍歷購(gòu)買(mǎi)商品的集合
    HashMap<Item, Integer> goods = c.getGoods();
    Set<Map.Entry<Item, Integer>> entries = goods.entrySet();
    for(Map.Entry<Item, Integer> itemEntry:entries){
      System.out.println(itemEntry.toString());
    }
    System.out.println("購(gòu)物車(chē)總金額:"+c.getTotalPrice());
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.cnblogs.com/MrXiaoAndDong/p/Cart.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜亚洲WWW湿好爽 午夜想想爱午夜剧场 | 白丝女仆被啪到深夜漫画 | 人人做人人爽人人爱 | 99热精品国产麻豆 | 精品午夜中文字幕熟女人妻在线 | 2020年精品国产午夜福利在线 | 性德国高清xxxxbbbb | 欧美精品一区二区在线观看 | 国产剧情在线播放 | 色橹| 亚洲国产cao | 青青青手机在线视频 | 国产香蕉久久 | 99在线精品免费视频九九视 | 日本一区二区视频免费播放 | 久久亚洲伊人 | julia ann全部在线hd | 99热国产这里只有精品 | 密臀tv| 亚洲高清毛片一区二区 | 思思玖玖玖在线精品视频 | 好紧好爽再叫浪一点点潘金莲 | 性xxxx直播放免费 | 日本一片免费观看高清完整 | www四虎| 九九热综合 | 高h生子双性美人受 | 999久久免费高清热精品 | 免费aⅴ片 | 把美女屁股眼扒开图片 | 情缘免费观看完整版 | 男人都懂www深夜免费网站 | xxoosex久久久久久 | 久久精品在现线观看免费15 | tube性睡觉hd | 夫妇交换小说 | 日本欧美一二三区色视频 | porno18老师hd| 日韩欧美一区二区三区免费观看 | 草莓永久地域网名入2022 | 国产va免费精品高清在线 |