功能调查:图书馆系统主要功能包括查看馆内藏书,查看书籍借阅状态,借阅图书以及归还图书的基本信息登记。
public class Book {
private String name;
private int state;
private String date;
private int count;
public Book(String name,int state, String date, int count)
{
this.name = name;
this.state = state;
this.date = date;
this.count = count;
}
public String getName() {
return name;
}
public int getState() {
return state;
}
public String getDate() {
return date;
}
public int getCount() {
return count;
}
public void setName(String name) {
this.name = name;
}
public void setState(int state) {
this.state = state;
}
public void setDate(String date) {
this.date = date;
}
public void setCount(int count) {
this.count = count;
}
}
package Library;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class bookMgr {
ArrayList<Book> bookList = new ArrayList<Book>();
public void initial(){
//初始化
Book book1 = new Book("java程序设计",0,"2020-12-01",1);
Book book2 = new Book("python教程",1,"",15);
Book book3 = new Book("C语言程序设计",1,"",23);
bookList.add(book1);
bookList.add(book2);
bookList.add(book3);
}
public void showBooks(){
System.out.println("---->查看图书\n");
System.out.println("序号\t状态\t名称\t\t剩余数量\t\t借出日期");
for(int i=0;i<bookList.size();i++){
if(bookList.get(i).getState()==0){
System.out.println((i+1)+"\t已借出\t"+"<<"+bookList.get(i).getName()+">>\t\t\t"
+bookList.get(i).getDate());
}else if(bookList.get(i).getState()==1){
System.out.println((i+1)+"\t可借\t"+"<<"+bookList.get(i).getName()+">>\t"+bookList.get(i).getCount());
}
}
System.out.println("*************************************************");
return;
}
public void lend(){
boolean flag = false;
System.out.println("---->借出图书\n");
Scanner input = new Scanner(System.in);
System.out.println("请输入图书名称:");
String wantname = input.next();
for(int i=0;i<bookList.size();i++){
if(bookList.get(i).getName()==null){
System.out.println("没有找到该图书!");
break;
}else if( bookList.get(i).getName().equals(wantname)
&& bookList.get(i).getState()==1){
bookList.get(i).setState(0);
System.out.println("请输入借书日期(年-月-日):");
String date=input.next();
bookList.get(i).setDate(date);
System.out.println("图书借出成功,欢迎下次使用!");
break;
}else if( bookList.get(i).getName().equalsIgnoreCase(wantname)
&& bookList.get(i).getState()==0){
System.out.println("《"+wantname+"》已被借出,抱歉!");
break;
}
}
System.out.println("*************************************************");
return;
}
public void returnBook(){
boolean flag = false;
Scanner input = new Scanner(System.in);
System.out.println("---->归还图书\n");
System.out.println("请输入图书名称:");
String wantname = input.next();
for(int i=0;i<bookList.size();i++){
if(bookList.get(i).getName()==null){
System.out.println("没有找到该图书!");
break;
}else if(bookList.get(i).getName().equalsIgnoreCase(wantname)
&&bookList.get(i).getState()==0){
bookList.get(i).setState(1);
System.out.println("请输入日期(年-月-日):");
String returndate = input.next();
System.out.println("借出日期为:"+bookList.get(i).getDate());
System.out.println("归还日期为:"+returndate);
System.out.println("\n归还<<"+wantname+">>成功!");
bookList.get(i).setCount(bookList.get(i).getCount()+1);
break;
}else if(bookList.get(i).getName().equalsIgnoreCase(wantname)&&bookList.get(i).getState()==1){
System.out.println("输入错误!!《"+wantname+"未被借出");
break;
}
}
System.out.println("*************************************************");
return;
}
public class Test {
public static void main(String[] args) {
while(true) {
System.out.println("-------------------欢迎使用图书馆管理系统-------------------");
System.out.println("[1].图书列表\n[2].查找图书\n[3].借阅图书\n[4].归还图书\n[5].退出\n");
System.out.println("请选择:\n");
Scanner input = new Scanner(System.in);
bookMgr mgr=new bookMgr();
mgr.initial();
int choice = input.nextInt();
if(choice == 1) {
mgr.showBooks();
}else if(choice ==2) {
}else if(choice ==3) {
mgr.lend();
}else if(choice ==4) {
mgr.returnBook();
}
}
}
}
原文:https://www.cnblogs.com/dcftx/p/14110863.html