利用物件導向做簡略的圖書管理系統

aoyizhitatashi發表於2017-11-26
第一部分
package com.test;

public class Book {
	private int id;      //編號
	private String name;
	private int state;   //狀態
	private int date;    //日期(0代表未借出)
	private int count;   //次數
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
	
}

第二部分
package com.test;

import java.util.Scanner;


	
public class BookManager {
	static Scanner input=new Scanner(System.in);
	static Book[] book=new Book[6];
	//新增圖書
	public static void addBook()throws Exception{
		System.out.println("--->新增圖書");
		System.out.println("請輸入圖書名稱");
		String name=input.next();
		Book b=new Book();
		b.setName(name);
		b.setState(0);
		b.setDate(0);
		b.setCount(0);
		if(book[book.length-1]==null){
			for(int i=0;i<book.length;i++){
				if(book[i]==null){
					book[i]=b;
					System.out.println("新增圖書成功");
					break;
				}
			}
		}else{
			System.out.println("書架滿了,請稍後再試");
		}
	}
	//檢視圖書
	public static void lookBook()throws Exception{
		System.out.println("-->檢視圖書");
		int i=1;
		System.out.println("序號\t書名\t狀態\t日期\t次數");
		for(Book b:book){
			if(b!=null&&b.getState()==0){
				System.out.println(i+"\t"+b.getName()+"\t"+"可借"+"\t"+b.getDate()+"\t"+b.getCount());
			}else if(b!=null&&b.getState()==1){
				System.out.println(i+"\t"+b.getName()+"\t"+"可借"+"\t"+b.getDate()+"\t"+b.getCount());
			}
			i++;	
		}	
	}
	//刪除圖書
	public static void delBook()throws Exception{
		System.out.println("-->刪除圖書");
		System.out.println("請輸入需要刪除的圖書名稱:");
		String name=input.next();
		Book b=new Book();
		if(book[book.length-1]!=null&&book[book.length-1].getName().equals(name)){
			book[book.length-1]=null;
		}else{
			boolean flag=false;
			for(int i=0;i<book.length;i++){
				Book b1=book[i];
				if(b1!=null&&b1.getName().equals(name)&&b1.getState()==0){
					for(int j=i;j<book.length-1;j++){
						book[j]=book[j+1];
					}
					flag=true;
					book[book.length-1]=null;
					System.out.println(name+"刪除成功");
					break;
				}else if(b1!=null&&b1.getName().equals(name)&&b1.getState()==1){
					System.out.println("無法刪除,原因:已借出");
				}
			}
			if(flag==false){
				System.out.println("未找到圖書,刪除失敗!");
			}
		}
	}
	//借出圖書
	public static void lendBook()throws Exception{
		System.out.println("--->借出圖書");
		System.out.println("請選擇需要借出的圖書:");
		String name=input.next();
		boolean flag=false;
		for(int i=0;i<book.length;i++){
			Book b=book[i];
			if(b!=null&&b.getName().equals(name)&&b.getState()==0){
				b.setState(1);
				b.setCount(b.getCount()+1);
				System.out.println("請輸入借出日期");
				int user=input.nextInt();
				b.setDate(user);
				flag=true;
				System.out.println("借出成功");
				break;
			}else if(b!=null&&b.getName().equals(name)&&b.getState()==1){
				flag=true;
				System.out.println("借出失敗,原因:已借出");
			}
		}
		if(flag==false){
			System.out.println("圖書不存在");
		}
		
	}
	//歸還圖書
	/**
	 * 1: 歸還圖書是否存在
	 * 2: 計算租金
	 * 3: 狀態
	 * 4:輸入的歸還日期如果大於31或者小於借出日期
	 */
	public static void borrowBook()throws Exception{
		System.out.println("---> 歸還圖書");
		System.out.println("請輸入歸還的圖書名稱");
		String name=input.next();
		for(int i=0;i<book.length;i++){
			Book b=book[i];
			if(b!=null&&b.getName().equals(name)&&b.getState()==1){
				b.setState(0);
				int user=-1;
				//判斷日期是否合法
				do{
					System.out.println("請輸入歸還日期:");
					user=input.nextInt();
				}while(user>31||user<b.getDate());
				
				int day=user-b.getDate();
				double zj=day*1;
				System.out.println(name+"歸還成功");
				System.out.println("租金:"+zj);
				b.setDate(0);
			}
		}
	}
	//退出圖書
//	public static void exitBook(){
//		System.exit(0);;
//	}
	public static void main(String[] args)throws Exception {
		int choice=-1;
		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("請選擇:");
			choice=input.nextInt();
			switch(choice){
				case 1:
				try {
					addBook();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					System.out.println("請輸入0返回");
					choice=input.nextInt();
					break;
				case 2:
				try {
					lookBook();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					System.out.println("請輸入0返回");
					choice=input.nextInt();
					break;
				case 3:
				try {
					delBook();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					System.out.println("請輸入0返回");
					choice=input.nextInt();
					break;
				case 4:
				try {
					lendBook();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					System.out.println("請輸入0返回");
					choice=input.nextInt();
					break;
				case 5:
				try {
					borrowBook();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					System.out.println("請輸入0返回");
					choice=input.nextInt();
					break;
				case 6:
					//exitBook();
					choice=6;
					break;
			}
		}while(choice==0);
			System.out.println("退出系統,謝謝使用");	
	}
}

相關文章