//建议采用不同的类来保存众多方法
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Main {
static Scanner input = new Scanner(System.in);
static class Book{ //图书类 //内部静态类等于外部类
String name;
String id;
double price;
int count;
public Book() {}
public Book(String name, String id, double price, int count) {
super();
this.name = name;
this.id = id;
this.price = price;
this.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public String toString() {
return name+"\t"+id+"\t"+price+"\t"+count;
}
}
static ArrayList<Book> list = new ArrayList<>(); //图书集合库
public static void main(String[] args) {
int select = login(); //登录
Library(); //图书库
Judge(select); //判断选项
Again(select); //重复操作
}
public static int login() { //登录方法
String accout; //账号
String password; //密码
System.out.println("******************************************************************");
System.out.println(" 欢迎使用X13图书管理系统");
System.out.println("******************************************************************");
System.out.println("1·游客登入 2·管理员登录 3·退出系统");
System.out.print(" 请进行你的操作:");
Scanner input = new Scanner(System.in);
int ant = input.nextInt(); //保存选项
if(ant ==1) {
System.out.println("请输入姓名:");
String name = input.next();
System.out.println("登陆成功!欢迎您:"+name);
System.out.println("------ welcome to the Library ! -------------------------------");
return ant;
}
else if(ant == 2) {
String accouts = JOptionPane.showInputDialog("请输入账号:"); //输入框
accout = accouts;
String passwords = JOptionPane.showInputDialog("请输入密码:");
password = passwords;
for(int i = 2;i>0;i--) {
if("654321".equals(accout) && "123456".equals(password))
{
System.out.println("登录成功!欢迎您管理员!");
System.out.println("------ welcome to the Library Management System! ----------------");
return ant;
}
else
{
System.out.println("账号或者密码不正确,请重新输入!剩余输入机会;"+i);
accouts = JOptionPane.showInputDialog("请输入账号:");
accout = accouts;
passwords = JOptionPane.showInputDialog("请输入密码:");
password = passwords;
}
}
System.out.println("登录失败!");
}
else
System.out.println("系统已退出!");
System.exit(0);
return ant;
}
public static void Library() { //图书库存
System.out.println(" 图书书库");
System.out.println("书名\t编号\t 价格\t数量");
list.add(new Book("西游记","001",35.00,50));
list.add(new Book("红楼梦","002",28.00,50));
list.add(new Book("三国演义","003",32.00,50));
list.add(new Book("水浒传","004",34.00,50));
Iterator it = list.iterator();
while(it.hasNext())
{
System.out.println(it.next()); //迭代器遍历图书库
}
System.out.println("--------------------------------------------------------");
}
public static void Judge(int i) { //判断游客登入还是管理员
if(i == 1) { //游客登入
Visitor(); //调用游客界面
}
if(i == 2) { //管理员登入
Administrators(); //调用管理员界面
}
}
public static void Visitor() { //游客界面
System.out.println("X13图书管理系统(普通用户)>>主菜单");
System.out.println("1·借书 2·还书 3·退出系统");
System.out.println("请输入你要进行的操作:");
int nums = input.nextInt(); //保存选择
switch (nums) {
case 1:
Borrow(); //调用借书方法
break;
case 2:
Return(); //调用还书方法
break;
case 3:
System.out.println(" X13图书管理系统系统已退出!");
System.exit(0);
break;
default:
System.out.println(" 错误选项!系统已退出!");
System.exit(0);
break;
}
}
public static void Administrators() { //管理员界面
System.out.println("X13图书管理系统(管理员)>>主菜单");
System.out.println("1·借书 2·还书 3·增加书籍 4·删除书籍 5·退出系统");
System.out.println("请输入你要进行的操作:");
int nums = input.nextInt(); //保存选择
switch (nums) {
case 1:
Borrow(); //调用借书方法
break;
case 2:
Return(); //调用还书方法
break;
case 3:
addBook(); //调用增加书籍的方法
break;
case 4:
deleteBook(); //调用删除书籍的方法
break;
case 5:
System.out.println(" X13图书管理系统系统已退出!");
System.exit(0);
break;
default:
System.out.println(" 错误选项!系统已退出!");
System.exit(0);
break;
}
}
public static void Borrow(){ //借书
int i = -1; //控制循环钥匙
System.out.println("请输入你要借的书的书名:");
String Bname = input.next();
for (Book book : list) {
if(Bname.equals(book.name))
{
System.out.println("请输入借的书的数量:");
int counts = input.nextInt();
book.setCount(book.getCount()-counts);
System.out.println("借书成功!");
i = 1;
break;
}
}
if(i==-1) //没有找到
{
while(true) { //找了一圈没找到
System.out.println("查找失败,请重新输入:");
Bname = input.next();
for (Book book : list) {
if(Bname.equals(book.name))
{
System.out.println("请输入借的书的数量:");
int counts = input.nextInt();
book.setCount(book.getCount()-counts);
System.out.println("借书成功!");
i = 1;
break;
}
}
if(i != -1)
{
break;
}
}
}
System.out.println("--------------------------------------------------------");
System.out.println(" 图书书库");
System.out.println("书名\t编号\t 价格\t数量");
for (Book book : list) {
System.out.println(book);
}
System.out.println("--------------------------------------------------------");
}
public static void Return(){ //还书
int i = -1; //控制循环钥匙
System.out.println("请输入你要还的书的书名:");
String Bname = input.next();
for (Book book : list) {
if(Bname.equals(book.name))
{
System.out.println("请输入还的书的数量:");
int counts = input.nextInt();
book.setCount(book.getCount()+counts);
System.out.println("还书成功!");
i = 1;
break;
}
}
if(i==-1) //没有找到
{
while(true) { //找了一圈没找到
System.out.println("查找失败,请重新输入:");
Bname = input.next();
for (Book book : list) {
if(Bname.equals(book.name))
{
System.out.println("请输入还的书的数量:");
int counts = input.nextInt();
book.setCount(book.getCount()+counts);
System.out.println("还书成功!");
i = 1;
break;
}
}
if(i != -1)
{
break;
}
}
}
System.out.println("--------------------------------------------------------");
System.out.println(" 图书书库");
System.out.println("书名\t编号\t 价格\t数量");
for (Book book : list) {
System.out.println(book);
}
System.out.println("--------------------------------------------------------");
}
public static void addBook(){ //增加书籍(管理员)
System.out.println("请输入你要增加的书籍名:");
String Bname = input.next();
for (Book book : list) {
if(Bname.equals(book.name))
{
while(true) {
System.out.println("该书名已存在,请重新输入:");
Bname = input.next();
if(!Bname.equals(book.name))
{
break;
}
}
}
}
System.out.println("请输入你要增加的书籍编号:");
String id = input.next();
for (Book book : list) {
if(id.equals(book.id))
{
while(true) {
System.out.println("该编号已存在,请重新输入:");
id = input.next();
if(!id.equals(book.id))
{
break;
}
}
}
}
System.out.println("请输入你要增加的书籍价格:");
Double price = input.nextDouble();
System.out.println("请输入你要增加的书籍数量:");
int counts = input.nextInt();
list.add(new Book(Bname,id,price,counts)); //增加书籍
System.out.println(" 添加成功!!!");
System.out.println("--------------------------------------------------------");
System.out.println(" 图书书库");
System.out.println("书名\t编号\t 价格\t数量");
for (Book book : list) {
System.out.println(book);
}
System.out.println("--------------------------------------------------------");
}
public static void deleteBook(){ //删除图书(管理员)
int i = -1; 控制循环钥匙
System.out.println("请输入你要删除的书籍名:");
String Bname = input.next();
for (Book book : list) {
if(Bname.equals(book.name))
{
list.remove(book);//删除找到的图书
i = 1;
break;
}
}
while(true) { //找了一圈没找到
if(i==-1)
{
System.out.println("没有找到该图书,请重新输入:");
Bname = input.next();
for (Book book : list) {
if(Bname.equals(book.name))
{
list.remove(book);//删除找到的图书
i = 1;
break;
}
}
}
if(i != -1)
{
break;
}
}
System.out.println(" 删除成功!!!");
System.out.println("--------------------------------------------------------");
System.out.println(" 图书书库");
System.out.println("书名\t编号\t 价格\t数量");
for (Book book : list) {
System.out.println(book);
}
System.out.println("--------------------------------------------------------");
}
public static void Again(int i) { //是否还要进行其他操作
while(true) {
System.out.println("请问是否要进行其他操作(y/n):");
String op = input.next();
if(op.equalsIgnoreCase("y"))
{
if(i==1) //是游客登入
{
Visitor();
}
else if(i==2) //管理员登入
{
Administrators();
}
}
else
System.exit(0); //退出系统
}
}
}