一,Book包
1.1 Book类
定义一个书所拥有的属性:
package Book;
public class Book {
//书的属性 :名字,作者,类型,价格,是否被借出
private String name;
private String author;
private String type;
private int price;
private boolean borrowed;
public Book(String name, String author, String type, int price) {
this.name = name;
this.author = author;
this.type = type;
this.price = price;
}
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 String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isBorrowed() {
return borrowed;
}
public void setBorrowed(boolean borrowed) {
this.borrowed = borrowed;
}
//重写 toString 方法,输出 Book 的属性
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", type='" + type + '\'' +
", price=" + price +
((borrowed)?" 已被借出":" 未被借出") +
/*", borrowed=" + borrowed +*/
'}';
}
}
1.2 Booklist类
一个存储书的书架:
package Book;
//书架类
public class BookList {
Book[] books = new Book[10];//定义可以放几本书
private int usedSize;//已有书籍的数量
public BookList(){
//先存入三本书
this.books[0] = new Book("三国演义","罗贯中","小说",10);
this.books[1] = new Book("红楼梦","曹雪芹","小说",20);
this.books[2] = new Book("西游记","吴承恩","小说",100);
this.usedSize = 3;
}
//得到 pos 位置的书
public Book getBook(int pos){
return books[pos];
}
//令 pos 位置存储 book 书
public void setBooks(int pos,Book book){
books[pos] = book;
}
//得到已有书的数量
public int getUsedSize() {
return usedSize;
}
//改变已有书的数量
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
二,User包
2.1 User类
是一个抽象类,使用该系统的用户所拥有的共性:
package User;
import Book.BookList;
import Operation.IOperation;
//AdminUser类 和 NormalUser类 的父类
public abstract class User {
public String name;//使用者的姓名
public IOperation[] iOperations;//存储我们要调用的功能,通过下标去访问
public User(String name){
this.name = name;
}
public abstract int menu();//菜单
public void doOperations(int choice, BookList bookList){
//调用我们要实现的功能 - 增删查改....
iOperations[choice].work(bookList);
}
}
2.2 AdminUser 类
管理员
package User;
import Operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
//存入管理者可以使用的功能 - 菜单与下标一一对应
this.iOperations = new IOperation[]{
new Exit(),
new Add(),
new Find(),
new Del(),
new Show()
};
}
//管理者的菜单
@Override
public int menu() {
System.out.println("**************************");
System.out.println("欢迎 " + 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 scanner = new Scanner(System.in);
System.out.println("请选择操作:");
while (true){
int choice = scanner.nextInt();
if(choice<=4 && choice>=0)
return choice;
System.out.println("输入错误!");
}
}
}
2.3 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 Exit(),
new Borrowed(),
new Find(),
new Return()
};
}
//用户的菜单
@Override
public int menu() {
System.out.println("**************************");
System.out.println("欢迎 " + this.name +"来到图书小练习!" );
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("请选择操作:");
while (true){
int choice = scanner.nextInt();
if(choice<=3 && choice>=0)
return choice;
System.out.println("输入错误!");
}
}
}
2.4 Test类 - - - 测试
package User;
import Book.BookList;
import java.util.Scanner;
public class Test {
//登录方法 - 得到我们的身份和姓名
public static User login(){
System.out.println("请输入姓名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请选择身份:1-》管理员 0-》用户");
int ret = scanner.nextInt();
if(ret == 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.doOperations(choice,bookList);
}
}
}
三,Operation包
3.1 Add类 - - -增加图书
package Operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class Add implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("增加图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("书的书名:");
String name = scanner.nextLine();
int ret = bookList.getUsedSize();
for (int i = 0; i < ret; i++) {
Book book1 = bookList.getBook(i);
if(book1.getName().equals(name)){
System.out.println("已经有这本书!");
return;
}
}
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,type,price);
bookList.setBooks(ret, book);
bookList.setUsedSize(ret + 1);
System.out.println("添加成功!");
}
}
3.2 Del类 - - - 删除图书
package Operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class Del implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("输入要删除的书名:");
String name = scanner.nextLine();
int ret = bookList.getUsedSize();
int pos = -1;
for (int i = 0; i < ret; i++) {
Book book = bookList.getBook(i);
if(name.equals(book.getName())){
pos = i;
break;
}
}
if(pos == -1){
System.out.println("没有该书!");
return;
}
for (int i = pos; i < ret - 1; i++) {
Book book = bookList.getBook(i + 1);
bookList.setBooks(i,book);
}
bookList.setUsedSize(ret - 1);
System.out.println("删除成功!");
}
}
3.3 Borrowed类 - - - 借出图书
package Operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class Borrowed 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 ret = bookList.getUsedSize();
for (int i = 0; i < ret; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
if(!book.isBorrowed()){
book.setBorrowed(true);
System.out.println("借书成功!");
return;
}else {
System.out.println("已经借出!");
return;
}
}
}
System.out.println("没有这本书!");
}
}
3.4 Return类 - - - 归还图书
package Operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class Return 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 ret = bookList.getUsedSize();
for (int i = 0; i < ret; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
if(book.isBorrowed()){
book.setBorrowed(false);
System.out.println("还书成功!");
return;
}else {
System.out.println("已经归还该书!");
return;
}
}
}
System.out.println("没有这本书!无法归还!");
}
}
3.5 Find类 - - - 查找图书
import Book.BookList;
import java.util.Scanner;
public class Find implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int ret = bookList.getUsedSize();
for (int i = 0; i < ret; i++) {
if(name.equals(bookList.getBook(i).getName())){
System.out.println(bookList.getBook(i));
return;
}
}
System.out.println("没有该书!");
}
}
3.6 Exit类 - - - 退出系统
package Operation;
import Book.BookList;
public class Exit implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
int ret = bookList.getUsedSize();
for (int i = 0; i < ret; i++) {
//将已有书籍置为空
bookList.setBooks(i,null);
}
//将书籍数目置为0
bookList.setUsedSize(0);
//退出程序 --- JAVA用法
System.exit(0);
}
}
3.7 Show类 - - - 展示图书
package Operation;
import Book.Book;
import Book.BookList;
public class Show implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("展示图书!");
int ret = bookList.getUsedSize();
for(int i = 0; i < ret; i++){
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}