图书管理系统

import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;


class Book{
	
	//书名集合
	List<String> nameList =new ArrayList<String>();
	//书借出状态   1:表示未借出		0:表示已借出
	List<Integer> stateList = new ArrayList<Integer>();
	//借出时间
	List<String> timeList = new ArrayList<String>();
	//记录借书次数
	List<Integer> countBook = new ArrayList<Integer>();
	
	//输入对象
	Scanner scan = new Scanner(System.in);
	
	//记录所选的功能
	int number;

	//判断是否退出
	int systemNum;
	
	public void initBook(){
		nameList.add("世界是数字的");
		stateList.add(1);
		timeList.add("");
		countBook.add(100);
		
		nameList.add("罗氏家族语录");
		stateList.add(0);
		timeList.add("2016-9-8");
		countBook.add(150);
		
		nameList.add("钢铁是怎样炼成的");
		stateList.add(1);
		timeList.add("");
		countBook.add(10);
	}
	
	//欢迎界面
	public void helloBook(){
		
		System.out.println("*************欢迎进入图书管理系统***************");
		
		System.out.println("**********     1. 新增DVD        ***************");
		System.out.println("**********     2. 查看DVD        ***************");
		System.out.println("**********     3. 删除DVD        ***************");
		System.out.println("**********     4. 借出DVD        ***************");
		System.out.println("**********     5. 归还DVD        ***************");
		System.out.println("**********	   6. 排行榜                         ***************");
		System.out.println("**********     6. 排行榜                 ***************");
		System.out.println("**********     7. 退    出                ***************");
		System.out.println("************************************************");
		System.out.println("请输入你的选择:");
		number = scan.nextInt();		
	}
	
	//功能区
	public void funtion(){
		helloBook();
		switch (number) {
		//添加
		case 1:
			addBook(scan);
			logDown(scan);
			if (systemNum == 0){
				funtion();
			}
			break;
		//查看书籍
		case 2:
			check();
			logDown(scan);
			if (systemNum == 0) {
				funtion();
			}	
			break;
		//删除书籍
		case 3:
			removeBook(scan);
			logDown(scan);
			if (systemNum == 0) {
				funtion();
			}
			break;
			
		case 4:
			loan(scan);
			logDown(scan);
			if (systemNum == 0) {
				funtion();
			}
			break;
			
		case 5:
			returnBook(scan);
			logDown(scan);
			if (systemNum == 0) {
				funtion();
			}
			break;
		case 6:
			rankList();
			logDown(scan);
			if (systemNum == 0) {
				funtion();
			}
			break;
			
		case 7:
			System.out.println("谢谢使用图书管理系统......");
			break;
			
		default :
			System.out.println("输入有误!!!");
			funtion();
			break;
		}	
	}
	
	//打印方法
	public void print(List<String> name,List<Integer> state,List<String> time){
		Iterator runList = name.iterator();
		System.out.println("序号\t"+"状态\t"+"书名\t\t\t"+"借出时间");
		int count = 0;
		String stateStr = null;
		
		while(runList.hasNext()){
			if (stateList.get(count).intValue() == 0) {
				stateStr = "已借出";
			}else {
				stateStr = "可借";
			}
			runList.next();
			System.out.println((count+1) + "\t" + stateStr + "\t<<" + name.get(count) + ">>\t\t" + time.get(count));
			count++;
		}
	}
	
	//退出系统
	public void logDown(Scanner scan){
		while (true) {
			System.out.println("输入0退出!!!");
			systemNum = scan.nextInt();
			if (systemNum == 0) {
				break;
			}else {
				System.out.println("输入有误!!!");
			}
		}
		
		
	}
	
	//添加书籍
	public void addBook(Scanner scan){
		
		System.out.println("请输入书名***");
		nameList.add(scan.next());
		stateList.add(1);
		timeList.add("");
		countBook.add(0);
	}
	
	//查看书籍
	public void check(){
		print(nameList, stateList, timeList);
	}
	
	
	//删除书籍
	public void removeBook(Scanner scan){
		System.out.println("请输入要删除的书名...");
		String str = scan.next();
		int count = nameList.indexOf(str);
		if (count >= 0) {
			if (stateList.get(count).intValue() != 0) {
				nameList.remove(count);
				stateList.remove(count);
				timeList.remove(count);
				System.out.println("删除/成功...");
			}else {
				System.out.println("此书以被借出,不能删除...");
			}
		}else {
			System.out.println("图书馆没有此书,请核对...");
		}
	}
	
	//借出
	public void loan(Scanner scan){
		System.out.println("输入要借的书名...");
		String str = scan.next();
		int count = nameList.indexOf(str);
		System.out.println("*********\t"+count);
		if (count >= 0) {
			if(stateList.get(count).intValue() != 0){
				System.out.println("输入借书时间(年-月-日)...");
				String timeStr = scan.next();
				timeList.set(count, timeStr);
				stateList.set(count, 0);
				countBook.set(count, countBook.get(count)+1);
				System.out.println("操作成功...");
			}else {
				System.out.println("此书已被借出,抱歉...");
			}
		}else {
			System.out.println("图书馆没有你要借的书,请核对...");
		}
	}
	
	//归还
	public void returnBook(Scanner scan){
		System.out.println("你好,谢谢你来还书...");
		System.out.println("请输入你所还书籍的名称...");
		String str = scan.next();
		int count = nameList.indexOf(str);
		if (count >= 0) {
			if (stateList.get(count).intValue() == 0) {
				System.out.println("请输入还书时间(年-月-日)");
				String timeStr = scan.next();
				long mon = money(timeList.get(count), timeStr);
				System.out.println("操作成功,欢迎下次再来......");
				System.out.println("你的借书天数为:" + mon + "天   租金为:" + (mon*1) + "元");
				timeList.set(count, "");
				stateList.set(count, 1);
			}else {
				System.out.println("抱歉,此书从未借出过...");
			}
		}else {
			System.out.println("此书没有从图书馆中借出,请核对...");
		}
	}
	
	public long money(String date1,String date2){
		//做时间差
		//将String转为Date做计算yyyy-mm-dd是格式
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		long charge = 0;
		try{
			Date d1 = sd.parse(date1);  
			Date d2 = sd.parse(date2);
			//时间也是以毫秒为单位。
			charge = (d2.getTime() - d1.getTime()) / (1000*24*60*60);
		}catch(ParseException e){
			System.out.println(e);
		}
		return charge;
	}
	
	
	//排行榜
	public void rankList(){
		Object[] nameArr = nameList.toArray();
		Object[] countArr = countBook.toArray();
		
		int temp = 0;
		String str = null;
		for (int i = 0; i < countArr.length-1; i++) {
			for (int j = 0; j < countArr.length-1-i; j++) {
				if ((int)countArr[j] < (int)countArr[j+1]) {
					temp = (int)countArr[j];
					countArr[j] = (int)countArr[j+1];
					countArr[j+1] = temp;
					
					str = (String)nameArr[j];
					nameArr[j] = (String)nameArr[j+1];
					nameArr[j+1] = str;
				}
			}
		}
		
		System.out.println("序号\t"+"书名\t\t"+"\t"+"排行榜");
		for (int i = 0; i < countArr.length; i++) {
			System.out.println((i+1) + "\t" + nameArr[i] + "\t\t" + countArr[i]);
		}
		
	}
	
}

public class demo {

	public static void main(String[] args) {
		Book book = new Book();
		//图书馆书籍初始化
		book.initBook();
		//图书馆管理系统核心方法
		book.funtion();
	}
}

  

原文地址:https://www.cnblogs.com/12kk/p/6124684.html