简易商城系统

简易商城系统_第1张图片
image.png

需求分析:
1.用户类:账号,密码,购物车
2.用户管理类:
(1)注册,将注册后的user对象存到一个集合然后存到文件里
(2)登陆,从文件中获取集合,遍历集合,如果账号密码相等则登陆成功
3.管理员类
4.管理员管理类
5.书类
6.商城类
(1)遍历文件里的书集合
(2)管理员具备的权利 :添加商品
(3)2.管理具备的权利:下架商品
(4)利用分页的方法显示商品
(5)把书添加到用户的购物车
(6)查看购物车
(7)改变购物车
(8)搜索商品

用户类

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class User implements Serializable {
    
    private static final long serialVersionUID = 1L;
    private String account;
    private String password;
    Listlist=new ArrayList<>();
    public User(String account, String password) {
        super();
        this.account = account;
        this.password = password;
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((account == null) ? 0 : account.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (account == null) {
            if (other.account != null)
                return false;
        } else if (!account.equals(other.account))
            return false;
        return true;
    }
}

用户管理类

import java.io.*;
import java.util.*;
import java.util.*;
public class UsersManger {
    
    //注册
    public void registUser(String account,String password) throws Exception {
        User user=new User(account,password);
        File file=new File("user.txt");
        ObjectOutputStream oos=null;
        ObjectInputStream ois=null;
        if(file.length()==0){
            List list=new ArrayList<>();
            list.add(user);
            oos=new ObjectOutputStream(new FileOutputStream(file)); 
            oos.writeObject(list);
            System.out.println("注册成功1");
        }else{
            ois=new ObjectInputStream(new FileInputStream(file));
            Listlist=(List)ois.readObject();
            for(User u:list){
                if(u.getAccount().equals(account)){
                    System.out.println("你注册的账号已存在");
                }else{
                    list.add(user);
                    oos=new ObjectOutputStream(new FileOutputStream(file)); 
                    oos.writeObject(list);
                    System.out.println("注册成功2");
                }
            }       
        }
            
        //ois.close();
        //oos.close();
    }
    //登陆
    public boolean loginUser(String account,String password) throws Exception{
        File file=new File("user.txt");
        ObjectInputStream ois=null;
        ois=new ObjectInputStream(new FileInputStream(file));
        Listlist=(List)ois.readObject();
        boolean b=false;
        for(User u:list){
            if(u.getAccount().equals(account)&&u.getPassword().equals(password)){
                b=true;
            }else{
                b=false;
            }
        }
        if(b){
            System.out.println("登陆成功");
            
        }else{
            System.out.println("账号或者密码错误再想一想");
            
        }   
        ois.close();        
        return b;
}
}

管理员类

public class Administrator implements Serializable {
    
    private static final long serialVersionUID = 1L;
    private String account;
    private String password;
    public Administrator(String account, String password) {
        super();
        this.account = account;
        this.password = password;
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}

管理员管理类

public class Administrator implements Serializable {
    
    private static final long serialVersionUID = 1L;
    private String account;
    private String password;
    public Administrator(String account, String password) {
        super();
        this.account = account;
        this.password = password;
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}


书类

import java.io.Serializable;

public class Book implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String bookName;
    private String bookAuther;
    private int price;
    private int amount;
    public Book(String bookName, String bookAuther, int price, int amount) {
        super();
        this.bookName = bookName;
        this.bookAuther = bookAuther;
        this.price = price;
        this.amount = amount;
    }
    public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public String getBookAuther() {
        return bookAuther;
    }
    public void setBookAuther(String bookAuther) {
        this.bookAuther = bookAuther;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
    @Override
    public String toString() {
        return "书名:" + bookName + ", 作者:" + bookAuther + ", 价钱:" + price + ", 数量:" + amount;
    }
}

商城类

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.io.*;
public class Shop {

    //列出商品,返回的是商城里的商品集合
    public List listBook() throws FileNotFoundException, IOException, ClassNotFoundException{
        File file=new File("book.txt");
        List list=new ArrayList();
        ObjectInputStream ois=null;
        if(file.length()==0){
            System.out.println("商城里没有商品");
        }else{
        ois=new ObjectInputStream(new FileInputStream(file));
        list=(List)ois.readObject();
        }
            ois.close();
        return list;
    }
    
    //遍历商城了里书的集合
    public void bianliBook(List list){
        System.out.print("书名\t");
        System.out.print("作者\t");
        System.out.print("价钱\t");
        System.out.print("数量\t");   
        System.out.println();
        for(Book u:list){
            System.out.print(u.getBookName()+"\t");
            System.out.print(u.getBookAuther()+"\t");
            System.out.print(u.getPrice()+"\t");
            System.out.print(u.getAmount()+"\t");
            System.out.println();
        }
    }
    
        //1.管理员具备的权利 :添加商品
    public void addBook(Book book) throws FileNotFoundException, IOException, ClassNotFoundException{
        File file=new File("book.txt");
        ObjectOutputStream oos=null;    
        if(file.length()==0){  //如果里面没有书直接往里面加书
            Listlist=new ArrayList<>();
            list.add(book);
            oos=new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(list);  
        }else{ //获取文件中集合,添加商品到集合,然后重新存入
        List list=listBook();
        list.add(book);
        oos=new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(list);
        }
        System.out.println("添加成功");
        //oos.close();
        //ois.close();  
    }
    
    //2.管理具备的权利:下架商品
    public void removeBook(String bname) throws FileNotFoundException, IOException, ClassNotFoundException{
        File file=new File("book.txt");
        ObjectOutputStream oos=null;
        if(file.length()==0){    
            System.out.println("没有书了不能下架了");
            
        }else{  //先找出文件中存商品的集合
        List list=listBook();
        Iterator it=list.iterator();
        while(it.hasNext()){ //如果文件中商品是你想下架的商品就从集合删除
            if(it.next().getBookName().equals(bname)){
                it.remove();
                break;
            }
        }   //把删除后的集合重新存到文件中
        oos=new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(list);
        }   
    }
    
    //利用分页的方法显示商品
    public void fenYe(List list){
        Scanner in=new Scanner(System.in);
        int i=0;
        while(true){
            System.out.println("1.首页2.下一页3.上一页4.尾页5.退出选择商品");
            int a=in.nextInt();
        
            if(a==1){  //首页
                i=0;
            for(;i<3;i++){
            System.out.println((i+1)+"."+list.get(i).getBookName());
            }
            }else if(a==2){ //下一页       
                int j=i+3;
                if(j<=list.size()){
                for(;i=0){
                    i=i-3;
                for(;jlist=listBook();
        fenYe(list);
        System.out.println("请输入你选择商品");
        Scanner in=new Scanner(System.in);
        int d=in.nextInt();
        System.out.println(list.get(d-1));//查看选择的商品详情
        System.out.println("是/否加入购物车?");
        in=new Scanner(System.in);
        String buy=in.next();
        if(buy.equals("是")){  //加入购物车
            addCar(u,list.get(d-1));
            System.out.println("1返回上一级2.查看购物车");
            in=new Scanner(System.in);
            int fan=in.nextInt();
            if(fan==1){
                listBook(u);   //返回到商品分页查看的地方           
            }else if(fan==2){           
                List lists=checkCar(u);  //查看购物车  
                changeCar(u,lists);
                
            }else{
                System.out.println("别瞎输入好么");   
            }   
        }else if(buy.equals("否")){    //不加入购物车
        listBook(u);      //返回listbook
        }
    }
    
    //把书添加到用户的购物车   
    public void addCar(User u,Book book) throws FileNotFoundException, IOException, ClassNotFoundException{           
        u.list.add(book);     //添加到用户购物车
        File file=new File("user.txt");
        ObjectOutputStream oos=null;
        ObjectInputStream ois=null; //重新获取这个用户
        ois=new ObjectInputStream(new FileInputStream(file));
        Listlist=(List)ois.readObject();
        for(User us:list){   //将当前用户与存到文件中的用户交换数据
            if(us.getAccount().equals(u.getAccount())){
                us=u;       
            }
    }                       //重新把用户存进去
        oos=new ObjectOutputStream(new FileOutputStream(file)); 
        oos.writeObject(list);
    }
    
    
    //查看购物车
    public List checkCar(User u) throws FileNotFoundException, ClassNotFoundException, IOException{
        int num=1;
        Listlist=u.list;
        System.out.print("ID\t");
        System.out.print("书名\t");
        System.out.print("单价\t");
        System.out.print("购买数量\t");
        System.out.print("总价\t");
        System.out.println();   
        for(int i=0;i list) throws FileNotFoundException, ClassNotFoundException, IOException{
        System.out.println("请输入你想修改的数量的书的名字");
        Scanner in=new Scanner(System.in);
        String name=in.next();
        System.out.println("请输入想买多少本");
        in=new Scanner(System.in);
        int shu=in.nextInt();
        List l=listBook();
        for(Book b:l){
            if(b.getBookName().equals(name)){
                if(b.getAmount()>shu){
                    System.out.println("修改成功");
                    int i=0;
                    for(;i list=(List)ois.readObject();
        for(Book b:list){
            if(b.getBookName().equals(name)){
                System.out.println(b);
                return b;
            }
        }
        return null;    
    }   
}


测试类

import java.util.*;
public class Demo1 {
    public static void main(String[] args) throws Exception {
        System.out.println("欢迎来到千锋商城");
        while(true){
        System.out.println("选择你想要进行的操作1登陆2注册");
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        if(a==1){//登陆
            System.out.println("1登陆管理员2登陆用户");
            in = new Scanner(System.in);
            int b=in.nextInt();
            if(b==1){   //登陆管理员
                System.out.println("请输入账号");
                in = new Scanner(System.in);
                String account=in.next();
                System.out.println("请输入密码");
                in = new Scanner(System.in);
                String password=in.next();
                AdministratorManger am=new AdministratorManger();
                boolean deng=am.loginAdministrator(account, password);
                Administrator at=new Administrator(account,password);
                if(deng){
                    Shop shop=new Shop();
                    while(true){
                    System.out.println("1添加商品2下架商品3退出");
                    in=new Scanner(System.in);
                    int ada=in.nextInt();
                    if(ada==1){
                    System.out.println("请输入书名");
                    in=new Scanner(System.in);
                    String bname=in.next();
                    System.out.println("请输入作者");
                    in=new Scanner(System.in);
                    String rname=in.next();
                    System.out.println("请输入价钱");
                    in=new Scanner(System.in);
                    int price=in.nextInt();
                    System.out.println("请输入数量");
                    in=new Scanner(System.in);
                    int ac=in.nextInt();
                    Book book=new Book(bname,rname,price,ac);
                    shop.addBook(book);
                    
                    }else if(ada==2){
                        shop.bianliBook(shop.listBook());
                        System.out.println("请输入书名");
                        in=new Scanner(System.in);
                        String bname=in.next();
                        shop.removeBook(bname);
                        System.out.println("下架成功");             
                    }else if(ada==3){
                    
                    System.exit(0);
                }   
                }
                
                }
            }else if(b==2){  //登陆用户
                System.out.println("请输入账号");
                in = new Scanner(System.in);
                String account=in.next();
                System.out.println("请输入密码");
                in = new Scanner(System.in);
                String password=in.next();
                UsersManger um=new UsersManger();
                boolean deng=um.loginUser(account, password);
                User u=new User(account,password);
                Shop shop=new Shop();
                    if(deng){
                        System.out.println("1.查看商品2.搜索商品3.退出");
                        in=new Scanner(System.in);
                        int c=in.nextInt();
                        if(c==1){  //查看商品
                            shop.listBook(u);                       
                        }else if(c==2){//搜索商品
                            System.out.println("请输入你想搜索的书名");
                            in=new Scanner(System.in);
                            String name=in.next();
                            Book book=shop.searchBook(name);
                            shop.listBook(u);                   
                        }else if(c==3){//退出
                            System.exit(0);                 
                        }
                    }   
        }
        }else if(a==2){ //注册
            System.out.println("1注册管理员2注册用户");
            in = new Scanner(System.in);
            int c=in.nextInt();
            if(c==1){   //注册管理员
                System.out.println("请输入账号");
                in = new Scanner(System.in);
                String account=in.next();
                System.out.println("请输入密码");
                in = new Scanner(System.in);
                String password=in.next();
                AdministratorManger am=new AdministratorManger();
                am.registAdministrator(account, password);
                
            }else if(c==2){  //注册用户
                System.out.println("请输入账号");
                in = new Scanner(System.in);
                String account=in.next();
                System.out.println("请输入密码");
                in = new Scanner(System.in);
                String password=in.next();
                UsersManger um=new UsersManger();
                um.registUser(account, password);           
            }else{
                System.out.println("请按照规则输入");
            }       
        }else{
            System.out.println("请按照规则输入");
        }   
    }
    }
    }

你可能感兴趣的:(简易商城系统)