No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

131 líneas
2.3 KiB

  1. /*
  2. * Written by : Bin Hong Lee
  3. * Last edited : 5/31/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. if(currentDay[0] < dueDate[0] || (currentDay[0] == dueDate[0] && currentDay[1] < dueDate[1]) || (currentDay[0] == dueDate[0] && currentDay[1] == dueDate[1] && currentDay[2] <= dueDate[2]))
  77. {
  78. return 0;
  79. }
  80. while(currentDay[0] > dueDate[0])
  81. {
  82. currentDay[0]--;
  83. currentDay[1]+=12;
  84. }
  85. while(currentDay[1] > dueDate[1])
  86. {
  87. int mon = currentDay[1] % 12;
  88. if(mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12)
  89. {
  90. currentDay[2]+=31;
  91. }
  92. else if(mon == 2)
  93. {
  94. currentDay[2]+=28;
  95. }
  96. else
  97. {
  98. currentDay[2]+=30;
  99. }
  100. currentDay[1]--;
  101. }
  102. fine = (double)(currentDay[2] - dueDate[2]) * 0.25;
  103. if(fine > 5)
  104. {
  105. return 5;
  106. }
  107. else
  108. {
  109. return fine;
  110. }
  111. }
  112. }