- 超市管理系统v2.0
- 比起v1.0增加了 用户注册登录的功能(超市管理系统v1.0)
- 并且使用ArrayList改进了代码,减少了代码冗杂。(ArrayList类)
- 下面的v2.0的大概思路。
-
思路流程图:
-
登录
-
注册
-
注册流程:
- 明确用户信息?
- 设计类--->User
- 用户名
- 密码
- 验证码:作业?
- 要求用户输入个人信息:
- 用户名
- 密码
- 确认密码(防止用户输入错误)
- 需要判断两次密码是否一致,不一致则需要重新输出,一致则需要组装成一个用户对象。
-
超市类代码:
package com.zhou.supermarket1;
import java.util.ArrayList;
import java.util.Scanner;
public class SuperMarket1 {
private ArrayList<Goods1> goodslist = new ArrayList<>();
private String name;
private ArrayList<User1> users = new ArrayList<User1>();
//记录当前用户是否登录
private User1 currentUser;
public SuperMarket1(String name) {
ArrayList<Goods1> goodslist = new ArrayList<>();
ArrayList<User1> users = new ArrayList<User1>();
this.name = name;
initialization();
}
public User1 getCurrentUser() {
return currentUser;
}
//菜单
public void menu() {
System.out.println("==================== 欢迎光临【" + name + "超市】 ====================");
System.out.print("1:商品列表 ");
System.out.print("2:添加商品 ");
System.out.print("3:删除商品 ");
System.out.println("4:修改商品 ");
System.out.print("5:用户注册 ");
System.out.print("6:用户登录 ");
System.out.println("7:查看用户信息");
}
//初始化三个商品信息到容器中
public void initialization() {
Goods1 g1 = new Goods1();
g1.setCode("9527");
g1.setName("苹果");
g1.setPrice(9.9);
Goods1 g2 = new Goods1();
g2.setCode("9528");
g2.setName("柠檬");
g2.setPrice(5.0);
Goods1 g3 = new Goods1();
g3.setCode("9529");
g3.setName("蓝莓");
g3.setPrice(9.2);
goodslist.add(g1);
goodslist.add(g2);
goodslist.add(g3);
}
//展示商品
public void showGoods() {
System.out.println("货物清单");
System.out.println("==================== 商品库存清单 ====================");
System.out.println("商品编号 商品名称 商品单价");
for (int i = 0;i<goodslist.size();i++) {
Goods1 g = goodslist.get(i);
System.out.println(g.getCode()+" "+g.getName()+" "+g.getPrice());
}
}
//添加商品
public void addGoods() {
System.out.println("==================== 添加商品 ====================");
Scanner sc = new Scanner(System.in);
System.out.println("请输入要添加商品编号:");
String code = sc.next();
System.out.println("请输入要添加商品名称:");
String name = sc.next();
System.out.println("请输入要添加商品价格:");
double price = sc.nextDouble();
goodslist.add(new Goods1(code,name,price));
System.out.println("添加数据成功!!!");
}
//删除商品
public void delGoods() {
System.out.println("==================== 删除商品 ====================");
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的商品编号;");
String code = scanner.next();
for (int i = 0; i < goodslist.size(); i++) {
Goods1 g = goodslist.get(i);
if (code.equals(g.getCode())) {
goodslist.remove(i);
System.out.println("?删除成功");
return;
}
}
System.out.println("输入的编号:" + code + " 不存在,请重新输入!");
}
}
//修改商品
public void updateGoods() {
System.out.println("==================== 修改商品 ====================");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入要修改的商品编号;");
String code = scanner.next();
for (int i = 0; i < goodslist.size(); i++) {
Goods1 g = goodslist.get(i);
if (code.equals(g.getCode())) {
System.out.println("请输入要修改的商品名称:");
String name = scanner.next();
System.out.println("请输入要修改的商品价格:");
double price = scanner.nextDouble();
g.setName(name);
g.setPrice(price);
System.out.println("运气真好,修改成功!!!");
return;
}
}
System.out.println("您修改的商品编号:"+code+" 不存在,请重新输入。");
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//用户注册
public void register() {
System.out.println("==================== 用户注册 ====================");
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.next();
String password = "123456";
while (true) {
System.out.println("请输入密码:");
password = sc.next();
System.out.println("请输入确认密码:");
String password1 = sc.next();
if (!password.equals(password1)) {
System.out.println("您输入的两次密码不一致,请重新输入密码");
continue;
}else {
System.out.println("恭喜你注册成功!!!");
users.add(new User1(username,password));
return;
}
}
}
//用户登录
public void login() {
System.out.println("==================== 用户登录 ====================");
//根据用户输入的用户名和密码 进行匹配
Scanner sc = new Scanner(System.in);
System.out.println("如果你还没有注册账户,是否需要注册? yes/on");
String judege = sc.next();
if (judege.equals("yes")) {
register();
System.out.println("开始登陆:");
}
while (true) {
System.out.println("请输入用户名:");
String username = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
for (int i = 0;i<users.size();i++) {
User1 u = users.get(i);
if (!username.equals(u.getUsername())) {
System.out.println("您输入的用户名有误,请重新输入");
continue;
}else if(!password.equals(u.getPassword())) {
System.out.println("您输入的密码错误,请重新输入");
continue;
}else {
System.out.println("登陆成功!");
currentUser = u;
return;
}
}
}
}
//查看当前系统注册的用户信息
public void showUsers() {
System.out.println("=================== 查看用户信息 ===================");
System.out.println("用户名 密码");
for (int i = 0;i<users.size();i++) {
User1 u = users.get(i);
System.out.println(u.getUsername()+" "+u.getPassword());
}
}
}
- 商品类代码:
package com.zhou.supermarket1;
public class Goods1 {
private String code;
private String name;
private double price;
public Goods1() {
}
public Goods1(String code, String name, double price) {
this.code = code;
this.name = name;
this.price = price;
}
public void setCode(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
- 用户类代码:
package com.zhou.supermarket1;
public class User1 {
//用户信息
private String username;
private String password;
public User1() {
}
public User1(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- 测试类代码:
package com.zhou.supermarket1;
import java.util.Scanner;
public class SuperMarketTest1 {
public static void main(String[] args) {
SuperMarket1 supermarket1 = new SuperMarket1("白昼");
Scanner sc = new Scanner(System.in);
while (true) {
supermarket1.menu();
System.out.print("请输入对应的功能编号:");
int num = sc.nextInt();
if (num == 2 || num == 3 || num == 4 || num == 7) {
if (supermarket1.getCurrentUser()==null) {
System.out.println("当前操作需要登录");
supermarket1.login();
}
}
switch (num) {
case 1:
supermarket1.showGoods();
break;
case 2:
supermarket1.addGoods();
break;
case 3:
supermarket1.delGoods();
break;
case 4:
supermarket1.updateGoods();
break;
case 5:
supermarket1.register();
break;
case 6:
supermarket1.login();
break;
case 7:
supermarket1.showUsers();
break;
default:
System.out.println("输入的数据有误");
break;
}
}
}
}
标签:String,name,管理系统,System,v2.0,超市,supermarket1,public,out
From: https://www.cnblogs.com/xzandjava/p/16612369.html