簡(jiǎn)易商城系統(tǒng)

image.png

需求分析:
1.用戶類:賬號(hào),密碼,購(gòu)物車
2.用戶管理類:
(1)注冊(cè)翠语,將注冊(cè)后的user對(duì)象存到一個(gè)集合然后存到文件里
(2)登陸,從文件中獲取集合民镜,遍歷集合啡专,如果賬號(hào)密碼相等則登陸成功
3.管理員類
4.管理員管理類
5.書類
6.商城類
(1)遍歷文件里的書集合
(2)管理員具備的權(quán)利 :添加商品
(3)2.管理具備的權(quán)利:下架商品
(4)利用分頁(yè)的方法顯示商品
(5)把書添加到用戶的購(gòu)物車
(6)查看購(gòu)物車
(7)改變購(gòu)物車
(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;
    List<Book>list=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 {
    
    //注冊(cè)
    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<User> list=new ArrayList<>();
            list.add(user);
            oos=new ObjectOutputStream(new FileOutputStream(file)); 
            oos.writeObject(list);
            System.out.println("注冊(cè)成功1");
        }else{
            ois=new ObjectInputStream(new FileInputStream(file));
            List<User>list=(List<User>)ois.readObject();
            for(User u:list){
                if(u.getAccount().equals(account)){
                    System.out.println("你注冊(cè)的賬號(hào)已存在");
                }else{
                    list.add(user);
                    oos=new ObjectOutputStream(new FileOutputStream(file)); 
                    oos.writeObject(list);
                    System.out.println("注冊(cè)成功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));
        List<User>list=(List<User>)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("賬號(hào)或者密碼錯(cuò)誤再想一想");
            
        }   
        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 + ", 價(jià)錢:" + price + ", 數(shù)量:" + 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<Book> listBook() throws FileNotFoundException, IOException, ClassNotFoundException{
        File file=new File("book.txt");
        List<Book> list=new ArrayList<Book>();
        ObjectInputStream ois=null;
        if(file.length()==0){
            System.out.println("商城里沒(méi)有商品");
        }else{
        ois=new ObjectInputStream(new FileInputStream(file));
        list=(List<Book>)ois.readObject();
        }
            ois.close();
        return list;
    }
    
    //遍歷商城了里書的集合
    public void bianliBook(List<Book> list){
        System.out.print("書名\t");
        System.out.print("作者\(yùn)t");
        System.out.print("價(jià)錢\t");
        System.out.print("數(shù)量\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.管理員具備的權(quán)利 :添加商品
    public void addBook(Book book) throws FileNotFoundException, IOException, ClassNotFoundException{
        File file=new File("book.txt");
        ObjectOutputStream oos=null;    
        if(file.length()==0){  //如果里面沒(méi)有書直接往里面加書
            List<Book>list=new ArrayList<>();
            list.add(book);
            oos=new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(list);  
        }else{ //獲取文件中集合,添加商品到集合制圈,然后重新存入
        List<Book> list=listBook();
        list.add(book);
        oos=new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(list);
        }
        System.out.println("添加成功");
        //oos.close();
        //ois.close();  
    }
    
    //2.管理具備的權(quán)利:下架商品
    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("沒(méi)有書了不能下架了");
            
        }else{  //先找出文件中存商品的集合
        List<Book> list=listBook();
        Iterator<Book> it=list.iterator();
        while(it.hasNext()){ //如果文件中商品是你想下架的商品就從集合刪除
            if(it.next().getBookName().equals(bname)){
                it.remove();
                break;
            }
        }   //把刪除后的集合重新存到文件中
        oos=new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(list);
        }   
    }
    
    //利用分頁(yè)的方法顯示商品
    public void fenYe(List<Book> list){
        Scanner in=new Scanner(System.in);
        int i=0;
        while(true){
            System.out.println("1.首頁(yè)2.下一頁(yè)3.上一頁(yè)4.尾頁(yè)5.退出選擇商品");
            int a=in.nextInt();
        
            if(a==1){  //首頁(yè)
                i=0;
            for(;i<3;i++){
            System.out.println((i+1)+"."+list.get(i).getBookName());
            }
            }else if(a==2){ //下一頁(yè)       
                int j=i+3;
                if(j<=list.size()){
                for(;i<j;i++){
                    System.out.println((i+1)+"."+list.get(i).getBookName());        
                }
            }else{
                System.out.println("不能再往后了");
                continue;
            }
                        
            }else if(a==3){//上一頁(yè)    
                int j=i-6;
                if(j>=0){
                    i=i-3;
                for(;j<i;j++){
                    System.out.println((j+1)+"."+list.get(j).getBookName());                            
                }
                }else{
                    System.out.println("不能再往前了");
                    continue;
                }
                
            }else if(a==4){//尾頁(yè)
                int j=list.size()-3;
                i=6;
                for(;j<list.size();j++){
                    System.out.println((j+1)+"."+list.get(j).getBookName());
                }       
            }else{
                break;
            }
        }   
    }
    
    
    public void listBook(User u) throws FileNotFoundException, ClassNotFoundException, IOException{
        List<Book>list=listBook();
        fenYe(list);
        System.out.println("請(qǐng)輸入你選擇商品");
        Scanner in=new Scanner(System.in);
        int d=in.nextInt();
        System.out.println(list.get(d-1));//查看選擇的商品詳情
        System.out.println("是/否加入購(gòu)物車们童?");
        in=new Scanner(System.in);
        String buy=in.next();
        if(buy.equals("是")){  //加入購(gòu)物車
            addCar(u,list.get(d-1));
            System.out.println("1返回上一級(jí)2.查看購(gòu)物車");
            in=new Scanner(System.in);
            int fan=in.nextInt();
            if(fan==1){
                listBook(u);   //返回到商品分頁(yè)查看的地方           
            }else if(fan==2){           
                List<Book> lists=checkCar(u);  //查看購(gòu)物車  
                changeCar(u,lists);
                
            }else{
                System.out.println("別瞎輸入好么");   
            }   
        }else if(buy.equals("否")){    //不加入購(gòu)物車
        listBook(u);      //返回listbook
        }
    }
    
    //把書添加到用戶的購(gòu)物車   
    public void addCar(User u,Book book) throws FileNotFoundException, IOException, ClassNotFoundException{           
        u.list.add(book);     //添加到用戶購(gòu)物車
        File file=new File("user.txt");
        ObjectOutputStream oos=null;
        ObjectInputStream ois=null; //重新獲取這個(gè)用戶
        ois=new ObjectInputStream(new FileInputStream(file));
        List<User>list=(List<User>)ois.readObject();
        for(User us:list){   //將當(dāng)前用戶與存到文件中的用戶交換數(shù)據(jù)
            if(us.getAccount().equals(u.getAccount())){
                us=u;       
            }
    }                       //重新把用戶存進(jìn)去
        oos=new ObjectOutputStream(new FileOutputStream(file)); 
        oos.writeObject(list);
    }
    
    
    //查看購(gòu)物車
    public List<Book> checkCar(User u) throws FileNotFoundException, ClassNotFoundException, IOException{
        int num=1;
        List<Book>list=u.list;
        System.out.print("ID\t");
        System.out.print("書名\t");
        System.out.print("單價(jià)\t");
        System.out.print("購(gòu)買數(shù)量\t");
        System.out.print("總價(jià)\t");
        System.out.println();   
        for(int i=0;i<u.list.size();i++){
            System.out.print(num+"\t");
            System.out.print(u.list.get(i).getBookName()+"\t");
            System.out.print(u.list.get(i).getPrice()+"\t");
            System.out.print(u.list.get(i).getAmount()+"\t");
            System.out.print(u.list.get(i).getAmount()*u.list.get(i).getPrice()+"\t");
            System.out.println();       
        }
        return list;        
    }
    
    public void changeCar(User u,List<Book> list) throws FileNotFoundException, ClassNotFoundException, IOException{
        System.out.println("請(qǐng)輸入你想修改的數(shù)量的書的名字");
        Scanner in=new Scanner(System.in);
        String name=in.next();
        System.out.println("請(qǐng)輸入想買多少本");
        in=new Scanner(System.in);
        int shu=in.nextInt();
        List<Book> l=listBook();
        for(Book b:l){
            if(b.getBookName().equals(name)){
                if(b.getAmount()>shu){
                    System.out.println("修改成功");
                    int i=0;
                    for(;i<u.list.size();i++){
                        if(b.getBookName().equals(u.list.get(i).getBookName())){
                            break;
                        }
                    }
                    u.list.get(i).setAmount(shu);
                    //這有個(gè)小bug,數(shù)量改變沒(méi)有添加到文件中  
                    //而且shop里的商品數(shù)量也還沒(méi)有減掉
                    checkCar(u);
                    System.exit(0);
                }else{
                    System.out.println("沒(méi)有那么些書");
                }
            }
        }   
    }
    
    
    //搜索書
    public Book searchBook(String name) throws FileNotFoundException, IOException, ClassNotFoundException{
        File file=new File("book.txt");
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
        List<Book> list=(List)ois.readObject();
        for(Book b:list){
            if(b.getBookName().equals(name)){
                System.out.println(b);
                return b;
            }
        }
        return null;    
    }   
}


測(cè)試類

import java.util.*;
public class Demo1 {
    public static void main(String[] args) throws Exception {
        System.out.println("歡迎來(lái)到千鋒商城");
        while(true){
        System.out.println("選擇你想要進(jìn)行的操作1登陸2注冊(cè)");
        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("請(qǐng)輸入賬號(hào)");
                in = new Scanner(System.in);
                String account=in.next();
                System.out.println("請(qǐng)輸入密碼");
                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("請(qǐng)輸入書名");
                    in=new Scanner(System.in);
                    String bname=in.next();
                    System.out.println("請(qǐng)輸入作者");
                    in=new Scanner(System.in);
                    String rname=in.next();
                    System.out.println("請(qǐng)輸入價(jià)錢");
                    in=new Scanner(System.in);
                    int price=in.nextInt();
                    System.out.println("請(qǐng)輸入數(shù)量");
                    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("請(qǐng)輸入書名");
                        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("請(qǐng)輸入賬號(hào)");
                in = new Scanner(System.in);
                String account=in.next();
                System.out.println("請(qǐng)輸入密碼");
                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("請(qǐng)輸入你想搜索的書名");
                            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){ //注冊(cè)
            System.out.println("1注冊(cè)管理員2注冊(cè)用戶");
            in = new Scanner(System.in);
            int c=in.nextInt();
            if(c==1){   //注冊(cè)管理員
                System.out.println("請(qǐng)輸入賬號(hào)");
                in = new Scanner(System.in);
                String account=in.next();
                System.out.println("請(qǐng)輸入密碼");
                in = new Scanner(System.in);
                String password=in.next();
                AdministratorManger am=new AdministratorManger();
                am.registAdministrator(account, password);
                
            }else if(c==2){  //注冊(cè)用戶
                System.out.println("請(qǐng)輸入賬號(hào)");
                in = new Scanner(System.in);
                String account=in.next();
                System.out.println("請(qǐng)輸入密碼");
                in = new Scanner(System.in);
                String password=in.next();
                UsersManger um=new UsersManger();
                um.registUser(account, password);           
            }else{
                System.out.println("請(qǐng)按照規(guī)則輸入");
            }       
        }else{
            System.out.println("請(qǐng)按照規(guī)則輸入");
        }   
    }
    }
    }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鲸鹦,一起剝皮案震驚了整個(gè)濱河市慧库,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌馋嗜,老刑警劉巖齐板,帶你破解...
    沈念sama閱讀 211,123評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡甘磨,警方通過(guò)查閱死者的電腦和手機(jī)橡羞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)济舆,“玉大人卿泽,你說(shuō)我怎么就攤上這事∽叹酰” “怎么了签夭?”我有些...
    開(kāi)封第一講書人閱讀 156,723評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)椎侠。 經(jīng)常有香客問(wèn)我第租,道長(zhǎng),這世上最難降的妖魔是什么我纪? 我笑而不...
    開(kāi)封第一講書人閱讀 56,357評(píng)論 1 283
  • 正文 為了忘掉前任慎宾,我火速辦了婚禮,結(jié)果婚禮上宣羊,老公的妹妹穿的比我還像新娘璧诵。我一直安慰自己,他們只是感情好仇冯,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,412評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布之宿。 她就那樣靜靜地躺著,像睡著了一般苛坚。 火紅的嫁衣襯著肌膚如雪比被。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 49,760評(píng)論 1 289
  • 那天泼舱,我揣著相機(jī)與錄音等缀,去河邊找鬼。 笑死娇昙,一個(gè)胖子當(dāng)著我的面吹牛尺迂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播冒掌,決...
    沈念sama閱讀 38,904評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼噪裕,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了股毫?” 一聲冷哼從身側(cè)響起膳音,我...
    開(kāi)封第一講書人閱讀 37,672評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎铃诬,沒(méi)想到半個(gè)月后祭陷,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體苍凛,經(jīng)...
    沈念sama閱讀 44,118評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,456評(píng)論 2 325
  • 正文 我和宋清朗相戀三年兵志,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了醇蝴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,599評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡想罕,死狀恐怖哑蔫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情弧呐,我是刑警寧澤,帶...
    沈念sama閱讀 34,264評(píng)論 4 328
  • 正文 年R本政府宣布嵌纲,位于F島的核電站俘枫,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏逮走。R本人自食惡果不足惜鸠蚪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,857評(píng)論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望师溅。 院中可真熱鬧茅信,春花似錦、人聲如沸墓臭。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 30,731評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)窿锉。三九已至酌摇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間嗡载,已是汗流浹背窑多。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 31,956評(píng)論 1 264
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留洼滚,地道東北人埂息。 一個(gè)月前我還...
    沈念sama閱讀 46,286評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像遥巴,于是被迫代替她去往敵國(guó)和親千康。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,465評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容