图书馆系统分三部分,书类,图书馆类,管理员类
书类
有属性和构造方法,并重写toString方法,用于输出
package day13;
import java.util.Date;
public class Book {
//图书名称 图书编号 图书简介 图书作者 出版日期 图书价格
private String name;
private String number;
private String info;
private String author;
private int time;
private double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
Book(String name, String number, String info, String author, int time, double price) {
this.name = name;
this.number = number;
this.info = info;
this.author = author;
this.time = time;
this.price = price;
}
Book() {
}
public String toString() {
return "图书名称:" + name + "图书编号:" + number + "图书简介:" + info + "图书作者:" + author + "出版日期:" + time + "图书价格:" + price;
}
}
图书馆类
放置管理员信息,当创建管理员对象时,会进行遍历,判断是否有管理员权限,初始账号000000,000000
package day13;
import java.util.ArrayList;
import java.util.LinkedList;
public class Library {
// 存图书信息
public static ArrayList<Book> list = new ArrayList<>();
//管理员信息,设20个
private static String[][] admin = new String[20][2];
//初始管理员身份 账号:000000 密码:000000
static {
admin[0][0] = "000000";
admin[0][1] = "000000";
}
static public int count = 1;
//新增管理员
public void addAdmin(String name, String password) {
admin[count][0] = name;
admin[count][0] = password;
count++;
}
public String getAdminName(int i) {
return admin[i][0];
}
public String getAdminPassword(int i) {
return admin[i][1];
}
}
管理员类
创建管理员对象,需要账号登入,才能继续操作
add——添加新书
alter——根据人工记录上的序号,进行修改
remove——删除,图书馆主要是借书还书,所以考虑保留其序号,还书时则可以重新录入,也是防止其他书序号改变,造成其他方法出现错误
find——查找
say——各种要求的遍历
package day13;
import java.util.*;
public class Admin extends Library {
// 管理员登陆 账号密码
Admin() {
String quit = "退出";
System.out.println("请输入账号密码(输入退出则退出登入)");
h:
while (1 > 0) {
Scanner input = new Scanner(System.in);
System.out.println("请输入账号:");
String name = input.next();
System.out.println("请输入密码:");
String password = input.next();
if (quit.equals(name) || quit.equals(password)) {
System.exit(0);
}
for (int i = 0; i < count; i++) {
if (getAdminName(i).equals(name) && getAdminPassword(i).equals(password)) {
System.out.println("登入成功");
break h;
}
}
}
}
// 图书管理图书新增 新书信息
public void add(Book book) {
list.add(book);
}
// 图书修改 根据图书记录表查找序号
public void alter(int order, Book book) {
list.set(order - 1, book);
}
// 图书删除 仍保留其位置,防止其他序列改变
public void delete(int order) {
list.remove(order - 1);
list.add(order - 1, new Book("null", "null", "null", "null", 99999999, 99999999));
}
// 根据图书名称模糊查找图书
public void find(String name) {
Iterator i = list.iterator();
while (i.hasNext()) {
Book book = (Book) i.next();
if (book.getName().equals(name)) {
System.out.println(book.toString());
}
}
}
// 查看所有图书(3种排序显示) 价格从高到低排序 价格从低到高排序 新旧排序(出版日期排序)
public void sayPriceHigh() {
ArrayList<Book> books = new ArrayList<>(list);
//Collections.sort(books,new ComBookPriceHigh());
books.sort(new ComBookPriceHigh());
Iterator i = books.iterator();
while (i.hasNext()) {
System.out.println(i.next().toString());
}
}
public void sayPriceLow() {
ArrayList<Book> books = new ArrayList<>(list);
books.sort((Book b1, Book b2) -> (int) (b1.getPrice() * 100 - b2.getPrice() * 100));
Iterator i = books.iterator();
while (i.hasNext()) {
System.out.println(i.next().toString());
}
}
public void sayTime() {
ArrayList<Book> books = new ArrayList<>(list);
books.sort((Book b1, Book b2) -> (int) (b1.getTime() - b2.getTime()));
Iterator i = books.iterator();
while (i.hasNext()) {
System.out.println(i.next().toString());
}
}
//所有元素
public void say() {
Iterator i = list.iterator();
while (i.hasNext()) {
System.out.println(i.next().toString());
}
}
}
class ComBookPriceHigh implements Comparator<Book>{
@Override
public int compare(Book o1, Book o2) {
if(o1.getPrice() > o2.getPrice()){
return -1;
}
return 1;
}
}