程序笔记   发布时间:2022-06-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了java实现网上购物车程序大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

用java代码写一个简单的网上购物车程序,供大家参,具体内容如下

需求:

1、写一个商品类,有商品编号、商品名称、商品分类、商品单价属性。

2、写一个商品条目信息类,有商品和数量两个属性,有商品总价格方法。

3、写一个购物车类,有添加商品方法、查看订单信息,删除商品,修改商品,清空购物车,求购物车中所有商品总金额方法。4、写一个测试类,测试上述方法。

商品类:

public class Product { 
  privatE int productID;// 商品编号 
  private String productname;// 商品名称 
  private String category;// 商品分类 
  private double price;// 单价 
 
  public Product() {// 无参构造 
    super(); 
  } 
 
  public Product(int productID,String productname,String category,double pricE) { 
    super(); 
    this.productID = productID; 
    this.productname = productname; 
    this.category = category; 
    this.price = price; 
  } 
 
  public String toString() { 
    return "Product [productID=" + productID + ",productname=" 
        + productname + ",category=" + category + ",price=" + price 
        + "]"; 
  } 
 
  public int getProductID() { 
    return productID; 
  } 
 
  public voID setProductID(int productID) { 
    this.productID = productID; 
  } 
 
  public String getProductname() { 
    return productname; 
  } 
 
  public voID setProductname(String productName) { 
    this.productname = productname; 
  } 
 
  public String getcategory() { 
    return category; 
  } 
 
  public voID setcategory(String category) { 
    this.category = category; 
  } 
 
  public double getPrice() { 
    return price; 
  } 
 
  public voID setPrice(double pricE) { 
    this.price = price; 
  } 
 
} 

商品条目信息类:

public class ProductItem { 
  private Product product;//购买的商品 
  private int count;//商品数量 
  public double @R_433_10586@lMoney(){//小计 
    double price=product.getPrice();//获取商品单价 
    return price*count; 
  } 
   
  public ProductItem() { 
    super(); 
  } 
 
  public ProductItem(Product product,int count) { 
    super(); 
    this.product = product; 
    this.count = count; 
  } 
 
  public Product getProduct() { 
    return product; 
  } 
  public voID setProduct(Product product) { 
    this.product = product; 
  } 
  public int getCount() { 
    return count; 
  } 
  public voID setCount(int count) { 
    this.count = count; 
  } 
   
} 

购物车类:

import java.util.Collection; 
import java.util.Iterator; 
import java.util.linkedHashMap; 
import java.util.Map; 
public class ShopPingCart {//购物车 
  //key:商品编号 value:商品条目 
  private Map<Integer,ProductItem> map=new linkedHashMap<Integer,ProductItem>(); 
   
  public voID addProduct(Product p){//添加商品 
    int productID=p.getProductID(); 
    if(map.containsKey(productID)){ 
      ProductItem productItem=map.get(productID); 
      productItem.setCount(productItem.getCount()+1); 
    }else{ 
      map.put(productID,new ProductItem(p,1)); 
    } 
  } 
  public voID showAll(){//查看订单信息 
    Collection<ProductItem> productItems = map.values(); 
    Iterator<ProductItem> iterator = productItems.iterator(); 
    while(iterator.hasNext()){ 
      ProductItem productItem = iterator.next(); 
      Product product = productItem.getProduct(); 
      System.out.println("商品编号:"+product.getProductID()+",商品名称:" 
      +product.getProductname()+",单价:"+product.getPrice()+",数量:"+productItem.getCount() 
      +",小计:"+productItem.@R_433_10586@lMoney()); 
    } 
  } 
  public Boolean deleteProduct(int productID){//删除商品 
    if(map.containsKey(productID)){ 
      map.remove(productID); 
      return true; 
    } 
    return false; 
  } 
  public Boolean modifyProduct(int productID,int count){//修改 
    if(map.containsKey(productID)){ 
      if(count>=1){ 
        ProductItem productItem = map.get(productID); 
        productItem.setCount(count); 
        return true; 
      }else if(count==0){//删除该商品 
        deleteProduct(productID); 
        return true; 
      }   
    } 
    return false; 
  } 
   
  public voID clearCart(){//清空购物车 
    map.clear(); 
  } 
   
  public double @R_433_10586@lAllMoney(){//商品总钱数 
    double @R_433_10586@l=0; 
    Collection<ProductItem> productItems = map.values(); 
    Iterator<ProductItem> iterator = productItems.iterator(); 
    while(iterator.hasNext()){ 
      ProductItem productItem = iterator.next(); 
      double money=productItem.@R_433_10586@lMoney(); 
      @R_433_10586@l+=money; 
    } 
    return @R_433_10586@l; 
  } 
} 

测试类:

public class ShopPingCartTest { 
 
  public static voID main(String[] args) { 
    ShopPingCart cart=new ShopPingCart(); 
    Product p1=new Product(101,"华硕笔记本","笔记本",4599); 
    Product p2=new Product(102,"苹果","水果",5.9); 
    Product p3=new Product(103,"彩电","家电",2799); 
    Product p4=new Product(104,"秋裤","服装",128); 
    Product p5=new Product(105,"华为手机","手机",2998); 
    Product P6=new Product(101,4599);//测试买两件商品的情况 
    cart.addProduct(p1); 
    cart.addProduct(p2); 
    cart.addProduct(p3); 
    cart.addProduct(p4); 
    cart.addProduct(p5); 
    cart.addProduct(P6); 
    cart.showAll(); 
    System.out.println("############"); 
    Boolean flag=cart.deleteProduct(p2.getProductID()); 
    if(flag){ 
      System.out.println("商品编号为:"+p2.getProductID()+"的商品删除成功!"); 
    }else{ 
      System.out.println("删除失败"); 
    } 
    cart.showAll(); 
    System.out.println("############"); 
    Boolean flag2=cart.modifyProduct(p3.getProductID(),2); 
    if(flag2){ 
      System.out.println("商品编号为:"+p3.getProductID()+"的商品修改成功!"); 
    }else{ 
      System.out.println("修改失败"); 
    } 
    cart.showAll(); 
     
    //cart.clearCart(); 
    //cart.showAll(); 
    System.out.println("商品总价钱为:"+cart.@R_433_10586@lAllMoney()); 
 
  } 
 
} 

运行效果图:

java实现网上购物车程序

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:

  • java web开发之实现购物车功能
  • javaweb图书商城设计之购物车模块(3)
  • java网上图书商城(4)购物车模块1
  • java网上图书商城(5)购物车模块2
  • JAVAEE中用Session简单实现购物车功能示例代码
  • java web开发之购物车功能实现示例代码

大佬总结

以上是大佬教程为你收集整理的java实现网上购物车程序全部内容,希望文章能够帮你解决java实现网上购物车程序所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。