菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
0
0

简易图书管理系统——Java实现

原创
05/13 14:22
阅读数 234

推荐课程:JavaWeb项目实战全程实录#学完你就可以自己用Java做项目了

本人还是小白,闲来无事,跟着网络上大牛们学习了这段程序,本身难度并不大,但自己在很多细节地方考虑的还是不够周全,一些功能还未实现,代码的可读性也不是很高,所以还请各位大牛能多指点!
主要分为3类,书本类Book,图书管理类LibraryManager,测试类Test。
Java Date格式化输出自行在CSDN上自己找哈,有很多,我在这里就偷个懒,嘻嘻!

代码附上:
Book类:
主要就是书本属性,以及set与get方法,可根据自身需求修改

import java.util.Date;

public class Book {
private String name;//书名
private int id;//序列号
private int price;//价格
private String authon;//作者
private boolean statc = true;//借阅状态ture在馆,false借出
private Date borrowData;//借阅日期
private int count;//被借次数

public Date getBorrowData() {
    return borrowData;
}

public void setBorrowData(Date borrowData) {
    this.borrowData = borrowData;
}

public int getCount() {
    return count;
}

public void setCount() {
    this.count++;
}

public boolean getStatc() {
    return statc;
}

public void setStatc(boolean statc) {
    this.statc = statc;
}

public Book(String name, int id, int price, String authon) {//构造函数
    this.name = name;
    this.id = id;
    this.price = price;
    this.authon = authon;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

public String getAuthon() {
    return authon;
}

public void setAuthon(String authon) {
    this.authon = authon;
}

}

LibraryManager类:
其中注释掉的是重复的
可读性有点差劲,希望大牛们多指点!

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class LibraryManager {
private Book[] book = new Book[10];//创建数组存储书本
Scanner input = new Scanner(System.in);
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

public void init(){//初始化
    Book book1 = new Book("《平凡的世界》",01,50,"贾平凹");
    Book book2 = new Book("《巴黎圣母院》",02,35,"雨果");
    Book book3 = new Book("《金陵十三钗》",03,26,"严歌苓");
    book[0] = book1;
    book[1] = book2;
    book[2] = book3;
}

public void menu(){//界面选择
    System.out.println("-------欢迎进入图书管理系统--------");
    System.out.println("请根据提示选择:");
    System.out.println("1.增加图书\t\t2.删除图书\t\t3.借阅图书\n4.归还图书\t\t5.图书借阅排行\t"+"6.书籍目录\n"+"0.退出系统");
    int choice = input.nextInt();
    switch (choice){
        case 1:
            addBook();
            break;
        case 2:
            deleteBook();
            break;
        case 3:
            checkOutBook();
            break;
        case 4:
            returnBook();
            break;
        case 5:
            rankBook();
            break;
        case 6:
            showBook();
            break;
        case 0:
            System.exit(0);
        default:
    }
}

public void showBook(){//输出图书
    for(Book x :book) {
        if (x!=null) {
            if (x.getStatc())
                System.out.println("名称:" + x.getName() +
                        "\t\t\t序列号:" + x.getId() + "\t\t作者:" + x.getAuthon() +
                        "\t\t被借"+x.getCount()+"次"+"\t\t状态:入藏");
            else
                System.out.println("名称:" + x.getName() +
                        "\t\t\t序列号:" + x.getId() + "\t\t作者:" + x.getAuthon() +
                        "\t\t被借"+x.getCount()+"次"+"\t\t状态:"+format.format(x.getBorrowData())+"借出");
        }
    }
}

public void rankBook() {//按借阅次数降序排序
    Book[] tempbook = new Book[book.length];//创建一个临时数组使之等于原数组
    for(int i = 0;i<book.length;i++){
        if(book[i] != null){
            tempbook[i] = book[i];
        }
    }
    System.out.println("借书次数由高到低为:");
    int i, j;
    for (i = 0;i < tempbook.length-1;i++) {//冒泡排序
        for (j = 0; j < tempbook.length - i - 1; j++) {
            if (tempbook[j] != null && tempbook[j + 1] != null) {
                if (tempbook[j].getCount() < tempbook[j + 1].getCount()) {
                    Book temp = tempbook[j];
                    tempbook[j] = tempbook[j + 1];
                    tempbook[j + 1] = temp;
                }
            }
        }
    }

    for (Book x : tempbook) {
        if (x != null) {
            if (x.getStatc())
                System.out.println("名称:" + x.getName() +
                        "\t\t序列号:" + x.getId() + "\t\t作者:" + x.getAuthon() +
                        "\t\t被借" + x.getCount() + "次" + "\t\t状态:入藏");
            else
                System.out.println("名称:" + x.getName() +
                        "\t\t序列号:" + x.getId() + "\t\t作者:" + x.getAuthon() +
                        "\t\t被借" + x.getCount() + "次" + "\t\t状态:" + x.getBorrowData() + "借出");
        }
    }
}

public void addBook(){//增加图书
    String name,authon;
    int id,price,i;
    System.out.println("请输入书籍名称,序列号,价格,作者");
    name = input.next();
    id = input.nextInt();
    price = input.nextInt();
    authon = input.next();
    Book temp = new Book(name,id,price,authon);
    for(i = 0;i<book.length;i++){
        if(book[i]==null)
            break;
    }
    book[i] = temp;
}

public int findName(int location){//查找与name相同的下标
    String name = input.next();
    for(int j = 0;j < book.length;j++){
        if(book[j].getName().equals(name)){
            location = j;
            break;
        }
    }
    return location;
}

public int findId(int location){//查找与序列号相同的下标
    int n = input.nextInt();
    for(int j = 0;j < book.length;j++){
        if(book[j].getId()==n){
            location = j;
            break;
        }
    }
    return location;
}

public void deleteBook(){//删除图书
    System.out.println("1.按名称删除\t"+"2.按序列号删除");
    int i = input.nextInt(),location = -1;//待删除下标
    switch (i){
        case 1:
            System.out.println("请输入要删除的书籍名称:");

// String name = input.next();
// for(int j = 0;j < book.length;j++){
// if(book[j].getName().equals(name)){
// location = j;
// break;
// }
// }
location = findName(location);
break;
case 2:
System.out.println("请输入要删除书籍的序列号:");
// int n = input.nextInt();
// for(int j = 0;j < book.length;j++){
// if(book[j].getId()==n){
// location = j;
// break;
// }
// }
location = findId(location);
break;
}
//进行移动
int start = location,end;
int nulllocation = book.length;//null的下标
for(int j = 0;j<book.length;j++){
if(book[j] == null) {
nulllocation = j;
break;
}
}
end = nulllocation-1;
for(int j = start;j < end;j++){
if(j==book.length-1)
book[j] = null;
else
book[j] = book[j+1];
}
book[end] = null;
}

public void checkOutBook(){//借阅图书
    System.out.println("1.按名称借阅\t"+"2.按序列号借阅");
    int i = input.nextInt(),location = -1;//待借阅下标
    switch (i){
        case 1:
            System.out.println("请输入要借阅的书籍名称:");

// String name = input.next();
// for(int j = 0;j < book.length;j++){
// if(book[j].getName().equals(name)){
// location = j;
// break;
// }
// }
location = findName(location);
break;
case 2:
System.out.println("请输入要借阅书籍的序列号:");
// int n = input.nextInt();
// for(int j = 0;j < book.length;j++){
// if(book[j].getId()==n){
// location = j;
// break;
// }
// }
location = findId(location);
break;
}

    book[location].setCount();//被借次数+1
    if(book[location].getStatc()){
        Date time = new Date();
        book[location].setBorrowData(time);
        book[location].setStatc(false);
    }else {
        System.out.println(book[location].getName()+"于"+book[location].getBorrowData()+"已借出!");
    }
}

public void returnBook(){//归还图书
    System.out.println("1.按书名归还\t2.按序列号归还");
    int i = input.nextInt(),location = -1;//待归还下标
    switch (i){
        case 1:
            System.out.println("请输入你要归还的书名");

// String name = input.next();
// for(int j = 0;j < book.length;j++){
// if(book[j].getName().equals(name)){
// location = j;
// break;
// }
// }
location = findName(location);
break;
case 2:
System.out.println("请输入你要归还的序列号");
// int n = input.nextInt();
// for(int j = 0;j < book.length;j++){
// if(book[j].getId()==n){
// location = j;
// break;
// }
// }
location = findId(location);
break;
}
book[location].setStatc(true);
book[location].setBorrowData(null);
}
}

Test类:

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String n = null;
LibraryManager manager = new LibraryManager();
manager.init();
do {//控制是否退出
manager.menu();
System.out.println("回主菜单?y(是)/其他(结束)");
n = input.next();
}while ("y".equals(n));
}
}

运行结果图:
file
file
file
file

原文来自:https://blog.csdn.net/weixin_45919137/article/details/109339705?utm_medium=distribute.pc_category.none-task-blog-hot-18.nonecase&depth_1-utm_source=distribute.pc_category.none-task-blog-hot-18.nonecase

发表评论

0/200
0 点赞
0 评论
收藏
为你推荐 换一批