public class StoreEntry {
// 文件的存储路径
private static final String GOODS_INFO_FILE_PATH = "goods.txt";
public static void main(String[] args) throws IOException {
// 创建用户集合的对象
ArrayList<User> users = new ArrayList<>();
while (true) {
// 商城用户注册和登录界面
System.out.println("--------------------------欢迎访问ITHEIMA购物商城-------------------------------");
System.out.println("1. 登录");
System.out.println("2. 注册");
System.out.println("3. 退出");
// 请输入您的选择
System.out.println();
System.out.println("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String next = sc.next();
// 选择
switch (next) {
case "1":
// TODO
boolean result = login(users);
if (result) {
// System.out.println(“登录成功…”);
// 商品管理
goodsManager();
break;
} else {
System.out.println(“用户名或者密码错误,请重新输入您的选择…”);
break;
}
case "2":
// TODO
register(users);
break;
case "3":
// TODO
System.out.println("谢谢使用,欢迎下次光临");
System.exit(0);
break;
default:
System.out.println("不存在您的选择,请重新输入!");
}
}
}
// 商品管理系统
private static void goodsManager() throws IOException {
while (true) {
// 商品管理
System.out.println("--------------------------商品管理模块-------------------------------");
System.out.println("1: 添加商品");
System.out.println("2: 查询商品");
System.out.println("3: 退出");
System.out.println("--------------------------商品管理模块-------------------------------");
// 请输入您的选择
System.out.println();
System.out.println("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String next = sc.next();
// 选择
switch (next) {
case "1":
// TODO
addGoods();
break;
case "2":
// TODO
findAllGoods();
break;
case "3":
// TODO
System.out.println("谢谢使用,欢迎下次光临");
System.exit(0);
break;
default:
System.out.println("不存在您输入的选择,请重新输入....");
break;
}
}
}
/**
* 查询所有的商品信息
*/
private static void findAllGoods() throws IOException {
System.out.println("商品名称" + " " + "商品价格");
FileInputStream fis = new FileInputStream("goods.txt");
int len;
byte[] bs = new byte[1024];
while ((len = fis.read(bs)) != -1){
System.out.println(new String(bs,0,len));
}
}
// 添加图书信息,使用文件存储商品信息
private static void addGoods() throws IOException {
System.out.println("请输入商品名称:");
Scanner sc = new Scanner(System.in);
String goodsName = sc.next();
System.out.println("请输入商品的价格:");
int goodsPrice = sc.nextInt();
Goods goods = new Goods(goodsName, goodsPrice);
storeGoodsInfoToFile(goods);
System.out.println("商品添加成功");
}
/**
* 商品数据保存到文件中
*
* @param goods
*/
private static void storeGoodsInfoToFile(Goods goods) throws IOException {
// 向文件中写数据
FileOutputStream fos = new FileOutputStream("goods.txt");
String s = goods.getName()+" "+goods.getPrice();
fos.write(s.getBytes(),0,s.length());
fos.write("\r\n".getBytes());
}
// 登录操作
private static boolean login(ArrayList<User> users) {
System.out.println("请输入用户名:");
Scanner sc = new Scanner(System.in);
String userName = sc.next();
boolean b = isUsed(users, userName);
if(b){
String ps = null;
for (int i = 0; i < users.size(); i++) {
if(userName.equals(users.get(i).getName())){
ps = users.get(i).getPassword();
}
}
System.out.println("请输入密码:");
String password = sc.next();
if(password.equals(ps)){
return true;
}else {
return false;
}
}else {
return false;
}
}
// 注册
private static void register(ArrayList<User> users) {
System.out.println("请输入用户名");
Scanner sc = new Scanner(System.in);
String username = sc.next();
// for (int i = 0; i < users.size(); i++) {
// if (username == users.get(i).getName()) {
// System.out.println(“该用户名已经存在,请重新输入…”);
// register(users);
// }
// }
boolean b = isUsed(users, username);
if(b){
System.out.println(“该用户名已经存在,请重新输入…”);
}else {
System.out.println(“请输入密码:”);
String password = sc.next();
User user = new User(username, password);
users.add(user);
}
}
//判断用户名是否存在
private static boolean isUsed(ArrayList<User> users, String userName) {
boolean flag = false;
for (int i = 0; i < users.size(); i++) {
if(userName.equals(users.get(i).getName())){
flag = true;
}
}
return flag;
}
}