(系统)网络电商购物系统

/**
 * 1.电商购物系统
具体要求
1.要求用户输入用户名,密码,验证码来进行登录,
用户名默认为jredu,密码默认为 1234,验证码由4位随机数组成,包含大小写字母,数字。验证码示例:a9B7
当用户输入用户名jredu及密码1234 及正确验证码时,登录成功,否则提示登录失败,要求用户重新输入,
不管用户登录成功与否,都将用户登录信息记录到d:/io/log.log文件中,
记录信息示例:
# 2017-09-01 16:03:32 用户名:abc 密码:123 登录失败 #
# 2017-09-01 16:13:22 用户名:jredu 密码:1234 登录成功 #

2.用户登录成功,展示商品列表
商品列表显示如下:
1.笔记本电脑 3500元
2.ipad air   4200元
3.iphone7    6800元
4.小米电视   7900元
3.用户可以选择购买的商品
请选择要购买的商品编号:2
请输入要购买的商品数量:1
是否要继续购买(y/n):y
请选择要购买的商品编号:3
请输入要购买的商品数量:2
是否要继续购买(y/n):n
您要购买的商品是:
编号  名称      单价    数量
2.   ipad air   4200元   1
3.   iphone7    6800元   2
总价:17800元
展示完成,将定单信息存入文件,user.data,存入时要求同时记录定单时间,精确到秒
用户再次登录时,可以展示历史定单信息及所有定单的总价和
附加:
在登录时考虑存在多个用户的情况
个人想加注册功能
 */
/**
 * 
 *
 */
package HomeWork1;

心得:

心得1
    //这个代码是用来更新hashmap中的信息的
    public boolean equals(Object obj) {
        if (!(obj instanceof Goods)) {
            return false;
        }
        Goods goods = (Goods)obj;
        return this.Goods_no.equals(goods.Goods_no)
                &&this.Goods_name.equals(goods.Goods_name)
                &&this.Goods_price == goods.Goods_price;
    }
    public int hashCode() {
        return this.Goods_no.hashCode();
    }
心得2
random在封装在方法里的时候。返回值应该放在一个定义的变量中。否则容易出现错误。错误性可能会是误多次调用。导致结果不同。
心得3
在使用arrarylist中。没有很好的处理链表。导致每次启动程序时都重置链表为空。处理的方法是。定义的链表时从文件中取出之前的链表赋值之。
心得4
我使用的是arrarylist来存储用户账号的。用hashmap来存储账单信息。序列化方法来存储用户账户信息  

遗憾

没有实现一个文件存储多个用户的历史账单的信息。
我的思路是订单出来之后用文件接收。接收的文件为“用户名+.dat/txt”,每个用户一个订单信息文件。
还没实现多个订单的总的价格

代码如下:

定义主要的类的属性

package HomeWork1;

public class Goods {
//定义一个商品类,属性有:商品编号.名字.价格
    private String Goods_no;
    private String Goods_name;
    private float Goods_price;
    public float getGoods_price() {
        return Goods_price;
    }
    public void setGoods_price(float goods_price) {
        Goods_price = goods_price;
    }
    public String getGoods_no() {
        return Goods_no;
    }
    public void setGoods_no(String goods_no) {
        Goods_no = goods_no;
    }
    public String getGoods_name() {
        return Goods_name;
    }
    public void setGoods_name(String goods_name) {
        Goods_name = goods_name;
    }
    public Goods(String goods_no, String goods_name,float goods_price) {
        super();
        Goods_no = goods_no;
        Goods_name = goods_name;
        this.Goods_price = goods_price;
    }
    public Goods() {
        super();
    }
    @Override
    //用以存储hashmap时的更新数据查重
    public boolean equals(Object obj) {
        if (!(obj instanceof Goods)) {
            return false;
        }
        Goods goods = (Goods)obj;
        return this.Goods_no.equals(goods.Goods_no)
                &&this.Goods_name.equals(goods.Goods_name)
                &&this.Goods_price == goods.Goods_price;
    }
    public int hashCode() {
        return this.Goods_no.hashCode();
    }

}
package HomeWork1;

import java.io.Serializable;
//定义用户类,属性有:用户账户名,用户密码
public class User implements Serializable {

    private String user_name;
    private String user_passwd;

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getUser_passwd() {
        return user_passwd;
    }

    public void setUser_passwd(String user_passwd) {
        this.user_passwd = user_passwd;
    }

    public User(String user_name, String user_passwd) {
        super();
        this.user_name = user_name;
        this.user_passwd = user_passwd;
    }

    public User() {
        super();
    }
}

程序的主要入口

package HomeWork1;
//程序的主入口
public class mainClass {
    public static void main(String[] args) {
        UserLogin login = new UserLogin();
        login.loginmain();
        ShowGoods showGoods = new ShowGoods();
        showGoods.buyGoods();

    }
}

登陆界面模块

package HomeWork1;

import java.util.ArrayList;
import java.util.Scanner;
//用以登陆界面
public class UserLogin {
    //定义静态存储用户信息地址
    public static String path = "d:/IO/log.txt";
    //定义静态用户登陆情况信息地址
    public static String datapath = "d:/IO/datalog.txt";
    public static void loginmain() {
        System.out.println("欢迎来到电商购物系统");
        System.out.println("用户请选择:\n1:注册账号密码  .        2:已有账户密码登录.");
        Scanner scanner = new Scanner(System.in);
        Choose(scanner);
    }
    //定义选择注册账号密码 还是已有账户密码登录的方法
    private static void Choose(Scanner scanner) {
        //用key来接收用户的选择,如果是1即进入注册界面。如果是2则进入登陆界面
        int key = scanner.nextInt();
        OutputUserList output = new OutputUserList();
        UserRegister userRegister = new UserRegister();
        //初始化用户表,然后建立文件.如果有,就不创建
        userRegister.reset(path);
        switch (key) {
        case 1:
            //注册.添加账号密码
            System.out.println("您选择了注册:");
            System.out.println("请输入您需要注册的用户名:");
            String name = scanner.next();
            System.out.println("请输入您需要注册的密码:");
            String passwd = scanner.next();
            //使用register()方法将用户的注册信息存入"d:/IO/log.txt"文件中
            userRegister.register(name, passwd, path);
            System.out.println("注册成功");
            System.out.println("用户请选择:\n1:注册账号密码  .        2:已有账户密码登录.");
            //返回选择界面
            Choose(scanner);
            break;
        case 2:
        //从"d:/IO/log.txt"中取出信息进行匹配。实现匹配登陆功能
            Object object = output.outputUser(path);
            ArrayList<User> list = null;
            if (object != null) {
                list = (ArrayList<User>) object;
//              for (User user : list) {
//                  System.out.println(user.getUser_name()+"\t"+user.getUser_passwd());
//              }
            }
                System.out.println("登录用户:");
                System.out.println("请输入您的用户名:");
                String user_name = scanner.next();
                System.out.println("请输入您的密码:");
                String user_passwd = scanner.next();
                IdentifyingCode code = new IdentifyingCode();
                String Randomcode = code.randomCode();
                System.out.println("请输入验证码:"+ Randomcode);
                String s = scanner.next();
                int loginkey = 0;
/*  遍历一遍用户账户数据库.并试图进行匹配
    此处定义一个loginkey进行判断。机制为:初始值为0;如果账号密码匹配成功则+1,不成功则不变。验证码匹配成功则不变,不成功则-1000,当loginkey>0,则验证成功*/
                for (User user : list) {
                    if (user_name.equals(user.getUser_name())
                            &&user_passwd.equals(user.getUser_passwd())) {
                        loginkey ++;
                        break;
                        }
                    }

                if (!s.equals(Randomcode)) {
                    loginkey -=1000;
                }
                String message = null;
                if (loginkey > 0) {
                    message = "登陆成功";
                    System.out.println(message);
                }else if(loginkey == 0){
                    message = "账号或者密码不正确";
                    System.out.println(message);
                    System.out.println("用户请选择:\n1:注册账号密码  .        2:已有账户密码登录.");
                    Choose(scanner);
                }else {
                    message = "验证码输入错误";
                    System.out.println(message);
                    System.out.println("用户请选择:\n1:注册账号密码  .        2:已有账户密码登录.");
                    Choose(scanner);
                }
                LoginData data = new LoginData();
                //将登陆信息存入d:/IO/datalog.txt
                data.saveLoginDate(user_name, user_passwd, message,datapath);
            break;

        default:
            System.out.println("输入有误!!!!!!!!!!!");
            System.out.println("用户请选择:\n1:注册账号密码  .        2:已有账户密码登录.");
            Choose(scanner);
            break;
        }
    }
}

向文件中存账号密码模块

package HomeWork1;

//inputUser(String path,ArrayList<User> arrayList).没有返回值.作用是存一个集合链表
//我用来对进行存储用户的账号密码等信息。存储到用户表"d:/IO/log.txt"中
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class InputUserlist {
    public static void inputUser(String path, ArrayList<User> arrayList) {
        ObjectOutputStream oostream;
        try {
            oostream = new ObjectOutputStream(new FileOutputStream(path));
            oostream.writeObject(arrayList);
            oostream.flush();
            oostream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

取出文件中用户账号密码模块

package HomeWork1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
//从地址path中取出信息
//返回一个obj
public class OutputUserList {
    public static Object outputUser(String path) {
        ObjectInputStream ois;
        try {
            ois = new ObjectInputStream(new FileInputStream(path));
            Object obj = ois.readObject();
            ois.close();
            if (obj != null) {
                return obj;
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

注册模块

package HomeWork1;
//用户注册模块
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
//path为账号密码的存储位置
public class UserRegister {
//定义一个链表用于存放用户注册的信息
    public static ArrayList<User> arrayList = new ArrayList<>();
    @SuppressWarnings("static-access")

    public static void reset(String path){
        InputUserlist input = new InputUserlist();
        File f = new File(path); 
        //检测文件是否存在
        if (!f.exists()) {
            try {
                f.createNewFile();
                //清空链表
                arrayList.clear();
                //重置链表
                addArrayreset();
                //存入文件中
                input.inputUser(path,arrayList); 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }   
    }
    @SuppressWarnings("static-access")
    //注册用户信息。存入表中
    public static void register(String name,String passwd,String path){
        @SuppressWarnings("unused")
        File f = new  File(path);
        InputUserlist input = new InputUserlist();
        addArrayList(name, passwd);
        input.inputUser(path,arrayList); 
    }
    private static void addArrayreset() {
    //重置注册信息。默认账号密码为"jredu","1234"
            ArrayList<User> arrayList1 = arrayList;
            arrayList1.add(new User("jredu","1234"));
            arrayList = arrayList1;

    }
    //在用户链表中添加新注册用户
    private static void addArrayList(String name, String passwd) {
        OutputUserList output = new OutputUserList();
        Object object = output.outputUser(UserLogin.path);
        if (object != null) {
            ArrayList<User> list = (ArrayList<User>) object;
            arrayList = list;
        }
        arrayList.add(new User(name,passwd));
    }
}

验证码生成模块

package HomeWork1;

//登陆时用到的验证码生成方法
import java.util.Random;

public class IdentifyingCode {
    public String code = "1234567890abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM";

    public String randomCode() {
        Random random = new Random();
        String string = "";
        for (int i = 0; i < 4; i++) {
            string += code.charAt(random.nextInt(code.length()));
        }
        return string;
    }
}

登陆日志模块

package HomeWork1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
//登录日志
import java.util.Date;

public class LoginData {
    public void saveLoginDate(String name, String passwd, String message, String path) {
        Date date = new Date();
        //定义输出时的模板
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");
        String dateString = sf.format(date);
        String string = " #" + dateString + " 用户名: " + name + " 密码: " + passwd + " " + message + " #";
        File file = new File(path);
        //检测文件是否存在
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        FileWriter fileWriter;
        try {
        //写入登陆日志
            fileWriter = new FileWriter(path, true);
            fileWriter.write(string);
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

商品展示及购买界面

package HomeWork1;

import java.util.HashMap;
import java.util.Scanner;

public class ShowGoods {

    private static HashMap<Goods, Integer> goods = new HashMap<Goods, Integer>();

    public static void buyGoods() {
        GoodsShowHistory goodsShowHistory = new GoodsShowHistory();
//展示用户购物历史信息    
    goodsShowHistory.showBuyingHistory("d:/IO/user.txt");
        Scanner scanner = new Scanner(System.in);
        System.out.println("*************************************");
        System.out.println("*****可供选择商品如下:*****");
        System.out.println("1.   笔记本电脑             3500元");
        System.out.println("2.   ipad air   4200元");
        System.out.println("3.   iphone7    6800元");
        System.out.println("4.   小米电视                  7900元");
        System.out.println("**用户可以选择购买的商品的编号**");
        System.out.println("*************************************");
        String key = "n";
        do {
            System.out.println("请选择要购买的商品编号:");
            String Good_no = scanner.next();
            System.out.println("请选择要购买的商品数量:");
            int Good_num = scanner.nextInt();
            choose(goods, Good_no, Good_num);
            System.out.println("是否要继续购买(y/n):");
            key = scanner.next();
        } while (key.equals("y"));
        show(goods);
        GoodBuyingDate goodBuyingDate = new GoodBuyingDate();
        goodBuyingDate.saveGoodBuyingDate(goods, "d:/IO/user.txt");

    }

    public static void choose(HashMap<Goods, Integer> goods, String good_no, Integer good_num) {
        Goods goods1 = new Goods("1", "笔记本电脑", 3500);
        Goods goods2 = new Goods("2", "ipad air", 4200);
        Goods goods3 = new Goods("3", "iphone7", 6800);
        Goods goods4 = new Goods("4", "小米电视 ", 7900);
        switch (good_no) {
        case "1":
            goods.put(goods1, good_num);
            break;
        case "2":
            goods.put(goods2, good_num);
            break;
        case "3":
            goods.put(goods3, good_num);
            break;
        case "4":
            goods.put(goods4, good_num);
            break;
        default:
            break;
        }
    }

    public static void show(HashMap<Goods, Integer> goods) {
    //展示订单详情在控制台
        System.out.println("您的购物清单为:");
        System.out.println(("商品编号\t" + "商品名称\t" + "商品价格\t" + "商品数量\t"));
        float sum = 0;
        for (Goods i : goods.keySet()) {
            System.out.println(i.getGoods_no() + "\t" + i.getGoods_name() + "\t" + i.getGoods_price() + "元\t"
                    + goods.get(i).intValue() + "个\n");
            sum += i.getGoods_price() * goods.get(i).intValue();
        }
        System.out.println("您选购的商品的总价为:" + sum);

    }
}

存储订单情况

package HomeWork1;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class GoodBuyingDate {
    public void saveGoodBuyingDate(HashMap<Goods, Integer> goods, String path) {
        Date date = new Date();
        //定义时间的格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        String dateString = sf.format(date);
        String string = " #" + dateString + "#\n";
        FileWriter fw;
        try {
            fw = new FileWriter(path, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.newLine();
            bw.write("**************************************");
            bw.newLine();
            bw.write(string);
            bw.newLine();
            bw.write("商品编号\t" + "商品名称\t" + "商品价格\t" + "商品数量\t");
            bw.newLine();
            float sum = 0;
            for (Goods i : goods.keySet()) {
                String s = new String(i.getGoods_no() + "\t" + i.getGoods_name() + "\t" + i.getGoods_price() + "元\t"
                        + goods.get(i).intValue() + "个");
                sum += i.getGoods_price() * goods.get(i).intValue();
                bw.write(s);
                bw.newLine();
            }
            String sumstring = new String("总价为:" + sum);
            bw.write(sumstring);
            bw.newLine();
            bw.write("**************************************");
            bw.newLine();
            bw.flush();
            fw.flush();
            bw.close();
            fw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

展示历史消费记录的类与方法

package HomeWork1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//历史消费记录的展示
public class GoodsShowHistory {
    public void showBuyingHistory(String path) {
        System.out.println("您的历史订单如下:");
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //定义一个fileReader
        //从path地址中读出里面的订单信息
        FileReader fr;
        try {
            fr = new FileReader(path);
            BufferedReader br = new BufferedReader(fr);
            String s = null;
            try {
                while ((s = br.readLine()) != null) {
                    System.out.println(s);
                }

                fr.close();
                br.close();
                System.out.println("*****您的账单打印完毕****");
                System.out.println("\n");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值