圖書館管理系統

    圖書館管理系統,這個小程序是Java基礎的應用,比較簡單,沒有用到太難的東西。同時,受於技術所限,未能連接到數據庫。等以後學了,看有機會再加上,現在就這樣吧。

    代碼:

User類

package com.Library;


public class User {

    public String username="張三";

    public int password=123456;

}

Book類

package com.Library;


public class Book {

    int id;

    String name;

    int State;

    public String ToString(){

    String BookState="";

    if(this.State==1)

    {

    BookState="未借閱";

    }

    else{

    BookState="已借閱";

    }

    return this.id+"\t"+this.name+"\t\t"+BookState;

    }

}


BookData類

package com.Library;


public class BookData {

   Book[] books=new Book[50];

   

   public Book[] AllBook(){

  Book book1=new Book();

  book1.id=1;

  book1.name="Java基礎";

  book1.State=1;

  books[0]=book1;

  

  Book book2=new Book();

  book2.id=2;

  book2.name="Html基礎";

  book2.State=1;

  books[1]=book2;

  

  Book book3=new Book();

  book3.id=3;

  book3.name="Matlab基礎";

  book3.State=1;

  books[2]=book3;

  

  Book book4=new Book();

  book4.id=4;

  book4.name="Lingo基礎";

  book4.State=1;

  books[3]=book4;

  

  Book book5=new Book();

  book5.id=5;

  book5.name="Spss基礎";

  book5.State=2;

  books[4]=book5;

  

  return books;


   }

}

Menu類

package com.Library;


import java.util.Scanner;


public class Menu {

Scanner input= new Scanner(System.in);

    User u=new User();

    BookData bookdata=new BookData();

    Book[] bs=new Book[50];

    

    public void leading(){

         bs=bookdata.AllBook();

    }

    

    public void StartMenu(){

    System.out.println("*****************************************\t\n");

    System.out.println("歡迎進入網上圖書館\t\n");

        System.out.println("1.登錄\t\n");

        System.out.println("2.離開\t\n");

        System.out.println("*****************************************\t\n");

        int choice1=input.nextInt();

        switch(choice1){

        case 1:

        this.Login();

            break;

        case 2:

        System.exit(0);

        break;

        default:

        this.StartMenu();

        break;

        }

    }

    

    public void Login(){

    System.out.println("*****************************************\t\n");

    System.out.println("輸入信息\t\n");

        System.out.println("用戶名:\t\n");

        String Busername=input.next();

        System.out.println("密碼:\t\n");

        int Bpassword=input.nextInt();

        int Count=3;

        boolean flag=true;

        do{

        if (Busername.equals(u.username) && Bpassword==u.password)

        {

        System.out.println("登錄成功\t\n");

        this.NextMenu();

        flag=false;

        }

        else{

        Count--;

        if(Count==0)

        {

        System.out.println("登錄失敗,次數已用完,請5分鐘後再試\n");

        }

        else{

        System.out.println("登錄失敗,還有"+Count+"次\n");

        this.Login();

        }

        }

        }while(flag);

    }

      

    public void NextMenu(){

    System.out.println("*****************************************\t\n");

    System.out.println("1.圖書目錄\t\n");

    System.out.println("2.借閱圖書\t\n");

    System.out.println("3.歸還圖書\t\n");

    System.out.println("4.查找圖書\t\n");

    System.out.println("5.添加圖書\t\n");

    System.out.println("6.借閱情況\t\n");

    System.out.println("7.刪除圖書\t\n");

    System.out.println("8.修改圖書\t\n");

    System.out.println("9.退出\t\n");

        System.out.println("*****************************************\t\n");

        int choice2=input.nextInt();

        switch (choice2) {

case 1:

this.BookMenu();

break;

case 2:

this.BrowserBook();

break;

case 3:

this.BackBook();

break;

case 4:

this.FindBook();

break;

case 5:

this.AddBook();

break;

case 6:

this.finddetail();

break;

case 7:

this.DeleteBook();

break;

case 8:

this.UpdataBook();

break;

case 9:

System.out.println("謝謝使用,再見");

System.exit(0);

break;

default:

break;

}

    } 

    // 1

    public void BookMenu(){

    System.out.println("編號\t\t"+"書名\t"+"借閱信息\t\t");

    for(int i=0;i<bs.length;i++){

    if(bs[i]!=null){

    System.out.println(bs[i].ToString());

    }

    }

    returnMenu();

    }

    // T1

    public void returnMenu(){

    System.out.println("輸入$返回上一級");

    String a=input.next();

    if (a.equals("$")){

    this.NextMenu();

    }

    else{

    System.out.println("輸入錯誤,請重新輸入");

    returnMenu();

    }

    }

    //2

    int index=0;

    Book[] Browser=new Book[50];

    public void BrowserBook(){

    System.out.println("請選擇借閱方式:\t\n");

    System.out.println("1.按編號借閱\t\n");

    System.out.println("2.按書名借閱\t\n");

    int choice2 = input.nextInt();

    switch (choice2) {

case 1:

BrowserId();

break;

case 2:

BrowserName();

break;

default:

System.out.println("輸入錯誤,請重新輸入");

BrowserBook();

break;

}

    }

    //2-1

    public void BrowserId(){

    System.out.println("請輸出圖書編號:");

    int id=input.nextInt();

    boolean flag=true;

    for(int i=0;i<bs.length;i++){

    if(bs[i]!=null){

    if(bs[i].id==id){

    if(bs[i].State==1){

    System.out.println("借閱成功");

    bs[i].State=0;

    Browser[index++]=bs[i];

    flag=false;

    break;

    }

    else{

    System.out.println("此書已借閱!");

    flag=false;

    break;

    }

    }

    }

    }

    if(flag){

    System.out.println("書庫內無此書!");

    }

    returnMenu();

    }

    //2-2

    public void BrowserName(){

    System.out.println("請輸出圖書名字:");

    String name=input.next();

    boolean flag=true;

    for(int i=0;i<bs.length;i++){

    if(bs[i]!=null){

    if(bs[i].name==name){

    if(bs[i].State==1){

    System.out.println("借閱成功");

    bs[i].State=0;

    flag=false;

    break;

    }

    else{

    System.out.println("此書已借閱!");

    flag=false;

    break;

    }

    }

    }

    }

    if(flag){

    System.out.println("書庫內無此書!");

    returnMenu();

    }

    }

    //3

    public void BackBook(){

    System.out.println("輸入還書方式\n\t"+"1.輸入所還圖書名稱\t"+"2.輸入所還圖書編號\t\n");

        int a=input.nextInt();

        switch (a) {

case 1:

BackName();

break;

case 2:

BackId();

break;


default:

System.out.println("輸入錯誤,請重新輸入:");

BackBook();

break;

}

    }

    //3-1

    public void BackName(){

    System.out.println("請輸入所還圖書名稱:");

    String bookname=input.next();

    boolean flag=true;

    for (int i = 0; i < bs.length; i++) {

if (bs[i]!=null) {

if (bs[i].State!=1) {

if (bookname.equals(bs[i].name)) {

System.out.println("歸還成功");

bs[i].State=1;

for (int j = 0; j < Browser.length; j++) {

if(Browser[j]!=null){

if (Browser[j]==bs[i]) {

Browser[j]=null;

flag=false;

break;

}

}

}

}

}

}

}

    if (flag) {

    System.out.println("操作失敗!");

    BackName();

}

    returnMenu();

    }

    //3-2

    public void  BackId(){

    System.out.println("請輸入所還圖書編號:");

    int bookid=input.nextInt();

    boolean flag=true;

    for (int i = 0; i < bs.length; i++) {

if (bs[i]!=null) {

if (bs[i].State!=1) {

if (bookid==bs[i].id) {

System.out.println("歸還成功");

bs[i].State=1;

for (int j = 0; j < Browser.length; j++) {

if(Browser[j]!=null){

if (Browser[j]==bs[i]) {

Browser[j]=null;

flag=false;

break;

}

}

}

}

}

}

}

    if (flag) {

    System.out.println("操作失敗!請重新操作");

    BackId();

}

    returnMenu();

    }

    //4

    public void FindBook(){

    System.out.println("輸入查詢方式\n\t"+"1.輸入查詢圖書名稱\t"+"2.輸入查詢圖書編號\t\n");

        int a=input.nextInt();

        switch (a) {

case 1:

FindName();

break;

case 2:

FindId();

break;


default:

System.out.println("輸入錯誤,請重新輸入:");

BackBook();

break;

}

    }

    //4-1

    public void FindName(){

    System.out.println("請輸入查詢圖書名稱:");

    String bookname=input.next();

    boolean flag=true;

    for (int i = 0; i < bs.length; i++) {

if (bs[i]!=null) {

if (bookname.equals(bs[i].name)) {

System.out.println("編號\t\t"+"書名\t"+"借閱信息\t\t");

    System.out.println(bs[i].ToString());

    flag=false;

   }

}

}

    if(flag){

    System.out.println("查無此書!請重新查找");

    FindName();

    }

   }


    //4-2

    public void  FindId(){

    System.out.println("請輸入查詢圖書編號:");

    int bookid=input.nextInt();

    boolean flag=true;

    for (int i = 0; i < bs.length; i++) {

if (bs[i]!=null) {

if (bookid==(bs[i].id)) {

System.out.println("編號\t\t"+"書名\t"+"借閱信息\t\t");

    System.out.println(bs[i].ToString());

   }

}

}

    if(flag){

    System.out.println("查無此書!請重新查找");

    FindId();

    }

    } 

    //5

    public void AddBook(){

    Book book=new Book();

    System.out.println("請輸入圖書名稱:");

    String bookname=input.next();

    System.out.println("請輸入圖書狀態:");

    int bookstate=input.nextInt();

    for (int i = 0; i < bs.length; i++) {

if (bs[i]==null) {

System.out.println("添加成功!");

book.name=bookname;

book.State=bookstate;

book.id=i+1;

bs[i]=book;

break;

}

}

    returnMenu();

    }

    //6

    public void finddetail(){

    System.out.println("編號\t\t"+"書名\t"+"借閱信息\t\t");

    boolean flag=true;

    for(int i=0;i<Browser.length;i++){

    if(Browser[i]!=null){

    System.out.println(Browser[i].ToString());

    flag=false;

    }

    }

    if(flag){

    System.out.println("無借閱記錄,請先借閱");

    }

    returnMenu();

    }

    //7

    public void DeleteBook(){

    System.out.println("輸入刪除編號:");

    int newId=input.nextInt();

    boolean flag=true;

    for (int i = 0; i < bs.length; i++) {

if (bs[i]!=null) {

if (bs[i].id==newId) {

if (bs[i].State==1) {

System.out.println("操作成功,已刪除");

bs[i]=null;

flag=false;

break;

}

else {

System.out.println("此書已借出,不能刪除!\n請重新操作:");

flag=false;

DeleteBook();

}

}

}

}

    if (flag) {

    System.out.println("操作失敗,此書不存在,不能刪除!\n請重新操作:");

    DeleteBook();

}

    returnMenu();

    }

  //8

    public void UpdataBook(){

    System.out.println("輸入所修改書的編號:");

    int Id=input.nextInt();

    boolean flag=true;

    for (int i = 0; i < bs.length; i++) {

if (bs[i]!=null) {

if(bs[i].id==Id){

System.out.println("編號\t\t"+"書名\t"+"借閱信息\t\t");

System.out.println(bs[i].ToString());

if (bs[i].State==1) {

System.out.println("請輸入新的書名:");

String name=input.next();

bs[i].name=name;

System.out.println("是否修改其他屬性(y/n)");

String s=input.next();

if (s.equals("y")) {

System.out.println("請輸入新的狀態:\n1.可借閱\t2.不可借閱\t");

   int a=input.nextInt();

   bs[i].State=a;

}

System.out.println("修改成功!");

flag=false;

break;

}

else {

System.out.println("此書已借出,禁止修改!");

flag=false;

break;

}

}

}

}

if (flag) {

System.out.println("此書不存在,禁止修改!請重新輸入:");

UpdataBook();

}

returnMenu();

    }

}

MainMenu類

package com.Library;


public class MainMenu {

    public static void main(String[] args){

    Menu m=new Menu();

   

    m.leading();

    m.StartMenu();

    }

}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章