Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

Book.java 2.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Written by : Bin Hong Lee
  3. * Last edited : 4/27/2016
  4. */
  5. class Book
  6. {
  7. //Declaring variable storages
  8. //Book title
  9. private String title;
  10. //Book ID
  11. private int id;
  12. //( AVAILABLE || RENTED || RESERVED || NOT AVAILABLE)
  13. private String status;
  14. //Book due date
  15. private int[] dueDate = new int[3];
  16. //Constructor without book title
  17. public Book(int id)
  18. {
  19. this.id = id;
  20. this.status = "NOT AVAILABLE";
  21. }
  22. //Constructor with book title
  23. public Book(String title, int id)
  24. {
  25. this.title = title;
  26. this.id = id;
  27. this.status = "AVAILABLE";
  28. }
  29. //Getters and setters
  30. public String getTitle()
  31. {
  32. if (title!=null)
  33. {
  34. return title;
  35. } else {
  36. return "UNDEFINED";
  37. }
  38. }
  39. public String getStatus()
  40. {
  41. return status;
  42. }
  43. public int getId()
  44. {
  45. return id;
  46. }
  47. public void setTitle(String title)
  48. {
  49. this.title = title;
  50. }
  51. //Call to rent book, return if renting is successful
  52. public boolean rent(int[] dueDate)
  53. {
  54. //Check if the book is available to be rented
  55. if(status != "AVAILABLE")
  56. {
  57. return false;
  58. }
  59. //Update the due date of the book
  60. this.dueDate = dueDate;
  61. //Update book status
  62. status = "RENTED";
  63. return true;
  64. }
  65. //Call to return the book
  66. public void returned()
  67. {
  68. //Update book status
  69. status = "AVAILABLE";
  70. }
  71. //Calculate the overdue fine
  72. public double overdueFine(int[] currentDay)
  73. {
  74. double fine;
  75. boolean monChange = false;
  76. while(currentDay[0] > dueDate[0])
  77. {
  78. currentDay[0]--;
  79. currentDay[1]+=12;
  80. }
  81. while(currentDay[1] > dueDate[1])
  82. {
  83. int mon = currentDay[1] % 12;
  84. if(mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12)
  85. {
  86. currentDay[2]+=31;
  87. }
  88. else if(mon == 2)
  89. {
  90. currentDay[2]+=28;
  91. }
  92. else
  93. {
  94. currentDay[2]+=30;
  95. }
  96. currentDay[1]--;
  97. }
  98. fine = (double)(currentDay[2] - dueDate[2]) * 0.25;
  99. if(fine > 5)
  100. {
  101. return 5;
  102. }
  103. else
  104. {
  105. return fine;
  106. }
  107. }
  108. }