码迷,mamicode.com
首页 > 编程语言 > 详细

java--模拟图书管理系统

时间:2020-05-01 12:36:21      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:date   离散   方法   book   OLE   owa   顺序   数组   count   

//图书管理系统需要图书,书架,还有图书管理员。

图书:有名字,状态(借出去没),日期,借出次数;

书架:存放图书的数组,最多存放多少本书,目前有多少本书 ;书架可以新增图书,获取书的名字,数量并可以设置,

可以把借出的图书按次数从高到低排序,可以查看所有图书的信息。

图书管理员:有一个书架,有借出时间和归还时间,能管理书架的费用,可以初始化系统里的书,能借书,还书,增加图书,删除图书,费用统计,

有一个主界面提供各种功能,比如:排行榜,增加删除借出归还统计图书等。

1.图书类

public class Book {

//1.书名2.状态3.日期4.借出次数
private String bookname;
private int status;//0:已借出1:可借
private String data;
private int count;

public Book(String bookname, int status, String data, int count) {//构造方法
this.bookname=bookname;
this.status = status;
this.data = data;
this.count = count;
}
public String getBookname() {
return bookname;
}

public void setBookname(String bookname) {
this.bookname = bookname;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

public int getCount() {
return count;
}
//执行该方法,记录借出一次
public void setCount() {
this.count ++;
}



}

2.书架类

import java.lang.reflect.Array;
import java.util.Arrays;

public class BookSet {
//1.存放图书的数组2.最多存放多少本书3.目前有多少本
private Book [] books;

public Book[] getBooks() {
return books;
}

public void setBooks(Book[] books) {
this.books = books;
}

private final int Max_Book_NUM=50;
private int bookNum;
//构造方法:给成员变量初始化

public BookSet() {
this.bookNum=0;
books=new Book[Max_Book_NUM];
}
public void addBook (Book newBook){
System.out.println("**********");
//确定将新书放哪个位置
int i;
for (i=0;i<books.length;i++){
if (books[i]==null){
books[i]=newBook;
bookNum++;
System.out.println("新增《"+newBook.getBookname()+"》成功!");
break;
}
}
if (i==Max_Book_NUM){
System.out.println("新增图书失败,书架满了!");
}
System.out.println("******************************");
}

public int getBooknum() {
return bookNum;
}

public void setBooknum(int booknum) {
this.bookNum = booknum;
}
//排行榜的功能
public void rankList(){
//定义一个新的数组
Book [] sortedBooks=new Book[Max_Book_NUM];
//拷贝数组
sortedBooks=Arrays.copyOf(books,Max_Book_NUM);
//输出排序后的数组
for(int i=0;i<bookNum;i++){
int maxindex=i;
for (int j=i+1;j<bookNum;j++){
if (sortedBooks[j].getCount() > sortedBooks[maxindex].getCount()){
maxindex=j;
}
}
if (maxindex!=i){
Book tempbook;
tempbook=sortedBooks[i];
sortedBooks[i]=sortedBooks[maxindex];
sortedBooks[maxindex]=tempbook;
}
}
System.out.println("-----排行榜");
System.out.println("********************************");
System.out.println("次数\t名称");
for (int i=0;i<bookNum;i++){
System.out.println(sortedBooks[i].getCount()+"\t《"+sortedBooks[i].getBookname()+"》");

}
System.out.println("********************************");

}

//查看所有图书的信息
public void showAllBookInfo(){
String zhuangt="未借出";
String zhungt1="已借出";
System.out.println("********************************************************************************************");
System.out.println(" 状态\t\t\t 书名\t\t\t借出日期\t\t借出书籍数量\t\t未借出书籍数量");
for (int i=0;i<bookNum;i++){
int s= books [i].getStatus();
if (s==1) {
System.out.println(zhuangt + "\t\t《" + books[i].getBookname() +
"》 \t\t" + zhuangt+ "\t\t\t\t" + books[i].getCount() + " \t\t\t\t\t" + ( Max_Book_NUM-books[i].getCount()));
}
else if (s==0){
System.out.println(zhungt1 + "\t\t《" + books[i].getBookname() +
"》 \t\t" + books[i].getData() + "\t\t\t" + books[i].getCount() + " \t\t\t\t\t" + ( Max_Book_NUM-books[i].getCount()));
}

}
System.out.println("********************************************************************************************");

}
}
3.管理员类

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

public class BookSetManger {
//管理书架费用
private int charge;
private BookSet bookSet;
private String lendtime="";
private String returntime="";
private String wantbookname="";
public BookSetManger(){
bookSet=new BookSet();
setCharge(0);
}
//初始化系统里的书
public void init(){
bookSet.addBook(new Book("数据结构",1,"null",5));
bookSet.addBook(new Book(" 运筹学 ",1,null,2));
bookSet.addBook(new Book("高等数学",1,null,8));
bookSet.addBook(new Book("离散数学",1,null,10));
bookSet.addBook(new Book("计算机基础",1,null,1));
bookSet.addBook(new Book("机器学习",1,null,6));
bookSet.addBook(new Book(" 数据库 ",1,null,2));
}
//主界面
public void startMenu(){
System.out.println(" \t欢迎使用热门图书借阅系统");
System.out.println("-----------------------------------------\n");
System.out.print("1.热门书排行榜 \t");
System.out.println(" 2.查询全部图书");
System.out.print("3.新增图书信息 \t");
System.out.println(" 4.删除图书信息\t");
System.out.print("5.借出图书 \t\t");
System.out.println(" 6.归还图书\t\t");
System.out.print("7.费用统计\t\t");
System.out.println("\t 0.退 出");
System.out.println("-----------------------------------------\n");
System.out.println("请选择:");
Scanner scanner=new Scanner(System.in);
String chose=scanner.next();
switch (chose){
case "1": bookSet.rankList();break;
case "2": bookSet.showAllBookInfo(); break;
case "3":{
//输入名字
String bookName=inputBookname();
//实例化对象放到书架中
bookSet.addBook(new Book(bookName,1,null,0));
}
break;
case "4": DelBook(); break;
case "5": lendBook(); break;
case "6": ReturnBook(); break;
case "7": CostCt(); break;
case "0": {
System.out.println("感谢使用,系统退出");
System.exit(0);
}
default: System.out.println("输入有误,我看不懂");
}
returnStartmenu();

}
//返回主菜单
public void returnStartmenu(){
System.out.println("请按回车键返回主菜单");
Scanner scanner=new Scanner(System.in);
scanner.nextLine();
startMenu();
}
//借书
public String lendBook(){
System.out.println("-------->借出图书");
String wantBn= inputBookname();//读者想要看的书名\
Boolean exist=false;//读者要看的书不存在
for (int i=0;i<bookSet.getBooknum();i++){
Book currentBook=bookSet.getBooks()[i];//从书架上按顺序拿一本书
String name=currentBook.getBookname().replace(" ","");
if (name.equals(wantBn)&&currentBook.getStatus()==1){
exist=true;
currentBook.setStatus(0);
lendtime=inputdate("借书");
System.out.println("借书费用两元一天");
currentBook.setData(lendtime);
currentBook.setCount();
System.out.println("恭喜你,《"+wantBn+"》借出成功!");
break;
}
else if (name.equals(wantBn)&&currentBook.getStatus()==0){
exist=true;
System.out.println("Scorry,《"+wantBn+"》已借出!");
}
}
if (exist==false){
System.out.println("没有找到你想借的书!");
System.out.println("请重新输入:");

}
return wantbookname=wantBn;
}
//还书
public String ReturnBook(){
System.out.println("归还图书 >>>");
System.out.println("*******************************\n");
String wantlend=inputBookname();
String warn="恭喜,《"+wantlend+"》还书成功";
Boolean zhuangT=false;//要还的书不存在
for (int i=0;i<bookSet.getBooknum();i++){
Book book=bookSet.getBooks()[i];
String bookname=book.getBookname().replace(" ","");
if (book.getStatus()==0&&bookname.equals(wantlend)){
returntime=inputdate("还书");
zhuangT=true;
book.setStatus(1);
book.setCount();//加一本书
System.out.println(warn);
break;
}
else if (book.getStatus()==1&&bookname.equals(wantlend)){
zhuangT=true;
System.out.println("搞错了把,《"+wantlend+"》这本书还没借出去");
break;
}

}
if (zhuangT==false){
System.out.println("丫丫哎呀,没有!!");
}
System.out.println("*******************************\n");

return returntime;


}
//费用统计
public void CostCt(){

try {
System.out.println("----------->费用统计");
System.out.println("********************************************");
SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
Date enddate=format.parse(returntime);
Date startDate=format.parse(lendtime);
long totalPrice = ((enddate.getTime()-startDate.getTime())/(24*60*60*1000))*2;
for (int i=0;i<bookSet.getBooknum();i++){
Book book=bookSet.getBooks()[i];
String bookname=book.getBookname().replace(" ","");
if (bookname.equals(wantbookname)) {
System.out.println("书名\t" + "借书时间\t" + "还书时间\t" + "总时长");
System.out.println(bookname + "\t"
+ lendtime + "\t" + returntime + "\t"
+ ((enddate.getTime() - startDate.getTime())/ (24 * 60 * 60 * 1000))+"天");
}
}

System.out.println("您的借书费用为:" + totalPrice+"元");
}
catch (Exception e){
System.out.println("系统检测到你还没借书并还书,请检查并重试!");
}


}
//删除图书

public void DelBook(){
System.out.println("---------->删除图书");
System.out.println("**************************************\n");
String wantbn=inputBookname();
Book [] b=new Book[bookSet.getBooknum()];
Boolean cunz=false;//不存在
for (int i=0;i<bookSet.getBooknum();i++){
Book book=bookSet.getBooks()[i];
String bookname=book.getBookname().replace(" ","");
if (book.getStatus()==1&&bookname.equals(wantbn)){
int index=i;
cunz=true;
//最后那本书代替要删除的书
b[index]=b[b.length-1];
//从书架剪掉一本书
bookSet.setBooknum(bookSet.getBooknum()-1);
System.out.println("《"+bookname+"》删除成功!!");
break;
}
else if (book.getStatus()==0&&bookname.equals(wantbn)){
cunz=true;
System.out.println("《"+book.getBookname()+"》借出去了,不能删除");
break;
}

}
if (cunz==false){
System.out.println("能不能长点心,这书这里没有");
}
System.out.println();
System.out.println("**************************************");
returnStartmenu();

}
//输入日期
public String inputdate(String info){
System.out.println("请输入"+info+"日期<yyyymmdd>:");
Scanner scanner=new Scanner(System.in);
String date=scanner.next();
return date;
}

//输入书名
public String inputBookname (){
System.out.print(">>请输入书名>>");
Scanner scanner=new Scanner(System.in);
String bookname= scanner.next();
String newbookname=bookname.replace(" ","");
return newbookname;
}
public int getCharge() {
return charge;
}

public void setCharge(int charge) {
this.charge = charge;
}



}
4.测试

public class Starter {
public static void main(String[] args) {
BookSetManger bsm=new BookSetManger();
bsm.init();
bsm.startMenu();

}
}

java--模拟图书管理系统

标签:date   离散   方法   book   OLE   owa   顺序   数组   count   

原文地址:https://www.cnblogs.com/meng--yu/p/12812963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!