购物车

cn.oop.shopping.dao:数据访问接口
cn.oop.shopping.dao.impl:数据访问接口实现类
cn.oop.shopping.entity:实体类
cn.oop.shopping.test:测试类

 

/**

*实体类:封装对象,一个Product对象可以描述一个商品

*属性多了可以考虑用类来存

*对象多了可以考虑用集合或数组来存

*/

1.创建菜单

cn.oop.shopping.entity:实体类

public class Product{

private int id;//编号

private String name;//名称

private double price;//价格

private String color;//颜色

private int size;//尺寸

private int stock;//库存

public Product(){//无参构造

super()

}

public Product(int id ,String name,double price, String color,int size,int stock){

this.id =id;//有参构造   this关键字 区分类变量和局部变量

this.name=name;

this.price=price;

this.color= color;

this.size=size;

this.stock;

}

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 double getProce(){

return proce;

}

public void setProce(double proce){

this.proce=proce;

}

public String getColor(){

return color;

}

public void setColor(String color){

this.color=color;

}

public int getSize(){

return size;

}

public void setSize(int size){

this.size=size;

}

public int getStock(){

return stock;

}

public void setStock(int stock){

this.stock=stock;

}
 public String toString() {
  return "Product [id=" + id + ", name=" + name + ", price=" + price + ", color=" + color + ", size=" + size
    + ", stock=" + stock + "]";
 }

}

package cn.oop.shopping.dao;

public interface ProductDao{//创建接口

//查看所有商品

public List findAll();//放进一个可扩展的List集合中展示

//根据编号查询商品信息

public Product finById(int id);//Product商品类

//添加商品

public boolean addProduct(Product product);//product 封装了要添加的商品信息

}

package cn.oop.shopping.dao.impl;//包名

import java.util.ArrayList;//使用的工具类
import java.util.List;

import cn.zzsxt.shopping.dao.ProductDao;//引入的包
import cn.zzsxt.shopping.entity.Product;

public class ProductDaoImpl implements ProdusDao{

List<Product> list =new ArrayList<Product>();//成员变量

public ProductDaoImpl(){//构造函数

Product p1=new Product (001,"华为手机",1899,"土豪金",5,500);

Product p2=new Product (002,"小米手机",2499,"白色",6,200);

Product p3=new Product (003,"vivo手机",2100,"黑色",6,300);

list.add(p1);//添加到集合中

list.add(p2);

list.add(p3);

}

public List findAll(){

return list;//输出集合内容

}

public Product findById(int id){

for(Product f:list){//遍历

if(f.getId()==id){

 return f;

}

}

return null;

}

public boolean addProduct(Product product){

//判断商品是否存在

for(Product p:list){

if(product.getId()==p.getId()){

return false;

}

}

list.add(product);

return true;

}

 

}

 

 

 

package cn.oop.shopping.entity;

 // CartItem封装购买的一个商品信息及对应的数量

public class CartItem {
 private int id;//商品编号
 private String name;//商品名称
 private double price;//商品价格
 private int amount;//购买数量
 public CartItem() {
  super();
  // TODO Auto-generated constructor stub
 }
 public CartItem(int id, String name, double price, int amount) {
  super();
  this.id = id;
  this.name = name;
  this.price = price;
  this.amount = amount;
 }
 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 double getPrice() {
  return price;
 }
 public void setPrice(double price) {
  this.price = price;
 }
 public int getAmount() {
  return amount;
 }
 public void setAmount(int amount) {
  this.amount = amount;
 }
 
 public String toString() {
  return "CartItem [id=" + id + ", name=" + name + ", price=" + price + ", amount=" + amount + "]";
 }

 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + amount;
  result = prime * result + id;
  result = prime * result + ((name == null) ? 0 : name.hashCode());
  long temp;
  temp = Double.doubleToLongBits(price);
  result = prime * result + (int) (temp ^ (temp >>> 32));
  return result;
 }

 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  CartItem other = (CartItem) obj;
  if (amount != other.amount)
   return false;
  if (id != other.id)
   return false;
  if (name == null) {
   if (other.name != null)
    return false;
  } else if (!name.equals(other.name))
   return false;
  if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
   return false;
  return true;
 }

 
}

 

 package cn.oop.shopping.dao;

import java.util.Map;

import cn.oop.shopping.entity.CartItem;

public interface CartItemDao{

//购买记录

public boolean addCartItem(CarItem cartItem);

//查询购物车中商品信息

public Map<Integer,CartItem> showCart();

 

}

 

package cn.oop.shopping.dao.impl;

import java.util.LinkedHashMap;
import java.util.Map;

import cn.oop.shopping.dao.CartItemDao;
import cn.oop.shopping.entity.CartItem;

public class CartItemDaoImpl implements CartItemDao{

//购物车:用商品编号做key,利用购物车项(编号,名称,价格,数量)

Map<Integer,CartItem> cart=new LinkedHashMap<Integer,CartItem>();

/**
  * 添加购物车
  *   a.购物车中不存在要添加的商品信息(直接添加)
     b.购物车中存在要添加的商品信息(更改数量)
  */

public boolean addCartItem(CartItem cartItem){

//判断该购物车中是否存在要添加的商品信息

if(cart.containsKey(cartItem.getId())){

 //购物车中已经存在要添加的商品

//获取新添加到购物车中的商品数量

int newAmount=cartItem.getAmount());

CartItem oldCartItem = cart.get(cartItem.getId());//获取原有购物车中商品的信息

int oldAmount=oldCartItem.getAmount();

int amount =oldAmount+newAmount;

oldCartItem.setAmount(amount);//更改购物车中的商品数量

}else{

//购物车中不存在要购买的商品

cart.put(cartItem.getId(),cartItem);//将商品添加到购物车中

}

return true;

}

//查看购物车

public Map<Integer,CartItem> showCart(){

return cart;

}

}

package cn.oop.shopping.test;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import cn.oop.shopping.dao.CartItemDao;
import cn.oop.shopping.dao.ProductDao;
import cn.oop.shopping.dao.impl.CartItemDaoImpl;
import cn.oop.shopping.dao.impl.ProductDaoImpl;
import cn.oop.shopping.entity.CartItem;
import cn.oop.shopping.entity.Product;

public class Test{

ProductDao productDao =new ProductDaoImpl();//向上转型

CartItemDao cartItemDao=new CartItemDaoImpl();

//打印菜单

public void startMenu(){

Scanner input =new Scanner(System.in);

public void startMenu(){
  Scanner input = new Scanner(System.in);
  do{
   System.out.println();
   System.out.println("*****欢迎进入京东商城*****");
   System.out.println("1.添加商品");
   System.out.println("2.查看所有商品");
   System.out.println("3.查看指定编号商品");
   System.out.println("4.添加到购物车");
   System.out.println("5.显示购物车");
   System.out.println("6.退出");
   System.out.println("***************************");
   System.out.println("请选择菜单:");
   int choice = input.nextInt();
   switch (choice) {
   case 1:
//     System.out.println("添加商品功能");
    addProduct();
    break;
   case 2:
//    System.out.println("查看所有商品功能");
    showAll();
    break;
   case 3:
//     System.out.println("查看指定编号商品功能");
     showById();
    break;
   case 4:
//     System.out.println("添加到购物车功能");
    addCart();//添加购物车
    break;
   case 5:
//     System.out.println("显示购物车功能");
    showCart();
    break;
   case 6:
     System.out.println("谢谢使用!");
     System.exit(1);//终止菜单
    break;
   default:
     System.out.println("您输入的有误,请输入1~6的数字!");
    break;
   }
  }while(true);
 }

}

//获取所有商品信息

public void showAll(){

List<Product>list =productDao.findAll();

System.out.println("编号\t\t商品名称\t\t价格\t\t颜色\t\t尺寸\t\t库存");

 for (Product product : list) {
   int id = product.getId();
   String name=product.getName();
   double price = product.getPrice();
   String color = product.getColor();
   int size = product.getSize();
   int stock = product.getStock();
   System.out.println(id+"\t\t"+name+"\t\t"+price+"\t\t"+color+"\t\t"+size+"\t\t"+stock);
  }
 }

//根据用户输入的编号查询信息.

public void showById(){

Scanner input =new Scanner(System.in);

System,out,println("请输入商品编号");

int id=input.newxInt();

Product product=productDao.findById(id);

if(product!=null){

System.out.println("编号\t\t商品名称\t\t价格\t\t颜色\t\t尺寸\t\t库存");

String name=product.getName();
   double price = product.getPrice();
   String color = product.getColor();
   int size = product.getSize();
   int stock = product.getStock();
   System.out.println(id+"\t\t"+name+"\t\t"+price+"\t\t"+color+"\t\t"+size+"\t\t"+stock);

}else{

System.out.println("商品不存在");

}

}

 //添加商品的方法

public void addProduct(){

 Scanner input = new Scanner(System.in);
  System.out.println("请输入商品编号:");
  int id = input.nextInt();
  System.out.println("请输入商品名称:");
  String name = input.next();
  System.out.println("请输入商品价格:");
  double price = input.nextDouble();
  System.out.println("请输入商品颜色:");
  String color = input.next();
  System.out.println("请输入商品尺寸:");
  int size = input.nextInt();
  System.out.println("请输入商品库存:");
  int stock = input.nextInt();

Product product =new Product(id,name,price,color,size,stock);

boolean isSucc=productDao.addProduct(product);

if(isSucc){

  System.out.println("添加成功!");
 }else{
   System.out.println("商品无法重复添加!");

}

}

//将要购买的商品添加到购物车中

public void addCart(){

Scanner input =new Scanner(System.in);

System.out.println("请输入商品编号");

int id=input.nextInt();

System.out.println("请输入数量");

int amount=input.nextInt;

 //根据商品编号获取商品信息 CartItem(编号,名称,价格,数量)

Product product=productDao.findById(id);

if(product!=null){

String name=product.getName();

double price =product.getProduct();

 //将商品编号,商品名称,价格,数量封装成CartItem

CartItem carItem=new CartItem(id,name.price,amount);

 //将cartItem添加到购物车中

cartItemDao.addCartItem(cartItem);

}else{

System.out.println("你输入商品编号有误!");

}

}

//显示购物车

public void showCart(){

double total=0;

//获取商品信息

Map<Integer,CartItem> cartItem=cartItemDao.showCart();

Collection<CartItem> cartValue=cart.value();//获取购物车中的值

 System.out.println("编号\t\t商品名称\t\t商品价格\t\t数量\t\t小计");

for (CartItem cartItem : cartValues) {
   int id = cartItem.getId();
   String name = cartItem.getName();
   double price = cartItem.getPrice();
   int amount = cartItem.getAmount();
   double oneTotal = price*amount;//计算小计金额
   total+=oneTotal;//计算总金额
   System.out.println(id+"\t\t"+name+"\t\t"+price+"\t\t"+amount+"\t\t"+oneTotal);
  }
  System.out.println("总计:"+total);
 }
 public static void main(String[] args) {
  Test test = new Test();
  test.startMenu();
 }

}

 

posted @ 2017-04-23 22:13  代鱼  阅读(276)  评论(0编辑  收藏  举报