详细步骤概述
步骤一:创建类Tushuxitong
package edu.tuhsu;
public class Tushuxitong {
/**
* 图书类
*/
// 名字 状态 借出日期 次数
public Stringname; //图书名称
public int state; //图书状态 0已借出1可借
public Stringdate; //借出日期
public double number; // 库存
/*
图书信息展示*/
public void dayin(int index) {
System.out.println(index +"\t\t\t\t" +name +"\t\t\t\t" +state +"\t\t\t\t" +date +"\t\t\t\t" +number);
}
public void setBook(String mingzi, int zhuangtai, String riqi, double kucun) {
this.name = mingzi;
this.state = zhuangtai;
this.date = riqi;
this.number = kucun;
}
}
步骤二:创建类Tushuxitong2
package edu.tuhsu;
public class Tushuxitong2 {
Tushuxitong[]books =new Tushuxitong[50];
}
步骤三:创建类Tushuxitong3
Tushuxitong03中含有initial、startMenu。
package edu.tuhsu;
import java.util.Scanner;
public class Tushuxitong3 {
Tushuxitong2bookSet =new Tushuxitong2();
/**
* 初始化
*/
public void initial() {
Tushuxitong java =new Tushuxitong();
java.setBook("《java基础》", 1, "1990/2/3", 20);
Tushuxitong renyue =new Tushuxitong();
renyue.setBook("《软件技术》", 1, null, 0);
Tushuxitong jichu =new Tushuxitong();
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("--------------------------------------------");
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("新增图书");
zeng();
break;
case 2:
System.out.println("查看图书");
kan();
break;
case 3:
System.out.println("删除图书");
shanchu();
break;
case 4:
System.out.println("借出图书");
jiechu();
break;
case 5:
System.out.println("归还图书");
guihuan();
break;
case 6:
System.out.println("谢 谢 使 用 !");
default:
System.out.println("输入不符合要求请重新选择");
}
}while (flag);
}
private void kan() {
System.out.println("编号 \t\t\t 名称 \t\t\t\t 状态 \t\t\t 借出日期 \t\t\t 库存");
for (int i =0; i
if (this.bookSet.books[i] !=null) {
this.bookSet.books[i].dayin(i +1);
}
}
}
private void zeng() {
Scanner scanner =new Scanner(System.in);
System.out.println("请输入图书的名称:");
String name = scanner.next();
System.out.println("请输入库存:");
int number = scanner.nextInt();
Tushuxitong newBook =new Tushuxitong();
newBook.setBook(name, newBook.state, newBook.date, newBook.number);
for (int i =0; i
if (bookSet.books[i] ==null) {
bookSet.books[i] = newBook; //把新建的对象放在数组中的第一个空位置
bookSet.books[i].number = number; //库存
System.out.println("添加成功");
break; //后续的空位置直接跳过
}
}
}
private void guihuan () {
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
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;
}
}
}
private void jiechu () {
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
if (bookSet.books[i] !=null &&bookSet.books[i].name.equals(jie)) {
if (bookSet.books[i].state ==1) {//在书的状态为1(可借)可以借出
bookSet.books[i].state =0; //借出后把书的状态改为0(不可借)
flag =true;
bookSet.books[i].date = riqi; //日期改为现在借书的日期
}
break;
}
}
if (flag) {
System.out.println("借出成功");
}else {
System.out.println("借出失败");
}
}
private void shanchu () {
System.out.println("请输入删除图书的编号:");
Scanner scanner =new Scanner(System.in);
int bianhao = scanner.nextInt();
boolean flag =true;
for (int i =0; 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("删除失败");
}
}
}
步骤四:创建测试类Tushuxitong4
package edu.tuhsu;
public class Tushuxitong4 {
public static void main(String[] args) {
Tushuxitong3 bookManage = new Tushuxitong3();
bookManage.initial();
bookManage.startMenu();
}
}
运行结果显示:1