Filip_Markoski

[NP] Архива

Nov 12th, 2017
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.50 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.Scanner;
  5.  
  6. class NonExistingItemException extends Exception {
  7.     int id;
  8.  
  9.     public NonExistingItemException(int id) {
  10.         this.id = id;
  11.     }
  12.  
  13.     @Override
  14.     public String getMessage() {
  15.         return String.format("Item with id %d doesn't exist", id);
  16.     }
  17. }
  18.  
  19. class Archive {
  20.     private int id;
  21.     private Date dateArchived;
  22.  
  23.     public Archive(int id) {
  24.         this.id = id;
  25.         this.dateArchived = null;
  26.     }
  27.  
  28.     public int getId() {
  29.         return id;
  30.     }
  31.  
  32.     public Date getDateArchived() {
  33.         return dateArchived;
  34.     }
  35.  
  36.     public void setDateArchived(Date dateArchived) {
  37.         this.dateArchived = (Date) dateArchived.clone();
  38.     }
  39.  
  40.     public String getType() {
  41.         return "A";
  42.     }
  43.  
  44.     public boolean equals(Archive that) {
  45.         if (this.id != that.id) {
  46.             return false;
  47.         }
  48.         return true;
  49.     }
  50.  
  51.     @Override
  52.     public String toString() {
  53.         return String.format("Item %d opened at %s", id, dateArchived);
  54.     }
  55. }
  56.  
  57. class LockedArchive extends Archive {
  58.     private Date dateToOpen;
  59.  
  60.     public LockedArchive(int id, Date dateToOpen) {
  61.         super(id);
  62.         this.dateToOpen = (Date) dateToOpen.clone();
  63.     }
  64.  
  65.     public String getType() {
  66.         return "L";
  67.     }
  68.  
  69.     @Override
  70.     public String toString() {
  71.         if (getDateArchived().before(dateToOpen)) {
  72.             return String.format("Item %d cannot be opened before %s", getId(), dateToOpen);
  73.         }
  74.         return super.toString();
  75.     }
  76.  
  77.     public boolean equals(Archive that) {
  78.         return super.equals(that);
  79.     }
  80.  
  81.     @Override
  82.     public int hashCode() {
  83.         return dateToOpen != null ? dateToOpen.hashCode() : 0;
  84.     }
  85. }
  86.  
  87. class SpecialArchive extends Archive {
  88.     int maxOpen;
  89.     int opened;
  90.  
  91.     public SpecialArchive(int id, int maxOpen) {
  92.         super(id);
  93.         this.maxOpen = maxOpen;
  94.         this.opened = 0;
  95.     }
  96.  
  97.     public String getType() {
  98.         return "S";
  99.     }
  100.  
  101.  
  102.     public boolean equals(Archive that) {
  103.         return super.equals(that);
  104.     }
  105.  
  106.     @Override
  107.     public String toString() {
  108.         if (opened == maxOpen) {
  109.             return String.format("Item %d cannot be opened more than %d times", getId(), maxOpen);
  110.         }
  111.         opened++;
  112.         return super.toString();
  113.     }
  114. }
  115.  
  116. class ArchiveStore {
  117.     ArrayList<Archive> archives;
  118.  
  119.     public ArchiveStore() {
  120.         this.archives = new ArrayList<>();
  121.     }
  122.  
  123.     public void archiveItem(Archive item, Date date) throws IOException {
  124.         item.setDateArchived(date);
  125.         archives.add(item);
  126.         /* Writing to log file */
  127.         String temp = String.format("Item %d archived at %s", item.getId(), date.toString());
  128.         appendToFile(temp);
  129.     }
  130.  
  131.     public void openItem(int id, Date date) throws NonExistingItemException, IOException {
  132.         //System.out.println("Open Item");
  133.         //System.out.println(archives);
  134.         boolean found = false;
  135.  
  136.         Archive temp = new Archive(id);
  137.  
  138.         for (int i = 0; i < archives.size(); i++) {
  139.             /*System.out.println(i + ": " + archives.get(i));
  140.             System.out.println("TEMP: " + temp);*/
  141.             if (archives.get(i).equals(temp)) {
  142.  
  143.                 temp = archives.get(i);
  144.  
  145.                 found = true;
  146.                 break;
  147.             }
  148.         }
  149.         if (!found) {
  150.             throw new NonExistingItemException(id);
  151.         }
  152.         /* Appending to file */
  153.         appendToFile(temp.toString());
  154.     }
  155.  
  156.     public void appendToFile(String line) throws IOException {
  157.         File fileName = new File("archive.txt");
  158.         //System.out.println("Writing to file");
  159.         fileName.createNewFile();
  160.         try (PrintWriter pw = new PrintWriter(new FileWriter(fileName, true))) {
  161.             pw.println(line);
  162.             pw.close();
  163.         }
  164.     }
  165.  
  166.     public void clearFile() throws IOException {
  167.         File fileName = new File("archive.txt");
  168.         //System.out.println("Clearing file");
  169.         fileName.createNewFile();
  170.         try (PrintWriter pw = new PrintWriter(new FileWriter(fileName))) {
  171.             pw.print("");
  172.             pw.close();
  173.         }
  174.     }
  175.  
  176.     public String getLog() throws IOException {
  177.         String line = null;
  178.         StringBuffer sb = new StringBuffer();
  179.         //System.out.println("Reading new file");
  180.         /* Reading from file */
  181.         File fileName = new File("archive.txt");
  182.         if (fileName.exists()) {
  183.             try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
  184.                 while ((line = br.readLine()) != null) {
  185.                     sb.append(line);
  186.                     sb.append("\n");
  187.                 }
  188.                 br.close();
  189.             }
  190.         }
  191.         return sb.toString().replace("GMT", "UTC");
  192.     }
  193. }
  194.  
  195. public class ArchiveStoreTest {
  196.     public static void main(String[] args) throws IOException {
  197.         ArchiveStore store = new ArchiveStore();
  198.         store.clearFile();
  199.         Date date = new Date(113, 10, 7);
  200.         Scanner scanner = new Scanner(System.in);
  201.         scanner.nextLine();
  202.         int n = scanner.nextInt();
  203.         scanner.nextLine();
  204.         scanner.nextLine();
  205.         int i;
  206.         for (i = 0; i < n; ++i) {
  207.             int id = scanner.nextInt();
  208.             long days = scanner.nextLong();
  209.             Date dateToOpen = new Date(date.getTime() + (days * 24 * 60
  210.                     * 60 * 1000));
  211.             LockedArchive lockedArchive = new LockedArchive(id, dateToOpen);
  212.             store.archiveItem(lockedArchive, date);
  213.         }
  214.         scanner.nextLine();
  215.         scanner.nextLine();
  216.         n = scanner.nextInt();
  217.         scanner.nextLine();
  218.         scanner.nextLine();
  219.         for (i = 0; i < n; ++i) {
  220.             int id = scanner.nextInt();
  221.             int maxOpen = scanner.nextInt();
  222.             SpecialArchive specialArchive = new SpecialArchive(id, maxOpen);
  223.             store.archiveItem(specialArchive, date);
  224.         }
  225.         scanner.nextLine();
  226.         scanner.nextLine();
  227.         while (scanner.hasNext()) {
  228.             int open = scanner.nextInt();
  229.             try {
  230.                 store.openItem(open, date);
  231.             } catch (NonExistingItemException e) {
  232.                 System.out.println(e.getMessage());
  233.             }
  234.         }
  235.         //System.out.println("\nLOG\n");
  236.         System.out.println(store.getLog());
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment