图书管理是许多高校的期末经典项目,在学完了类,对象,抽象类,接口,继承,封装和多态及组合等知识后,要及时的练手,以此来巩固这部分的内容,Java中基本都是在和对象这些的打交道,所以利用这些知识来写一个图书管理系统。
项目简介:
该项目为图书管理系统,实现用户的区分,包括管理员和普通用户,管理员的行为包括:添加图书,删除图书,查找图书,显示图书和退出程序等,普通用户的用法包括:查找图书,借阅图书,归还图书和退出程序。
利用类,对象,抽象类,接口,继承,封装和多态及组合等知识,将对象提取出来,对象----用户和书,用户可以分为管理员和普通用户,所以设置一个抽象类User,管理员和普通用户继承该抽象类,由于书还要包括书架,所以书和书架分两个类,为了实现对应的操作,并且是操作的方法一致,所以要构造一个操作的接口,其他的操作用类来实现该接口,就完成了对应的操作。具体实现方式见详细代码!
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)?"该书已借出":"该书未借出")+
'}';
}
}
package book;
public class BookList {
public Book[] books=new Book[10];
public int usedSize;//书架上书的数量
/**
*
* 事先通过构造方法放入3本书
*/
public BookList(){
books[0]=new Book("三国演义","罗贯中",89,"小说");
books[1]=new Book("西游记","吴承恩",79,"小说");
books[2]=new Book("红楼梦","曹雪芹",83,"小说");
this.usedSize=3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
//下标
public Book getPos(int pos){
return books[pos];
}
/**
*
* 存储一本书到指定位置
* @param book
* @param pos 在这里指的是当前最后可以存储书的位置
*/
public void setBooks(Book book,int pos){
books[pos]=book;
}
}
package operations;
//接口
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
package operations;
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("请输入你要添加的图书的价格:");
int price=scanner.nextInt();
scanner.nextLine();//读取回车
System.out.println("请输入你要添加的图书的类型:");
String type=scanner.nextLine();
Book book=new Book(name,author,price,type);
//获取当前可以存放书的位置
int currentSize=bookList.getUsedSize();
//书放到指定位置
bookList.setBooks(book,currentSize);
//书的数量加1
bookList.setUsedSize(currentSize+1);
System.out.println("添加成功!");
}
}
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowedOperation 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 currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
book.setBorrowed(true);
System.out.println("借阅成功!");
return;
}
}
System.out.println("没有你要借阅的书!");
}
}
package operations;
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 index=-1;
int currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
index=i;
break;
}
}
if(index==-1){
System.out.println("没有你要删除的这本书!");
return;
}
for (int i = index; i
package operations;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("显示所有图书!");
int currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
System.out.println(book);
}
}
}
package operations;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出程序!");
System.exit(0);//退出
}
}
package operations;
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();
int currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
book.setBorrowed(false);
System.out.println("归还图书成功!");
return;
}
}
System.out.println("没有你要归还的书!");
}
}
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class SearchOperation 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 currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(name.equals(book.getName())){
System.out.println("找到啦这本书!");
System.out.println(book);
return;
}
}
System.out.println("没有这本书!");
}
}
package user;
import book.BookList;
import operations.IOperation;
//抽象类
public abstract class User {
protected String name;
protected IOperation[] iOperations;//接口数组
public User(String name){
this.name=name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
iOperations[choice].work(bookList);
}
}
package user;
import operations.*;
import java.util.Scanner;
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
//接口数组
this.iOperations=new IOperation[]{
new ExitOperation(),
new SearchOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
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;
}
}
package user;
import operations.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.iOperations=new IOperation[]{
new ExitOperation(),
new SearchOperation(),
new BorrowedOperation(),
new ReturnOperation(),
};
}
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("0.退出系统");
System.out.println("*********************************");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
}
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 userName=scanner.nextLine();
System.out.println("请输入你的身份:1-》管理员,0-》普通用户");
int choice= scanner.nextInt();
if(choice==1){
return new AdminUser(userName);//向上转型
}else{
return new NormalUser(userName);//向上转型
}
}
public static void main(String[] args) {
//1、准备数据
BookList bookList=new BookList();
//2、登录,
User user= login();
while(true){
int choice= user.menu();//多态,动态绑定
user.doOperation(choice,bookList);
}
}
}
码云上传地址:图书管理系统
谢谢观看!!!