文章目录
- book包
-
- operation包
-
- AddOperation 类
- BorrowOperation 类
- DelOperation类
- DisplayOperation 类
- ExitOperation 类
- FindOperation 类
- IOperation 类
- ReturnOperation 类
- User包
-
- AdminUser类
- NormalUser 类
- User 类
- Main 类
我们将先前学习的知识运用起来,写一篇图书管理系统简易版 链接
book包
Book类
编写每一本书的基本属性和方法
package Book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrow;
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 isBorrow() {
return isBorrow;
}
public void setBorrow(boolean borrow) {
isBorrow = borrow;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrow == true) ? " 已借出 " : " 未借出 ") +
'}';
}
}
BookList类
对整个“书架”操作
package Book;
public class BookList {
private static int SIZE = 5;
private Book[] books = new Book[SIZE];
private int usedSize = 0;
public BookList() {
books[0] = new Book("三国演义","罗贯中",90,"小说");
books[1] = new Book("西游记","吴承恩",78,"小说");
books[2] = new Book("红楼梦","曹雪芹",89,"小说");
books[3] = new Book("水浒传", "施耐庵",91,"小说");
books[4] = new Book("推背图","袁天罡",93,"谶颂述");
this.usedSize = 5;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getPos(int pos) {
return books[pos];
}
public void setPos(int pos, Book book) {
books[pos] = book;
}
public boolean isFull(int newSize) {
return newSize > SIZE ? true : false;
}
public void addCapacity() {
SIZE *= 2;
Book[] newBooks = new Book[SIZE];
System.arraycopy(books,0,newBooks,0,usedSize);
this.books = newBooks;
System.out.println("扩容成功!");
}
}
operation包
AddOperation 类
package operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("新增图书!");
System.out.println("请输入图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入图书的作者:");
String author = scanner.nextLine();
System.out.println("请输入图书的类型:");
String type = scanner.nextLine();
System.out.println("请输入图书的价格:");
int price = scanner.nextInt();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
if (bookList.isFull(currentSize + 1)) {
bookList.addCapacity();
}
bookList.setPos(currentSize,book);
bookList.setUsedSize(currentSize + 1);
System.out.println("新增成功!");
}
}
BorrowOperation 类
package operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
System.out.println("请输入你要借阅图书的名字:");
Scanner input = new Scanner(System.in);
String name = input.nextLine();
int currentSize = bookList.getUsedSize();
int i = 0;
for (; i < currentSize; i++) {
Book book = bookList.getPos(i);
if (book.getName().equals(name)) {
if(book.isBorrow() == true) {
System.out.println("该书已被借出!");
return;
} else {
book.setBorrow(true);
System.out.println("借阅成功!");
return;
}
}
}
if (i == bookList.getUsedSize()) {
System.out.println("没有这本书!");
}
}
}
DelOperation类
package operation;
import Book.Book;
import Book.BookList;
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();
int i = 0;
for (; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if (book.getName().equals(name)) {
break;
}
}
if (i == bookList.getUsedSize()) {
System.out.println("删除图书不存在!");
return;
}
for (int j = i; j < bookList.getUsedSize() - 1; j++) {
Book book = bookList.getPos(j + 1);
bookList.setPos(j, book);
}
int currentSize = bookList.getUsedSize();
bookList.setPos(currentSize - 1,null);
bookList.setUsedSize(currentSize - 1);
System.out.println("删除成功!");
}
}
DisplayOperation 类
package operation;
import Book.Book;
import Book.BookList;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示图书!");
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
System.out.println(book);
}
}
}
ExitOperation 类
package operation;
import Book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
for (int i = 0; i < bookList.getUsedSize(); i++) {
bookList.setPos(i,null);
}
System.exit(0);
}
}
FindOperation 类
package operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
System.out.println("请输入你要查找图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int i = 0;
for (; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if (book.getName().equals(name)) {
System.out.println("找到了,信息如下:");
System.out.println(book);
}
}
if (i == bookList.getUsedSize()) {
System.out.println("图书不存在");
}
}
}
IOperation 类
package operation;
import Book.BookList;
public interface IOperation {
void work(BookList bookList);
}
ReturnOperation 类
package operation;
import Book.Book;
import Book.BookList;
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();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if (book.getName().equals(name)) {
book.setBorrow(false);
System.out.println("归还成功");
return;
}
}
System.out.println("归还图书不存在!");
}
}
User包
AdminUser类
package User;
import Book.BookList;
import operation.*;
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 DisplayOperation()
};
}
@Override
public int menu() {
System.out.println("Hello" + this.name);
System.out.println("1.查找图书");
System.out.println("2.增加图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("请输入你的操作: ");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
return choice;
}
}
NormalUser 类
package User;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("hello "+this.name);
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("请输入你的操作:");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
return choice;
}
}
User 类
package User;
import Book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;
public IOperation[] iOperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
iOperations[choice].work(bookList);
}
}
Main 类
import Book.BookList;
import User.User;
import User.AdminUser;
import User.NormalUser;
import java.util.Scanner;
public class Main {
public static User login() {
System.out.println("请输入姓名:");
Scanner input = new Scanner(System.in);
String name = input.nextLine();
System.out.println("请输入你的身份:1 -> 管理员 2 -> 普通用户");
int choice = input.nextInt();
if (choice == 1) {
return new AdminUser(name);
} else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while (true) {
int choice = user.menu();
user.doOperation(choice,bookList);
}
}
}