Book类: 定义了书的一些属性(书名,作者,价格,分类,状态)并且写了属性的get、set方法
Library类: 写了登录函数(1、普通用户,2、管理员,3、退出系统),
Person类:定义的人的属性(姓名,性别,年龄)并且写了属性的get、set方法,定义了一些书。
operate接口:操作的接口,操作方法:查询/查阅,删除/借阅,增加/还书,显示书籍列表
Root类:继承了Person类,实现operate(查阅,删除,增加,显示书籍列表)接口
User类:继承了Person类,实现operate(查询,借阅,还书,显示书籍列表)接口
Main类:Main中的主函数开启整个项目。
class Book{
private String name; //书名
private String author; //作者
public double price; //价格
private String category; //分类
private boolean state; //状态 true-未借出 false-已借出
//通过构造函数给定书的属性
public Book(String name,String author,double price,String category,boolean state){
this.author = author;
this.name = name;
this.price = price;
this.category = category;
this.state = state;
}
public Book() { //默认10本书
this(10);
}
public Book(int i) {
Person.books = new Book[i];
}
//属性的获取和设置
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.Scanner;
public class Library {
Scanner sc = new Scanner(System.in);
//登录函数:显示登录界面进行选择登录
public void login() {
int count = 0;
Person p = null;
while (count < 2) {
System.out.println("===============jewel图书管理系统===============");
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(); //调用普通用户的操作方法
}
}
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();//调用管理员的操作方法
}
} break;
}
}
}
}
abstract class Person {
public String name; //姓名
public String sex; //性别
public int age; // 年龄
public static Book[] books = new Book[]{ //默认定义书籍
new Book("西游记","吴承恩",10,"名著",true),
new Book("红楼梦","曹雪芹",20,"名著",true),
new Book("三国演义","罗贯中",15,"名著",true),
new Book("小茗同学","小明",5,"杂志",true),
new Book()
};
public Person(String name,String sex,int age) {
this.age = age;
this.name = name;
this.sex = sex;
}
//get,set方法。
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void operate();//抽象类中的抽象方法
}
public interface operate{ //操作的接口
void select(Book[] books); //查询,查阅
void delete(Book[] books);//删除,借阅
void add(Book[] books); //增加,还书
void list(Book[] books); //显示书列表
}
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Root extends Person implements operate { //继承了人的类,需要重写它的抽象方法operate,实现了operate接口,需要重写接口内的方法
private String Rname;
private String Rsex;
private int Rage;
public Root(String name, String sex, int age) {
super(name, sex, age);//调用人基类的构造函数
}
Scanner sc = new Scanner(System.in);
@Override
public void operate() { //管理员的操作
System.out.print("请输入确认操作:");
int i = sc.nextInt();
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("输入有误!");
break;
}
}
//compareable整理,重写compare to,如果数组中有空的,则会出现空指针异常,把不空的拷到另一个新的数组,然后实现
public void sort(Book[] books) {
System.out.println("图书馆中的书按价格整理如下:");
Arrays.sort(books, new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
return (int)(o1.getPrice()-o2.getPrice());
}
});
for (int i = 0; i < books.length; i++) { //遍历数组打印书的列表
System.out.println(books[i].getName() + " " + books[i].getAuthor() + " " + books[i].getPrice() + " " + books[i].getCategory() + " " + books[i].isState());
}
System.out.println("整理结束!");
}
@Override
public void select(Book[] books) {
System.out.println("请输入书名进行查询:");
String name = sc.next();
if(name != null) {
for (int i = 0; i < books.length; i++) {
if (books[i].getName().equals(name)) {
System.out.println("有此书籍!");
System.out.println("书籍信息为:(书名-作者-价格-分类-状态)");
System.out.println(books[i].getName() + " "+books[i].getAuthor() + " "+books[i].getPrice() + " "+books[i].getCategory() + " "+books[i].isState());
break;
}
System.out.println("暂时没有此书!");
break;
}
}
}
@Override
public void delete(Book[] books) {
System.out.println("请输入要删除的书名:");
String str = sc.next();
if(str != null) {
for(int i = 0;i<books.length;i++) {
if(books[i].getName().equals(str)) {
books[i] = null;
System.out.println("删除成功!");
break;
}
}
}
}
@Override
public void add(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();
for (int i = 0; i < books.length; i++) {
if (books[i].getName() == null) {
books[i].setName(name);
books[i].setAuthor(author);
books[i].setPrice(price);
books[i].setCategory(category);
books[i].setState(true);
System.out.println("添加成功");
break;
}
}
}
@Override
public void list (Book[]books){
System.out.println("图书列表如下:(书名-作者-价格-分类-状态)"); //打印Book数组
for (int i = 0; i < books.length; i++) {
if (books[i] != null) {
System.out.println(books[i].getName() + " " + books[i].getAuthor() + " " + books[i].getPrice() + " " + books[i].getCategory() + " " + books[i].isState());
}
}
}
}
import java.util.Scanner;
User extends Person implements operate { //user类继承了人的类, 实现operate接口
private String Uname;
private String Usex;
private int Uage;
public User(String name, String sex, int age) {
super(name, sex, age); //调用人基类的构造函数(派生类不继承基类的构造函数)
}
Scanner sc = new Scanner(System.in);
@Override
public void operate() { //普通用户的操作
System.out.print("请输入确认操作:");
int i = sc.nextInt();
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("输入有误!");
break;
}
}
@Override
public void select(Book[] books) {
System.out.println("请输入书名进行查询:");
String name = sc.next();
if(name != null) {
for (int i = 0; i < books.length; i++) {
if (books[i].getName().equals(name)) {
System.out.println("有此书籍!");
System.out.println("书籍信息为:(书名-作者-价格-分类-状态)");
System.out.println(books[i].getName() + " "+books[i].getAuthor() + " "+books[i].getPrice() + " "+books[i].getCategory() + " "+books[i].isState());
break;
}
System.out.println("暂时没有此书!");
break;
}
}
}
@Override
public void delete(Book[] books) {
System.out.println("请输入要借阅的书名:");
String name = sc.next();
if (name != null) {
for (int i = 0; i < books.length-1; i++) {
if (books[i].getName().equals(name)) { //借走书时设置状态为false
books[i].setState(false);
System.out.println("借阅成功!");
break;
} else { //没书
System.out.println("没有此书!");
break;
}
}
}
}
@Override
public void add(Book[] books) {
System.out.println("请输入要还的书名:");
String name = sc.next();
if (name != null) {
for (int j = 0; j < books.length-1; j++)
if (books[j].getName().equals(name)) {
books[j].setState(true);
System.out.println("还书成功!");
break;
}
}
}
@Override
public void list(Book[] books) {
System.out.println("图书列表如下:(书名-作者-价格-分类-状态)"); //打印Book数组
for (int i = 0; i < books.length; i++) {
if (books[i] != null) {
System.out.println(books[i].getName() + " " + books[i].getAuthor() + " " + books[i].getPrice() + " " + books[i].getCategory() + " " + books[i].isState());
}
}
}
}
public class Main {
public static void main(String[] args) {
Library library = new Library(); //构造Library对象
library.login(); //调用对象的登录方法。
}
}
如有错误欢迎讨论指正