JavaWeb后臺(tái)購(gòu)物車(chē)類(lèi)實(shí)現(xiàn)代碼詳解
相信大家肯定都在電商網(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ū)ο?,并且有著封裝繼承多態(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ù)
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è)試方法:
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í)有所幫助,也希望大家多多支持腳本之家。
- JavaWeb購(gòu)物車(chē)項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)指南
- eclipse的web項(xiàng)目實(shí)現(xiàn)Javaweb購(gòu)物車(chē)的方法
- javaweb購(gòu)物車(chē)案列學(xué)習(xí)開(kāi)發(fā)
- java web開(kāi)發(fā)之購(gòu)物車(chē)功能實(shí)現(xiàn)示例代碼
- javaweb圖書(shū)商城設(shè)計(jì)之購(gòu)物車(chē)模塊(3)
- java web開(kāi)發(fā)之實(shí)現(xiàn)購(gòu)物車(chē)功能
- java商城項(xiàng)目實(shí)戰(zhàn)之購(gòu)物車(chē)功能實(shí)現(xiàn)
- JAVAEE中用Session簡(jiǎn)單實(shí)現(xiàn)購(gòu)物車(chē)功能示例代碼
- java實(shí)現(xiàn)網(wǎng)上購(gòu)物車(chē)程序
- Java?web實(shí)現(xiàn)購(gòu)物車(chē)案例
相關(guān)文章
用Rational Rose逆向工程(java)生成類(lèi)圖(教程和錯(cuò)誤解決)
Rational Rose有個(gè)很方便的功能,將項(xiàng)目中的JAVA代碼自動(dòng)轉(zhuǎn)換成UML類(lèi)圖2013-02-02java中的靜態(tài)代碼塊、構(gòu)造代碼塊、構(gòu)造方法詳解
下面小編就為大家?guī)?lái)一篇java中的靜態(tài)代碼塊、構(gòu)造代碼塊、構(gòu)造方法詳解。小編覺(jué)得挺好的,現(xiàn)在分享給大家。給大家一個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-03-03Springboot實(shí)現(xiàn)Activemq死信隊(duì)列詳解
這篇文章主要介紹了Springboot實(shí)現(xiàn)Activemq死信隊(duì)列詳解,Activemq服務(wù)端配置重新投遞次數(shù)超過(guò)?MaximumRedeliveries?,則會(huì)進(jìn)入死信隊(duì)列,默認(rèn)情況,有一個(gè)死信隊(duì)列:AcitveMQ.DLQ,所有的消息都投遞到此隊(duì)列,包括過(guò)期消息,重投遞失敗消息,需要的朋友可以參考下2023-12-12SpringMVC獲取請(qǐng)求參數(shù)的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringMVC中獲取請(qǐng)求參數(shù)的方法,例如通過(guò)ServletAPI獲取和通過(guò)控制器方法的形參獲取請(qǐng)求參數(shù)等,需要的可以參考下2023-07-07