第一个文件:主函数和添加图书,删除图书,查看图书,修改图书,查看图书借阅状态,借阅图书,还书7种方法的代码块
package TUshu;
import jdk.swing.interop.SwingInterOpUtils;
import java.util.ArrayList;
import java.util.Scanner;
public class MUlu {
public static void main(String[] args) {
ArrayList<book> Book = new ArrayList<>();
while (true) {
System.out.println("--------欢迎来到图书管理系统-------");
System.out.println("1.添加图书");
System.out.println("2.删除图书");
System.out.println("3.查看图书");
System.out.println("4.修改图书");
System.out.println("5.查看图书借阅状态");
System.out.println("6.借阅图书");
System.out.println("7.还书:");
System.out.println("8.退出");
Scanner sc = new Scanner(System.in);
int may = sc.nextInt();
if(may == 1)
{
addbook(Book);
}
if(may == 2)
{
deletebook(Book);
}
if(may == 3)
{
ckbook(Book);
}
if(may ==4)
{
changebook(Book);
}
if(may == 5)
{
jkbook(Book);
}
if(may == 6)
{
jiebook(Book);
}
if(may == 7)
{
hbook(Book);
}
if(may == 8)
{
break;
}
}
}
public static void addbook(ArrayList<book> Book)
{
Scanner sc = new Scanner(System.in);
book s = new book();
String temp;
System.out.println("请输入书名");
temp = sc.nextLine();
s.setName(temp);
System.out.println("请输入作者");
temp = sc.nextLine();
s.setAuthor(temp);
System.out.println("请输入价格");
temp = sc.nextLine();
s.setPrice(temp);
s.setStatus("在馆");
Book.add(s);
System.out.println("信息录入成功");
}
public static void deletebook(ArrayList<book> Book)
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的书名");
String b = sc.nextLine();
int flag = 0;
for(int i=0;i<Book.size();i++)
{
book t = Book.get(i);
if(b.equals(t.getName())&&t.getStatus().equals("在馆"))
{
flag =1;
Book.remove(i);
}
}
if(flag == 1)
System.out.println("删除成功");
else
System.out.println("删除失败,可能原因:1.书籍名称对应错误。2.书籍已经被借阅");
}
public static void ckbook(ArrayList<book> Book)
{
int j = 0;
for(int i=0;i<Book.size();i++)
{
j++;
book t = Book.get(i);
System.out.println("书名:"+t.getName());
System.out.println("作者:"+t.getAuthor());
System.out.println("价格:"+t.getPrice());
}
if(j==0)
{
System.out.println("当前无书籍信息,请录入信息后再进行查询操作");
}
}
public static void changebook(ArrayList<book>Book)
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要更改信息的书名");
String t =sc.nextLine();
int flag = 0;
for(int i=0;i<Book.size();i++)
{
book s =Book.get(i);
if(t.equals(Book.get(i).getName()))
{
System.out.println("-------请输入你要修改的信息------");
System.out.println("1:书名");
System.out.println("2:作者");
System.out.println("3:价格");
int tt = sc.nextInt();
if(tt == 1)
{
flag =1;
String name1 = sc.nextLine();
s.setName(name1);
}
if(tt ==2)
{
flag =1;
String author1 = sc.nextLine();
s.setAuthor(author1);
}
if(tt == 3)
{
flag =1;
String price1 = sc.nextLine();
s.setPrice(price1);
}
}
}
if(flag == 1)
{
System.out.println("更改成功");
}else
{
System.out.println("更改失败,可能为信息输入错误");
}
}
public static void jkbook(ArrayList<book>Book)
{
System.out.println("请输入你要查阅的书名:");
Scanner sc = new Scanner(System.in);
String t = sc.nextLine();
int num=0;
int num1=0;
for(int i=0;i<Book.size();i++)
{
book tt = Book.get(i);
if(t.equals(tt.getName()))
{
num++;
if(tt.getStatus().equals("在馆"))
{
num1++;
}
}
}
System.out.println("你查阅的书籍共有"+num+"本,其中在馆有"+num1+"本");
}
public static void jiebook(ArrayList<book>Book)
{
System.out.println("请输入你要借阅的书:");
Scanner sc = new Scanner(System.in);
String t = sc.nextLine();
int flag =0;
for(int i=0;i<Book.size();i++)
{
book tt = Book.get(i);
if(t.equals(tt.getName())&&tt.getStatus().equals("在馆"))
{
flag = 1;
tt.setStatus("已被借阅");
break;
}
}
if(flag == 1)
{
System.out.println("借阅成功,请在40天后归还此书");
}
}
public static void hbook(ArrayList<book>Book)
{
System.out.println("请输入你要归还的书籍名称");
Scanner sc = new Scanner(System.in);
String t =sc.nextLine();
int flag =0;
for(int i=0;i<Book.size();i++)
{
book tt = Book.get(i);
if(t.equals(tt.getName())&&tt.getStatus().equals("已被借阅"))
{
flag = 1;
tt.setStatus("在馆");
break;
}
}
if(flag == 1)
{
System.out.println("书籍归还成功");
}else if(flag == 0)
{
System.out.println("书籍归还失败,请检查输入名称");
}
}
}
第二个文件:书籍的管理信息
package TUshu;
public class book {
private String name;
private String author;
private String price;
private String status;
public book(){
}
public book(String name,String author,String price,String status)
{
this.name = name;
this.author = author;
this.price = price;
this.status = status;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getAuthor()
{
return this.author;
}
public void setPrice(String price)
{
this.price = price;
}
public String getPrice()
{
return this.price;
}
public void setStatus(String status)
{
this.status = status;
}
public String getStatus()
{
return this.status;
}
}
**