JavaSE——图书管理

简介: JavaSE——图书管理

 

文章目录

前言

book

operation

user

Main

前言

抽象出来的对象有:书,书架,用户等,分别建立三个包管理 

book
Book
package book;
public class Book {

//书名
private String  name;
//作者
private String author;
//价格
private int price;
//类型
private String type;
//状态
private boolean isBorrow;
public Book(String name, String author, int price, String type) {
    this.name = name;
    this.author = author;
    this.price = price;
    this.type = type;

}
@Override
public String toString() {
    return "Book{" +
            "name='" + name + '\'' +
            ", author='" + author + '\'' +
            ", price=" + price +
            ", type='" + type + '\'' +
            ((isBorrow==true)?"已借出":"未借出")+
            '}';
}
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 isBorrow() {
    return isBorrow;
}

public void setBorrow(boolean borrow) {
    isBorrow = borrow;
}

}
BookList
先存储三本书:

package book;

public class BookList {

private Book[] books = new Book[10];
private int usedSize;//存储当前书的个数

public BookList() {

    books[0] = new Book("三国演义","罗贯中",90,"小说");
    books[1] = new Book("红楼梦","曹雪芹",80,"小说");
    books[2] = new Book("水浒传","施耐庵",70,"小说");

    this.usedSize = 3;
}

public int getUsedSize() {
    return usedSize;
}

public void setUsedSize(int usedSize) {
    this.usedSize = usedSize;
}
public Book getPos(int pos){
    return books[pos];

}
//存储一本书到指定的位置
public void setBooks(Book book,int pos){
    books[pos] = book;
}

}
//将对数组的操作写到一个接口中
operation
IOperation
package operation;

import book.BookList;

public interface IOperation {

void work(BookList bookList);

}
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("请输入书名");
    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);
    //书的个数加一
    bookList.setUsedSize(currentSize+1);
}

}
BorrowOperation
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation{

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())){
            if(book.isBorrow()){
                System.out.println("已借出");
            }
            else{
                book.setBorrow(true);
                System.out.println("借阅成功");
            }
            return;
        }
    }
    System.out.println("没有要借阅的书");
}

}
DelOperation
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{

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 < currentSize-1 ; i++) {

        Book book = bookList.getPos(i+1);
        bookList.setBooks(book,i);
    }
    
    bookList.setBooks(null,currentSize-1);

    //重置有效长度
    bookList.setUsedSize(currentSize-1);
    System.out.println("删除成功");
}

}
假设有四本书,那么i+1最大值就是3,即 i < currentSize-1才不会越界

删除书之后要置空,否则可能会出现内存泄漏
一个对象没有人引用才会回收 

DisplayOperation
package operation;

import book.Book;
import book.BookList;

public class DisplayOperation implements IOperation{

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);
    }
}

}
ExitOperation
package operation;

import book.BookList;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

public class ExitOperation implements IOperation{

public void work(BookList bookList){
    System.out.println("退出系统");
    System.exit(0);
}

}
FindOperation
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation{

//查找图书
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(book);
            return;
        }
    }
    System.out.println("没有这本书");
}

}
ReturnOperation
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperation implements IOperation{

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())){
            book.setBorrow(false);
            System.out.println("归还成功");
            return;
        }
    }
    System.out.println("没有要归还的书");
}

}
user
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 doOperation (int choice, BookList bookList){
    iOperations[choice].work(bookList);

}

}

 需要写一个操作接口数组的方法doOperation()

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"+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;

}

}
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" + name + "欢迎来到图书管理");
    System.out.println("1. 查找图书");
    System.out.println("2. 借阅图书");
    System.out.println("3. 归还图书");
    System.out.println("0. 退出系统");
    System.out.println("*************");
    System.out.println("请输入你的操作:");
    Scanner scanner = new Scanner(System.in);
    int choice = scanner.nextInt();
    return choice;
}

}
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 userName = scanner.nextLine();
    System.out.println("请输入你的身份:1. 管理员  2. 用户");
    int choice = scanner.nextInt();
    if(choice==1){
       return new AdminUser(userName);
    }
    else{
        return new NormalUser(userName);
    }
}

public static void main(String[] args) {
    //先放三本书
    BookList bookList = new BookList();
    //登录
    User user =  login();
    while(true){
        int choice = user.menu();
        user.doOperation(choice,bookList);
    }
}

}

当引用的对象不同时,会打印不同的菜单

相关文章
|
6月前
|
Java 数据库 Android开发
图书销售系统【纯控制台】(Java课设)
图书销售系统【纯控制台】(Java课设)
48 1
【javaSE】 实现图书管理系统
【javaSE】 实现图书管理系统
【JAVASE】图书管理系统 下
【JAVASE】图书管理系统
|
5月前
|
前端开发 JavaScript Java
图书借阅管理平台|基于JavaWeb实现图书借阅系统
图书借阅管理平台|基于JavaWeb实现图书借阅系统
112 1
|
1月前
|
Java
JavaSE图书管理系统
JavaSE图书管理系统
21 5
|
6月前
|
Java 关系型数据库 MySQL
java毕业设计之酒店管理系统(源码+文档)
酒店管理系统的主要使用者分为管理员、用户;用户:首页、个人中心、客房预订管理、入住登记管理、服务费用管理、退房登记管理、我的收藏管理。管理员:首页、个人中心、用户管理、客房类型管理、客房服务管理、客房信息管理、客房预订管理、入住登记管理、服务费用管理、退房登记管理、系统管理等功能。通过这些功能模块的设计,基本上实现了整个酒店信息管理的过程。
|
6月前
|
JavaScript Java 测试技术
基于Java的图书借阅管理系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的图书借阅管理系统的设计与实现(源码+lw+部署文档+讲解等)
98 2
|
6月前
|
Java
图书信息管理系统【JSP+Servlet+JavaBean】(Java课设)
图书信息管理系统【JSP+Servlet+JavaBean】(Java课设)
30 0
|
6月前
|
JavaScript Java 测试技术
基于Java的超市商品管理系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的超市商品管理系统的设计与实现(源码+lw+部署文档+讲解等)
122 0
|
Java
Java图书管理代码
Java图书管理代码
107 1