JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區 » 學習筆記  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 初學java - 圖書館系統題目(需耐心者進入協助修改或指導)
tiro





發文: 5
積分: 0
於 2018-09-21 02:47 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
<<<< 附上如下code連結網址(有漂亮的顏色區分): https://paste.ofcode.org/R4NDTQvXheaM2pcy6gyFh5 >>>>>
<<<< 附上code連結網址(有漂亮的顏色區分): https://paste.ofcode.org/R4NDTQvXheaM2pcy6gyFh5 >>>>>[/color]

/**圖書館借閱系統:
*
* 一.使用者透過命令提示字元操作系統
* 二. 輸入 1.借書2.還書3.捐書4.離開
* 1.借書,列出圖書中所有書供使用者選擇,確認借閱書籍從館書中找出對應書籍,使用者狀態"XX使用者已成功借出"。
* 2.還書,列出使用者身上目前所借閱的書籍供選擇,確認還回的書本則變更書本的借出狀態,並更新使用者的借閱清單。
* 3.捐書,則需輸入書籍資訊(以分號隔開),
例:哈利波特;Harry Potter;ABCD123456;450;
(中文書名;英文書名;ISBN;價格) ,確認輸入後,將書本加入館藏。
* 4.離開,則跳結束程式。
* 三.
* 圖書館中本來就有十「種」書(書可重覆),
每一本書要記錄的資訊如下:
a.編號
b.中文書名
c.英文書名(可選)
d.ISBN
e.價格
f.借/還記錄
g.借出與否
*
*/

/**===============================================================================================================================================================
*問題:
* 1.我知道我寫java程式很糟糕,可以請大神們說說我需改進地方在哪邊?
* 2.我現在print 出來東西邏輯都不是很對,但我也想了三個禮拜,就是到頂了,無法完成,寫了又改改了又寫,9/9號日期是我已經寫了第三遍了,可以引領初學者我嗎?給點方向了(跪求)
* 3.如果內文寫的不完整再次跟我說,我隨時改正自己缺陷。
* 4.總之借閱、捐書、還書部分都是有問題的,可以修正我寫出東西嗎(ps: 雖然都靠google大神完成但畢竟題目不同還是很多撰寫不出來甚至錯誤)?
* 最後 ps: 學java時間是從2018/07底到現在,沒任何程式相關經驗,如果寫不好,可以噴小力一點嗎,拜託了(我蠻笨的)! 先謝了~~~
=====================================================================================================================================================================*/

package library.project.Book001;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @since 2018/09/09
* @author GG
* 書本紀錄
*/
public class Book {
/** 屬性編號 **/
  private String no;
  /** 屬性中文書名 **/
  private String chineseBook;
  /** 屬性英文書名 **/
  private String engBook;
  /** 屬性ISBN **/
  private String isbn;
  /** 屬性價格 **/
  private double price;
  /** 借還記錄 **/
private BorrowRecord borrowRecord;
/** 借出與否 **/
private boolean borrowedOut;
boolean borrowed;
  
public Book(String no, String chineseBook, String engBook, String isbn, double price, BorrowRecord borrowRecord, boolean borrowedOut){
    this.no = no;
    this.chineseBook = chineseBook;
    this.engBook = engBook;
    this.isbn = isbn;
    this.price = price;
    this.borrowRecord = borrowRecord;
    this.borrowedOut = borrowedOut;    
}
/**
** 這邊建構式好像不確定為何需要可能需要問問看吧
*/
  public Book() {
    // TODO 自動產生的建構子 Stub
  }

  @Override
public String toString() {
  return "/nNo: " + no + "\nISBN: " + isbn + "\nChineseBook" + "chineseBook" + "\nPrice: " + price + "\n";
}

  public String getNo() {
    return no;
  }
  public void setNo(String no) {
    this.no = no;
  }
  public String getChineseBook() {
    return chineseBook;
  }
  public void setChineseBook(String chineseBook) {
    this.chineseBook = chineseBook;
  }
  public String getEngBook() {
    return engBook;
  }
  public void setEngBook(String engBook) {
    this.engBook = engBook;
  }
  public String getIsbn() {
    return isbn;
  }
  public void setIsbn(String isbn) {
    this.isbn = isbn;
  }
  public double getPrice() {
    return price;
  }
  public void setPrice(double price) {
    this.price = price;
  }
  //借出紀錄 : 借閱總次
  public BorrowRecord getBorrowRecord() {
    return borrowRecord;
  }
  public void setBorrowRecord(BorrowRecord borrowRecord) {
    this.borrowRecord = borrowRecord;
  }
  //借閱狀態: 借出與否
  public boolean isBorrowedOut() {
    return borrowedOut;
  }
  public void setBorrowedOut(boolean borrowedOut) {
    this.borrowedOut = borrowedOut;
  }
  public boolean isBorrowed() {
    return borrowed;
  }
  public void setBorrowed(boolean borrowed) {
    this.borrowed = borrowed;
  }
}
/*================================================================================================================*/
package library.project.Book001;
import java.util.Date;
/**
* @since 2018/09/10
* @author GG
*
**借書:
**一、選擇借書,則列出圖書館中所有的書供使用者選擇,
**確認借閱書籍後從館藏中找出對應的書本變更借出狀態。
*/
public class BorrowRecord {

/** 編號 **/
private int id;

/** 借出日期 **/
private Date borrowedDate;

/** 還書日期 **/
private Date returnedDate;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Date getBorrowedDate() {
return borrowedDate;
}

public void setBorrowedDate(Date borrowedDate) {
this.borrowedDate = borrowedDate;
}

public Date getReturnedDate() {
return returnedDate;
}

public void setReturnedDate(Date returnedDate) {
this.returnedDate = returnedDate;
}

}
/*================================================================================================================*/
package library.project.Book001;
/**
* @since 2018/09/09
* @author GG
* 紀錄書本倉庫
*/
public class Datas {
public static Book[] bookStore = new Book[999];
}

/*================================================================================================================*/
package library.project.Book001;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.InputMismatchException;
/**
* @since 2018/09/09
* @author GG
* 一.使用者透過命令提示字元操作系統
* 二. 輸入 1.借書2.還書3.捐書4.離開
* 1.借書,列出圖書中所有書供使用者選擇,確認借閱書籍從館書中找出對應書籍,使用者狀態"XX使用者已成功借出"。
* 2.還書,列出使用者身上目前所借閱的書籍供選擇,確認還回的書本則變更書本的借出狀態,並更新使用者的借閱清單。
* 3.捐書,則需輸入書籍資訊(以分號隔開),
例:哈利波特;Harry Potter;ABCD123456;450;
(中文書名;英文書名;ISBN;價格) ,確認輸入後,將書本加入館藏。
* 4.離開,則跳結束程式。
* 三.
* 圖書館中本來就有十「種」書(書可重覆),
每一本書要記錄的資訊如下:
a.編號
b.中文書名
c.英文書名(可選)
d.ISBN
e.價格
f.借/還記錄
g.借出與否
*
*/
class Main {
Scanner scanner = new Scanner(System.in);
  ArrayList<Book> booklist = new ArrayList<Book>();
  public static int count = 0;
  
  //圖書搜索類型
  private int type = 0 ;
  //圖書名稱
  private static String name = "" ;
  
  public Main() {
    printMenu();
    while(true) {
      //讀取用戶輸入
      int choice = scanner.nextInt();
      if(choice == 6) {
        System.out.println("成功退出系統,歡迎再次光臨!!");
        break;
      }
      switch(choice) {
        case 5: borrowBook(); break;//借書
//        case 5: returnBook(); break;//還書
        case 1: addBook(null, null, null, null); break;//函數裡面帶有4個參數值
        case 2: delBook(); break;
        case 3: changeBook(choice); break;//函數裡面帶有物件參數值
        case 4: findBook(); break;
        default:
          System.out.println("輸入錯誤,請重新輸入。");
          printMenu();
          continue;
      }
    }
  }//Main

//  print
  public void printMenu() {
    //打印選單菜單
    System.out.println("歡迎蒞臨圖書館XD");
    
    System.out.println("5. 借書功能書籍");
//    System.out.println("2. 還書功能書籍");
    
    System.out.println("1. 新增 / 捐書(功能)書籍");
    System.out.println("2. 刪除(功能)書籍");
    System.out.println("3. 修改(功能)書籍");
    System.out.println("4. 查詢(功能)書籍");
    
    System.out.println("6. 退出(功能)系統");
  }
  
  /** * 借書類型 */
  public void borrowBook() {
    String[] borrowbooks = {"外國史","人命關天","三紹空","JAVA入門到放棄","Mysql學到到跑路","黑客攻心防"} ;
    Main a = new Main();
    a.book_list(borrowbooks);
    //圖書列表
    a.book_list(borrowbooks);
    //選擇搜尋類型
    a.search_type();
    //搜尋書籍
    a.search_book(borrowbooks);
    //顯示圖書(書籍)
    System.out.println("您選擇書籍為:"+ a.name);
  }
  

  public void borrowBook(String chineseBook) {
    int found = 0;
   for (Book b : booklist){
   if (b.getChineseBook().equals(chineseBook)){
   if (found == 0){
      found = 1;
   }
     if (!b.isBorrowedOut()) {
        b.borrowed=true;
     found = 2;
     break;
     }
   }
   }
   if (found == 0) {
   System.out.println("對不起,這本書不在我們的目錄中");
   } else if (found == 1) {
   System.out.println("對不起,這本書已經借出了.");
   } else if (found == 2) {
   System.out.println("你成功借出了 " + chineseBook + "書本!");
   }
  }
  
  public void book_list(String[] books) {
    int num = 0 ;
    System.out.println("序號名稱: ");
    for ( String book : books ) {
      num++ ;
//      System.out.println(" 1" + m + " 2" + books);
    }
  }
  
  /** * 設計搜尋類型 */
  public void search_type() {
    Scanner type_number = new Scanner(System.in);
    while (true) {
      try {
//        System.out.println("輸入: 1.名稱查詢 ; 2.序號查詢 " );
        int number = type_number.nextInt();
        if ( number == 1 || number == 2 ) {
          this.type = number ; break;
          } else {
            throw new Exception();
            }
      } catch ( InputMismatchException e ) {
        System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");
        type_number.nextLine();
      } catch ( Exception e ) {
        System.out.println("命令輸入錯誤!請根據提示輸入數字命令!" );
      }
    }
  }
  /** * 搜尋書本 * @param books */
  public void search_book(String[] books) {
    while( true ) {
      try {
        Scanner search = new Scanner( System.in) ;
        if( this.type == 1 ) {
          System.out.println("輸入書名稱:");
          String book_name = search.nextLine();
          //初始化
          boolean is_true = false ;
          //循環判斷圖書名稱是否存在
          for( String book : books ) {
            if ( book.equals(book_name) ) {
              is_true = true ; }
          }
          if ( is_true == false ) {
            throw new Exception() ;
          }
            this.name = book_name ; break;
        } else if( this.type == 2 ) {
          System.out.println("輸入書序號:");
          int book_number = search.nextInt() ;
          if ( book_number < 1 || book_number > books.length ) {
            throw new Exception() ;
          }
//          
          this.name = books[book_number-1] ; break;
//          
        } else {
          System.out.println("系統輸入錯誤!"); System.exit(0);
        }
      } catch ( Exception e ) {
        System.out.println("書本不存在");
      }
    }
  }
  //借書end

  //新增
  public void addBook(String chineseBook, String engBook, String isbn, String price) {
    if(count < booklist.size()-1) {//A+
      System.out.println("當前共有: " + count + "本書! ");
      Scanner scanner = new Scanner(System.in);
      
      System.out.println("中文書名: " + chineseBook + "書本 ! ");
      System.out.println("請輸入中文書名:");
      String bookNameChinese = scanner.nextLine();
      
      System.out.println("英文書名: " + engBook + "書本 ! ");
      System.out.println("請輸入英文書名:");
      String bookNameEng = scanner.nextLine();
      
      System.out.println("ISBN: " + isbn + "號 ! ");
      System.out.println("請輸入ISBN碼:");
      String bookIsbn = scanner.nextLine();
      
      System.out.println("價格: " + price + "元 ! ");
      System.out.println("請輸入價格:");
      double bookPrice = scanner.nextDouble();
      
      Book book = new Book(chineseBook, engBook, isbn, price, bookPrice, null, false);
//      booklist[count] = book;//A-
      booklist.add(book);//A+
      count++;
      System.out.println("新增書本成功XD");
      printAllBook();
    }else {
      System.out.println("圖書倉庫已滿RRRRRRRRRRR!");
    }
  }//新增end
  //刪除
  public void delBook() {
    while(true) {
      System.out.println("請輸入要按哪種方式刪除書籍: 1. 編號\t 2.書名\t 3.返回助選單\t");
      int delB = scanner.nextInt();
      
      if(delB == 1) {
        System.out.println("請問要刪除第幾本: ");
        int id = scanner.nextInt();
        id = orderFind(id);
        System.out.println(id);
      
        if(id > -1) {
//          for(int i = id; i < count-1; i++) {
//            booklist[i] = booklist[i+1];
//          }//A-
          booklist.remove(id);//A+
          count--;
          System.out.println("刪除成功");
          printAllBook();
        }else {
          System.out.println("輸入錯誤!!!");
        }
      }//1
      if(delB == 2) {
        System.out.println("請輸入您要刪除書名: ");
        String name = scanner.next();
        int id = nameFind(name);
        if(id > -1) {
//          for(int j = id; j < count-1; j++) {
//            booklist[j] = booklist[j+1];
//          }//A-
          booklist.remove(id);//A+
          count--;
          System.out.println("刪除成功!");
          printAllBook();
        }else {
          System.out.println("為查找到您想要書名!!");
        }
      }else if (delB == 3){
        printMenu();
        break;
      }else {
        System.out.println("輸入不正確,請再次輸入正確數。");
      }//2
    }
  }//刪除end
  //修改
  public void changeBook(Object books) {
    while(true) {
      System.out.println("請輸入哪種方法修改書籍: 1.編號\t2.書名\t3.返回主選單\t");
      int change = scanner.nextInt();
      if(change == 1) {
        System.out.println("請輸入要修改幾本書: ");
        int number = scanner.nextInt();
        int id = orderFind(number);
        if(id > -1) {
//          System.out.println("中文原書名為: "+booklist[id].getChineseBook()+" 請輸入你要修改中文書名: ");
          System.out.println("中文原書名為:"+((Book) books).getChineseBook()+" 請輸入你要修改中文書名:");//A+
          String str = scanner.next();
          System.out.println("請輸入作者:");
          String author = scanner.next();
          System.out.println("請輸入單價:");
          float price = scanner.nextFloat();
          //booklist[id].setChineseBook(str);//A-
          System.out.println("修改成功!");
          printAllBook();
        }else {
          System.out.println("輸入有錯誤,請更正!");
        }
      }//1
      else if(change == 2) {
        System.out.println("請輸入您要修改的書名: ");
        String name = scanner.next();
        int id = nameFind(name);
        if(id > -1){
          Book book = (Book)booklist.get(id);
          //System.out.println("原書名為:"+booklist[id].getChineseBook()+" 請輸入你要修改書名:");
          String str = scanner.next();
          System.out.println("請輸入作者:");
          String author = scanner.next();
          System.out.println("請輸入單價:");
          float price = scanner.nextFloat();
          
          //booklist[id].setChineseBook(str);//A-
          System.out.println("修改成功!");
          printAllBook();  
        }
      }else if(change == 3) {
        printMenu();
        break;
      }else {
        System.out.println("輸入錯誤!");
      }//2
    }
  }
  //修改end
  //查詢尋找
  public void findBook(){
    while(true){
      System.out.println("請輸入按哪種方法查詢書籍:1、編號/2、書名/3、返回主選單");
      int choose = scanner.nextInt();
      if(choose == 1){
        System.out.println("請輸入您要查詢第幾本書: ");
        int number = scanner.nextInt();
        int id = orderFind(number);
        if(id > -1){
          Book book = (Book)booklist.get(id);//ArrayList獲取指定元素的方法
          //System.out.println("你要查詢書名為:"+booklist[id].getChineseBook()+" 作者:"+booklist[id].getIsbn()+" 單價:"+booklist[id].getPrice()+"元/本");
        }
        else{
          System.out.println("輸入錯誤!");
        }
      }
      else if(choose == 2){
        System.out.println("請輸入您要查詢的書名: ");
        String name = scanner.next();
        int id = nameFind(name);
        if(id > -1){
          Book book = (Book)booklist.get(id);//(Book)booklist強制轉型
          //System.out.println("查找成功,您查詢到的書為第"+(id+1)+"本书!"+"書名為:"+booklist[id].getChineseBook()+" 作者:"+booklist[id].getIsbn()+" 單價:"+booklist[id].getPrice()+"元/本");    
        }
      }
      else if(choose == 3){
        printMenu();
        break;
      }
      else{
        System.out.println("輸入錯誤,請更新!");
      }
    }
  }//查詢尋找end
  

  
  public void printAllBook(){ //循環重複打印出所有的書籍
    for (int i = 0; i < count; i++){
      Book book = (Book)booklist.getLight Bulb;
      //System.out.println("第"+(i+1)+"本书名:"+booklist[i].getChineseBook()+" 作者:"+booklist[i].getIsbn()+" 单价:"+booklist[i].getPrice()+"元/本");
    }
  }
  
  public int orderFind(int number) {//按編號查找,返回id
    //System.out.println(number);
    if(number <= count){
      int id = number - 1;
      return id;
    }
    else {
      return -1;
    }
  }
  
  public int nameFind(String name) {//按照書名尋找,返回id
    int id = -1;
    for(int i = 0; i < count; i++){
      Book book = (Book)booklist.getLight Bulb;
      //if(booklist[i].getChineseBook().equals(name)){
      if(book.getChineseBook().equals(name)) {
        id = i;
        break;
      }
      else if(i<count) {
        continue;
      }
      else{
        System.out.println("尚未查詢到您想要書名! ");
        break;
      }
    }
    return id;
  }

//
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Main startmenu = new Main();
    startmenu.borrowBook();
    Book books = new Book();
    
    //
    books.setNo("123");
    books.setChineseBook("java入門到放棄");
    books.setEngBook(name);
    books.setIsbn(name);
    books.setPrice(count);
    books.setBorrowRecord(null);
  }
}//類別class

/*================================================================================================================*/

附上如上code連結網址(有漂亮的顏色區分): https://paste.ofcode.org/R4NDTQvXheaM2pcy6gyFh5

<<<< 附上code連結網址(有漂亮的顏色區分): https://paste.ofcode.org/R4NDTQvXheaM2pcy6gyFh5 >>>>>[color=red]


ps ...............謝謝耐心收看(鞠躬)


reply to postreply to post
作者 Re:初學java - 圖書館系統題目(需耐心者進入協助修改或指導) [Re:tiro]
ggininder





發文: 35
積分: 0
於 2018-09-21 03:20 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
borrowed 初始值?

你可以設計一個登入系統,這也是滿常見的設計,
因為你一個系統需要初始一些相關邏輯的值。才能確保每次的結果或者可以重現相關BUG。

再來你設計完登入系統,你可以試著加入DB。將CODE跟DATABASE勾稽再一起。
再來想其他的東西吧。


reply to postreply to post
作者 Re:初學java - 圖書館系統題目(需耐心者進入協助修改或指導) [Re:ggininder]
tiro





發文: 5
積分: 0
於 2018-09-21 04:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1. borrowed 初始值?
/*==================================================================*/
回: 意思是borrowed 我沒有給初始值嗎?

2.你可以設計一個登入系統,這也是滿常見的設計,
因為你一個系統需要初始一些相關邏輯的值。才能確保每次的結果或者可以重現相關BUG。

再來你設計完登入系統,你可以試著加入DB。將CODE跟DATABASE勾稽再一起。
再來想其他的東西吧。
/*==================================================================*/
回: 快3個月的初學者已經需要運用到DB了嗎?
因為我只是先做測試,是看看自己是否能夠把之前章節學到的皮毛做出來而已,API也只學到一些東西可以利用,再學寫DB會不會掛點壓? (有點好奇~~~~
我希望目標是能印出些甚麼,然後搭配一些邏輯判斷print出與我題目指定東西呈現,是我目前唯一能辦到的目標(雖然邏輯太差)~~再多對我有點困難,還是有甚麼另外的見解嗎????


reply to postreply to post
作者 Re:初學java - 圖書館系統題目(需耐心者進入協助修改或指導) [Re:tiro]
ggininder





發文: 35
積分: 0
於 2018-09-21 05:51 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
那你可以先預期你會打印出什麼,跟你實際打印出什麼,來比對,再來看你哪裡邏輯有問題。

reply to postreply to post
作者 Re:初學java - 圖書館系統題目(需耐心者進入協助修改或指導) [Re:tiro]
clock

小時候在看國外測試機器人的影片



發文: 65
積分: 0
於 2018-09-21 23:39 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這是中肯圖書館的寫作方式希望能幫上忙
沒有修改功能
XD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import java.util.ArrayList;
import java.util.Scanner;
import java.util.function.Consumer;
 
public class CK_Library {
 
    public ArrayList<Book> alist = new ArrayList();
 
    public CK_Library() {
        loadData();
    }
 
    public static void main(String[] args) {
 
        CK_Library library = new CK_Library();
        library.systemOn();
    }
 
    private void systemOn() {
        Scanner scan = new Scanner(System.in);
 
        while (true) {
            printMenu();
            System.out.print("請輸入選項數字(1~5)=>");
            String input = scan.next();
            if (input.equals("1")) {
                System.out.print("請輸入 國際標準書號(ISBN),中文書名,英文書名,價格,數量(以逗點隔開)=>");
                scan = new Scanner(System.in, "big5");
                String[] bookInfo = scan.nextLine().toUpperCase().trim().split(",");
                System.out.println("-------------------------------------------------新增結果------------------------------------------------------");
                if (bookInfo.length != 5) {
                    System.out.println("新增書籍的資訊不足!請重新操作");
                } else {
                    addBook(bookInfo);
                }
 
            } else if (input.equals("2")) {
                System.out.print("請輸入 國際標準書號(ISBN)=>");
                scan = new Scanner(System.in, "big5");
                String isbn = scan.nextLine().toUpperCase().trim();
                System.out.println("-------------------------------------------------還書結果------------------------------------------------------");
                returnBook(isbn);
 
            } else if (input.equals("3")) {
                System.out.print("請輸入中文或英文書名=>");
                scan = new Scanner(System.in, "big5");
                String name = scan.nextLine().toUpperCase().trim();
                ArrayList<Book> resultList = findBooksByName(name);
                System.out.println("-------------------------------------------------查詢結果------------------------------------------------------");
                if (!resultList.isEmpty()) {
                    printBookList(resultList);
                }
 
            } else if (input.equals("4")) {
                System.out.print("請輸入書名=>");
                scan = new Scanner(System.in, "big5");
                String name = scan.nextLine().toUpperCase().trim();
                System.out.println("------------------------------------------------------------------------------------------------------------------");
                checkOutBook(name);
 
            } else if (input.equals("5")) {
                System.out.println("退出系統選單,記得再次光臨!!");
                break;
 
            } else {
                System.out.println("!!!! 請輸入工作選項 1~5 !!!!");
            }
            System.out.println("-----------------------------------------------------------------------------------------------------------------");
        }
 
    }
 
    private void printBookList(ArrayList alist) {
 
        alist.forEach(new Consumer<Book>() {
            int count = 1;
            @Override
            public void accept(Book b) {
                System.out.println(count + "." +b.toString());
                count++;
            }
        });
 
    }
 
    private void addBook(String... bookInfo) {
 
        Book book = new Book(bookInfo[0], bookInfo[1], bookInfo[2], Double.parseDouble(bookInfo[3]), Integer.parseInt(bookInfo[4]));
        boolean flag = false;
 
        for (Book b : alist) {
            if (b.ChineseName.equals(book.ChineseName) && b.EnglishName.equalsIgnoreCase(book.EnglishName)) {
                b.onShelf++;
                System.out.println("新增書籍已存在!數量由" + (b.onShelf - 1) + "=>" + b.onShelf);
                System.out.println(b.toString());
                flag = true;
                break;
            }
        }
        if (flag == false) {
            alist.add(book);
            System.out.println(book.toString());
        }
 
    }
 
    private void returnBook(String isbn) {
        Book book = queryByISBN(isbn);
        if (book != null) {
            book.onShelf++;
            System.out.println("歸還後架上數量:" + book.onShelf);
        } else {
            System.out.println("國際標準書號(ISBN)=" + isbn + " 沒有登記在本館目錄");
        }
 
    }
 
    private void checkOutBook(String name) {
 
        Book book = queryByName(name);
        if (book != null) {
            if (book.onShelf > 0) {
                book.onShelf = book.onShelf - 1;
                System.out.println("借閱書名:" + book.ChineseName + "(" + book.EnglishName + ")" + " 數量:" + 1);
            } else {
                System.out.println(book.ChineseName + "(" + book.EnglishName + ") 已全部借出");
            }
 
        } else {
            System.out.println("書名沒有登記在本館目錄");
        }
    }
 
    private Book queryByName(String bookName) {
        Book book = alist.stream()
                .filter(a -> a.ChineseName.equals(bookName) || a.EnglishName.equalsIgnoreCase(bookName))
                .findAny().orElse(null);
 
        return book;
 
    }
 
    private Book queryByISBN(String isbn) {
        Book book = alist.stream()
                .filter(a -> a.isbn.equals(isbn))
                .findAny().orElse(null);
        return book;
 
    }
 
    private ArrayList<Book> findBooksByName(String bookName) {
        ArrayList<Book> resultList = new ArrayList();
        for (Book b : alist) {
 
            if (b.ChineseName.startsWith(bookName)
                    || b.EnglishName.toUpperCase().startsWith(bookName.toUpperCase())) {
                resultList.add(b);
            }
 
        }
 
        return resultList;
 
    }
 
    private void loadData() {
        Book book1 = new Book("ABCD111111", "爪哇1", "java1", 450, 1);
        Book book2 = new Book("ABCD222222", "爪哇2", "java2", 450, 2);
        Book book3 = new Book("ABCD333333", "爪哇3", "java3", 450, 3);
        Book book4 = new Book("ABCD444444", "爪哇4", "java4", 450, 4);
        Book book5 = new Book("ABCD555555", "爪哇5", "java5", 450, 5);
        Book book6 = new Book("ABCD666666", "爪哇6", "java6", 450, 6);
        Book book7 = new Book("ABCD777777", "爪哇7", "java7", 450, 7);
        alist.add(book1);
        alist.add(book2);
        alist.add(book3);
        alist.add(book4);
        alist.add(book5);
        alist.add(book6);
        alist.add(book7);
    }
 
    public void printMenu() {
 
        System.out.println("中肯圖書館");
        System.out.println("1. 新增 書籍");
        System.out.println("2. 歸還 書籍 ");
        System.out.println("3. 查詢 書籍");
        System.out.println("4  借閱 書籍 ");
        System.out.println("5. 退出 系統");
 
    }
}
 


reply to postreply to post
c:你好像很喜歡研究機械產品
X:我從小就跟博士做研究
c:哪位博士
X:南宮博士
作者 Re:初學java - 圖書館系統題目(需耐心者進入協助修改或指導) [Re:clock]
tiro





發文: 5
積分: 0
於 2018-09-22 07:18 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
真的很謝謝你花時間,讓我有一絲絲希望,感激感激!我再繼續努力~~

reply to postreply to post
» JWorld@TW »  Java 新手區 » 學習筆記

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8