Student.java
package com.briup.login;
public class Student {
private int id;
private String name;
private int age;
private double source;
public Student() {
super();
}
public Student(int id, String name, int age, double source) {
super();
this.id = id;
this.name = name;
this.age = age;
this.source = source;
}
public int getId() {
return id;
}
public void setId(int 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 double getSource() {
return source;
}
public void setSource(double source) {
this.source = source;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", source=" + source + "]";
}
}
package com.briup.login;
public class Customer {
private String username;
private String password;
public Customer() {
super();
}
public Customer(String username, String password) {
super();
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;
}
@Override
public String toString() {
return "Customer [username=" + username + ", password=" + password + "]";
}
}
package com.briup.login;
import java.util.HashSet;
import java.util.Scanner;
import com.briup.login2.Customer;
public class Login {
public static HashSet set = new HashSet<>();;
public Login() {
super();
}
// 判断登录是否成功
public static void login(String username, String password) {
if (set.size() == 0) {
System.out.println("*登录失败,可能没有注册,请重试!*");
return;
}
for (Customer cust : set) {
if (cust.getUsername().equals(username)) {
if (cust.getPassword().equals(password)) {
System.out.println("*登录成功!*");
StudentMessages sm = new StudentMessages();
sm.index();
} else {
System.out.println("*登录失败!密码错误!*");
break;
}
} else {
System.out.println("*登录失败,用户名不存在!*");
break;
}
}
}
// 判断注册是否成功
public static boolean register(String username, String password) {
if (set.size() == 0) {
System.out.println("*注册成功!!*");
}
for (Customer cust : set) {
if (cust.getUsername().equals(username)) {
System.out.println("*用户名已存在!!*");
return false;
} else {
System.out.println("*注册成功!!*");
return true;
}
}
return true;
}
// 注册主界面
public void loginindex() {
while (true) {
System.out.println("******主界面******");
System.out.println("* 1.登录 *");
System.out.println("* 2.注册 *");
System.out.println("* 0.退出 *");
System.out.println("*请选择:");
@SuppressWarnings("resource")
int num = new Scanner(System.in).nextInt();
while (true) {
if (num == 1) {
System.out.println("*请输入用户名:");
@SuppressWarnings("resource")
String name = new Scanner(System.in).nextLine();
System.out.println("*请输入密码:");
@SuppressWarnings("resource")
String password = new Scanner(System.in).nextLine();
login(name, password);
break;
} else if (num == 2) {
System.out.println("*请输入用户名:");
@SuppressWarnings("resource")
String name = new Scanner(System.in).nextLine();
System.out.println("请输入密码:");
@SuppressWarnings("resource")
String password = new Scanner(System.in).nextLine();
if (register(name, password) == false) {
break;
}
Customer cust = new Customer(name, password);
set.add(cust);
break;
} else if (num == 0) {
System.out.println("***系统已退出***");
System.exit(0);
break;
} else {
System.out.println("输入的格式不正确,请重试!!");
}
}
}
}
}
package com.briup.login;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import com.briup.login2.Student;
public class StudentMessages {
public static ArrayList list = new ArrayList<>();
public StudentMessages() {
super();
}
// 查询所有学生信息
public void findAll() {
if (list.size() == 0) {
System.out.println("*没有任何学生的信息!!*");
return;
} else {
Iterator iter = list.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
// 添加学生,判断学号是否存在
public boolean add(int id) {
if (list.size() == 0) {
return true;
}
for (Student stu : list) {
if (stu.getId() == id) {
System.out.println("*学号已存在*");
return false;
}
}
return true;
}
// 修改学生信息,判断学号是否存在
public boolean update(int id, String name, int age, double source) {
if (list.size() == 0) {
return false;
}
for (Student stu : list) {
if (stu.getId() == id) {
stu.setName(name);
stu.setAge(age);
stu.setSource(source);
list.set(id - 1, stu);
return true;
}
}
if (true) {
System.out.println("系统中没有该学生的信息!!");
}
return false;
}
// 删除指定id学生信息
public boolean delete(int id) {
if (list.size() == 0) {
return false;
}
for (Student stu : list) {
if (stu.getId() == id) {
list.remove(id - 1);
System.out.println("删除成功");
return true;
}
}
System.out.println("此学号的学生不存在!!");
return true;
}
// 设计学生通讯录管理系统主界面
public void index() {
StudentMessages sm = new StudentMessages();
s: while (true) {
System.out.println("*********学生信息管理系统*********");
System.out.println("* 1.查询学生信息 *");
System.out.println("* 2.增加学生信息 *");
System.out.println("* 3.修改学生信息 *");
System.out.println("* 4.删除学生信息 *");
System.out.println("* 0.退出管理系统 *");
System.out.println("*请输入:");
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
s2: while (true) {
if (num == 1) {
sm.findAll();
break;
} else if (num == 2) {
System.out.println("*请输入学生的id:");
@SuppressWarnings("resource")
int id = new Scanner(System.in).nextInt();
if (sm.add(id) == false) {
break s2;
}
System.out.println("*请输入学生的姓名:");
@SuppressWarnings("resource")
String name = new Scanner(System.in).nextLine();
System.out.println("*请输入学生的年龄:");
@SuppressWarnings("resource")
int age = new Scanner(System.in).nextInt();
System.out.println("*请输入学生的成绩:");
@SuppressWarnings("resource")
double source = new Scanner(System.in).nextDouble();
Student stu = new Student(id, name, age, source);
list.add(stu);
System.out.println("添加成功!!");
break;
} else if (num == 3) {
System.out.println("*请输入要修改学生的id:");
@SuppressWarnings("resource")
int id = new Scanner(System.in).nextInt();
System.out.println("*请输入学生的姓名:");
@SuppressWarnings("resource")
String name = new Scanner(System.in).nextLine();
System.out.println("*请输入学生的年龄:");
@SuppressWarnings("resource")
int age = new Scanner(System.in).nextInt();
System.out.println("*请输入学生的成绩:");
@SuppressWarnings("resource")
double source = new Scanner(System.in).nextDouble();
if (sm.update(id, name, age, source) == false) {
break s2;
}
System.out.println("修改成功!!");
break;
} else if (num == 4) {
System.out.println("*请输入学生的id:");
@SuppressWarnings("resource")
int id = new Scanner(System.in).nextInt();
if (sm.delete(id) == false) {
break s2;
}
break;
} else if (num == 0) {
System.out.println("****系统已退出!****");
System.exit(0);
break s;
} else {
System.out.println("输入错误,请重新输入!!");
}
}
}
}
}
package com.briup.login;
public class LoginTest {
public static void main(String[] args) {
Login l = new Login();
l.loginindex();
}
}
****注意,类名和包名必须相同。
在运行的时候,必须先注册信息,成功后再登录,登录成功之后,才可以进行信息的查询、增加、修改、删除、退出。