完整代码地址
1.运行效果图
2.主要代码
2.1.连接数据库
package com.my.homework.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtils {
public static Connection getConnection() throws Exception {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");//全路径
//2.获取数据库连接
String url="jdbc:mysql://localhost:3306/library-management?useUnicode=true&useSSL=false&characterEncoding=gbk&serverTimezone=Asia/Shanghai" ;
Connection c = DriverManager.getConnection(url,"root","root");
//3.返回给调用者
return c;
}
}
2.2.实体类
package com.my.homework.entity;
public class Book {
private int id;
private String name;
private String author;
private String isBorrowed;
private String type;
private String price;
public Book() {
}
public Book(String name, String author, String type, String price) {
this.name = name;
this.author = author;
this.type = type;
this.price = price;
}
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 String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getIsBorrowed() {
return isBorrowed;
}
public void setIsBorrowed(String isBorrowed) {
this.isBorrowed = isBorrowed;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String toString() {
//重写toString方法
return "Book{" +
"书名:'" + name + '\'' +
", 作者:'" + author + '\'' +
", 类型:'" + type + '\'' +
", 价格:'" + price + '\'' +
(","+isBorrowed) +
'}';
}
}
package com.my.homework.entity;
import com.my.homework.operation.*;
import java.util.Scanner;
public class ManagerUser extends User{
public ManagerUser(String name) {
super(name);
this.ioPerations = new IOperation[]{ new Exit(),
new FindBook(),
new ShowBook(),
new DeleteBook(),
new AddBook()
};
}
@Override
public int menu() {
System.out.println("***********************");
System.out.println(this.name+",欢迎来到管理员菜单!");
System.out.println("请输入你的操作:0.退出系统" +
" 1.查找图书 " +
" 2.显示图书 " +
" 3.删除图书 " +
" 4.添加图书 ");
System.out.println("***********************");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
package com.my.homework.entity;
import com.my.homework.operation.*;
import java.util.Scanner;
public class OrdinaryUser extends User{
public OrdinaryUser(String name) {
super(name);
this.ioPerations=new IOperation[]{ new Exit(),
new FindBook(),
new BorrowBook(),
new ReturnBook()
};
}
@Override
public int menu() {
System.out.println("***********************");
System.out.println(this.name+",欢迎来到普通用户菜单!");
System.out.println("请输入你的操作:0.退出系统" +
" 1.查找图书 " +
" 2.借阅图书 " +
" 3.归还图书 ");
System.out.println("***********************");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
package com.my.homework.entity;
import com.my.homework.operation.IOperation;
abstract public class User {
protected String name;
protected IOperation[] ioPerations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doWork(int choice) throws Exception {
this.ioPerations[choice].work();
}
}
2.3.操作类
package com.my.homework.operation;
import com.my.homework.dao.BookDao;
import com.my.homework.entity.Book;
import java.util.Scanner;
public class AddBook implements IOperation{
@Override
public void work() {
Scanner scanner = new Scanner(System.in);
System.out.println("添加图书!");
System.out.println("请输入书名:");
String name = scanner.nextLine();
BookDao bookDao=new BookDao();
Book book=bookDao.getBookById(name);
if(book!=null){
if(book.getName().equals(name)){
System.out.println("书架中已有此书,不可添加!");
return;
}
}
System.out.println("请输入作者:");
String author = scanner.nextLine();
System.out.println("请输入类型:");
String type = scanner.nextLine();
System.out.println("请输入价格:");
String price = scanner.nextLine();
Book book1 = new Book(name,author,type,price);
bookDao.addBook(book1);
System.out.println("添加成功!");
System.out.println(book1);
}
}
package com.my.homework.operation;
import com.my.homework.dao.BookDao;
import com.my.homework.entity.Book;
import java.util.Scanner;
public class BorrowBook implements IOperation {
@Override
public void work() {
System.out.println("借阅图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
BookDao bookDao=new BookDao();
Book book=bookDao.getBookById(name);
if(book!=null){
if(book.getIsBorrowed().equals("未被借出")){
bookDao.updateBookStatus(name,"借阅中");
return;
}
}
System.out.println("书架中没有这本书,借阅失败!");
}
}
package com.my.homework.operation;
import com.my.homework.dao.BookDao;
import com.my.homework.entity.Book;
import com.my.homework.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.Statement;
import java.util.List;
import java.util.Scanner;
public class DeleteBook implements IOperation{
@Override
public void work() throws Exception {
System.out.println("删除图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
BookDao bookDao=new BookDao();
bookDao.deleteBook(name);
System.out.println("删除成功!");
}
}
package com.my.homework.operation;
public class Exit implements IOperation{
@Override
public void work() {
System.exit(0);
}
}
package com.my.homework.operation;
import com.my.homework.dao.BookDao;
import com.my.homework.entity.Book;
import java.util.Scanner;
public class FindBook implements IOperation{
@Override
public void work() {
System.out.println("查找图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
BookDao bookDao=new BookDao();
Book book=bookDao.getBookById(name);
if(book!=null){
if(book.getName().equals(name)){
System.out.println("找到了这本书!");
System.out.println(book);//显示该书
return;//找到了就终止该功能,否则没找到
}
}
System.out.println("书架中没有这本书!");
}
}
package com.my.homework.operation;
import com.my.homework.entity.Book;
import java.util.List;
public interface IOperation {
void work() throws Exception;
}
package com.my.homework.operation;
import com.my.homework.dao.BookDao;
import com.my.homework.entity.Book;
import java.util.Scanner;
public class ReturnBook implements IOperation{
@Override
public void work() {
System.out.println("归还图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
BookDao bookDao=new BookDao();
bookDao.updateBookStatus(name,"未被借出");
System.out.println("已归还!");
}
}
package com.my.homework.operation;
import com.my.homework.dao.BookDao;
import com.my.homework.entity.Book;
import java.util.List;
public class ShowBook implements IOperation{
@Override
public void work() {
System.out.println("显示图书!");
BookDao bookDao=new BookDao();
List<Book> booklist= bookDao.getAllBooks();
for (int i = 0; i < booklist.size(); i++) {
Book book = booklist.get(i);
System.out.println(book);
}
}
}
package com.my.homework;// Main类的完整实现
import com.my.homework.entity.ManagerUser;
import com.my.homework.entity.OrdinaryUser;
import com.my.homework.entity.User;
import java.util.Scanner;
public class Main {
public static User login() {
System.out.println("请输入你的姓名");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入你的身份, 1:管理员 2:普通用户 ->");
int choice = scanner.nextInt();
if (choice == 1) {
return new ManagerUser(name);
}
else {
return new OrdinaryUser(name);
}
}
public static void main(String[] args) throws Exception {
User user = login();
while (true) {
int choice = user.menu();
user.doWork(choice);
}
}
}