1 封装用户对象
public class User {
private String userName;
private String userPsw;
private String userTel;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPsw() {
return userPsw;
}
public void setUserPsw(String userPsw) {
this.userPsw = userPsw;
}
public String getUserTel() {
return userTel;
}
public void setUserTel(String userTel) {
this.userTel = userTel;
}
public User(String userName, String userPsw, String userTel) {
super();
this.userName = userName;
this.userPsw = userPsw;
this.userTel = userTel;
}
public void show(){
System.out.println(this.userName+"\t"+this.userPsw+"\t"+this.userTel);
}
}
2 用户数据
public class userData {
public static User[] users = { new User("1", "1", "3"), new User("2", "2", "3") };
/*
* 注册用户(往数组中添加数据)
*/
public void addUser(User user) {
User[] newArray = new User[users.length + 1];
for (int i = 0; i < users.length; i++) {
newArray[i] = users[i];
}
newArray[newArray.length - 1] = user;
users = newArray;
}
/*
* 用户登录 (查询)
*/
public boolean login(String uname,String upass){
boolean flag=false;
for(int i=0;i
3 封装学生
public class Student {
private String id;
private String name;
private int age;
private String sex;
private String address;
private String telePhone;
private int mark;
public Student() {
}
public Student(String id, String name, int age, String sex, String address, String telePhone, int mark) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.address = address;
this.telePhone = telePhone;
this.mark = mark;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelePhone() {
return telePhone;
}
public void setTelePhone(String telePhone) {
this.telePhone = telePhone;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", address=" + address
+ ", telePhone=" + telePhone + ", mark=" + mark + "]";
}
public void show() {
// TODO Auto-generated method stub
System.out.println(this.id + "\t" + this.name + "\t" + this.age + "\t" + this.sex + "\t" + this.address + "\t"
+ this.telePhone + "\t" + this.mark);
}
}
4学生数据
public class StudentData {
//学生数组对象
public static Student[] stus = { new Student("a1", "laowang", 10, "nan", "shanxi", "15019262511", 92),
new Student("a2", "lali", 10, "nan", "shanxi", "15019262511", 92),
new Student("a3", "laosun", 10, "nan", "shanxi", "15019262511", 92) };
/**
* 添加学生
*/
public void addstu(Student stu) {
Student[] newStu = new Student[stus.length+1];
for (int i = 0; i < stus.length; i++) {
newStu[i] = stus[i];
}
newStu[newStu.length - 1] = stu;
stus = newStu;
}
/**
* 查询所有信息
*/
public void queryAllStudent() {
System.out.println("显示所有学生信息");
System.out.println("学好\t姓名\t年龄\t性别\t地址\t电话\t成绩");
for (int i = 0; i < stus.length; i++) {
stus[i].show();
}
}
/**
* 按照姓名查询单条数据
*/
public void queryName(String name) {
int index = -1;
for (int i = 0; i < stus.length; i++) {
if (stus[i].getName().equals(name)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("你输入的姓名不存在");
} else {
System.out.println("查询成功");
stus[index].show();
}
}
/**
* 根据姓名修改用户成绩
*/
public void updateMark(String name, int newMark) {
int index = -1;
for (int i = 0; i < stus.length; i++) {
if (stus[i].getName().equals(name)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("你输入的姓名不存在,请重新输入");
} else {
stus[index].setMark(newMark);
System.out.println("修改成功");
;
}
}
/**
* 根据姓名修改用户年龄和地址
*/
public void update(String name, int newAge, String newAddress) {
int index = -1;
for (int i = 0; i < stus.length; i++) {
if (stus[i].getName().equals(name)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("你输入的姓名不存在,请重新输入");
} else {
stus[index].setAge(newAge);
stus[index].setAddress(newAddress);
}
}
/**
* 删除单条数据
*/
public void delete(String name) {
int index = -1;
if (stus.length > 0) {
Student[] newStudent = new Student[stus.length - 1];
for (int i = 0; i < stus.length; i++) {
if (stus[i].getName().equals(name)) {
index = i;
break;
}
}
int count = 0;
if (index == -1) {
System.out.println("你删除的数据不存在");
} else {
for (int i = 0; i < stus.length; i++) {
if (i != index) {
newStudent[count] = stus[i];
count++;
}
}
stus = newStudent;
System.out.println("删除成功");
}
}
}
}
5界面
public class MainView {
Scanner sc = new Scanner(System.in);
StudentData st = new StudentData();
userData uds = new userData();
/**
* 初始化界面
*/
public void init() {
while (true) {
System.out.println("************************");
System.out.println("| 1.用户登录 ]");
System.out.println("| 2.用户注册 |");
System.out.println("| 3.退出系统 |");
System.out.println("| @@请你选择 |");
System.out.println("************************");
String selNum = sc.next();
if ("1".equals(selNum)) {
// 调用登录界面
loginView();
} else if ("2".equals(selNum)) {
// 调用注册界面
registerView();
} else if ("3".equals(selNum)) {
System.out.println("成功退出系统!!!");
break;
} else {
System.out.println("您输入的数据格式不合格!!!");
}
}
}
/**
* 用户注册
*/
private void registerView() {
// TODO Auto-generated method stub
boolean a = true;
while (a) {
System.out.println("请输入用户名\t密码\t电话");
System.out.println("请输入用户名");
String userName = sc.next();
System.out.println("请输入密码");
String userPsw = sc.next();
System.out.println("请输入电话");
String userTel = sc.next();
User ud = new User(userName, userPsw, userTel);
uds.addUser(ud);
System.out.println("用户注册成功");
while (true) {
System.out.println("是否返回上一个界面\ty返回上一个界面\tn继续注册用户\t按任意键继续");
String yes = sc.next();
if ("n".equals(yes)) {
System.out.println("请继续注册");
break;
// a=true;
} else if ("y".equals(yes)) {
a = false;
break;
} else {
System.out.println("你输入有误");
}
}
}
}
/**
* 用户登陆
*/
private void loginView() {
// TODO Auto-generated method stub
int loginNum = 0;
while (true) {
System.out.println("请输入用户名\t密码");
System.out.println("请输入用户名");
String userName = sc.next();
System.out.println("请输入密码");
String userPsw = sc.next();
// System.out.println("请输入");
if (uds.login(userName, userPsw)) {
loginNum++;
System.out.println("这是你第" + loginNum + "次登录");
successful();
} else {
System.out.println("登陆失败,是否重新登陆");
System.out.print("是否继续y/n:");
String ok = sc.next();
if ("n".equals(ok)) {
break;
}
}
}
}
/**
* 返回上一个界面
*/
public void myReturn() {
System.out.println("按任意键继续");
String yes = sc.next();
if ("y".equals(yes)) {
return;
}
}
/**
* 学生登录界面
*/
private void successful() {
// TODO Auto-generated method stub
while (true) {
System.out.println("************************");
System.out.println("| 1.显示所有学生 ]");
System.out.println("| 2.修改学生信息 |");
System.out.println("| 3.添加学生信息 |");
System.out.println("| 4.删除学生信息 |");
System.out.println("| 5.查询学生信息 |");
System.out.println("| 0.退出到上界面 |");
System.out.println("| 请 你 选 择 |");
System.out.println("************************");
String selNum = sc.next();
if ("1".equals(selNum)) {
// 显示所有学生信息
st.queryAllStudent();
// 是否返回上一页
myReturn();
} else if ("2".equals(selNum)) {
// 修改学生信息界面
updateStudentView();
} else if ("3".equals(selNum)) {
// 添加学生信息
addStudentImg();
myReturn();
} else if ("4".equals(selNum)) {
// 删除学生信息
deleteStudent();
} else if ("0".equals(selNum)) {
break;
//init();
} else if ("5".equals(selNum)) {
queryStudentView();
}
}
}
/**
* 查询数据界面
*/
private void queryStudentView() {
// TODO Auto-generated method stub
while (true) {
System.out.println("| 1.查询所有学生成绩 ]");
System.out.println("| 2.查询某个学生成绩 |");
System.out.println("| 0.返回上一界面 |");
String selNum = sc.next();
if ("1".equals(selNum)) {
st.queryAllStudent();
// 是否继续查询
System.out.println("是否继续删除\t按任意键继续\tn停止修改");
String yes = sc.next();
if ("n".equals(yes)) {
break;
}
} else if ("2".equals(selNum)) {
queryOneSutdent();
} else if ("0".equals(selNum)) {
break;
}
}
}
/**
* 5.2查询某一个学生成绩
*/
private void queryOneSutdent() {
// TODO Auto-generated method stub
while (true) {
System.out.println("请输入要查询学生的姓名");
String name = sc.next();
st.queryName(name);
// 是否继续查询
System.out.println("是否继续删除\t按任意键继续\tn停止修改");
String yes = sc.next();
if ("n".equals(yes)) {
break;
}
}
}
/**
* 4删除学生信息
*/
private void deleteStudent() {
// TODO Auto-generated method stub
while (true) {
System.out.println("请输入要删除学生名称");
String name = sc.next();
st.delete(name);
// 显示删除后的数据
System.out.println("显示删除后的所有数据");
st.queryAllStudent();
// 是否继续修改
System.out.println("是否继续修改学生年龄和地址\t按任意键继续\tn停止修改");
String yes = sc.next();
if ("n".equals(yes)) {
break;
}
}
}
/**
* 2.修改学生信息界面
*/
private void updateStudentView() {
while (true) {
System.out.println("| 1.修改学生成绩 ]");
System.out.println("| 2.修改年龄和地址 |");
System.out.println("| 0.返回上一界面 |");
String selNum = sc.next();
if ("1".equals(selNum)) {
updateStudentMark();
} else if ("2".equals(selNum)) {
updateStudent();
} else if ("0".equals(selNum)) {
break;
}
}
}
/**
* 2.2修改学生年龄和地址
*/
private void updateStudent() {
// TODO Auto-generated method stub
while (true) {
System.out.print("请输入要修改学生的姓名");
String name = sc.next();
System.out.println("请输入年龄");
int age = sc.nextInt();
System.out.println("请输入地址");
String address = sc.next();
st.update(name, age, address);
System.out.println("显示所有信息");
st.queryAllStudent();
// 是否继续修改
System.out.println("是否继续修改学生年龄和地址\t按任意键继续\tn停止修改");
String yes = sc.next();
if ("n".equals(yes)) {
break;
}
}
}
/**
* 2.1修改学生成绩
*/
private void updateStudentMark() {
// TODO Auto-generated method stub
while (true) {
System.out.println("请输入学生名称");
String name = sc.next();
System.out.println("请输入学生成绩");
int mark = sc.nextInt();
st.updateMark(name, mark);
// 显示所有数据
st.queryAllStudent();
// 是否继续修改
System.out.println("是否继续修改学生分数\t按任意键继续\tn停止修改");
String yes = sc.next();
if ("n".equals(yes)) {
break;
}
}
}
/**
* 3添加学生信息
*/
private void addStudentImg() {
// TODO Auto-generated method stub
// 添加学生信息
while (true) {
System.out.println("学号\t姓名\t年龄\t性别\t地址\t电话\t成绩");
System.out.println("");
String id = sc.next();
String name = sc.next();
int age = sc.nextInt();
String sex = sc.next();
String address = sc.next();
String tel = sc.next();
int mark = sc.nextInt();
Student student = new Student(id, name, age, sex, address, tel, mark);
System.out.println("添加成功,显示所有学生数据");
st.addstu(student);
st.queryAllStudent();
System.out.println("是否继续添加\t任意键继续添加\tn停止添加");
String yes = sc.next();
if ("n".equals(yes)) {
break;
}
}
}
}
6测试
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
MainView mv = new MainView();
mv.init();
}
}
注册
登录