public class 图书管理系统{
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Register().setVisible(true);
}
});
```
// 图书管理类publicclassBookManage{BookSet bookSet=newBookSet();/**
* 初始化
*/publicvoidinitial(){book java=newbook();java.setBook("java",1,2021/4/15",20);
book renyue = new book();
renyue.name = "程序语言基础";
renyue.setBook(."程序语言基础",1 ,null,0);
book jichu = new book();
jichu.setBook("计算机基础",1,null,0);
bookSet.books[0] = java ;
bookSet.books[1] = renyue ;
bookSet.books[2] = jichu ;
}
/*
菜单切换
*/
public void startMenu(){
boolean flag = true ;
do {
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("*********************");
System.out.println("请选择");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt() ;
switch (choice){
case 1:
System.out.println("新增图书");
add();
break;
case 2:
System.out.println("查看图书");
chakan();
break;
case 3:
System.out.println("删除图书");
shan();
break;
case 4:
System.out.println("借出图书");
jie();
break;
case 5:
System.out.println("归还图书");
huan();
break;
case 6:
System.out.println("退出,谢谢使用");
default:
System.out.println("输入不符合要求请重新选择");
}
}while (flag) ;
}
/**
* 查看图书信息
*/
public void chakan(){
System.out.println("编号 \t\t\t名称 \t\t\t状态 \t\t\t借出日期 \t\t\t库存");
for (int i = 0 ; i < this.bookSet.books.length ; i ++){
if ( this.bookSet.books[i]!=null){
this.bookSet.books[i].dayin(i+1);
}
}
}
/**
* 添加图书
*/
public void add(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入图书的名称:");
String name = scanner.next();
System.out.println("请输入库存:");
int number = scanner.nextInt() ;
book newBook = new book();
newBook.setBook(name , newBook.state, newBook.date, newBook.number);
for (int i = 0 ; i < bookSet.books.length ; i++){
if (bookSet.books[i] == null){
bookSet.books[i] = newBook ; //把新建的对象放在数组中的第一个空位置
bookSet.books[i].number = number ; //库存
System.out.println("添加成功");
break; //后续的空位置直接跳过
}
}
}
/**
* 归还
*/
public void huan(){
System.out.println("请输入要归还书的名称");
Scanner scanner = new Scanner(System.in);
String huan = scanner.next();
boolean flag = true ; //判断是否归还成功
System.out.println("请输入归还日期");
String riqi = scanner.next() ;
for (int i = 0 ; i < bookSet.books.length ; i ++){
if ( bookSet.books[i].name.equals(huan) &&bookSet.books[i].state == 0 ) { // 在书的状态为一借出的状态下可以归还
bookSet.books[i].state = 1 ;
bookSet.books[i].date = riqi ;
}else {
System.out.println("归还成功");
flag = true ;
break;
}
}
}
/**
* 借书
*/
public void jie(){
System.out.println("请输入要借书的名称");
Scanner scanner = new Scanner(System.in);
String jie = scanner.next();
boolean flag = true ;
System.out.println("请输入借书的日期");
String riqi = scanner.next() ;
for (int i = 0 ; i < bookSet.books.length ; i ++){
if (bookSet.books[i] != null && bookSet.books[i].name.equals(jie)){
if (bookSet.books[i].state == 1){
bookSet.books[i].state = 0 ;
flag = true ;
bookSet.books[i].date=riqi;
}
break;
}
}
if (flag){
System.out.println("借出成功");
}else {
System.out.println("借出失败");
}
}
/**
* 删除
*/
public void shan(){
System.out.println("请输入删除图书的编号:");
Scanner scanner = new Scanner(System.in);
int bianhao = scanner.nextInt();
boolean flag = true ;
for (int i = 0 ; i< bookSet.books.length ; i ++){
if (bookSet.books[i] != null &&(i+1) == bianhao){
if (bookSet.books[i].state != 0) {
int j = i;
while (bookSet.books[j + 1] != null) {
//后面元素向前移动
bookSet.books[j] = bookSet.books[j + 1];
j++;
}
bookSet.books[j] = null;
flag = true;
break; //后面元素没必要执行
}else {
System.out.println("图书为借出状态不能删除");
flag= true ;
}
}else {
flag = false ;
}
}
if (flag){
System.out.println("删除成功");
}else {
System.out.println("删除失败");}}}
```