代碼位址:https://download.csdn.net/download/xlkyujhc/11834485
實作了管理者與學生的登入與注冊,管理者的功能,學生的功能。
管理者的功能有 1.顯示圖書 2.新增圖書 3.删除圖書 4.修改密碼 5.查詢已被借閱圖書 6.查詢已借閱圖書數量 7.退出系統
學生的功能有 1.查詢圖書 2.借閱圖書 3.歸還圖書 4.修改密碼 5.退出系統
流程圖:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2csoGatFWMsNTYzh2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49DctJmL1cTOyEDMyAjM3ADMxkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.bmp)
使用MySQL資料庫存儲資料,建立四張表:book表,student表,manager表,borrowinfo表
以下是登入與注冊的代碼,其他代碼在資源連接配接中。
學生S ,管理者M,登入L,注冊R
注冊完成後會跳到登入
package system;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import java.util.Scanner;
import model.Manager;
import model.Student;
//登入與注冊
public class LoginAndRegister {
//進入系統
public void initial() throws ClassNotFoundException, SQLException, ParseException{
System.out.println("學生S/管理者M?");
Scanner sc=new Scanner(System.in);
String sm=sc.next();
//學生
if ("S".equals(sm)) {
System.out.println("登入L/注冊R ?");
String lr=sc.next();
if ("L".equals(lr)) {
studentLogin();
}
//學生注冊
if ("R".equals(lr)) {
studentRegister();
}
}
//管理者
if ("M".equals(sm)) {
System.out.println("登入L/注冊R ?");
String lr=sc.next();
//管理者登入
if ("L".equals(lr)) {
managerLogin();
}
//管理者注冊
if ("R".equals(lr)) {
managerRegister();
}
}
}
//學生登入
public void studentLogin() throws ClassNotFoundException, SQLException, ParseException{
System.out.println("請進行登入");
System.out.println("請輸入賬号:");
Scanner sc=new Scanner(System.in);
String name=sc.next();
boolean n=true;//姓名
boolean p=true;//密碼
StudentDao studentDao=new StudentDao();
List<Student> list=studentDao.selectStudent();
for (Student student : list) {
if (name.equals(student.getStudentName())) {
System.out.println("賬号存在,請輸入密碼:");
n=false;
}
}
if (n) {
System.out.println("賬号不存在,請重新登入");
}else{
String password=sc.next();
for (Student student : list) {
if ((name.equals(student.getStudentName()))&&(password.equals(student.getPassword()))) {
System.out.println("登入成功");
StudentChoose studentChoose=new StudentChoose();
studentChoose.choose();
p=false;
}
}
}
if (p) {
System.out.println("密碼錯誤,登入失敗");
}
}
//學生注冊
public void studentRegister() throws ClassNotFoundException, SQLException, ParseException{
Scanner sc=new Scanner(System.in);
System.out.println("請輸入你的新增賬號:");
String name=sc.next();
StudentDao studentDao=new StudentDao();
List<Student> list=studentDao.selectStudent();
for (Student student : list) {
if (name.equals(student.getStudentName())) {
System.out.println("賬号已被注冊,請重新輸入");
studentRegister();
}else{
continue;
}
}
System.out.println("請輸入登入密碼:");
String password1=sc.next();
System.out.println("請再次輸入登入密碼:");
String password2=sc.next();
if (password1.equals(password2)) {
studentDao.insertStudent(name, password1);
System.out.println("注冊成功,請登入");
studentLogin();
}else{
System.out.println("兩次輸入密碼不比對,請重新注冊:");
studentRegister();
}
}
//管理者登入
public void managerLogin() throws ClassNotFoundException, SQLException, ParseException{
System.out.println("請進行登入");
System.out.println("請輸入賬号:");
Scanner sc=new Scanner(System.in);
String name=sc.next();
boolean n=true;
boolean p=true;
ManagerDao managerDao=new ManagerDao();
List<Manager> list=managerDao.selectManager();
for (Manager manager : list) {
if (name.equals(manager.getManagerName())) {
System.out.println("賬号存在,請輸入密碼:");
n=false;
}
}
if (n) {
System.out.println("賬号不存在,請重新登入");
}else{
String password=sc.next();
for (Manager manager : list) {
if ((name.equals(manager.getManagerName()))&&(password.equals(manager.getPassword()))) {
System.out.println("登入成功");
ManagerChoose managerChoose=new ManagerChoose();
managerChoose.choose();
p=false;
}
}
}
if (p) {
System.out.println("密碼錯誤,登入失敗");
}
}
//管理者注冊
public void managerRegister() throws ClassNotFoundException, SQLException, ParseException{
Scanner sc=new Scanner(System.in);
System.out.println("請輸入你的新增賬號:");
String name=sc.next();
ManagerDao managerDao=new ManagerDao();
List<Manager> list=managerDao.selectManager();
for (Manager manager : list) {
if (name.equals(manager.getManagerName())) {
System.out.println("賬号已被注冊,請重新輸入");
managerRegister();
}else{
continue;
}
}
System.out.println("請輸入登入密碼:");
String password1=sc.next();
System.out.println("請再次輸入登入密碼:");
String password2=sc.next();
if (password1.equals(password2)) {
managerDao.insertManager(name, password1);
System.out.println("注冊成功,請登入");
managerLogin();
}else{
System.out.println("兩次輸入密碼不比對,請重新注冊:");
managerRegister();
}
}
}