赞
踩
一.写主类
public class Message {
//属性
private String name;
private int state;
private int date;
private int count;//构造器
public Message() {
super();
}public Message(String name, int state, int date, int count) {
super();
this.name = name;
this.state = state;
this.date = date;
this.count = count;
}//get set 方法
public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public int getState() {
return state;
}public void setState(int state) {
this.state = state;
}public int getDate() {
return date;
}public void setDate(int date) {
this.date = date;
}public int getCount() {
return count;
}public void setCount(int count) {
this.count = count;
}}
二.初始化三本书
Scanner sc = new Scanner(System.in);
Message[] arr = new Message[6];
Message[] brr=new Message[4];
// 原本就已经被借的书,创建代码块。创建对象代码块里面执行
Message m1 = new Message("罗马假日", 1, 1, 15);
arr[0] = m1;
Message m2 = new Message("白雪公主", 0, 0, 12);
arr[1] = m2;
Message m3 = new Message("葫芦兄弟", 0, 0, 30);
arr[2] = m3;
三.完整图书管理系统代码
- import java.util.Scanner;
-
- public class Operate {
- Scanner sc = new Scanner(System.in);
- Message[] arr = new Message[6];
- Message[] brr=new Message[4];
- // 原本就已经被借的书,创建代码块。创建对象代码块里面执行
- {
- Message m1 = new Message("罗马假日", 1, 1, 15);
- arr[0] = m1;
- Message m2 = new Message("白雪公主", 0, 0, 12);
- arr[1] = m2;
- Message m3 = new Message("葫芦兄弟", 0, 0, 30);
- arr[2] = m3;
- }
- boolean flag2 = false;
- boolean flag3 = false;
- public int start() {
- 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("请输入您想选择的序号");
- int input = sc.nextInt();
- return input;
- }
-
- public void choose(int input) {
-
- switch (input) {
- case 1:
- // 新增图书
- add();
- break;
- case 2:
- //查看图书
- look();
- break;
- case 3:
- delete();
- //删除图书
- break;
- case 4:
- borrow();
- //借出图书
- break;
- case 5:
- //归还图书
- revert();
- break;
- case 6:
- System.out.println("退出系统");
- break;
- default:
- System.out.println("您输入错误,请您重新输入");// 输入0时可以继续循环
- break;
- }
-
- }
-
- public boolean result() {
- System.out.println("请输入一个数字,输入0时返回");
- int i = sc.nextInt();
- return i == 0 ? false : true;
-
- }
-
- public void add() {
- System.out.println("-->新增图书\n");
- System.out.println("请输入图书名称:");
- String name = sc.next();
- System.out.println("请输入借阅状态");
- int jie = sc.nextInt();
- while (jie < 0 || jie > 1) {
- System.out.println("您输入错误请重新输入");
- jie = sc.nextInt();
- }
- System.out.println("请输入借出日期");
- int ri = sc.nextInt();
- while (jie < 0 || jie > 30) {
- System.out.println("您输入错误请重新输入");
- ri = sc.nextInt();
- }
- System.out.println("请输入借出次数");
- int ci = sc.nextInt();
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] == null) {
- flag2 = true;
- Message message = new Message(name, jie, ri, ci);
- arr[i]=message;
- System.out.println("添加成功名字是:" + name);
- System.out.println("请输入借阅状态" + jie);
- System.out.println("请输入借出日期" + ri);
- System.out.println("请输入借出次数" + ci);
- break;
- }
-
- }
- if (!flag2) {
- System.out.println("抱歉,新增《" + name + "》失败!货架已满!");
- }
- }
-
- public void look() {
- System.out.println("查看图书");
- System.out.println("序号\t状态\t名称\t借出日期\t借出次数");
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] != null) {
- String BookState = (arr[i].getState() == 0) ? "可借阅" : "已借出";
- String dateStr = (arr[i].getDate() == 0) ? "" :
- (arr[i].getDate() + "日");
- System.out.println((i + 1) + "\t" + BookState + "\t"
- + arr[i].getName() + "\t" + dateStr + "\t"
- + arr[i].getCount() + "次");
-
- }
- }
- }
-
- public void delete(){
- System.out.println("-->删除图书\n");
- System.out.println("请输入要删除图书的序号:");
- int delete=sc.nextInt();
- int index=-1;
- for(int i=0;i<arr.length;i++){
- if(arr[i]!=null&&arr[i].getState()==0&&delete==i+1){
- index=i;
-
- break;
- }else if(arr[i]!=null&&arr[i].getState()!=0&&delete==i+1){
- System.out.println("该图书已被借出,目前无法删除!");
- break;
- }
- }
- if(index!=-1){
- for(int j=index+1;j<arr.length;j++){
- arr[j-1]=arr[j];
-
- }
- System.out.println("图书删除成功!");
- }
- }
-
- public void borrow(){
- System.out.print("请输入想借图书名字:");
- String want=sc.next();
- System.out.println("请输入要借的日期,日期不能超过三十天");
- int day=sc.nextInt();
- while(day<0||day>30){
- System.out.println("您输入错误,请您重新输入");
- day=sc.nextInt();
- }
- for(int i=0;i<arr.length;i++){
- if(arr[i]==null){
- System.out.println("没有这本书,借书失败");
- break;
- }
- if(want.equals(arr[i].getName())&&arr[i].getState()==0){
- flag3=true;
- arr[i].setState(1);
- arr[i].setDate(day);
- System.out.println("恭喜您,借书成功");
- break;
- }else if(want.equals(arr[i].getName())&&arr[i].getState()==1){
- flag3=true;
- System.out.println("已经被借出,不能再次外借");
- break;
- }
- }
-
-
-
- }
-
-
- public void revert(){
- System.out.print("请输入归还图书名称:");
- String want1=sc.next();
- for(int i=0;i<arr.length;i++){
- if(arr[i]==null){
- System.out.println("没有找到匹配信息!");
- break;
- }else if(want1.equals(arr[i].getName())&&arr[i].getState()==1){
- arr[i].setState(0);
- System.out.print("请输入归还日期:");
- int redate = sc.nextInt();
- while (redate < arr[i].getDate() || redate > 31) { // 归还日期不能小于借出日期,也不能大于31
- if (redate < arr[i].getDate()) {
- System.out.println("归还日期不能小于借出日期,请重新输入:");
- } else {
- System.out.println("一个月只有31天,请重新输入:");
- }
- redate = sc.nextInt();
- }
- System.out.println("\n归还《" + want1 + "》成功!");
- System.out.println("借出日期为:" + arr[i].getDate() + "日");
- System.out.println("归还日期为:" + redate + "日");
- break;
-
- }else if(want1.equals(arr[i].getName())&&arr[i].getState()==0){
- System.out.println("该图书没有被借出!无法进行归还操作。");
- break;
- }
-
-
- }
-
- }
- }
四.测试
- public class Test {
-
- public static void main(String[] args) {
- Operate o=new Operate();
- do{
- int start=o.start();
- o.choose(start);
- }while(!o.result());
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。