{:soso_e113:}
模拟购物商城:
思路:
a) 抽象现实世界中的商品. Product.java
商品的属性:
商品编号(String no)
商品的名称(String name)
商品的单价(double price)
属性私有化,提供公开的访问方法.
重写hashCode和equals方法(凡是商品的编号一致的,这两个商品就是同一个商品.)
b) 抽象现实世界中的顾客. Customer.java
顾客的属性:
顾客的名字(String name)
顾客的购物车(ShoppingCart cart)
在构造方法中为顾客提供购物车.
属性私有化,提供公开的访问方法.
重写hashCode,equals,toString方法.
(顾客的名字一致就代表是同一个顾客.)
c) 编写购物车 ShoppingCart.java
属性:
Map<Product,Integer> productsMap;
购物车总价(double totalPrice;)
方法:
清空
加1个商品
加多个商品
删1个商品
删多个商品
打印购物车明细
d) 抽象现实世界中的 SuperMarket.java
属性:
货架: Map<Product,Integer>;
e) 测试程序: Test.java
- public class Product{
- private String no;
- private String name;
- private double price; //单价
-
- public Product(){}
-
- public Product(String no,String name,double price){
- this.no = no;
- this.name = name;
- this.price = price;
- }
-
-
- public void setNo(String no){
- this.no = no;
- }
- public String getNo(){
- return no;
- }
- public void setName(String name){
- this.name = name;
- }
- public String getName(){
- return name;
- }
- public void setPrice(double price){
- this.price = price;
- }
- public double getPrice(){
- return price;
- }
-
- public boolean equals(Object obj){
- if(this==obj) return true;
- if(obj instanceof Product){
- Product product = (Product)obj;
- if(product.getNo().equals(this.getNo())){
- return true;
- }
- }
- return false;
- }
-
- public int hashCode(){
- return (no!=null)?no.hashCode():0;
- }
-
- public String toString(){
- //return "Product [no="+no+",name="+name+",price="+price+"]";
- return name;
- }
- }
复制代码
- public class Customer{
-
- //属性
- private String name;
- private ShoppingCart cart;
-
- //构造方法
- public Customer(){}
- public Customer(String name){ //在创建Customer对象的时候分配购物车对象
- this.name = name;
- this.cart = new ShoppingCart();
- }
-
- //setter and getter
- public void setName(String name){
- this.name = name;
- }
- public String getName(){
- return name;
- }
-
- public void setCart(ShoppingCart cart){
- this.cart = cart;
- }
- public ShoppingCart getCart(){
- return cart;
- }
-
-
- //equals
- public boolean equals(Object o){
-
- if(this==o){
- return true;
- }
-
- if(o instanceof Customer){
- Customer customer = (Customer)o;
- if((customer.getName().equals(this.getName()))){
- return true;
- }
-
- }
- return false;
-
- }
-
- //hashCode
- public int hashcode(){
- return name.hashCode();
- }
-
- //toString
- public String toString(){
- // Customer[name=sun]
- return "Customer[name=" + this.getName() + "]";
- }
- }
复制代码
- import java.util.*;
- public class ShoppingCart {
-
- private Map<Product,Integer> productsMap ;
- private double totalPrice;
-
- public ShoppingCart() {
- this.productsMap = new HashMap<Product,Integer>();
- }
-
- public Map<Product, Integer> getProductsMap() {
- return productsMap;
- }
- public void setProductsMap(Map<Product, Integer> productsMap) {
- this.productsMap = productsMap;
- }
- public double getTotalPrice() {
- return totalPrice;
- }
- public void setTotalPrice(double totalPrice) {
- this.totalPrice = totalPrice;
- }
-
- //清空购物车
- public void clear(){
- productsMap.clear(); //清空集合
- totalPrice = 0.0; //总价归零
- }
-
- //向购物车中添加一个商品
- public void add(Product p){
- if(!productsMap.containsKey(p)){
- productsMap.put(p,1);
- }else{
- productsMap.put(p,productsMap.get(p).intValue()+1);
- }
- this.setTotalPrice(this.getTotalPrice()+ p.getPrice());
-
- System.out.println("成功添加1个"+p);
- }
-
- public void add(Product p,int count){
- if(!productsMap.containsKey(p)){
- productsMap.put(p,count);
- }else{
- productsMap.put(p, productsMap.get(p) + count);
- }
- this.setTotalPrice(this.getTotalPrice()+ p.getPrice()*count);
- System.out.println("成功添加"+count+"个"+p);
- }
-
-
- //删除一个商品
- public void remove(Product p){
- if(!productsMap.containsKey(p)){
- System.out.println("购物车中没有" + p);
- return;
- }
- if(productsMap.get(p)==1){
- productsMap.remove(p); //删除一个条目
- }else{
- productsMap.put(p,productsMap.get(p).intValue()-1);
- }
- System.out.println("成功删除1个" + p + ",该购物车中"+p+"剩余"+ productsMap.get(p) +"个");
-
- this.setTotalPrice(this.getTotalPrice()-p.getPrice());
-
- }
-
- //删除多个商品
- public void remove(Product p,int count){
- if(!productsMap.containsKey(p)){
- System.out.println("购物车中没有" + p);
- return;
- }
-
- Integer productCount = productsMap.get(p); //获取p商品的数量
- if(productCount<count){
- System.out.println("购物车中一共有"+productCount+"个"+p+",您却要删除"+count+"个.这是不行的");
- return;
- }
-
- if(count==productCount){
- productsMap.remove(p);
- }else{
- productsMap.put(p,productCount-count);
- }
-
- System.out.println("成功删除"+count+"个" + p + ",该购物车中"+p+"剩余"+ productsMap.get(p) +"个");
- this.setTotalPrice(this.getTotalPrice()- p.getPrice()*count);
- }
-
- //显示购物车中的明细(遍历Map)
- public void printProductList(){
- if(productsMap.size()==0){
- System.out.println("购物车中没有商品");
- return;
- }
- Set<Product> keys = productsMap.keySet();
- System.out.println("购物车明细:");
- for(Product k:keys){
- System.out.println("\t" + k + "" + productsMap.get(k) +"个");
- }
- System.out.println("购物车总价是:" + this.getTotalPrice());
- }
-
-
- }
复制代码
- import java.util.HashMap;
- import java.util.Map;
- public class SuperMarket{
-
- private Map<Product,Integer> productsMap;
- public SuperMarket(Map<Product,Integer> productsMap) {
- super();
- this.productsMap = productsMap;
- }
- public Map<Product, Integer> getProductsMap() {
- return productsMap;
- }
- public void setProductsMap(Map<Product, Integer> productsMap) {
- this.productsMap = productsMap;
- }
- }
复制代码
- import java.util.*;
- public class Test{
-
- public static void main(String[] args){
-
- //0.准备货物
-
- Product p1 = new Product("001","西瓜",5.1);
- Product p2 = new Product("002","冬瓜",6.2);
- Product p3 = new Product("003","南瓜",7.3);
- Product p4 = new Product("004","北瓜",8.4);
- Product p5 = new Product("005","香瓜",5.5);
- Product p6 = new Product("006","哈密瓜",5.2);
- Product p7 = new Product("007","黄瓜",10.3);
- Product p8 = new Product("008","丝瓜",8.56);
-
- Map<Product,Integer> productsMap = new HashMap<Product,Integer>();
-
- productsMap.put(p1,3);
- productsMap.put(p2,10);
- productsMap.put(p3,4);
- productsMap.put(p4,11);
- productsMap.put(p5,5);
- productsMap.put(p6,12);
- productsMap.put(p7,6);
- productsMap.put(p8,13);
-
- //1.创建超市
- SuperMarket sm = new SuperMarket(productsMap);
-
- //2.创建一个顾客
- Customer c = new Customer("张三");
-
-
- ShoppingCart cart = c.getCart();
- //3.添加1商品
- cart.add(p1);
- cart.add(p2);
- cart.add(p3);
- cart.add(p4);
- cart.add(p5);
- cart.add(p6);
- cart.add(p7);
-
- cart.printProductList();
-
- //4.添加多个商品
- cart.add(p1,2);
- cart.add(p8,10);
- cart.printProductList();
-
- //5.删除1个商品
- cart.remove(p8);
- cart.printProductList();
-
- //6.删除多个商品
- cart.remove(p8,8);
- cart.printProductList();
-
- //7.清空购物车
- cart.clear();
- cart.printProductList();
-
- }
-
- }
复制代码 大家指点指点{:soso_e104:} |
|