控制台编写购物中心会员管理系统
分析需求
要求要有管理员界面和用户界面,管理员还必须能管理用户信息实现增删改。所以,我创建了四个类,分别是测试类、管理员类、会员类、数据类。
其中,数据类为管理员信息与会员信息的存储类,也作为管理员类、会员类的父类。
数据类代码
public class Data {
public static String username;// 登录的用户名
public static String password;// 登录的密码
public static String[] user = { "admin", "张三", "小花", "李四", "王五", "溜溜" };//登录用户名
public static String[] pwd = { "123", "123", "123", "123", "123", "123" };//登录用的密码
public static String[] sex = { "", "男", "女", "男", "男", "女" };//用户性别
public static int[] age = { 0, 18, 20, 19, 21, 22 };//用户年龄
public static String getUsername() {
return username;
}
public static void setUsername(String username) {
Data.username = username;
}
public static String getPassword() {
return password;
}
public static void setPassword(String password) {
Data.password = password;
}
public boolean judge(String username, String password, int i) {//登录时判断用户名与密码
this.username = username;
this.password = password;
if (i == 0 && this.user[0].equals(username)
&& this.pwd[0].equals(password)) {
return true;
} else if (i != 0) {
for (int y = 1; y <= user.length; y++) {
if (this.user[y].equals(username) && this.pwd[y].equals(password)) {
User.i = y;
return true;
}
}
}
return false;
}
public void homePage(int key) {//首页
switch (key) {
case 1:
Admin a = new Admin();
a.login();
break;
case 2:
User s = new User();
s.login();
break;
case 3:
System.out.println("已退出系统");
System.exit(0);
return;
default:
System.out.println("输入错误,请重新输入");
break;
}
}
}
管理员类代码
public class Admin extends Data {
Scanner input = new Scanner(System.in);
public void login() {//管理员登录
for (int i = 0; i < 3; i++) {
System.out.print("请输入用户名:");
String username = input.next();
System.out.print("请输入密码:");
String password = input.next();
if (super.judge(username, password, 0)) {
System.out.println("登陆成功");
AdminHomePage();
} else {
System.out.println("用户名或密码错误,请重试");
if (i == 2) {
System.out.println("对不起,登录已超时!");
System.exit(0);
}
}
}
}
public void AdminHomePage() {//管理员首页
System.out.println("欢迎进入管理员管理界面:");
while (true) {
System.out.println("1.查询会员信息");
System.out.println("2.增加会员信息");
System.out.println("3.删除会员信息");
System.out.println("4.修改会员信息");
System.out.println("5.退出");
System.out.print("请选择管理员业务:");
switch (input.nextInt()) {
case 1:
information();
break;
case 2:
add();
information();
break;
case 3:
delete();
information();
break;
case 4:
modify();
information();
break;
case 5:
Demo01 d = new Demo01();
d.main(user);
break;
default:
System.out.println("选择错误,请重新选择");
break;
}
}
}
public void information() {//查询会员信息
System.out.println("会员信息如下:");
if (user.length > 1) {
System.out.println("编号\t姓名\t密码\t性别\t年龄");
for (int i = 1; i < super.user.length; i++) {
System.out.println(i + "\t" + super.user[i] + "\t"
+ super.pwd[i] + "\t" + super.sex[i] + "\t"
+ super.age[i]);
}
} else {
System.out.println("还没有会员");
}
}
public void add() {//添加会员信息
String username = null;
String password = null;
String sex = null;
int age;
System.out.print("请输入会员名:");
username = input.next();
System.out.print("请输入密码:");
password = input.next();
System.out.print("请输入性别:");
sex = input.next();
System.out.print("请输入年龄:");
age = input.nextInt();
String user1[] = new String[user.length + 1];
String pwd1[] = new String[user.length + 1];
String sex1[] = new String[user.length + 1];
int age1[] = new int[user.length + 1];
for (int i = 0; i < user1.length; i++) {
if (i < user.length) {
user1[i] = user[i];
pwd1[i] = pwd[i];
sex1[i] = super.sex[i];
age1[i] = super.age[i];
} else {
user1[i] = username;
pwd1[i] = password;
sex1[i] = sex;
age1[i] = age;
}
}
user = new String[user1.length];
pwd = new String[user1.length];
super.sex = new String[user1.length];
super.age = new int[user1.length];
for (int i = 0; i < user.length; i++) {
user[i] = user1[i];
pwd[i] = pwd1[i];
super.sex[i] = sex1[i];
super.age[i] = age1[i];
}
}
public void delete() {//删除会员信息
String user1[] = new String[user.length - 1];
String pwd1[] = new String[user.length - 1];
String sex1[] = new String[user.length - 1];
int age1[] = new int[user.length - 1];
System.out.print("请输入删除会员的编号:");
int number = input.nextInt();// 要删除的编号
level:
for (int i = 0; i < user.length - 1; i++) {
if (number > 0 && number < user.length) {
if(i < number) {
user1[i] = user[i];
pwd1[i] = pwd[i];
sex1[i] = super.sex[i];
age1[i] = super.age[i];
}else if(i >= number) {
// System.out.println(user.length);
// System.out.println(user1.length);
// System.out.println(i);
user1[i] = user[i + 1];
pwd1[i] = pwd[i + 1];
sex1[i] = super.sex[i + 1];
age1[i] = super.age[i + 1];
}
} else {
System.out.print("你输入的编号不存在,请输入正确的编号:");
number = input.nextInt();
continue level;
}
}
user = new String[user1.length];
pwd = new String[user1.length];
super.sex = new String[user1.length];
super.age = new int[user1.length];
for (int i = 0; i < user.length; i++) {
user[i] = user1[i];
pwd[i] = pwd1[i];
super.sex[i] = sex1[i];
super.age[i] = age1[i];
}
}
public void modify(){//修改会员信息
System.out.print("请输入要修改的会员编号:");
int i = input.nextInt();
System.out.println("您要修改的信息如下:");
System.out.print("用户名:");
user[i] = input.next();
System.out.print("密码:");
pwd[i] = input.next();
System.out.print("性别:");
super.sex[i] = input.next();
System.out.print("年龄:");
super.age[i] = input.nextInt();
}
}
用户类代码
public class User extends Data {
public static int i = 0;//通过它传递用户编号
Scanner input = new Scanner(System.in);
public void login() {//登录
for (int i = 0; i < 3; i++) {
System.out.print("请输入用户名:");
String username = input.next();
System.out.print("请输入密码:");
String password = input.next();
if (super.judge(username, password, 1)) {
System.out.println("登陆成功");
AdminHomePage();
} else {
System.out.println("用户名或密码错误,请重试");
}
}
}
public void AdminHomePage() {//用户首页
System.out.println("欢迎进入管理员管理界面:");
while (true) {
System.out.println("1.查看个人信息");
System.out.println("2.客户真情回馈");
System.out.println("3.退出");
System.out.print("请选择会员业务:");
switch (input.nextInt()) {
case 1:
information();
break;
case 2:
lucky();
break;
case 3:
Demo01 d = new Demo01();
d.main(user);
break;
default:
System.out.println("选择错误,请重新选择");
break;
}
}
}
public void information() {//查看用户信息
System.out.println("您的个人信息如下:");
System.out.println("用户名" + user[i]);
System.out.println("密码" + pwd[i]);
System.out.println("性别" + sex[i]);
System.out.println("年龄" + age[i]);
}
public void lucky() {//抽奖
System.out.println("为了答谢各会员对本购物中心的关爱,现在进行抽奖");
System.out.println("活动按 1 开始抽奖,按 0 退出");
int x = -1;
int cs = 0;//抽奖次数,防止二次抽奖
while (x != 0) {
System.out.print("请选择");
x = input.nextInt();
if (x == 1 && cs == 0) {
Random r = new Random();
int a = r.nextInt(4) + 1;
if (a == 1) {
System.out
.println("恭喜您中了一等奖,请到柜台领一张价值九万九千九百九十九亿九千九百九十九万九千九百九十九的支票");
} else if (a == 2) {
System.out.println("恭喜您中了二等奖,请到柜台领一台电脑");
} else if (a == 3) {
System.out.println("恭喜您中了三等奖,请到柜台领一台冰箱");
} else if (a == 4) {
System.out.println("恭喜您中了特等奖,一包价值九百九十九的方便面");
}
cs++;
}else if(x == 1 && cs != 0){
System.out.println("您已完成抽奖,请选择 0 退出");
}
}
}
}
测试类
public class Demo01 {
public static void main(String[] args) {
System.out.println("-----欢迎进入我行我素购物管理系统-----");
Data d = new Data();
Scanner input = new Scanner(System.in);
while(true){
System.out.println("1.管理员登录");
System.out.println("2.会员登录");
System.out.println("3.退出");
System.out.println("请选择登录 菜单:");
d.homePage(input.nextInt());
}
}
}