主要实现类
该项目主要采用的技术: 面向对象编程思想、数组、接口、if、for循环、方法重载等,非常适合新手学习。
1.Book类 图书类
package book_manager.book;
public class Book {
private String name;
private String id;
private String author;
private int price;
private String type;
private boolean isBorrow;
public Book(String name, String id, String author, int price, String type, boolean isBorrow) {
this.name = name;
this.id = id;
this.author = author;
this.price = price;
this.type = type;
this.isBorrow = isBorrow;
}
@Override // Object中内置的类,用来格式化打印book的信息
public String toString() {
return "Book{" + "name='" + name + '\'' + ", id='" + id + '\'' + ", author='" + author + '\'' + ", price="
+ price + ", type='" + type + '\'' + ", isBorrow=" + isBorrow + '}';
}
public String getName() {
return name;
}
public boolean isBorrow() {
return isBorrow;
}
public void setBorrow(boolean bool) {
this.isBorrow = bool;
}
public String getId() {
return id;
}
}
2.Booklist类 图书存放类
package book_manager.book;
import java.util.Arrays;
public class BookList {
private Book[] books = new Book[100];
private int size;
public BookList() {
books[0] = new Book("金瓶梅", "001", "兰陵笑笑生", 100, "古典名著", false);
books[1] = new Book("水浒传", "002", "施耐庵", 100, "古典名著", false);
books[2] = new Book("西游记", "003", "吴承恩", 100, "古典名著", false);
size = 3;
}
public int getSize() {
return size;
}
public void setBooks(int index, Book book) {
books[index] = book;
}
public void setSize(int size) {
this.size = size;
}
public Book getBook(int index) {
return books[index];
}
}
3.Io接口
package book_manager.book;
public interface IO {
}
4.ADD类 添加图书
package book_manager.Operation;
import book_manager.book.*;
import java.util.Scanner;
public class ADD implements IO {
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名");
String name = scanner.next();
System.out.println("请输入序号");
String id = scanner.next();
System.out.println("请输入作者");
String author = scanner.next();
System.out.println("请输入价格");
int price = scanner.nextInt();
System.out.println("请输入类型");
String type = scanner.next();
Book book = new Book(name, id, author, price, type, false);
bookList.setBooks(bookList.getSize(), book);
bookList.setSize(bookList.getSize() + 1);
System.out.println("添加成功");
}
}
5.Borrow类 借书
package book_manager.Operation;
import book_manager.book.Book;
import book_manager.book.BookList;
import java.util.Scanner;
public class Borrow implements IO {
public void work(BookList bookList) {
int i = 0;
int flag = 0;
Scanner scan = new Scanner(System.in);
System.out.println("请输入需要借阅的书名:");
String name = scan.next();
for (; i < bookList.getSize(); i++) {
if (name.equals(bookList.getBook(i).getName())) {
if (bookList.getBook(i).isBorrow() == false) {
System.out.println("借阅成功");
flag = 1;
bookList.getBook(i).setBorrow(true);
}
}
}
if (flag == 0) {
System.out.println("不好意思,借阅失败");
}
}
}
6.Delete类 删除图书
package book_manager.Operation;
import book_manager.book.BookList;
import java.util.Scanner;
public class Delete implements IO {
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入想要删除的编号");
String id = scanner.next();
for (int i = 0; i < bookList.getSize(); i++) {
if (bookList.getBook(i).getId().equals(id)) {
bookList.setBooks(i, bookList.getBook(bookList.getSize()));
bookList.setSize(bookList.getSize() - 1);
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
}
}
}
7.exit类 退出
package book_manager.Operation;
import book_manager.book.BookList;
public class Exit implements IO{
public void work(BookList bookList) {
System.out.println("退出成功");
System.exit(0);
}
}
8.Find 类 查找图书
package book_manager.Operation;
import book_manager.book.BookList;
import java.util.Scanner;
public class Find implements IO {
@Override
public void work(BookList bookList) {
int i = 0;
int count = 0;
Scanner scan = new Scanner(System.in);
System.out.println("请输入需要查找的书名");
String name = scan.next();
for (; i < bookList.getSize(); i++) {
if (name.equals(bookList.getBook(i).getName())) {
count++;
}
}
if (count == 0) {
System.out.println("不好意思,没有找到");
} else {
System.out.println("找到了,共计" + count + "本");
}
}
}
9.IO类
package book_manager.Operation;
import book_manager.book.BookList;
public interface IO {
abstract public void work(BookList bookList);
}
10.PrintAll类 打印图书信息
package book_manager.Operation;
import book_manager.book.BookList;
public class PrintAll implements IO{
@Override
public void work(BookList bookList) {
for(int i=0;i<bookList.getSize();i++){
System.out.println(bookList.getBook(i));
}
}
}
11.Return类 归还图书
package book_manager.Operation;
import book_manager.book.BookList;
import java.util.Scanner;
public class Return implements IO{
@Override
public void work(BookList bookList) {
int i=0;
int flag=0;
Scanner scan = new Scanner(System.in);
System.out.println("请输入需要归还的ID");
String id = scan.next();
for(;i<bookList.getSize();i++){
if(id.equals(bookList.getBook(i).getId())){
if(bookList.getBook(i).isBorrow()==true){
System.out.println("归还成功");
bookList.getBook(i).setBorrow(false);
flag=1;
}
else{
System.out.println("归还失败");
}
}
}
if(flag==0){
System.out.println("不好意思,没有此书");
}
}
}
12.Admin 管理员用户
package book_manager.user;
import book_manager.Operation.*;
import java.util.Scanner;
public class Admin extends User{
public Admin(String name){
super(name);
operation=new IO[]{
new Exit(),
new Find(),
new ADD(),
new Delete(),
new PrintAll(),
};
}
public int menu() {
System.out.println("============");
System.out.println("hello " + name);
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 choice = scanner.nextInt();
return choice;
}
}
13.NormalUser类 普通用户
package book_manager.user;
import book_manager.Operation.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
operation = new IO[]
{ new Exit(),
new Find(),
new Borrow(),
new Return(),
new PrintAll()
};
}
public int menu() {
System.out.println("============");
System.out.println("hello " + name);
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 choice = scanner.nextInt();
// close 本质上是在关闭 System.in
// 由于后面还需要用到 System.in, 此处不能盲目关闭.
// scanner.close();
return choice;
}
}
14.User类 用户类
package book_manager.user;
import book_manager.Operation.*;
import book_manager.Operation.IO;
import book_manager.book.BookList;
abstract public class User {
String name;
protected IO[] operation;
public User(String name){
this.name=name;
}
abstract public int menu();//该方法被重写
public void doOperation(int choice, BookList bookList) {
operation[choice].work(bookList);
}
}
15.Test类 测试类,主程序入口
package book_manager.user;
import book_manager.book.BookList;
import book_manager.user.Admin;
import book_manager.user.NormalUser;
import book_manager.user.User;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
BookList list = new BookList();
User user = login();
//通过不同的choice和身份调用不同的Operation方法
while(true){
int choice = user.menu();
user.doOperation(choice, list);
}
}
public static User login(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名");
String name = scanner.next();
System.out.println("请输入你的身份");
System.out.println("1.普通用户 2.管理员");
int role= scanner.nextInt();
if(role==1){
return new NormalUser(name);
}
else{
return new Admin(name);
}
}
}
@版权信息归创作者所有,请勿抄袭转载,如有侵权,必当追责!