import Book.Bookshelf;import User.Adminuser;import User.User;import User.Ornuser;import java.util.Scanner;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 19:54
*/
public class Main {
public static User func(){
System.out.println("请输入你的姓名:");
Scanner scanner = new Scanner(System.in);
String nema = scanner.nextLine();
System.out.println("请选择你的身份: 1 -》管理员 2 -》普通用户");
int i = scanner.nextInt();
if(i ==1){return new Adminuser(nema);}else{return new Ornuser(nema);}}
public static void main(String[] args){
Bookshelf bookshelf = new Bookshelf();
User user = func();
while(true){
int i = user.menu();
user.dowork(i, bookshelf);}}}
二、用户包
2.1、用户类
package User;import Book.Bookshelf;import Function.Function;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 20:00
*/
public abstract class User {
protected String name;
protected Function[] array;
public User(String name){
this.name = name;}
public abstract void dowork(int pot , Bookshelf bookshelf);
public abstract int menu();}
2.2、普通用户类
package User;import Book.Bookshelf;import Function.*;import Function.Function;import java.util.Scanner;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 20:00
*/
public class Ornuser extends User{
public Ornuser(String name){
super(name);
array = new Function[]{
new outfunc(),
new seekfunc() ,
new Borrowfunc(),
new Returnfunc(),
};}
@Override
public void dowork(int pot, Bookshelf bookshelf){
array[pot].work(bookshelf);}
@Override
public int menu(){
System.out.println("欢迎普通用户" + name + "来到图书管理系统");
System.out.println("**************************");
System.out.println("1、查找图书");
System.out.println("2、借阅图书");
System.out.println("3、归还图书");
System.out.println("0、退出系统");
System.out.println("**************************");
System.out.println("请选择你的操作");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();return i;}}
2.3、管理用户
package User;import Book.Bookshelf;import Function.Function;import Function.seekfunc;import Function.newlyfunc;import Function.*;import java.util.Scanner;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 19:59
*/
public class Adminuser extends User{
public Adminuser(String name){
super(name);
array = new Function[]{
new outfunc(),
new seekfunc() ,
new newlyfunc() ,
new delfunc() ,
new Showfunc() ,
};}
@Override
public void dowork(int pot, Bookshelf bookshelf){
array[pot].work(bookshelf);}
@Override
public int menu(){
System.out.println("欢迎管理员" + name + "来到图书管理系统");
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("**************************");
System.out.println("请选择你的操作");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();return i;}}
三、书包
3.1、书类
package Book;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 20:02
*/
public class Book {
private String name;
private int price;
private String author;
private String type;
protected Boolean whet;//默认是false,也就是未被借出
public Book(String name, int price, String author, String type){
this.name = name;
this.price = price;
this.author = author;
this.type =type;
this.whet =false;}
public String getName(){return name;}
public void setName(String name){
this.name = name;}
public int getPrice(){return price;}
public void setPrice(int price){
this.price = price;}
public String getAuthor(){return author;}
public void setAuthor(String author){
this.author = author;}
public String getType(){returntype;}
public void setType(String type){
this.type =type;}
public Boolean getWhet(){return whet;}
public void setWhet(Boolean whet){
this.whet = whet;}
@Override
public String toString(){return"Book{" +
"name='" + name + '\'' +
", price=" + price +
", author='" + author + '\'' +
", type='" + type + '\'' +
((whet ==true) ? ",已被借出":",未被借出") +
//", whet=" + whet +
'}';}}
3.2、书架类
package Book;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 20:08
*/
public class Bookshelf {
private Book[] books = new Book[10];
private int num;
public Bookshelf(){
this.num =3;
books[0]= new Book("西游记",10,"1","四大名著");
books[1]= new Book("三国演义",25,"1","四大名著");
books[2]= new Book("红楼梦",55,"1","四大名著");}
public Book getBooks(int pot){return books[pot];}
public void setBooks(int pot , Book book){
books[pot]= book;}
public int getNum(){return num;}
public void setNum(int num){
this.num = num;}}
四、功能包
4.1、功能接口
package Function;import Book.Bookshelf;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 20:19
*/
public interface Function {
void work(Bookshelf bookshelf);}
4.1、查找功能类
package Function;import Book.Book;import Book.Bookshelf;import java.util.Scanner;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 20:23
*/
public class seekfunc implements Function{
@Override
public void work(Bookshelf bookshelf){
//System.out.println("查找图书");
System.out.println("请输入你要查找的图书名称:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int num = bookshelf.getNum();for(int i =0; i < num; i++){
Book book = bookshelf.getBooks(i);
if(book.getName().equals(name)){
System.out.println("找到了你需要查找的图书");
System.out.println(book);return;}}
System.out.println("你需要查找的图书不存在");}}
4.2、显示功能类
package Function;import Book.Book;import Book.Bookshelf;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 20:24
*/
public class Showfunc implements Function{
@Override
public void work(Bookshelf bookshelf){
//System.out.println("显示图书");
int num = bookshelf.getNum();for(int i =0; i < num; i++){
Book book = bookshelf.getBooks(i);
System.out.println(book);}}}
4.3、新增功能类
package Function;import Book.Book;import Book.Bookshelf;import java.util.Scanner;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 21:04
*/
public class newlyfunc implements Function{
@Override
public void work(Bookshelf bookshelf){
//System.out.println("新增图书");
System.out.println("请输入你要新增的图书名称:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int num = bookshelf.getNum();for(int i =0; i < num; i++){
Book book = bookshelf.getBooks(i);
if(book.getName().equals(name)){
System.out.println("该书已存在,无需再次添加");return;}}
System.out.println("请输入新增图书作者:");
String author = scanner.nextLine();
System.out.println("请输入新增图书价格:");
int price = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入新增图书类型:");
String type= scanner.nextLine();
Book book = new Book(name,price,author,type);
bookshelf.setBooks(num,book);
bookshelf.setNum(++num);
System.out.println("新增图书成功!");}}
4.4、删除功能类
package Function;import Book.Book;import Book.Bookshelf;import java.util.Scanner;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 20:25
*/
public class delfunc implements Function{
@Override
public void work(Bookshelf bookshelf){
//System.out.println("删除图书");
System.out.println("请输入你要删除的图书名称:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int num = bookshelf.getNum();
int i =0;for(; i < num; i++){
Book book = bookshelf.getBooks(i);
if(book.getName().equals(name)){break;}}
if(i >= num){
System.out.println("你需要删除的图书不存在");}else{for(int j =0; j < num - 1; j++){
Book book = bookshelf.getBooks(j + 1);
bookshelf.setBooks(j , book);}
System.out.println("删除成功");
bookshelf.setNum(num-1);}}}
4.5、借阅功能类
package Function;import Book.Book;import Book.Bookshelf;import java.util.Scanner;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 20:26
*/
public class Borrowfunc implements Function{
@Override
public void work(Bookshelf bookshelf){
//System.out.println("借阅图书");
System.out.println("请输入你要借阅的图书名称:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int num = bookshelf.getNum();for(int i =0; i < num; i++){
Book book = bookshelf.getBooks(i);
if(book.getName().equals(name)){
if(book.getWhet()==false){
book.setWhet(true);
System.out.println("借阅成功");return;}else{
System.out.println("该书已被借出,无法再次借阅");return;}}}
System.out.println("该书不存在");}}
4.6、归还功能类
package Function;import Book.Book;import Book.Bookshelf;import java.util.Scanner;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 20:27
*/
public class Returnfunc implements Function{
@Override
public void work(Bookshelf bookshelf){
//System.out.println("归还图书");
System.out.println("请输入你要归还的图书名称:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int num = bookshelf.getNum();for(int i =0; i < num; i++){
Book book = bookshelf.getBooks(i);
if(book.getName().equals(name)){
if(book.getWhet()==true){
book.setWhet(false);
System.out.println("图书归还成功");return;}else{
System.out.println("该图书未被借出");return;}}}
System.out.println("未查找到你需要归还的图书");}}
4.7、退出功能类
package Function;import Book.Book;import Book.Bookshelf;
/**
* Created with Intellij IDEA.
* Description:
* User: 神舟
* Date: 2024-11-14
* Time: 20:28
*/
public class outfunc implements Function{
@Override
public void work(Bookshelf bookshelf){
//System.out.println("退出系统");
int num = bookshelf.getNum();for(int i =0; i < num; i++){
bookshelf.setBooks(i,null);}
System.exit(0);}}