JAVA购物车

小组成员

| 班级 | 成员 | 成员 | 成员 | 成员 |
| ---- | ---- | ---- | ---- | ---- | ---- |
| 网络1912 | 吴奕恒 | 蔡浩伟 | 朱韶华 | 郑剑龙 |

小组分工

  • 1.吴奕恒:前期调查,登录注册.商城代码实现

  • 2.蔡浩伟:Dao模式,面向对象设计,GUI

  • 3.郑剑龙:UML类图,代码实现

  • 4.朱韶华:博客制作,代码规范

一.前期调查

1.主页面信息

观察:主界面显示商品的分类,并且分类里含有各种商品的信息,使得购物者能一目了然了解到该商城所拥有的所有的商品的信息。

系统功能结构图

2.商品展示

观察:当进入到该商城或者该搜索信息的商品展示,该条目应该含有各种各样的商品信息以及产地之类的东西,还必须含有单价,使得购物者能够根据物品的信息来了解并且购买

3.购物车界面

观察:购物车包含了所有购物者选择将进行购买的商品,购物者可以自行挑选某几个选中的物品进行结算,而购物车里也必须含有商品相对于的信息;
商品的数量应该可在购物车里面进行添加和减少,并且可以在购物车里删除不想购买的物品,移除购物车。最后购物者选择完毕进行结算即可,并且结算后购物车内的该物品应该去除。

二.流程图

三.UML类图

四.效果展示

1.登录界面

2.商品界面

3.购物车功能

4.添加商品,删除商品且清空购物车

添加

删除

清空

5.结算

五.购物车核心代码

1.DAO展示

  • 购物车DAO
package shoppingcart;

import java.util.ArrayList;



public interface CartDao{
	public List getList();
	public void addGoods(Goods goods);  //加入购物车
	public void deleteGoods(int num);  //删除购物车
	public void showCart();		//查看购物车
	public void clearCart();		//清空购物车
	public double checkList();      //计算购物车内商品总价
	
	
}


2.用户注册,用户登录

package shoppingcart;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;  

public class User {  

    public void registered() { 
    	
        Scanner scan=new Scanner(System.in);  
        System.out.println("请输入您的手机号注册:"); 
        String iname=scan.next();
        
        System.out.println("请输入您的密码:");  
        String ipassword=scan.next();  
      
        File file=new File("C:\\1.txt");
    	try {
            FileWriter writer = new FileWriter(file, true);
            writer.write("\r\n"+iname+" "+ipassword);
            writer.close();
       } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
       }
    	
    }
    //登陆  
    public int login() throws IOException {  
    	
        Scanner scan=new Scanner(System.in);  
        System.out.println("请输入您的用户名/手机号");  
        String iname=scan.next();  
        System.out.println("请输入您的密码:");  
        String ipassword=scan.next();  
        int flag=1;
        
        BufferedReader in = new BufferedReader(new FileReader("C:\\1.txt"));
        String s;
        s = in.readLine();
        
        while ((s = in.readLine()) != null) {
        	
            StringTokenizer p = new StringTokenizer(s, " ");
            String name=p.nextToken();
            String pass=p.nextToken();
            
            if(iname.equals(name))
            {
            	System.out.println("登录成功!");  
                flag=0;  
                break;
            }
        }
        
        if(flag==1)
        {
        	System.out.println("用户名或密码输入错误!");  
        }
        
        return flag;
    }

}

3.商城展示

package shoppingcart;

import java.io.BufferedReader;//读取文本
import java.io.FileReader;//读取文件
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;//分割字符串

public class Mall {

	
	static List<Goods>mallList = new ArrayList<>();
	
	public static list<Goods> getList() {
		return mallList;
	}
	
	public static void showAllGoods() throws NumberFormatException, IOException//抛出异常?
	{
		BufferedReader in = new BufferedReader(new FileReader("C:\\goods.txt"));
		String s;
		Goods goods;
		int i=1;
		while ((s = in.readLine()) != null)
		{
			StringTokenizer p = new StringTokenizer(s, " ");// " "为分隔符
			String goodsName=p.nextToken();// 将第一个记录赋给变量goodsName
			String goodsPrice=p.nextToken();// 将第二个记录赋给变量goodsPrice
			String goodsType=p.nextToken();// 将第三个记录赋给变量goodType
			String goodsOrigin=p.nextToken();// 将第四个记录赋给变量goodsOrigin
		
			if(i<=5)
			{
				String phoneRam=p.nextToken();
				String phoneColor=p.nextToken();
				goods=new Phone(goodsName,goodsPrice,goodsType,goodsOrigin,phoneRam,phoneColor);
				mallList.add(goods);
				System.out.println(i+":"+goods.toString());
				i++;
			}
			else if(i>5&&i<=12)
			{
				String shoeSize=p.nextToken();
				String shoeCharacteristic=p.nextToken();
				goods=new Shoe(goodsName,goodsPrice,goodsType,goodsOrigin,shoeSize,shoeCharacteristic);
				mallList.add(goods);
				System.out.println(i+":"+goods.toString());
				i++;
			}
			else if(i>12&&i<=19)
			{
				String bookAuthor=p.nextToken();
				goods=new Books(goodsName,goodsPrice,goodsType,goodsOrigin,bookAuthor);
				mallList.add(goods);
				System.out.println(i+":"+goods.toString());
				i++;
			}
			else if(i>19)
			{
				String cosmeticsType=p.nextToken();
				goods=new Cosmetics(goodsName,goodsPrice,goodsType,goodsOrigin,cosmeticsType);
				mallList.add(goods);
				System.out.println(i+":"+goods.toString());
				i++;
			}
			
			
		}
		in.close();
	}

}

4.商品类的处理

  • 1.主要子类有:手机,鞋,书籍,化妆品
package shoppingcart;

public class Goods {
	private String goodsName;		//商品名称
	private String goodsPrice;		//商品价格
	private String goodsType;		//商品类型
	private String goodsOrigin;		//产地
	
	public goods() {
		
	}
	public goods(String goodsName,String goodsPrice,String goodsType,String goodsOrigin) {
		this.goodsName = goodsName;
		this.goodsPrice = goodsPrice;
		this.goodsType = goodsType;
		this.goodsOrigin = goodsOrigin;
	}
	@Override
	public String toString() {
		return goodsName + "     " + goodsPrice + "     " + goodsType
				+ "     " + goodsOrigin;
	}
	//	setter/getter
	public String getGoodsName() {
		return goodsName;
	}
	public void setGoodsName(String  goodsName) {
		this.goodsName = goodsName;
	}
	public String getGoodsPrice() {
		return goodsPrice;
	}
	public void setGoodsPrice(String  goodsPrice) {
		this.goodsPrice = goodsPrice;
	}
	String getGoodsType() {
		return goodsType;
	}
	public void setGoodsType(String goodsType) {
		this.goodsType = goodsType;
	}
	public String getGoodsOrigin() {
		return goodsOrigin;
	}
	public void setGoodsOrigin(String goodsOrigin) {
		this.goodsOrigin = goodsOrigin;
	}
	
}
//手机类
class phone extends Goods{

	private String ram; //内存
	private String color; //颜色
	
	public phone(String goodsName, String goodsPrice, String goodsType, String goodsOrigin, String ram, String color) {
		this.setGoodsName(goodsName);
		this.setGoodsPrice(goodsPrice);
		this.setGoodsType(goodsType);
		this.setGoodsOrigin(goodsOrigin);
		this.ram=ram;
		this.color=color;
	}

	public String getRam() {
		return ram;
	}

	public void setRam(String ram) {
		this.ram = ram;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}
	
	@Override
	public String toString() {
		return this.getGoodsName() + "     价格:" + this.getGoodsPrice() + "     商品类型:" + this.getGoodsType()
				+ "      商品产地:" + this.getGoodsOrigin() + "     手机内存:" + ram + "     手机颜色:"+ color;
	}
	
}

//鞋类
class shoe extends Goods{
	private String size; //尺码
	private String characteristic; //特性
	
	public shoe(String goodsName, String goodsPrice, String goodsType, String goodsOrigin, String size, String characteristic) {
		this.setGoodsName(goodsName);
		this.setGoodsPrice(goodsPrice);
		this.setGoodsType(goodsType);
		this.setGoodsOrigin(goodsOrigin);
		this.size=size;
		this.characteristic=characteristic;
		
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getCharacteristic() {
		return characteristic;
	}

	public void setCharacteristic(String characteristic) {
		this.characteristic = characteristic;
	}
	@Override
	public String toString() {
		return this.getGoodsName() + "     价格:" + this.getGoodsPrice() + "     商品类型:" + this.getGoodsType()
				+ "     商品产地:" + this.getGoodsOrigin() + "     尺码大小:" + size + "     商品特性:"+ characteristic;
	}
}

//书籍类
class books extends Goods{
	private String author; //作者
	public books(String goodsName, String goodsPrice, String goodsType, String goodsOrigin, String author) {
		this.setGoodsName(goodsName);
		this.setGoodsPrice(goodsPrice);
		this.setGoodsType(goodsType);
		this.setGoodsOrigin(goodsOrigin);
		this.author=author;
		
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	@Override
	public String toString() {
		return this.getGoodsName() + "     价格:" + this.getGoodsPrice() + "     商品类型:" + this.getGoodsType()
				+ "     商品产地:" + this.getGoodsOrigin() + "     作者:" + author;
	}
}

//化妆品类
class cosmetics extends Goods{
	private String c_type;
	public cosmetics(String goodsName, String goodsPrice, String goodsType, String goodsOrigin, String c_type) {
		this.setGoodsName(goodsName);
		this.setGoodsPrice(goodsPrice);
		this.setGoodsType(goodsType);
		this.setGoodsOrigin(goodsOrigin);
		this.c_type=c_type;
	}
	public String getC_type() {
		return c_type;
	}
	public void setC_type(String c_type) {
		this.c_type = c_type;
	}
	@Override
	public String toString() {
		return this.getGoodsName() + "     价格:" + this.getGoodsPrice() + "     商品类型:" + this.getGoodsType()
				+ "     商品产地:" + this.getGoodsOrigin() + "     商品特性:" + c_type;
	}
}

5.购物车的处理:查看购物车,添加商品个数,删除商品,清空购物车,结算购物车

package shoppingcart;

import java.util.ArrayList;


public class CartDaoLisImpl implements CartDao{
	
	List cartList= new List();
	public List getList() {
		return cartList;
	}
	public void addGoods(Goods goods)
	{
		int i;
		for(i=0;i<cartList.size();i++)
		{
			if(cartList.get(i).getItem().equals(goods)==true)
			{
				cartList.get(i).increase();
				break;
			}
		}
		if(i>=cartList.size())
		{
			Item e = new Item(goods);
			cartList.add(e);
		}	
	}
	public void deleteGoods(int num)
	{
		if(cartList.size()==0)
		{
			System.out.println("购物车为空");
			return;
		}
		if(num<cartList.size())
		{
			if(cartList.get(num).getNum()==1) 
			{
				cartList.remove(num);
			}
			else
			{
				cartList.get(num).reduce();
			}
		}
		if(num>cartList.size())
		{
			System.out.println("该商品编号不存在");
		}	
	}
	public void showCart()		//查看购物车
	{
		for(int i=0;i<cartList.size();i++)
		{
			System.out.println((i+1)+":"+cartList.get(i).toString());
		}
	}
	public void clearCart()			//清空购物车
	{
		cartList.clear();
	}
	public double checkList() {      //计算购物车内商品总价
		double cost=0;
		for(int i=0;i<cartList.size();i++)
		{
			cost=cost+Double.parseDouble(cartList.get(i).getItem().getGoodsPrice())*cartList.get(i).getNum();
					
		}
		return cost;
	}
}
class item 							
{
	private Goods item;   //商品的信息
	private int num;      //商品的数量
	
	public item(Goods item)
	{
		this.item = item;
		num = 1;
	}
	public void increase() //增加商品数量
	{
		num++;
	}
	public void reduce() //减少商品数量
	{
		num--;
	}
	
	@Override
	public String toString() {
		return item + "      数量:" + num;
	}
	public Goods getItem() {
		return item;
	}
	public void setItem(Goods item) {
		this.item = item;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	
}

代码存储

https://gitee.com/haowei-Cai/CHW.git

posted @ 2020-11-17 15:56  相思语-往生人  阅读(498)  评论(0编辑  收藏  举报