1. 设计思路图
图书管理系统的使用者有两类,一类是图书管理员,一类是借书用户。
他们在管理系统中的需求与操作是不同的,所以要分别进行设计。
2. 创建 book 包
建立一个 book 包,用来存放与书相关的内容。
2.1 Book 类
Book 类中存放与书有关的属性,而一本书的属性有很多,比如:书名、作者、价格、类型、编号…
代码实现:
//属性 private String name;//书名 private String author;//作者 private double price;//价格 private String type;//类型 private boolean isBorrowed;//是否被借出
对以上私有的属性提供 Getter and Setter 方法:
右击选择 Generate,点击 Getter and Setter方法,然后选择所有属性
得到:
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 double getPrice() { return price; } public void setPrice(double 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; }
构造书的对象(没有必要写书是否被借出,只需提供书的信息即可):
右击选择 Generate,点击 Constructor方法,然后选择属性
得到:
//构造书的对象--没必要写书是否被借出 public Book(String name, String author, double price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; }
打印一本书:
右击选择 Generate,点击 toString方法,然后选择所有属性
得到:
//打印图书 @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price='" + price + '\'' + ", type='" + type + '\'' + ((isBorrowed == true) ? " 已经借出" : " 未被借出") + '}'; }
整合代码:
package book; //存放与书相关的 public class Book { //属性 private String name;//书名 private String author;//作者 private double price;//价格 private String type;//类型 private boolean isBorrowed;//是否被借出 //构造书的对象--没必要写书是否被借出 public Book(String name, String author, double 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 double getPrice() { return price; } public void setPrice(double 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 + '}'; } }
2.2 BookList 类
BookList 类(书架)用来存放书与借书,而对于书架上的书,我们需要知道一个书架能放多少本书,有几本书未被借出,书架上所存放的书都有哪些,书架上还能继续存放几本书等。
基于这些问题,我们先定义一个 books 数组来控制书架上存放书的数量,
private Book[] books ; private int usedSize;//记录当前书架上 实际存放的书的数量 private static final int DEFAULT_CAPACITY = 10; public BookList() { this.books = new Book[DEFAULT_CAPACITY];//初始化书的数量 }
因为usedSize是private修饰的,我们就可以提供Getter and Setter 方法:
public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; }
而对于books数组,当我们想获取或设置某个下标的书时,也可以提供Getter and Setter方法,但是需要进行改进:
public Book getBook(int pos) { return books[pos];//返回书的位置 } public void setBooks(int pos,Book book) { books[pos] = book;//将books的pos位置存放一本书 }
提前存入一些书进去:
this.books[0] = new Book("三国演义","罗贯中",10,"小说"); this.books[1] = new Book("西游记","吴承恩",9,"小说"); this.books[2] = new Book("红楼梦","曹雪芹",19,"小说"); this.usedSize = 3;
整合代码:
package book; //书架 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; } }
3. 创建 operation 包
存放操作相关的内容:
管理员:查找图书、增加图书、删除图书、显示图书、退出系统
用户:查找图书、借阅图书、归还图书、退出系统
其中查找图书、退出系统两者都具备,可以在一个类中进行操作。
在上述操作中都需要对 BookList 进行操作,为了统一管理,我们在 operation 包内定义一个接口 IOPeration,通过IOPeration就可以对这些操作进行组织,从而实现了接口的统一性:
在该接口中定义一个 work方法,对书架上的书进行统一管理:
package operation; import book.BookList; public interface IOPeration { void work(BookList bookList); }
3.1 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("查找图书!"); //通过书名查找图书 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("没有找到这本书!"); } }
3.2 AddOperation 类-增加图书
import book.Book; import book.BookList; import java.util.Scanner; public class AddOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("新增图书!"); Scanner scanner = new Scanner(System.in); System.out.println("请输入书名:"); 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(); for (int i = 0; i < currentSize; i++) { Book book1 = bookList.getBook(i); if(book1.getName().equals(name)) { System.out.println("有这本书,不进行存放了!"); return; } } if(currentSize == bookList.getBooks().length) { System.out.println("书架满了!"); }else { bookList.setBooks(currentSize,book); bookList.setUsedSize(currentSize+1); } } }
3.3 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("删除图书!"); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要删除的图书:"); String name = scanner.nextLine(); int pos = -1; int currentSize = bookList.getUsedSize(); int i = 0; for (; i < currentSize; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)) { pos = i; break; } } if(i == currentSize) { System.out.println("没有你要删除的图书!"); return; } //开始删除 int j = pos; for (; j < currentSize-1; j++) { //[j] = [j+1] Book book = bookList.getBook(j+1); bookList.setBooks(j,book); } bookList.setBooks(j,null); bookList.setUsedSize(currentSize-1); } }
3.4 ShowOperation 类-显示图书
因为我们的书都是在数组中保存,所以可以通过数组元素的打印方法,来显示所有图书:
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("借阅图书!"); /** * 1. 你要借阅哪本书? * 2. 你借阅的书有没有? * 3. 借阅的方式是什么? -》 isB.... = true */ Scanner scanner = new Scanner(System.in); System.out.println("请输入你要借阅的图书:"); 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)) { book.setBorrowed(true); System.out.println("借阅成功!"); System.out.println(book); return; } } System.out.println("你借阅的图书 不存在!! "); } }
3.5 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("借阅图书!"); /** * 1. 你要借阅哪本书? * 2. 你借阅的书有没有? * 3. 借阅的方式是什么? -》 isB.... = true */ Scanner scanner = new Scanner(System.in); System.out.println("请输入你要借阅的图书:"); 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)) { book.setBorrowed(true); System.out.println("借阅成功!"); System.out.println(book); return; } } System.out.println("你借阅的图书 不存在!! "); } }
3.6 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("归还图书!"); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要归还的图书:"); 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)) { book.setBorrowed(false); System.out.println("归还成功!"); System.out.println(book); return; } } System.out.println("你归还的图书 不存在!! "); } }