作者简介: zoro-1,目前大一,正在学习Java,数据结构等
作者主页: zoro-1的主页
欢迎大家点赞 👍 收藏 ⭐ 加关注哦!💖💖
图书管理系统
book包
Book
java
public class Book {
//书的各种属性
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
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;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
}
BookList
java
public class BookList {
private Book[] books ;
private int usedSize;//记录当前书架上 实际存放的书的 数量
private static final int DEFAULT_CAPACITY = 10;
public BookList() {
this.books = new Book[DEFAULT_CAPACITY];
//放好书!!
this.books[0] = new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",9,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
this.usedSize = 3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBook(int pos) {
return books[pos];
}
public void setBooks(int pos,Book book) {
books[pos] = book;
}
public Book[] getBooks() {
return books;
}
}
operation包
Add
java
import book.Book;
import book.BookList;
import java.util.Scanner;
public class Add implements Io{
@Override
public void work(BookList bookList) {
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();
System.out.println("请输入新增图书的类型");
String type=scanner.nextLine();
Book book=new Book(name,author,price,type);
int size=bookList.getUsedSize();
if (size==bookList.getBooks().length){
System.out.println("书架满了");
return;
}
for(int i=0;i<size;i++)
{
if (bookList.getBook(i).getName().equals(name)){
System.out.println("已经有这本书了");
return;
}
}
bookList.setBooks(size,book);
bookList.setUsedSize(size+1);
}
}
Borrow
java
import book.BookList;
import java.util.Scanner;
public class Borrow implements Io{
@Override
public void work(BookList bookList) {
System.out.println("请输入您要借的书名");
Scanner sc=new Scanner(System.in);
String name=sc.nextLine();
int size=bookList.getUsedSize();
for(int i=0;i<size;i++){
if(bookList.getBook(i).getName().equals(name)){
System.out.println("有您要借的书");
System.out.println(bookList.getBook(i));
}
}
System.out.println("您要借的书不存在");
}
}
Exit
java
import book.BookList;
public class Exit implements Io{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
Find
java
import book.Book;
import book.BookList;
import java.util.Scanner;
public class Find implements Io{
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
//遍历这个数组
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("找到了这本书,信息如下:");
System.out.println(book);
return;
}
}
System.out.println("没有找到这本书!");
}
}
Io
java
import book.BookList;
public interface Io {
void work(BookList bookList);
}
Return
java
import book.BookList;
import java.util.Scanner;
public class Return implements Io{
@Override
public void work(BookList bookList) {
System.out.println("请输入您要归还图书名");
Scanner sc=new Scanner(System.in);
String name=sc.nextLine();
int szie=bookList.getUsedSize();
for(int i=0;i<szie;i++){
if(bookList.getBook(i).getName().equals(name)){
bookList.getBook(i).setBorrowed(false);
System.out.println("归还成功");
return;
}
}
System.out.println("未找到您要归还的图书");
}
}
Show
java
import book.BookList;
public class Show implements Io{
@Override
public void work(BookList bookList) {
System.out.println("打印图书");
int size=bookList.getUsedSize();
for(int i=0;i<size;i++){
System.out.println(bookList.getBook(i));
}
}
}
user包
Max
java
import operation.*;
import java.util.Scanner;
public class Max extends Users{
public Max(String name){
//父类先进行构造
super(name);
this.io = new Io[]{
new Exit(),
new Find(),
new Add(),
new Del(),
new Show(),
};
}
@Override
public int menu() {
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);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
Min
java
import operation.*;
import java.util.Scanner;
public class Min extends Users{
public Min(String name){
super(name);
this.io = new Io[]{
new Exit(),
new Find(),
new Borrow(),
new Return()
};
}
@Override
public int menu() {
System.out.println("**********普通用户******");
System.out.println("1. 查找图书");
System.out.println("2. 借阅图书");
System.out.println("3. 归还图书");
System.out.println("0. 退出系统");
System.out.println("**********************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
Users
java
import book.BookList;
import operation.Io;
public abstract class Users {
private String name;
public Io io[];
public Users(String name){
this.name=name;
}
public abstract int menu();
public void choice(int choice, BookList bookList){
io[choice].work(bookList);
}
}
主函数
java
import book.BookList;
import user.Max;
import user.Users;
import java.util.Scanner;
public class Main {
public static Users shenfen(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入您的身份");
String name= sc.nextLine();
if(name.equals("Max")){
return new Max(name);
}
if(name.equals("Min")) {
return new Max(name);
}
return null;
}
public static void main(String[] args) {
Users users=shenfen();
Scanner sc=new Scanner(System.in);
BookList bookList=new BookList();
while (true){
int chioce=users.menu();
users.choice(chioce,bookList);
}
}
}
今天分享到这里就结束了,希望大家支持一下