Book类: 定义了书的属性(书名,作者,价格,分类,状态)并且定义了属性的get、set方法
Library类: 写了登录函数(1、普通用户,2、管理员,3、退出系统),记录Book的数据。
Person类:定义的人的属性(姓名,性别,年龄)并且写了属性的get、set方法。
operate接口:操作的接口,操作方法:查询/查阅,删除/借阅,增加/还书,显示书籍列表
Root类:继承了Person类,实现operate(查阅,删除,增加,显示书籍列表)接口
User类:继承了Person类,实现operate(查询,借阅,还书,显示书籍列表)接口
Main类:Main中的主函数开启整个项目。
class Book {
private String name;//书名
private String author;//作者
private double price;//价格
private String category;//分类
private boolean state;//状态 true-未借出 false-已借出
//通过构造函数给出书的属性
Book(String name,String author,double price,String category,boolean state){
this.name = name;
this.author = author;
this.price = price;
this.category = category;
this.state = state;
}
//属性的获取和设置
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
import java.util.List;
public interface operate {
void select(List<Book> books);
void delete(List<Book> books);
void add(List<Book> books);
void list(List<Book> books);
}
import java.util.List;
abstract class Person {
private String name;//姓名
private String sex;//性别
private int age;//年龄
Person(String name,String sex,int age) {
this.age = age;
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(int age) {
this.age = age;
}
public abstract void operate(List<Book> books, int i);
}
import java.util.*;
import static java.util.Arrays.sort;
public class Root extends Person implements operate{
Root(String name, String sex, int age) {
super(name, sex, age);
}
Scanner sc = new Scanner(System.in);
@Override
public void operate(List<Book> books, int i) {
switch (i) {
case 1://整理
sort(books);
break;
case 2://查
select(books);
break;
case 3://加
add(books);
break;
case 4://删
delete(books);
break;
case 5://列表
list(books);
break;
default:
System.out.println("输入有误!");
}
}
public void sort(List<Book> books) {
System.out.println("图书馆中的书按价格整理:");
class BookComparator implements Comparator<Book>{
@Override
public int compare(Book o1, Book o2) {
return (int)(o1.getPrice() - o2.getPrice());
}
}
Collections.sort(books, new BookComparator());
System.out.println("整理结束!");
}
@Override
public void select(List<Book> books) {
System.out.println("请输入书名进行查询:");
String name = sc.next();
if(name != null) {
for (Book book : books) {
if (book.getName().equals(name)) {
System.out.println("有此书籍!");
System.out.println("书名:"+book.getName()+" 作者:"+book.getAuthor()+ " 价格:"+book.getPrice()+" 分类:"+book.getCategory()+" 状态:"+book.isState());
return;
}
}
System.out.println("暂时没有此书!");
}
}
@Override
public void delete(List<Book> books) {
System.out.println("请输入要删除的书名:");
String str = sc.next();
if(str != null) {
for(Book book : books) {
if(book.getName().equals(str)) {
books.remove(book);
System.out.println("删除成功!");
return;
}
}
System.out.println("无此书籍!");
}
}
@Override
public void add(List<Book> books) {
System.out.println("请输入书名:");
String name = sc.next();
System.out.println("请输入作者:");
String author = sc.next();
System.out.println("请输入价格:");
double price = sc.nextDouble();
System.out.println("请输入分类:");
String category = sc.next();
books.add(new Book(name,author,price,category,true));
}
@Override
public void list(List<Book> books) {
System.out.println("图书列表如下:"); //打印Book数组
for (Book book : books) {
System.out.println("书名:"+book.getName()+" 作者:"+book.getAuthor()+ " 价格:"+book.getPrice()+" 分类:"+book.getCategory()+" 状态:"+book.isState());
}
}
}
import java.util.List;
import java.util.Scanner;
class User extends Person implements operate {
User(String name,String sex,int age){
super(name,sex,age);
}
Scanner sc = new Scanner(System.in);
@Override
public void operate(List<Book> books, int i) {
switch(i){
case 1:
select(books);
break;
case 2:
delete(books);
break;
case 3:
add(books);
break;
case 4:
list(books);
break;
default:
System.out.println("输入有误!");
}
}
@Override
public void select(List<Book> books) {
System.out.println("请输入书名查询:");
String name = sc.next();
if(name != null){
for(Book book : books){
if(book.getName().equals(name)){
System.out.println("有此书籍!");
System.out.println("书名:"+book.getName()+" 作者:"+book.getAuthor()+ " 价格:"+book.getPrice()+" 分类:"+book.getCategory()+" 状态:"+book.isState());
return;
}
}
System.out.println("暂时没有此书籍!");
}
}
@Override
public void delete(List<Book> books) {
System.out.println("请输入要借阅的书名:");
String name = sc.next();
if (name != null) {
for (Book book : books) {
if (book.getName().equals(name)) {
if(book.isState()){
book.setState(false);
System.out.println("借阅成功!");
break;
} else{
System.out.println("已被借阅!");
break;
}
} else { //没书
System.out.println("没有此书!");
break;
}
}
}
}
@Override
public void add(List<Book> books) {
System.out.println("请输入要还的书名:");
String name = sc.next();
if (name != null) {
for (Book book : books) {
if (book.getName().equals(name) && !book.isState()) {
book.setState(true);
System.out.println("还书成功!");
return;
}
}
System.out.println("还书错误!");
}
}
@Override
public void list(List<Book> books) {
System.out.println("图书列表如下:"); //打印Book数组
for (Book book : books) {
System.out.println("书名:"+book.getName()+" 作者:"+book.getAuthor()+ " 价格:"+book.getPrice()+" 分类:"+book.getCategory()+" 状态:"+book.isState());
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Library {
private List<Book> books = new ArrayList<>(){
{
add(new Book("西游记", "吴承恩", 10, "名著", true));
add(new Book("红楼梦", "曹雪芹", 20, "名著", true));
add(new Book("三国演义", "罗贯中", 15, "名著", true));
add(new Book("小茗同学", "小明", 5, "杂志", true));
}};
Scanner sc = new Scanner(System.in);
public void login(){
int count = 0;
Person p = null;
while(count<2){
System.out.println("==================图书管理系统==================");
System.out.println("请登录:1.普通用户 2.管理员 3.退出系统");
int n = sc.nextInt();
if(n == 3){
return;
}
switch(n){
case 1:
System.out.println("请输入姓名:");
String Uname = sc.next();
System.out.println("请输入性别:");
String Usex = sc.next();
System.out.println("请输入年龄:");
int Uage = sc.nextInt();
p = new User(Uname,Usex,Uage); //构造普通用户对象
System.out.println("登录成功!");
System.out.println("当前用户为普通用户:"+"姓名:" + p.getName() + " "+"性别:" + p.getSex() + " "+"年龄"+ p.getAge());
//普通用户循环操作
while (true) {
System.out.println("请输入你的操作: 1.查询书籍 2.借阅书籍 3.归还书籍 4.显示书籍列表 5.退出");
int i = sc.nextInt();
if (i == 5) {
System.out.println("您已成功退出!");
break;
}else {
p.operate(books,i); //调用普通用户的操作方法
}
}
break;
case 2:
System.out.println("请输入管理员姓名:");
String Rname = sc.next();
System.out.println("请输入性别:");
String Rsex = sc.next();
System.out.println("请输入年龄:");
int Rage = sc.nextInt();
p = new Root(Rname,Rsex,Rage);//构造管理员对象
System.out.println("登录成功!");
System.out.println("当前管理员:"+"姓名:" + p.getName() + " "+"性别:" + p.getSex() + " "+"年龄" + p.getAge());
while (true) {
System.out.println("请输入你的操作: 1.整理书籍 2.查阅书籍 3.增加书籍 4.删除书籍 5.显示书籍列表 6.退出");
int j = sc.nextInt();
if (j == 6) {
System.out.println("您已成功退出!");
break;
}else{
p.operate(books,j);//调用管理员的操作方法
}
} break;
}
}
}
}
public class Main {
public static void main(String[] args) {
Library library = new Library(); //构造Library对象
library.login(); //调用对象的登录方法。
}
}