Advertisement
SashkoKlincharov

[Java][НП] - Архива

Aug 27th, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1.  
  2.  
  3. Да се имплементира класа ArchiveStore во која се чува листа на архиви (елементи за архивирање).
  4.  
  5. Секој елемент за архивирање Archive има:
  6.  
  7. id - цел број
  8. dateArchived - датум на архивирање.
  9.  
  10. Постојат два видови на елементи за архивирање, LockedArchive за кој дополнително се чува датум до кој не смее да се отвори dateToOpen и SpecialArchive за кој се чуваат максимален број на дозволени отварања maxOpen. За елементите за архивирање треба да се обезбедат следните методи:
  11.  
  12. LockedArchive(int id, Date dateToOpen) - конструктор за заклучена архива
  13. SpecialArchive(int id, int maxOpen) - конструктор за специјална архива
  14.  
  15. За класата ArchiveStore да се обезбедат следните методи:
  16.  
  17. ArchiveStore() - default конструктор
  18. void archiveItem(Archive item, Date date) - метод за архивирање елемент item на одреден датум date
  19. void openItem(int id, Date date) - метод за отварање елемент од архивата со зададен id и одреден датум date. Ако не постои елемент со даденото id треба да се фрли исклучок од тип NonExistingItemException со порака Item with id [id] doesn't exist.
  20. String getLog() - враќа стринг со сите пораки запишани при архивирањето и отварањето архиви во посебен ред.
  21.  
  22. За секоја акција на архивирање во текст треба да се додаде следната порака Item [id] archived at [date], додека за секоја акција на отварање архива треба да се додаде Item [id] opened at [date]. При отварање ако се работи за LockedArhive и датумот на отварање е пред датумот кога може да се отвори, да се додаде порака Item [id] cannot be opened before [date]. Ако се работи за SpecialArhive и се обидиеме да ја отвориме повеќе пати од дозволениот број (maxOpen) да се додаде порака Item [id] cannot be opened more than [maxOpen] times.
  23.  
  24.  
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.List;
  28. import java.util.Scanner;
  29.  
  30. class NonExistingItemException extends Exception {
  31.  
  32.  
  33. public NonExistingItemException(String message) {
  34. super(message);
  35. }
  36. }
  37.  
  38.  
  39. abstract class Archive {
  40. private int id;
  41. private Date date_archived;
  42.  
  43. public Archive(int id) {
  44. this.id = id;
  45. }
  46.  
  47. public void setDate_archived(Date date_archived) {
  48. this.date_archived = date_archived;
  49. }
  50.  
  51. public Date getDate_archived() {
  52. return date_archived;
  53. }
  54.  
  55. public int getId() {
  56. return id;
  57. }
  58.  
  59.  
  60. @Override
  61. public String toString() {
  62. return "Archive{" +
  63. "id=" + id +
  64. ", date_archived=" + date_archived +
  65. '}';
  66. }
  67.  
  68. public abstract String open(Date date);
  69. }
  70.  
  71. class LockedArchive extends Archive {
  72. private Date dateToOpen;
  73.  
  74. public LockedArchive(int id, Date dateToOpen) {
  75. super(id);
  76. this.dateToOpen = dateToOpen;
  77. }
  78.  
  79. public Date getDateToOpen() {
  80. return dateToOpen;
  81. }
  82.  
  83. @Override
  84. public String open(Date date) {
  85. if (date.before(this.getDateToOpen()))
  86. return String.format("Item %d cannot be opened before %s\n", this.getId(), dateToOpen.toString()); return ("Item " + getId() + " opened at " + date.toString() + "\n");
  87. }
  88. }
  89.  
  90. class SpecialArchive extends Archive {
  91. private int maxOpen;
  92. private int currentTimesOpened;
  93.  
  94. public SpecialArchive(int id, int maxOpen) {
  95. super(id);
  96. this.maxOpen = maxOpen;
  97. }
  98.  
  99. public void setCurrentTimesOpened(int currentTimesOpened) {
  100. this.currentTimesOpened = currentTimesOpened;
  101. }
  102.  
  103. public int getCurrentTimesOpened() {
  104. return currentTimesOpened;
  105. }
  106.  
  107. public int getMaxOpen() {
  108. return maxOpen;
  109. }
  110.  
  111. @Override
  112. public String open(Date date) {
  113. if (this.getMaxOpen() == this.getCurrentTimesOpened())
  114. return ("Item " + getId() + " cannot be opened more than " + this.getMaxOpen() + " times\n");
  115. currentTimesOpened++;
  116. return ("Item " + getId() + " opened at " + date.toString() + "\n");
  117. }
  118. }
  119.  
  120. class ArchiveStore {
  121. private List<Archive> archiveList;
  122. private static StringBuilder sb;
  123.  
  124. public ArchiveStore() {
  125. archiveList = new ArrayList<>();
  126. sb = new StringBuilder();
  127. }
  128.  
  129. public void archiveItem(Archive item, Date date) {
  130. item.setDate_archived(date);
  131. archiveList.add(item);
  132. sb.append(String.format("Item %d archived at %s%n", item.getId(), date.toString()));
  133. }
  134.  
  135. public void openItem(int id, Date date) throws NonExistingItemException {
  136. Archive a = archiveList.stream().filter(each -> each.getId() == id).findAny().orElse(null);
  137. if (a == null)
  138. throw new NonExistingItemException(String.format("Item with id %d doesn't exist", id));
  139.  
  140. sb.append(a.open(date));
  141. }
  142.  
  143. public String getLog() {
  144. return sb.toString().replaceAll("GMT","UTC");
  145.  
  146. }
  147. }
  148.  
  149. public class ArchiveStoreTest {
  150. public static void main(String[] args) {
  151. ArchiveStore store = new ArchiveStore();
  152. Date date = new Date(113, 10, 7);
  153. Scanner scanner = new Scanner(System.in);
  154. scanner.nextLine();
  155. int n = scanner.nextInt();
  156. scanner.nextLine();
  157. scanner.nextLine();
  158. int i;
  159. for (i = 0; i < n; ++i) {
  160. int id = scanner.nextInt();
  161. long days = scanner.nextLong();
  162. Date dateToOpen = new Date(date.getTime() + (days * 24 * 60
  163. * 60 * 1000));
  164. LockedArchive lockedArchive = new LockedArchive(id, dateToOpen);
  165. store.archiveItem(lockedArchive, date);
  166. }
  167. scanner.nextLine();
  168. scanner.nextLine();
  169. n = scanner.nextInt();
  170. scanner.nextLine();
  171. scanner.nextLine();
  172. for (i = 0; i < n; ++i) {
  173. int id = scanner.nextInt();
  174. int maxOpen = scanner.nextInt();
  175. SpecialArchive specialArchive = new SpecialArchive(id, maxOpen);
  176. store.archiveItem(specialArchive, date);
  177. }
  178. scanner.nextLine();
  179. scanner.nextLine();
  180. while (scanner.hasNext()) {
  181. int open = scanner.nextInt();
  182. try {
  183. store.openItem(open, date);
  184. } catch (NonExistingItemException e) {
  185. System.out.println(e.getMessage());
  186. }
  187. }
  188. System.out.println(store.getLog());
  189. }
  190. }
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. Sample input
  198.  
  199. Locked Archive Count
  200. 2
  201. Id Date (Days in future)
  202. 1 50
  203. 2 -25
  204. Special Archive Count
  205. 2
  206. Id MaxOpen
  207. 3 5
  208. 4 2
  209. Opening
  210. 1 2 3 7 4 4 4 3 3 3 3 3
  211.  
  212. Sample output
  213.  
  214. Item with id 7 doesn't exist
  215. Item 1 archived at Thu Nov 07 00:00:00 UTC 2013
  216. Item 2 archived at Thu Nov 07 00:00:00 UTC 2013
  217. Item 3 archived at Thu Nov 07 00:00:00 UTC 2013
  218. Item 4 archived at Thu Nov 07 00:00:00 UTC 2013
  219. Item 1 cannot be opened before Fri Dec 27 00:00:00 UTC 2013
  220. Item 2 opened at Thu Nov 07 00:00:00 UTC 2013
  221. Item 3 opened at Thu Nov 07 00:00:00 UTC 2013
  222. Item 4 opened at Thu Nov 07 00:00:00 UTC 2013
  223. Item 4 opened at Thu Nov 07 00:00:00 UTC 2013
  224. Item 4 cannot be opened more than 2 times
  225. Item 3 opened at Thu Nov 07 00:00:00 UTC 2013
  226. Item 3 opened at Thu Nov 07 00:00:00 UTC 2013
  227. Item 3 opened at Thu Nov 07 00:00:00 UTC 2013
  228. Item 3 opened at Thu Nov 07 00:00:00 UTC 2013
  229. Item 3 cannot be opened more than 5 times
  230.  
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement