吐槽下一个不知名的小老弟
下午闲来无事逛了下问答区,看到有这个一个求助,用java写一个cmd的购物系统要有注册、登录、商品列表。
心想没事就开始写了,写完了提交答案居然提交不了,再一看,好家伙这个问答被删除了…
附上问答地址:https://ask.csdn.net/questions/7576553?spm=1005.2025.3001.5141
下面是搞了一下午的代码
package com.negen;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
/**
* @author Negen
* @createtime 2021年11月23日14:15:20
*/
public class MarketSysDemo {
private static final String USER_INFO_FILE_NAME = "/mksys-userInfos.txt";
private static final String GOODS_INFO_FILE_NAME = "/mksys-goodsInfos.txt";
private static final String SPLITSYMBOL = "-";
String rootPath=System.getProperty("user.dir");
private HashMap<String,String> userInfoMap = new HashMap<String, String>();
private HashMap<String,String> goodsInfoMap = new HashMap<String, String>();
private List<String> goodsInfoList = new ArrayList<String>();
Scanner scanner = new Scanner(System.in);
/**
* 初始化程序
*/
void initSys() throws IOException {
//根目录
File userInfoFile = new File(rootPath + USER_INFO_FILE_NAME);
File goodsInfoFile = new File(rootPath + GOODS_INFO_FILE_NAME);
if (!userInfoFile.exists()){
//创建文件
userInfoFile.createNewFile();
}else{
//加载用户信息
FileReader fileReaderUserInfo = new FileReader(userInfoFile);
BufferedReader bufferedReaderUserInfo = new BufferedReader(fileReaderUserInfo);
String line = null;
while ((line = bufferedReaderUserInfo.readLine()) != null){
String[] lineArr = line.split(SPLITSYMBOL);
String account = lineArr[0];
userInfoMap.put(account, line);
}
fileReaderUserInfo.close();
bufferedReaderUserInfo.close();
}
if (!goodsInfoFile.exists()){
//创建文件
goodsInfoFile.createNewFile();
FileWriter fileWriter = new FileWriter(goodsInfoFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("0001-爆米花-12\n" +
"0002-薯片-10\n" +
"0003-奥利奥-8\n" +
"0004-水杯-15\n" +
"0005-保温杯-99");
bufferedWriter.flush();
fileWriter.close();
bufferedWriter.close();
}
//加载商品信息
FileReader fileReaderGoodsInfo = new FileReader(goodsInfoFile);
BufferedReader bufferedReaderGoodsInfo = new BufferedReader(fileReaderGoodsInfo);
String line = null;
while ((line = bufferedReaderGoodsInfo.readLine()) != null){
String[] lineArr = line.split(SPLITSYMBOL);
String goodsNum = lineArr[0];
goodsInfoMap.put(goodsNum, line);
goodsInfoList.add(line);
}
fileReaderGoodsInfo.close();
bufferedReaderGoodsInfo.close();
System.out.println("========== 系统加载完成 ==========");
System.out.println("================================");
System.out.println("\" ");
System.out.println("\" 欢迎使用自助购物系统 ");
System.out.println("\" 1、注册 ");
System.out.println("\" 2、登录 ");
System.out.println("\" ");
System.out.println("\"==============================\"");
}
/**
* 账号注册
*/
void userRegister() throws IOException {
String account = null;
String passwrod = null;
String name = null;
while (true){
System.out.println("请输入账号:");
account = scanner.next();
//判断账号是否存在
if (userInfoMap.containsKey(account)){
System.out.println("账号已存在请重新输入!!!");
}else{
break;
}
}
System.out.println("请输入密码:");
passwrod = scanner.next();
System.out.println("请输入姓名:");
name = scanner.next();
String line = account + SPLITSYMBOL + passwrod + SPLITSYMBOL + name + "\n";
File userInfoFile = new File(rootPath + USER_INFO_FILE_NAME);
FileWriter fileWriter = new FileWriter(userInfoFile, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(line);
bufferedWriter.flush();
fileWriter.close();
bufferedWriter.close();
userInfoMap.put(account, line.substring(0,line.length()-1));
System.out.println("注册成功,返回首页");
System.out.println("================================");
System.out.println("\" ");
System.out.println("\" 欢迎使用自助购物系统 ");
System.out.println("\" 1、注册 ");
System.out.println("\" 2、登录 ");
System.out.println("\" ");
System.out.println("\"==============================\"");
}
/**
* 账号登录
*/
void userLogin(){
String account = null;
String passwrod = null;
System.out.println("请输入账号:");
account = scanner.next();
System.out.println("请输入密码:");
passwrod = scanner.next();
//查询账号是否存在
String accountAndPassword = userInfoMap.get(account);
if (null == accountAndPassword || "".equals(accountAndPassword)){
System.out.println("账号未注册");
System.out.println("返回首页");
System.out.println("================================");
System.out.println("\" ");
System.out.println("\" 欢迎使用自助购物系统 ");
System.out.println("\" 1、注册 ");
System.out.println("\" 2、登录 ");
System.out.println("\" ");
System.out.println("\"==============================\"");
}else{
String[] accountAndPasswordArr = accountAndPassword.split(SPLITSYMBOL);
if (!passwrod.equals(accountAndPasswordArr[1])){
System.out.println("密码错误!!!");
System.out.println("登录失败,返回首页");
System.out.println("================================");
System.out.println("\" ");
System.out.println("\" 欢迎使用自助购物系统 ");
System.out.println("\" 1、注册 ");
System.out.println("\" 2、登录 ");
System.out.println("\" ");
System.out.println("\"==============================\"");
}
}
System.out.println("登录成功!!!");
listGoods();
System.out.println("输入商品编号并支付");
selectAndPay();
}
/**
* 商品列表
*/
void listGoods(){
System.out.println("=======商品列表========");
System.out.println("编号\t\t\t名称\t\t\t价格");
goodsInfoList.forEach(goodsInfo->{
String[] goodsInfoArr = goodsInfo.split(SPLITSYMBOL);
String goodsNum = goodsInfoArr[0];
String name = goodsInfoArr[1];
String price = goodsInfoArr[2];
System.out.println(String.format("%s\t\t\t%s\t\t\t%s", goodsNum, name, price));
});
}
void selectAndPay(){
System.out.println("请输入要购买的商品编号并以英文逗号分隔,例如:0001,0002");
String goodsNumStr = scanner.next();
String[] goodsNumArr = goodsNumStr.split(",");
Float total = 0f;
System.out.println("=======待付款列表========");
for (String goodsNum : goodsNumArr) {
String goodsDetailStr = goodsInfoMap.get(goodsNum);
if (null != goodsDetailStr && !"".equals(goodsDetailStr)){
String[] goodsDetailArr = goodsDetailStr.split(SPLITSYMBOL);
goodsNum = goodsDetailArr[0];
String name = goodsDetailArr[1];
String price = goodsDetailArr[2];
float fPrice = Float.parseFloat(price);
total+=fPrice;
System.out.println(String.format("%s\t\t\t%s\t\t\t%s", goodsNum, name, price));
}
}
System.out.println("总价:" + total);
//获取商品详情
System.out.println("确认付款,Y 付款/N 返回首页");
String next = scanner.next();
if (next.equals("Y")){
System.out.println("付款成功,返回首页");
}
System.out.println("================================");
System.out.println("\" ");
System.out.println("\" 欢迎使用自助购物系统 ");
System.out.println("\" 1、注册 ");
System.out.println("\" 2、登录 ");
System.out.println("\" ");
System.out.println("\"==============================\"");
}
void mainApp () throws IOException {
while (true){
System.err.println("请输入序号选择操作:");
switch (scanner.nextInt()){
case 1:
//注册
userRegister();
break;
case 2:
//登录
userLogin();
break;
}
}
}
public static void main(String[] args) throws IOException {
MarketSysDemo marketSysDemo = new MarketSysDemo();
marketSysDemo.initSys();
marketSysDemo.mainApp();
}
}