Java学习【面向对象综合练习——实现图书管理系统】
前期效果
用户分为普通用户和管理员,登录进系统之后可以对图书进行一系列操作,此时我们要明白,对图书的操作是通过书架来执行的,我们平常在图书馆上借书就是在书架上
图书的创建
接下来我们来创建图书类,对图书的属性进行封装,并写上对应的get和set 方法,
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
public Book() {
}
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
/······get set toString······/
接下来就是创建书架的类,书架上肯定不止一本书,所以这里选择数组来存储多个book对象,因为在一开始就有了一部分书,所以在创建书架的对象时,就要把图书的对象放进去
public class BookList {
private Book[] books = new Book[10];//用到了组合
private int usedSize = 0;//实际放的数量
public BookList() {
//创建对象之后就已经有了三本书
books[0] = new Book("三国演义", "罗贯中", 20,"小说");
books[1] = new Book("西游记", "吴承恩", 30,"小说");
books[2] = new Book("红楼梦", "曹雪芹", 20,"小说");
usedSize = 3;
}
}
用户的创建
接着创建用户,而用户又分为普通用户和管理员,就可以通过继承来实现这种关系,这两种用户的菜单也不相同,就可以抽取出来,在子类进行重写
public abstract class User {
//不同包的子类可以访问
protected String name;
public User(String name) {
this.name = name;
}
public abstract int menu();
}
之后就可以在Main类里面以多态的方式,根据不同的对象调取不同的菜单
public class Main {
public static User login() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = sc.nextLine();
System.out.println("请输入身份:1.管理员 2. 普通用户");
int choice = sc.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();
}
}
操作的实现
菜单里的操作可以通过接口来实现这个业务逻辑,
public interface IOPeration {
void work(BookList bookList);
}
例如添加图书,直接去实现一个操作接口,具体的逻辑再在本类中进行重写
public class AddOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("添加图书");
}
}
接下来就是创建各种操作了
那么有了这些操作该怎么调用呢,无论时普通用户还是管理员,都是用户在操作,所以要在父类User中创建接口数组,再调用具体的方法实现操作
捋一下现在的逻辑
完善操作
接下来就是完善菜单中的操作内容了
显示图书
因为之前book是用数组存在BookList中的,所以只要遍历数组就可以了
BookList中要提供对应的get,set方法:
public int getUsedSize() {
return usedSize;
}
public Book getBook(int pos) {
return books[pos];
}
public void setBook(int pos, Book book) {
this.books[pos] = book;
}
显示图书:
public class ShowOperation implements IOPeration{
@Override
public void work(BookList bookList) {
int currentSize = bookList.getUsedSize();
for(int i= 0;i < currentSize;i++){
System.out.println(bookList.getBook(i));//输出当前书架上的所有书籍信息
}
}
}
查找图书
明白了怎么遍历之后,查找图书就非常简单了
public class FindOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("请输入查找的图书:");
Scanner sc = new Scanner(System.in);
String name = sc.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(book);
return;
}
}
System.out.println("不存在该图书");
}
}
新增图书
public class AddOperation implements IOPeration {
@Override
public void work(BookList bookList) {
int currentSize = bookList.getUsedSize();
if(currentSize == bookList.getBooks().length){
System.out.println("书架已满,无法添加");
return;
}
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);
//添加图书
bookList.setBook(currentSize, book);
bookList.setUsedSize(currentSize + 1);
System.out.println("添加成功");
}
}
添加之后要记得更改currentSize
借阅图书
借书其实就是查找之后满足借阅条件就可以借阅
public class BorrowOperation implements IOPeration{
public void work(BookList bookList) {
System.out.println("请输入借阅的图书:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int currentSize = bookList.getUsedSize();
for(int i = 0;i < currentSize;i++){
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
//判断是否借出
if(book.isBorrowed()){
System.out.println("该图书已被借阅");
return;
}
//修改借阅状态
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
System.out.println("不存在该图书");
}
}
归还图书
public class ReturnOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("请输入归还的图书:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
//判断是否借出
if (book.isBorrowed()) {
//修改借阅状态
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
}
System.out.println("此书未被借出");
}
}
删除图书
由于是用顺序表存储的图书,所以当删除其中的一本书之后,这本书后面的图书都要往前移
public class DeleteOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("请输入删除的图书:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int i = 0,pos = 0;
int currentSize = bookList.getUsedSize();
for(;i < currentSize;i++){
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
pos = i;
break;
}
}
if(i == currentSize){
System.out.println("没有找到该图书");
}
for(int j = pos;j < currentSize - 1;j++){
Book book = bookList.getBook(j + 1);
bookList.setBook(j,book);
}
bookList.setBook(currentSize - 1,null);
//更新currentSize
bookList.setUsedSize(currentSize - 1);
System.out.println("删除成功");
}
}
标签:Java,管理系统,System,面向对象,bookList,println,public,图书,out
From: https://blog.csdn.net/2202_76097976/article/details/139198933