在学习了面向对象编程语法知识以及简单数据结构–顺序表等等知识点以后,现在我们就可以运用这些知识来综合实现一个 图书管理系统 ,来检验前面知识的掌握程度以及练习代码的实际编写能力,让自己对Java开发更进一步理解,话不多说,开始讲解~
1.book包的实现
首先我们实现一个book包,里面分别定义一个Book类,用来实现对书的定义,对书的操作;以及一个BookList类,相当于书架,用来存放书,实质上就是一个数据结构中的顺序表,实现对具体书的查找与修改。
//相关讲解在代码中以注释形式给出,大家注意查看
1.1 Book类
package book;
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==true)?"已经被借出":"未被借出")+
'}';
}
}
1.2 BookList类
package book;
public class BookList {
private Book[] books=new Book[10];
private int usedSize;
public BookList(){
books[0]=new Book("三国演义","罗贯中",17,"小说");
books[1]=new Book("西游记","吴承恩",47,"小说");
books[2]=new Book("水浒传","施耐庵",37,"小说");
this.usedSize=3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getPos(int pos){
return this.books[pos];
}
public void setBook(int pos,Book book){
this.books[pos]=book;
}
}
2.operation包
因为需要进行面向对象编程练习,所以单独实现一个operation包,用于实现对书的各种操作,原本这部分的代码放在BookList 这个顺序表中即可,但是单独实现Operation包可以对面向对象编程有一个更加清晰的认识~
2.1 IOperation接口
package operation;
import book.BookList;
import java.util.Scanner;
public interface IOperation {
void work(BookList bookList);
Scanner scanner=new Scanner(System.in);
}
2.2 AddOperation类
这个类实现增加书籍的功能
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation {
public void work(BookList bookList) {
System.out.println("新增图书!");
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 size = bookList.getUsedSize();
bookList.setBook(size, book);
bookList.setUsedSize(size + 1);
System.out.println("新增图书成功!");
}
}
2.2 BorrowOperation类
借阅图书功能
package operation;
import book.Book;
import book.BookList;
public class BorrowOperation implements IOperation{
public void work(BookList bookList){
System.out.println("借阅图书!");
System.out.println("请输入你要借阅的图书的名字:");
String name=scanner.nextLine();
int size=bookList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
if (name.equals(book.getName())){
book.setBorrowed(true);
System.out.println("借阅成功!");
System.out.println(book);
return;
}
}
System.out.println("没有找到你要借阅的这本书!");
}
}
2.3 ReturnOperation类
归还图书功能
package operation;
import book.Book;
import book.BookList;
public class ReturnOperation implements IOperation{
public void work(BookList bookList){
System.out.println("归还图书!");
System.out.println("请输入你要归还的图书的名字:");
String name=scanner.nextLine();
int size=bookList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
if (name.equals(book.getName())){
book.setBorrowed(false);
System.out.println("归还成功!");
System.out.println(book);
return;
}
}
System.out.println("没有找到你要归还的这本书!");
}
}
2.4 DelOperation类
删除指定图书功能
package operation;
import book.Book;
import book.BookList;
public class DelOperation implements IOperation{
public void work(BookList bookList){
System.out.println("删除图书!");
System.out.println("请输入你要删除的书的书名:");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
int index=0;
int i = 0;
for (; i < currentSize; i++) {
Book book=bookList.getPos(i);
if (book.getName().equals(name)){
index=i;
break;
}
}
if (i>=currentSize){
System.out.println("没有你要删除的这本书!");
return;
}
for (int j = index; j < currentSize-1; j++) {
Book book=bookList.getPos(j+1);
bookList.setBook(j,book);
}
bookList.setBook(currentSize,null);
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功!");
}
}
2.5 DisplayOperation类
实现对当前书架信息的打印
package operation;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation{
public void work(BookList bookList){
System.out.println("打印图书!");
int size=bookList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
System.out.println(book);
}
}
}
2.6 FindOperation类
实现查找指定图书并打印图书信息功能
package operation;
import book.Book;
import book.BookList;
public class FindOperation implements IOperation{
public void work(BookList bookList){
System.out.println("查找图书!");
String name=scanner.nextLine();
int size=bookList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
if (name.equals(book.getName())){
System.out.println("找到了这本书,信息如下:");
System.out.println(book);
return;
}
}
System.out.println("没有找到这本书!");
}
}
2.7 ExitOperation类
实现退出图书管理系统功能
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
public void work(BookList bookList){
System.out.println("退出系统!");
System.exit(0);
}
}
3.user包
因为针对不同用户需要实现不同功能,这里user包就分别实现管理员需要的功能以及普通用户需要的功能
3.1 User类 (父类)
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;
protected IOperation[] iOperations;
public User(String name){
this.name=name;
}
public abstract int menu();
public void doWork(int choice, BookList bookList){
iOperations[choice].work(bookList);
}
}
3.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 ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
};
}
public int menu(){
System.out.println("==========管理员菜单==========");
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 scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
}
3.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 ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
public int menu(){
System.out.println("==========普通用户菜单==========");
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 scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
}
4. 程序入口 Main 类实现
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
System.out.println("请输入你的姓名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
System.out.println("请输入你的身份:1->管理员。0->普通用户");
int choice=scanner.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.doWork(choice, bookList);
}
}
}
5.整体代码实现展示
在实现了上面四大块的代码编写后,以及Main类将各个模块功能通过向上转型等方式串连起来,我们的简易图书管理系统就已经完成了!这是一个对Java学习以来的所有知识进行实践的很好的练习项目,值得大家反复学习以及反复进行代码练习 ~
整体代码实现展示如下:
5.1 管理员功能实现展示(部分)
5.2 普通用户功能实现展示(部分)
6 结尾
写到这里,一个简易的图书管理系统就已经完成了~ 如果觉得博主的文章对你有所帮助,欢迎大家多多点赞收藏~
|