Java图书管理系统需要哪些代码?
长春中软卓越
发表于:2022-07-26 14:31:441.Book类
public 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 String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public boolean getState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
}
2.Library类
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("欢迎讨论指正! qq505791225"+"\n");
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;
}
}
}
}
3.Person类(抽象类)
abstract class Person {
public String name;
public String sex;
public int age;
/**
* 默认定义10本书籍。(预留3本新书的buffer,用来添加书籍)
*/
public static Book[] books = new Book[]{
new Book("西游记", "吴承恩", 10, "名著", true),
new Book("红楼梦", "曹雪芹", 20, "名著", true),
new Book("三国演义", "罗贯中", 15, "名著", true),
new Book("小茗同学", "小明", 5, "杂志", true),
new Book("C与指针", "马老师", 35, "计算机", true),
new Book("Java编程思想", "王老师", 55, "计算机", true),
new Book("新世界", "陈宇", 15, "杂志", true),
new Book("阿衰与大头妹", "阿衰", 5, "漫画", true),
new Book("一个人就一个人", "刘同", 35, "小说", true),
new Book("活着", "陈彤", 36, "小说", true),
new Book(),
new Book(),
new Book(),
};
public Person(String name, String sex, int age) {
this.age = age;
this.name = name;
this.sex = sex;
}
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();
}
4.operate接口
public interface operate{ //操作的接口
void select(Book[] books); //查询,查阅
void delete(Book[] books);//删除,借阅
void add(Book[] books); //增加,还书
void list(Book[] books); //显示书列表
}
5.Root类
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Root extends Person implements Operate {
//继承了人的类,需要重写它的抽象方法operate,实现了operate接口,需要重写接口内的方法
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,如果数组中有空的,则会出现空指针异常,把不空的拷到另一个新的数组,然后实现
* @param books
*/
public void sort(Book[] books) {
System.out.println("图书馆中的书按价格整理如下:" + "\n" + "【书名】—— 【作者】——【价格】——【分类】——状态");
Arrays.sort(books, new Comparator() {
@Override
public int compare(Book o1, Book o2) {
return (int) (o1.getPrice() - o2.getPrice());
}
});
//遍历数组打印书的列表
for (int i = 0; i < books.length; i++) {
if (books[i].price != 0) {
System.out.println(
"【" + books[i].getName() + "】—— 【" + books[i].getAuthor() + "】——【" + books[i].getPrice()
+ "】——【" + books[i].getCategory() + "】——" + books[i].getState());
}
}
}
@Override
public void select(Book[] books) {
System.out.println("请输入书名进行查询:");
String name = sc.next();
if (name != null) {
boolean flag = false;
int bookNum = 0;
for (int i = 0; i < books.length; i++) {
if (books[i].getName() != null && name.equals(books[i].getName())) {
flag = true;
bookNum = i;
break;
}
}
if (!flag) {
System.out.println("未查询到此书籍!");
} else {
System.out.println("书籍信息为:" + "\n" + "【书名】—— 【作者】——【价格】——【分类】——状态");
System.out.println(
"【" + books[bookNum].getName() + "】—— 【" + books[bookNum].getAuthor() + "】——【" + books[bookNum]
.getPrice() + "】——【" + books[bookNum].getCategory() + "】——" + books[bookNum]
.getState());
}
}
}
@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();
boolean flag = false;
for (int i = 0; i < books.length; i++) {
if (books[i].getName() == null) {
flag = true;
books[i].setName(name);
books[i].setAuthor(author);
books[i].setPrice(price);
books[i].setCategory(category);
books[i].setState(true);
System.out.println("添加成功");
break;
}
}
if (!flag) {
System.out.println("【警告】数组预留buffer多只能添加3本新书,没有位置放置新书了,如需添加多余3本,需要在‘Person'类中添加buffer!" + "\n");
}
}
@Override
public void list(Book[] books) {
System.out.println("图书列表如下:" + "\n" + "【书名】—— 【作者】——【价格】——【分类】——状态"); //打印Book数组
for (int i = 0; i < books.length; i++) {
if (books[i] != null) {
if (books[i].price != 0) {
System.out.println(
"【" + books[i].getName() + "】—— 【" + books[i].getAuthor() + "】——【" + books[i].getPrice()
+ "】——【" + books[i].getCategory() + "】——" + books[i].getState());
}
}
}
}
}
6.User类
import java.util.Scanner;
public class User extends Person implements Operate { //user类继承了人的类, 实现operate接口
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) {
boolean flag = false;
int bookNum = 0;
for (int i = 0; i < books.length; i++) {
if (books[i].getName() != null && name.equals(books[i].getName())) {
flag = true;
bookNum = i;
break;
}
}
if (!flag) {
System.out.println("未查询到此书籍!");
} else {
System.out.println("书籍信息为:" + "\n" + "【书名】—— 【作者】——【价格】——【分类】——状态");
System.out.println(
"【" + books[bookNum].getName() + "】—— 【" + books[bookNum].getAuthor() + "】——【" + books[bookNum]
.getPrice() + "】——【" + books[bookNum].getCategory() + "】——" + books[bookNum]
.getState());
}
}
}
@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
if (!books[i].getState()) {
System.out.println("【" + name + "】" + "已经被借出!");
} else {
books[i].setState(false);
System.out.println("借阅成功!");
}
break;
}
}
}
}
@Override
public void add(Book[] books) {
System.out.println("请输入要还的书名:");
String name = sc.next();
if (name != null) {
boolean flag = true;
int i = 0;
while (flag) {
if (books[i].getName().equals(name)) {
if (!books[i].getState()) {
books[i].setState(true);
System.out.println("还书成功!");
flag = false;
} else {
System.out.println("该书籍已经被还回!");
break;
}
}
i++;
}
}
}
@Override
public void list(Book[] books) {
System.out.println("图书列表如下:" + "\n" + "【书名】—— 【作者】——【价格】——【分类】——状态"); //打印Book数组
for (int i = 0; i < books.length; i++) {
if (books[i] != null) {
if (books[i].price != 0) {
System.out.println(
"【" + books[i].getName() + "】—— 【" + books[i].getAuthor() + "】——【" + books[i].getPrice()
+ "】——【" + books[i].getCategory() + "】——" + books[i].getState());
}
}
}
}
}
7.Main类
public class Main {
public static void main(String[] args) {
Library library = new Library(); //构造Library对象
library.login(); //调用对象的登录方法。
}
}
Java图书管理系统需要哪些代码? 原创内容,请点击长春软件系统培训