• Java图书馆借阅功能实现


    某镇上有两个图书馆,请按照下面要求及提示帮助这两个图书馆实现图书电子借阅。

    完成Library.java,该类中的main()方法已经给出。但类成员和类方法需要完善。完成该类的时候请注意以下要求:

     A.该类中有类成员方法,也有实例成员方法;

     B. 不能改变main()方法的内容。

     C.main( )方法的输出为:

       Library hours:

       Libraries are open daily from 9am to 5pm.

       Library addresses:

       10 Main St.

       228 Liberty St.

      Borrowing The Lord of the Rings:

      You successfully borrowed The Lord of the Rings

      Sorry, this book is already borrowed.

      Sorry, this book is not in our catalog.

     Books available in the first library:

     The Da Vinci Code

     Le Petit Prince

     A Tale of Two Cities

    Books available in the second library:

    No book in catalog

    Returning The Lord of the Rings:

    You successfully returned The Lord of the Rings

    Books available in the first library:

    The Da Vinci Code

    Le Petit Prince

    A Tale of Two Cities

    The Lord of the Rings

    1. import java.util.ArrayList;
    2. /**
    3. * @version 1.0
    4. */
    5. public class Library {
    6. // Add the missing implementation to this class
    7. String Library_title;
    8. public static class Book {
    9. String title;
    10. boolean borrowed;
    11. public Book(String title) {
    12. this.title = title;
    13. borrowed = false;
    14. }
    15. }
    16. //Book[] books = new Book[6];
    17. ArrayList<Book> books= new ArrayList<>();
    18. public Library(String library_title) {
    19. Library_title = library_title;
    20. // books = new Book[6];
    21. }
    22. public void addBook(Book book) {
    23. // Book_num++;
    24. // books[Book_num].title = book.title;
    25. // books[Book_num].borrowed = book.borrowed;
    26. books.add(book);
    27. }
    28. public static void printOpeningHours() {
    29. System.out.println("Libraries are open daily from 9am to 5pm.");
    30. }
    31. public void printAddress() {
    32. System.out.println(Library_title);
    33. }
    34. public void borrowBook(String name) {
    35. int i;
    36. for (i = 0; i < books.size(); i++) {
    37. if (name.equals(books.get(i).title)) {
    38. break;
    39. }
    40. }
    41. if (i == books.size()) {
    42. System.out.println("Sorry, this book is not in our catalog.");
    43. } else {
    44. if (!books.get(i).borrowed) {
    45. System.out.println("You successfully borrowed The Lord of the Rings");
    46. books.get(i).borrowed = true;
    47. } else {
    48. System.out.println("Sorry, this book is already borrowed.");
    49. }
    50. }
    51. }
    52. public void printAvailableBooks(){
    53. int i,j=0;
    54. for(i=0;i<books.size();i++){
    55. if(!books.get(i).borrowed){
    56. System.out.println(books.get(i).title);
    57. j++;
    58. }
    59. }
    60. if(j==0){
    61. System.out.println("No book in catalog");
    62. }
    63. }
    64. public void returnBook(String name){
    65. int i;
    66. for (i = 0; i < books.size(); i++) {
    67. if (name.equals(books.get(i).title)) {
    68. break;
    69. }
    70. }
    71. if (i == books.size()) {
    72. System.out.println("Sorry, this book is not in our catalog.");
    73. } else {
    74. System.out.println("You successfully returned "+name);
    75. books.get(i).borrowed = false;
    76. }
    77. }
    78. public static void main(String[] args) {
    79. // Create two libraries
    80. Library firstLibrary = new Library("10 Main St.");
    81. Library secondLibrary = new Library("228 Liberty St.");
    82. // Add four books to the first library
    83. firstLibrary.addBook(new Book("The Da Vinci Code"));
    84. firstLibrary.addBook(new Book("Le Petit Prince"));
    85. firstLibrary.addBook(new Book("A Tale of Two Cities"));
    86. firstLibrary.addBook(new Book("The Lord of the Rings"));
    87. // Print opening hours and the addresses
    88. System.out.println("Library hours:");
    89. printOpeningHours();
    90. System.out.println();
    91. System.out.println("Library addresses:");
    92. firstLibrary.printAddress();
    93. secondLibrary.printAddress();
    94. System.out.println();
    95. // Try to borrow The Lords of the Rings from both libraries
    96. System.out.println("Borrowing The Lord of the Rings:");
    97. firstLibrary.borrowBook("The Lord of the Rings");
    98. firstLibrary.borrowBook("The Lord of the Rings");
    99. secondLibrary.borrowBook("The Lord of the Rings");
    100. System.out.println();
    101. // Print the titles of all available books from both libraries
    102. System.out.println("Books available in the first library:");
    103. firstLibrary.printAvailableBooks();
    104. System.out.println();
    105. System.out.println("Books available in the second library:");
    106. secondLibrary.printAvailableBooks();
    107. System.out.println();
    108. // Return The Lords of the Rings to the first library
    109. System.out.println("Returning The Lord of the Rings:");
    110. firstLibrary.returnBook("The Lord of the Rings");
    111. System.out.println();
    112. // Print the titles of available from the first library
    113. System.out.println("Books available in the first library:");
    114. firstLibrary.printAvailableBooks();
    115. }
    116. }

  • 相关阅读:
    docker 常用指令(启动,关闭,查看运行状态)
    IIC 通信协议 (一)
    AI技术崛起:数据可视化之路更近
    Tomcat 学习笔记及常见问题解决
    【torchvision】torchvision 介绍
    Nginx七层的负载均衡使用keepalived实现高可用
    LiveGBS流媒体平台GB/T28181常见问题-安全控制HTTP接口鉴权勾选流地址鉴权后401Unauthorized如何播放调用接口
    BUUCTF key不在这里
    Django的学习笔记
    C++ | Leetcode C++题解之第191题位1的个数
  • 原文地址:https://blog.csdn.net/weixin_62495164/article/details/127399234