AdminUser是管理员类
NormalUser是用户类
AddOperation是增加图书类
BorrowOperation是借书类
DelOperation是删除图书类
ExitOperation是退出类
FindOperation是查找图书类
IOPeration是接口
ReturnOperation是还书类
ShowOperation是显示所有图书类
注意:我的数据库有个cnm数据库,如果你没有的话,需要先创建一个cnm数据库
sql建表语句:
drop table if exists book;
create table book
( id bigint auto_increment,
name varchar(255),
author varchar(255),
price int,
type varchar(255),
isBorrowed varchar(255),
primary key (id)
);
insert book(name,author,price,type,isBorrowed) values('西游记','吴承恩',35,'小说','未借阅');
insert book(name,author,price,type,isBorrowed) values('活着','余华',40,'文学','未借阅');
commit;
select * from book;
Book类
package book;
public class Book {
private String name;//书名
private String author;//作者
private int price;//价格
private String type;//类型
private boolean isBorrowed;//是否被借出,初始值是false,在构造方法中不用写
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 int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"name:" + name +
", author:" + author +
", price:" + price +
", type=:" + type +
", isBorrowed:" +(isBorrowed==true?" 已借出":" 未借出") +
'}';
}
}
BookList类
package book;
public class BookList {
private static final int DEFAULT_SIZE=10;//该常量定义一个书架有多少本书
private Book[] books=new Book[DEFAULT_SIZE];
private int usedSize;//记录下当前book数组中有几本书
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
User类
package user;
import book.BookList;
import opera.IOPeration;
public abstract class User {//抽象类
protected String name;//名字.这边的protect代表的是名字的权限。如果是private,它只能在同一个包的同一类使用。就不能让AdminUser类继承了。写public的话
//权限太大了,不是很好。
protected IOPeration[] ioPerations;
public User(String name) {//构造方法
this.name = name;
}
public abstract int menu();//抽象方法,打印菜单,因为有了choice返回值int类型,所以void改成int
public void doWork(int choice, BookList bookList){//通过选择的操作,去选择执行数组下的哪个操作
this.ioPerations[choice].work(bookList);
}
}
AdminUser类
package user;
import opera.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.ioPerations=new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
System.out.println("欢迎管理员:"+name);
}
@Override
public int menu() {//因为返回值choice是int类型的
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 choice=scanner.nextInt();
return choice;
}
}
NormalUser类
package user;
import opera.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.ioPerations = new IOPeration[]{//引用,这边用super也可以,因为这里没有同名的,不需要做区分。用this最好
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
System.out.println("欢迎用户:"+name);
}
@Override
public int menu() {
System.out.println("_________________");
System.out.println("1.查找图书!");
System.out.println("2.借阅图书!");
System.out.println("3.归还图书!");
System.out.println("0.退出系统!");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
IOPeration接口
package opera;
import book.BookList;
public interface IOPeration {//创建接口
void work(BookList bookList);//抽象方法
//功能主要是针对图书的,也就是针对书架。
}
AddOperation类
package opera;
import book.Book;
import book.BookList;
import java.sql.*;
import java.util.Scanner;
public class AddOperation implements IOPeration {
public void work(BookList bookList) {
System.out.println("新增图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入新增图书名字:");
String name = scanner.nextLine();
System.out.println("请输入新增图书作者:");
String author = scanner.nextLine();
System.out.println("请输入价格");
int price = scanner.nextInt();
//吸收回车符号
scanner.nextLine();
System.out.println("请输入图书类型:");
String type = scanner.nextLine();
//添加到数据库
//定义3个变量
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");
//3.获取数据库操作对象(执行sql语句的对象)
stmt = conn.createStatement();
//4.执行sql语句
String sql = "select * from book where name='"+name+"'";
rs = stmt.executeQuery(sql);
if(!rs.next()) {
String s = "insert into book avalues('"+name+"','"+author+"','"+price+"','"+type+"','未借阅')";
int x= stmt.executeUpdate(s);
if(x>0)
System.out.println("新增图书成功!");
else
System.out.println("新增图书失败");
}
else{
System.out.println("已经有这本书了");
}
}catch (Exception e) {
e.printStackTrace();
} finally {
//6.释放资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
DelOperation类
package opera;
import book.Book;
import book.BookList;
import java.sql.*;
import java.util.Scanner;
public class DelOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
System.out.println("请输入要删除图书的名称");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
//添加到数据库
//定义3个变量
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");
//3.获取数据库操作对象(执行sql语句的对象)
stmt = conn.createStatement();
//4.执行sql语句
String sql = "select * from book where name='"+name+"'";
rs = stmt.executeQuery(sql);
if(rs.next()) {
String s = "delete from book where name='"+name+"'";
int x = stmt.executeUpdate(s);
if(x>0)
System.out.println("删除成功!");
else
System.out.println("删除失败!");
}
else{
System.out.println("没有这本书");
}
}catch (Exception e) {
e.printStackTrace();
} finally {
//6.释放资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
FindOperation类
package opera;
import book.Book;
import book.BookList;
import java.sql.*;
import java.util.Scanner;
public class FindOperation implements IOPeration{//继承
@Override
public void work(BookList bookList) {//重写IOPeration类中的work方法
System.out.println("查找图书!");
System.out.println("请输入要查找的图书名字");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
//添加到数据库
//定义3个变量
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");
//3.获取数据库操作对象(执行sql语句的对象)
stmt = conn.createStatement();
//4.执行sql语句
String sql = "select * from book where name='"+name+"'";
rs = stmt.executeQuery(sql);
if(rs.next()) {
while (true) {
String s1 = rs.getString("name");
String s2 = rs.getString("author");
int s3 = rs.getInt("price");
String s4 = rs.getString("type");
String s5 = rs.getString("isBorrowed");
System.out.print("name: " + s1);
System.out.print(",author:" + s2);
System.out.print(",price:" + s3);
System.out.print(",type:" + s4);
System.out.print(",isBorrowed:" + s5);
System.out.println();
if(!rs.next())break;
}
}
else{
System.out.println("没有这本书");
}
}catch (Exception e) {
e.printStackTrace();
} finally {
//6.释放资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
ShowOperation类
package opera;
import book.BookList;
import java.sql.*;
public class ShowOperation implements IOPeration{
@Override
public void work(BookList bookList) {
//添加到数据库
//定义3个变量
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");
//3.获取数据库操作对象(执行sql语句的对象)
stmt = conn.createStatement();
//4.执行sql语句
String sql = "select * from book ";
rs = stmt.executeQuery(sql);
if(rs.next()) {
while (true) {
String s1 = rs.getString("name");
String s2 = rs.getString("author");
int s3 = rs.getInt("price");
String s4 = rs.getString("type");
String s5 = rs.getString("isBorrowed");
System.out.print("name: " + s1);
System.out.print(",author:" + s2);
System.out.print(",price:" + s3);
System.out.print(",type:" + s4);
System.out.print(",isBorrowed:" + s5);
System.out.println();
if(!rs.next())break;
}
}
else{
System.out.println("没有这本书");
}
}catch (Exception e) {
e.printStackTrace();
} finally {
//6.释放资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
BorrowOperation类
package opera;
import book.Book;
import book.BookList;
import java.sql.*;
import java.util.Scanner;
public class BorrowOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
System.out.println("请输入要借阅的图书名字:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
//添加到数据库
//定义3个变量
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");
//3.获取数据库操作对象(执行sql语句的对象)
stmt = conn.createStatement();
//4.执行sql语句
String sql = "select * from book where name='"+name+"'";
rs = stmt.executeQuery(sql);
if(rs.next()) {
String s1 = "select isBorrowed from book where name='"+name+"'";
rs = stmt.executeQuery(s1);
if(rs.next()){
String str = rs.getString("isBorrowed");
if(str.equals("未借阅")) {
String s2 = "update book set isBorrowed='已借阅' where name='" + name + "'";
int x = stmt.executeUpdate(s2);
if (x > 0)
System.out.println("借阅成功!");
else
System.out.println("借阅失败");
}
}
}
else{
System.out.println("没有这本书");
}
}catch (Exception e) {
e.printStackTrace();
} finally {
//6.释放资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
ReturnOperation类
package opera;
import book.Book;
import book.BookList;
import java.sql.*;
import java.util.Scanner;
public class ReturnOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
System.out.println("请输入要归还的图书名字:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
//添加到数据库
//定义3个变量
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");
//3.获取数据库操作对象(执行sql语句的对象)
stmt = conn.createStatement();
//4.执行sql语句
String sql = "select * from book where name='"+name+"'";
rs = stmt.executeQuery(sql);
if(rs.next()) {
String s1 = "select isBorrowed from book where name='"+name+"'";
rs = stmt.executeQuery(s1);
if(rs.next()){
String str = rs.getString("isBorrowed");
if(str.equals("已借阅")) {
String s2 = "update book set isBorrowed='未借阅' where name='" + name + "'";
int x = stmt.executeUpdate(s2);
if (x > 0)
System.out.println("归还成功!");
else
System.out.println("归还失败");
}
}
}
else{
System.out.println("没有这本书");
}
}catch (Exception e) {
e.printStackTrace();
} finally {
//6.释放资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
ExitOperation类
package opera;
import book.BookList;
public class ExitOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);//退出系统。0代表正常退出
}
}
Main类
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.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->管理员 0->普通用户");
int choice = scanner.nextInt();
if (choice == 1) {
//说明是管理员
//由于有返回值,所以我们的方法返回值就不能写void了。但是我们也无法确定返回值是什么,可能是管理员,可能是用户。所以,用向上转型,写User.
return new AdminUser(name);//返回实例化一个管理员对象
} else {
return new NormalUser(name);//返回实例化一个用户对象
}
}
public static void main(String[] args) {
User user = login();//执行上面的login方法
BookList bookList = new BookList();
while (true) {
int choice = user.menu();//实现打印菜单
user.doWork(choice, bookList);
}
}
}