import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class BookSystem {
static Scanner scanner = new Scanner(System.in);
static File file = new File("user.dat");
static Bookmodify books = new Bookmodify();// 实例化书籍类
static Usermodify userm = new Usermodify();
static String sid = null;
public static void main(String[] args) throws ClassNotFoundException, IOException {
try {
if (file.length() == 0l || file.createNewFile() == true) {// 如果user.dat文件大小为0,或者创建空白文件成功,则表明第一次使用,需要注册管理员账户
firstland();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("创建\"user.dat\"文件错误。");
}
while (true) {// 循环实现选择错误时重新选择
System.out.println("*********************************************");
System.out.println("* *");
System.out.println("* 欢迎使用图书管理系统 *");
System.out.println("* *");
System.out.println("*********************************************");
System.out.println(" 请选择功能:用户登入(A)注册用户(B)退出系统(C) ");
String select = scanner.next();
if (select.equalsIgnoreCase("a")) {// 调用String类中equalsIgnoreCase方法来实现忽略大小写
landselect();// 登陆用户选择方法
} else if (select.equalsIgnoreCase("b")) {
regedit();// 注册方法
} else if (select.equalsIgnoreCase("c")) {
System.exit(1);
} else {
System.out.println("选择错误,请重新选择。");
}
}
}
// 普通用户注册方法
public static void regedit() throws IOException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));// 输入流,读取文件
Collection<User> users = (Collection<User>) in.readObject();
// Collection<User> users =(Collection<User>) in.readObject();
a: while (true) {
System.out.println("请输入ID:");
Iterator<User> t = users.iterator();// 迭代器
String id = scanner.next();
while (t.hasNext()) {
User user = t.next();
if (user.getId().equals(id)) {// 比较集合中的User类中的id是否和用户输入的id相同
System.out.println("此用户名已注册,请重新输入用户名");
continue a;
} else {
continue;
}
}
System.out.println("请输入密码:");
String key = scanner.next();
User user = new User(id, key, false);
users.add(user);// 将新用户写入集合
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));// 输出流,写入文件
out.writeObject(users);// 将集合写入文件
out.close();// 关闭流
in.close();
break;
}
}
// 登陆用户选择方法
public static void landselect() throws FileNotFoundException, ClassNotFoundException, IOException {
while (true) {
System.out.println("1)普通用户登陆 2)管理员用户登陆");
String landselect = scanner.next();
if (landselect.equals("1")) {
landall(false);
break;
} else if (landselect.equals("2")) {
landall(true);
break;
} else {
System.out.println("选择错误,请重新选择");
}
}
}
// landselect()通用登陆方法
public static void landall(boolean admin) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));// 输入流,读取文件
Collection<User> users = (Collection<User>) in.readObject();// 从文件中读出集合
b: while (true) {
if (admin == true) {// 选择了管理员登陆
System.out.println("请输入管理员ID:");
String id = scanner.next();
System.out.println("请输入管理员密码:");
String key = scanner.next();
Iterator<User> t = users.iterator();// 迭代器
while (t.hasNext()) {
User user = t.next();
if (user.getId().equals(id) && user.isAdmin() == true) {// 类中的id和输入的id相同并且类中的管理员标记为真
if (user.getKey().equals(key)) {
System.out.println("管理员登陆成功");
in.close();
bookselect(true);
sid = id;
break b;
} else {
continue;
}
} else {
continue;
}
}
} else {// 选择了普通用户登陆
System.out.println("请输入ID:");
String id = scanner.next();
System.out.println("请输入密码:");
String key = scanner.next();
Iterator<User> t = users.iterator();// 迭代器
while (t.hasNext()) {
User user = t.next();
if (user.getId().equals(id) && user.isAdmin() == false) {// 类中的id和输入的id相同并且类中的管理员标记为假
if (user.getKey().equals(key)) {
System.out.println("登陆成功");
in.close();
bookselect(false);
sid = id;
break b;
} else {
continue;
}
} else {
continue;
}
}
}
System.out.println("登录失败:用户名或密码错误");
}
}
// 第一次使用
public static void firstland() throws FileNotFoundException, IOException {
System.out.println("第一次使用。请注册管理员账户!");
Collection<User> users = new ArrayList<User>();
System.out.println("请输入ID:");
String id = scanner.next();
System.out.println("请输入密码:");
String key = scanner.next();
User user = new User(id, key, true);// 创建用户类
users.add(user);// 将用户类添加到集合
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));// 输出流,写入文件
out.writeObject(users);// 将集合写入文件
out.close();
}
// 登陆成功选择功能方法
public static void bookselect(boolean admin) throws FileNotFoundException, IOException, ClassNotFoundException {
while (true) {
System.out.println("请选择功能:查看图书(A)添加图书(B)修改图书(C)删除图书(D)");
System.out.print("修改用户名(E)修改密码(F)");
if (admin == true) {// 管理员用户多了G H 功能
System.out.println("修改用户(G)删除用户(H)");
System.out.println("查看所有用户(I)注销(Q)");
String s = scanner.next();
if ("g".equalsIgnoreCase(s)) {// 修改用户(G)
userm.userRe(sid);
} else if ("h".equalsIgnoreCase(s)) {// 删除用户(G)
userm.userDel(sid);
} else {
switchs(s);
}
} else {
System.out.println("查看所有用户(I)注销(Q)");
String s = scanner.next();
switchs(s);
}
}
}
public static void switchs(String s) throws FileNotFoundException, IOException, ClassNotFoundException {
if ("a".equalsIgnoreCase(s)) {// 查看图书(A)
books.booklook();
} else if ("b".equalsIgnoreCase(s)) {// 添加图书(B)
books.bookadd();
} else if ("c".equalsIgnoreCase(s)) {// 修改图书(C)
books.bookmodift();
} else if ("d".equalsIgnoreCase(s)) {// 删除图书(D)
books.bookdel();
} else if ("e".equalsIgnoreCase(s)) {// 修改用户名(E)
userm.userName(sid);
} else if ("f".equalsIgnoreCase(s)) {// 修改密码(F)
userm.userKey(sid);
} else if ("i".equalsIgnoreCase(s)) {// 查看所有用户(I)
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));// 输入流,读取文件
Collection<User> users = (Collection<User>) in.readObject();// 从文件中读出集合
Iterator<User> t = users.iterator();// 迭代器
while (t.hasNext()) {
User user = t.next();
System.out.println("用户名:" + user.getId());
}
in.close();
} else if ("q".equalsIgnoreCase(s)) {// 注销(Q)
main(null);
}
}
}
// 用户类,用于存放用户信息
class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2827586337696797367L;
private String id;// 用户名
private String key;// 密码
private boolean admin;// 标记是否是管理员用户
public User(String id, String key, boolean admin) {
super();
this.id = id;
this.key = key;
this.admin = admin;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public boolean isAdmin() {
return admin;
}
public void setAdmin(boolean admin) {
this.admin = admin;
}
}
// 图书类,用于存放书籍信息
class Book implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String bookname;// 书名
private String author;// 作者
private String press;// 出版社
private long price;// 价格
private int id;// 书号
private String date;// 出版日期
public Book(String bookname, String author, String press, long price, int id, String date) {
super();
this.bookname = bookname;
this.author = author;
this.press = press;
this.price = price;
this.id = id;
this.date = date;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public long getPrice() {
return price;
}
public void setPrice(long price) {
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {// 重写toString方法
return getBookname() + " " + getAuthor() + " " + getPress() + " " + getPrice() + " " + getId()
+ " " + getDate() + "\n";
}
}
class Usermodify {// 用户管理类
static File file = new File("user.dat");
static Scanner scanner = new Scanner(System.in);
public void userName(String name) throws FileNotFoundException, IOException, ClassNotFoundException {// 修改用户名
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));// 输入流,读取文件
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));// 输出流,写入文件
Collection<User> users = (Collection<User>) in.readObject();// 从文件中读出集合
Iterator<User> t = users.iterator();// 迭代器
while (t.hasNext()) {
User user = t.next();
if (user.getId().equals(name)) {
System.out.println("请输入新的用户名:");
String newid = scanner.next();
users.remove(user);
user.setId(newid);
users.add(user);
out.writeObject(users);
out.close();
in.close();
}
}
}
public void userKey(String name) throws FileNotFoundException, IOException, ClassNotFoundException {// 修改密码
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));// 输入流,读取文件
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));// 输出流,写入文件
Collection<User> users = (Collection<User>) in.readObject();// 从文件中读出集合
Iterator<User> t = users.iterator();// 迭代器
while (t.hasNext()) {
User user = t.next();
if (user.getId().equals(name)) {
System.out.println("请输入新的密码:");
String newkey = scanner.next();
users.remove(user);
user.setId(newkey);
users.add(user);
out.writeObject(users);
out.close();
in.close();
}
}
}
public void userRe(String name) throws FileNotFoundException, IOException, ClassNotFoundException {// 修改用户
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));// 输入流,读取文件
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));// 输出流,写入文件
Collection<User> users = (Collection<User>) in.readObject();// 从文件中读出集合
Iterator<User> t = users.iterator();// 迭代器
while (t.hasNext()) {
User user = t.next();
if (user.getId().equals(name)) {
System.out.println("请输入新的用户名:");
String newid = scanner.next();
users.remove(user);
user.setId(newid);
users.add(user);
out.writeObject(users);
out.close();
in.close();
}
}
}
public void userDel(String name) throws FileNotFoundException, IOException, ClassNotFoundException {// 删除用户
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));// 输入流,读取文件
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));// 输出流,写入文件
Collection<User> users = (Collection<User>) in.readObject();// 从文件中读出集合
Iterator<User> t = users.iterator();// 迭代器
while (t.hasNext()) {
User user = t.next();
if (user.getId().equals(name)) {
users.remove(user);
out.writeObject(users);
out.close();
in.close();
}
}
}
}
class Bookmodify {// 图书管理类
static Scanner scanner = new Scanner(System.in);
static File fbook = new File("book.dat");
static {
try {
fbook.createNewFile();
} catch (IOException e) {
e.printStackTrace();
System.out.println("创建文件错误。");
}
}
private void nobook() throws FileNotFoundException, IOException, ClassNotFoundException {
System.out.println("图书库无任何书籍,请添加书籍。");
System.out.println("请输入书名:");
String name = scanner.next();
System.out.println("请输入作者:");
String author = scanner.next();
System.out.println("请输入出版社:");
String press = scanner.next();
System.out.println("请输入价格:");
long price = scanner.nextLong();
System.out.println("请输入书号:");
int id = scanner.nextInt();
System.out.println("请输入出版日期:");
String date = scanner.next();
System.out.println(
"新书信息如下: 书名:" + name + "作者:" + author + "出版社:" + press + "价格" + price + "书号" + id + "出版日期" + date);
while (true) {
System.out.println("是否保存该书:(Y)是(N)否");
String s = scanner.next();
if (s.equalsIgnoreCase("y")) {
List<Book> books = new ArrayList<Book>();// 创建集合
Book book = new Book(name, author, press, price, id, date);
books.add(book);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fbook));// 输出流,写入文件
out.writeObject(books);// 将集合写入文件
out.close();
break;
} else if (s.equalsIgnoreCase("n")) {
break;
} else {
System.out.println("没有这个选项,请重新选择!");
}
}
}
// 查看图书方法
public void booklook() throws FileNotFoundException, IOException, ClassNotFoundException {
if (fbook.length() == 0l) {
nobook();
} else {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fbook));// 输入流,读取文件
List<Book> books = new ArrayList<Book>();// 创建集合
books = (List<Book>) in.readObject();// 从文件中读出集合
System.out.println("查看图书");
System.out.println("|书名| |作者| |出版社| |价格| |书号| |出版日期|");
Iterator<Book> t = books.iterator();// 迭代器
while (t.hasNext()) {
Book book = t.next();
System.out.println(book.getBookname() + " " + book.getAuthor() + " " + book.getPress() + " "
+ book.getPrice() + " " + book.getId() + " " + book.getDate());
}
in.close();
while (true) {
System.out.println("请选择查看方式:(A)价格排序(B)作者排序(C)出版日期排序(E)返回上一级");
String s = scanner.next();
if ("a".equalsIgnoreCase(s)) {// (A)价格排序
Collections.sort(books, new Comparator<Book>() {// 匿名内部类实现Comparator接口
/*
* 返回负数表示:o1<o2 返回0表示:o1=o2 返回正数表示o1>o2
*/
@Override
public int compare(Book o1, Book o2) {
if (o1.getPrice() > o2.getPrice()) {
return 1;
} else if (o1.getPrice() == o2.getPrice()) {
return 0;
} else {
return -1;
}
}
});
System.out.println("|书名| |作者| |出版社| |价格| |书号| |出版日期|");
System.out.println(books);
} else if ("b".equalsIgnoreCase(s)) {// (B)作者排序
Collections.sort(books, new Comparator<Book>() {// 匿名内部类实现Comparator接口
@Override
public int compare(Book o1, Book o2) {
return o1.getAuthor().compareTo(o2.getAuthor());
}
});
System.out.println("|书名| |作者| |出版社| |价格| |书号| |出版日期|");
System.out.println(books);
} else if ("c".equalsIgnoreCase(s)) {// (C)出版日期排序
Collections.sort(books, new Comparator<Book>() {// 匿名内部类实现Comparator接口
@Override
public int compare(Book o1, Book o2) {
return o1.getDate().compareTo(o2.getDate());
}
});
System.out.println("|书名| |作者| |出版社| |价格| |书号| |出版日期|");
System.out.println(books);
} else if ("e".equalsIgnoreCase(s)) {// (E)返回上一级
break;
} else {
System.out.println("选择错误,请重新选择!");
}
}
}
}
// 添加图书(B)
public void bookadd() throws FileNotFoundException, IOException, ClassNotFoundException {
if (fbook.length() == 0l) {
nobook();
} else {
System.out.println("请输入书名:");
String name = scanner.next();
System.out.println("请输入作者:");
String author = scanner.next();
System.out.println("请输入出版社:");
String press = scanner.next();
System.out.println("请输入价格:");
long price = scanner.nextLong();
System.out.println("请输入书号:");
int id = scanner.nextInt();
System.out.println("请输入出版日期:");
String date = scanner.next();
System.out.println(
"新书信息如下: 书名:" + name + "作者:" + author + "出版社:" + press + "价格" + price + "书号" + id + "出版日期" + date);
while (true) {
System.out.println("是否保存该书:(Y)是(N)否");
String s = scanner.next();
if (s.equalsIgnoreCase("y")) {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fbook));// 输入流,读取文件
List<Book> books = new ArrayList<Book>();// 创建集合
books = (List<Book>) in.readObject();// 从文件中读出集合
Book book = new Book(name, author, press, price, id, date);
books.add(book);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fbook));// 输出流,写入文件
out.writeObject(books);// 将集合写入文件
out.close();
in.close();
break;
} else if (s.equalsIgnoreCase("n")) {
break;
} else {
System.out.println("没有这个选项,请重新选择!");
}
}
}
}
// 修改图书(C)
public void bookmodift() throws FileNotFoundException, IOException, ClassNotFoundException {
if (fbook.length() == 0l) {
nobook();
} else {
System.out.println("请输入要修改的书号");
int id = scanner.nextInt();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fbook));// 输出流,写入文件
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fbook));// 输入流,读取文件
List<Book> books = new ArrayList<Book>();// 创建集合
books = (List<Book>) in.readObject();// 从文件中读出集合
Iterator<Book> t = books.iterator();// 迭代器
while (t.hasNext()) {
Book book = t.next();
if (id == book.getId()) {
int index = books.indexOf(book);
System.out.println("书名:" + book.getBookname() + "作者:" + book.getAuthor() + "出版社:" + book.getPress()
+ "价格" + book.getPrice() + "书号" + book.getId() + "出版日期" + book.getDate());
while (true) {
System.out.println("请输入要修改的属性:书名修改(A) 作者修改(B) 出版社修改(C) 价格修改(D) 书号修改(E) 出版日期修改(F) 返回上一级(G)");
String s = scanner.next();
if (s.equalsIgnoreCase("a")) {
System.out.println("请输入修改后的书名:");
String name = scanner.next();
book.setBookname(name);
books.remove(index);
books.add(book);
out.writeObject(books);
} else if (s.equalsIgnoreCase("b")) {
System.out.println("请输入修改后的作者:");
String author = scanner.next();
book.setAuthor(author);
books.remove(index);
books.add(book);
out.writeObject(books);
} else if (s.equalsIgnoreCase("c")) {
System.out.println("请输入修改后的出版社:");
String press = scanner.next();
book.setPress(press);
books.remove(index);
books.add(book);
out.writeObject(books);
} else if (s.equalsIgnoreCase("d")) {
System.out.println("请输入修改后的价格:");
long price = scanner.nextLong();
book.setPrice(price);
books.remove(index);
books.add(book);
out.writeObject(books);
} else if (s.equalsIgnoreCase("e")) {
System.out.println("请输入修改后的书号:");
int Id = scanner.nextInt();
book.setId(Id);
books.remove(index);
books.add(book);
out.writeObject(books);
} else if (s.equalsIgnoreCase("f")) {
System.out.println("请输入修改后的出版日期:");
String date = scanner.next();
book.setDate(date);
books.remove(index);
books.add(book);
out.writeObject(books);
} else if (s.equalsIgnoreCase("g")) {
break;
} else {
System.out.println("输入错误,请重新错误。");
}
}
}
}
out.close();
in.close();
}
}
// 删除图书(D)
public void bookdel() throws FileNotFoundException, IOException, ClassNotFoundException {
if (fbook.length() == 0l) {
nobook();
} else {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fbook));// 输出流,写入文件
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fbook));// 输入流,读取文件
List<Book> books = (List<Book>) in.readObject();// 从文件中读出集合
Iterator<Book> t = books.iterator();// 迭代器
while (true) {
System.out.println("请选择要删除的操作 (A)按书名删除 (B)按作者删除(C)按书号删除(E)返回上一级");
String s = scanner.next();
if ("a".equalsIgnoreCase(s)) {
System.out.println("请输入书名:");
String name = scanner.next();
while (t.hasNext()) {
Book book = t.next();
if (book.getBookname().equals(name)) {
while (true) {
System.out.println("书名:" + book.getBookname() + "作者:" + book.getAuthor() + "出版社:"
+ book.getPress() + "价格" + book.getPrice() + "书号" + book.getId() + "出版日期"
+ book.getDate());
System.out.println("是否确定删除(Y)是(N)否");
String ss = scanner.next();
if (ss.equalsIgnoreCase("y")) {
System.out.println("正在删除......");
int index = books.indexOf(book);
books.remove(index);
out.writeObject(books);
break;
} else if (ss.equalsIgnoreCase("n")) {
break;
} else {
System.out.println("没有这个选项,请重新选择!");
}
}
}
}
} else if ("b".equalsIgnoreCase(s)) {
System.out.println("请输入作者:");
String author = scanner.next();
while (t.hasNext()) {
Book book = t.next();
if (book.getAuthor().equals(author)) {
while (true) {
System.out.println("书名:" + book.getBookname() + "作者:" + book.getAuthor() + "出版社:"
+ book.getPress() + "价格" + book.getPrice() + "书号" + book.getId() + "出版日期"
+ book.getDate());
System.out.println("是否确定删除(Y)是(N)否");
String ss = scanner.next();
if (ss.equalsIgnoreCase("y")) {
System.out.println("正在删除......");
int index = books.indexOf(book);
books.remove(index);
out.writeObject(books);
break;
} else if (ss.equalsIgnoreCase("n")) {
break;
} else {
System.out.println("没有这个选项,请重新选择!");
}
}
}
}
} else if ("c".equalsIgnoreCase(s)) {
System.out.println("请输入书号:");
int id = scanner.nextInt();
while (t.hasNext()) {
Book book = t.next();
if (book.getId() == id) {
while (true) {
System.out.println("书名:" + book.getBookname() + "作者:" + book.getAuthor() + "出版社:"
+ book.getPress() + "价格" + book.getPrice() + "书号" + book.getId() + "出版日期"
+ book.getDate());
System.out.println("是否确定删除(Y)是(N)否");
String ss = scanner.next();
if (ss.equalsIgnoreCase("y")) {
System.out.println("正在删除......");
int index = books.indexOf(book);
books.remove(index);
out.writeObject(books);
break;
} else if (ss.equalsIgnoreCase("n")) {
break;
} else {
System.out.println("没有这个选项,请重新选择!");
}
}
}
}
} else if ("e".equalsIgnoreCase(s)) {
break;
} else {
System.out.println("选择错误,请重新选择");
}
}
out.close();
in.close();
}
}
}
图书管理系统(有BUG未修复)
最新推荐文章于 2021-11-02 18:00:13 发布