import java.text.SimpleDateFormat;
import java.util.*;
public class BookManageSys {
public static void main(String[] args) {
Map<String, BookInfo> map = new LinkedHashMap<>();
boolean status = true;
while (status) {
System.out.println("**********************欢迎来到图书馆平台*********************");
System.out.println("********************** 增加书本请按1 *********************");
System.out.println("********************** 查询书本请按2 *********************");
System.out.println("********************** 借书请按3 *********************");
System.out.println("********************** 归还请按4 *********************");
System.out.println("********************** 退出请按5 *********************");
System.out.println("**********************请输入编号:");
Scanner in = new Scanner(System.in);
int type = in.nextInt();
switch (type) {
case 1:
map = addBook(map);
break;
case 2:
searchBook(map);
break;
case 3:
map = rentBook(map);
break;
case 4:
map = bookBack(map);
break;
case 5:
status = false;
break;
}
}
}
// 增加图书的方法
static Map<String, BookInfo> addBook(Map<String, BookInfo> map) {
Scanner in = new Scanner(System.in);
System.out.println("增加书本");
System.out.println("请输入要增加的书本的名字:");
String name = in.next();
System.out.println("请输入库存:");
int num = in.nextInt();
// 首先判断该书库中是否已经存在(书名一致即为存在)
if (map.containsKey(name)) {
// 该书已经存在在库中,只需更新库存即可
BookInfo temp = new BookInfo();
temp = map.get(name);
temp.setNum(temp.getNum() + num);
System.out.println("----------增加成功-----------");
return map;
}
System.out.println("请输入作者:");
String author = in.next();
System.out.println("请输入价格:");
double price = in.nextDouble();
// 将书本添加入库
// 书本不存在在库存中,所有元素均需添加
map.put(name, new BookInfo(name, author, num, price));
System.out.println("----------增加成功-----------");
return map;
}
// 查询图书的功能
static void searchBook(Map<String, BookInfo> map) {
System.out.println("查询图书");
System.out.println("书名 作者 库存 价格 借出时间 归还时间");
Set<Map.Entry<String, BookInfo>> entries = map.entrySet();
if (!entries.isEmpty()) {
Iterator<Map.Entry<String, BookInfo>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, BookInfo> next = iterator.next();
BookInfo object = map.get(next.getKey());
System.out.println(object.getName() + " " + object.getAuthor() + " " +
object.getNum() + " " + object.getPrice() + " " + object.getTimeRent() +
" " + object.getTimeBack());
}
}
}
// 借书的方法
static Map<String, BookInfo> rentBook(Map<String, BookInfo> map) {
// 判断要借的书是否库中存在
Scanner in = new Scanner(System.in);
System.out.println("请输入书名:");
String name = in.next();
if (!map.containsKey(name)) {
System.out.println("请核对书名后输入。");
} else {
// 书已经在库中存在
// 判断书的库存是否大于0
int count = map.get(name).getNum();
if (count == 0) {
System.out.println("该书已被全部借出,请重新选择。");
} else {
String timeRent = new SimpleDateFormat("EEE MMM dd yyyy").format(new Date());
BookInfo temp = map.get(name);
temp.setNum(temp.getNum() - 1);
temp.setTimeRent(timeRent);
map.put(name, temp);
System.out.println("借出成功。");
}
}
return map;
}
// 归还系统
static Map<String, BookInfo> bookBack(Map<String, BookInfo> map) {
Scanner in = new Scanner(System.in);
System.out.println("请输入书名:");
String name = in.next();
// 判断该书在原数据中是否有记录,若没有则不视为借出图书
if (!map.containsKey(name)) {
System.out.println("请核对书名后输入。");
} else {
// 该书存在于数据中
String timeBack = new SimpleDateFormat("EEE MMM dd yyyy").format(new Date());
BookInfo temp = map.get(name);
temp.setNum(temp.getNum() + 1);
temp.setTimeBack(timeBack);
map.put(name, temp);
System.out.println("归还成功。");
}
return map;
}
}
class BookInfo {
private String name;
private String author;
private int num;
private double price;
private String timeRent;
private String timeBack;
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public int getNum() {
return num;
}
public double getPrice() {
return price;
}
public String getTimeRent() {
return timeRent;
}
public String getTimeBack() {
return timeBack;
}
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setNum(int num) {
this.num = num;
}
public void setPrice(double price) {
this.price = price;
}
public void setTimeRent(String timeRent) {
this.timeRent = timeRent;
}
public void setTimeBack(String timeBack) {
this.timeBack = timeBack;
}
public BookInfo(String name, String author, int num, double price) {
this.name = name;
this.author = author;
this.num = num;
this.price = price;
}
public BookInfo() {
}
}
利用oop思想实现的一个小demo【demo图书管理系统】
最新推荐文章于 2022-12-13 10:09:57 发布