Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.48 KB | None | 0 0
  1. import java.util.Date;
  2. import java.util.Scanner;
  3.  
  4. public class ArchiveStoreTest {
  5.         public static void main(String[] args) {
  6.                 ArchiveStore store = new ArchiveStore();
  7.                 @SuppressWarnings("deprecation")
  8.                 Date date = new Date(113, 10, 7);
  9.                 Scanner scanner = new Scanner(System.in);
  10.                 scanner.nextLine();
  11.                 int n = scanner.nextInt();
  12.                 scanner.nextLine();
  13.                 scanner.nextLine();
  14.                 int i;
  15.                 for (i = 0; i < n; ++i) {
  16.                         int id = scanner.nextInt();
  17.                         long days = scanner.nextLong();
  18.                         Date dateToOpen = new Date(date.getTime()
  19.                                         + (days * 24 * 60 * 60 * 1000));
  20.                         LockedArchive lockedArchive = new LockedArchive(id, dateToOpen);
  21.                         store.archiveItem(lockedArchive, date);
  22.                 }
  23.                 scanner.nextLine();
  24.                 scanner.nextLine();
  25.                 n = scanner.nextInt();
  26.                 scanner.nextLine();
  27.                 scanner.nextLine();
  28.                 for (i = 0; i < n; ++i) {
  29.                         int id = scanner.nextInt();
  30.                         int maxOpen = scanner.nextInt();
  31.                         SpecialArchive specialArchive = new SpecialArchive(id, maxOpen);
  32.                         store.archiveItem(specialArchive, date);
  33.                 }
  34.                 scanner.nextLine();
  35.                 scanner.nextLine();
  36.                 while (scanner.hasNext()) {
  37.                         int open = scanner.nextInt();
  38.                         try {
  39.                                 store.openItem(open, date);
  40.                         } catch (NonExistingItemException e) {
  41.                                 System.out.println(e.getMessage());
  42.                         }
  43.                 }
  44.                 System.out.println(store.getLog());
  45.         }
  46. }
  47.  
  48. class NonExistingItemException extends Exception {
  49.         public NonExistingItemException(String string) {
  50.                 super(string);
  51.         }
  52. }
  53.  
  54. abstract class Archive {
  55.         int id;
  56.         Date date;
  57.  
  58.         public int getId() {
  59.                 return id;
  60.         }
  61.  
  62.         public void setId(int id) {
  63.                 this.id = id;
  64.         }
  65.  
  66.         public Date getDate() {
  67.                 return date;
  68.         }
  69.  
  70.         public void setDate(Date date) {
  71.                 this.date = date;
  72.         }
  73.  
  74.         public Archive(int id) {
  75.                 this.id = id;
  76.         }
  77. }
  78.  
  79. class LockedArchive extends Archive {
  80.         Date dateToOpen;
  81.  
  82.         public LockedArchive(int id, Date dateToOpen) {
  83.                 super(id);
  84.                 // this.id = id;
  85.                 // this.date = date;
  86.                 this.dateToOpen = dateToOpen;
  87.         }
  88.  
  89.         public Date getDateToOpen() {
  90.                 return dateToOpen;
  91.         }
  92.  
  93. }
  94.  
  95. class SpecialArchive extends Archive {
  96.         int maxopen;
  97.  
  98.         public SpecialArchive(int id, int maxopen) {
  99.                 super(id);
  100.                 // this.id = id;
  101.                 // this.date = date;
  102.                 this.maxopen = maxopen;
  103.         }
  104.  
  105. }
  106.  
  107. class ArchiveStore {
  108.         Archive[] niza;
  109.         int[] count;
  110.         String[] journal;
  111.  
  112.         public ArchiveStore() {
  113.                 niza = null;
  114.                 journal = null;
  115.         }
  116.  
  117.         public void archiveItem(Archive item, Date date) {
  118.                 if (niza == null) {
  119.                         niza = new Archive[1];
  120.                         niza[0] = item;
  121.                         niza[0].setDate(date);
  122.                         StringBuilder sb = new StringBuilder();
  123.                         sb.append("Item " + niza[0].getId() + " archived at " + date
  124.                                         + "\n");
  125.                         writeInJournal(sb.toString());
  126.                         count = new int[1];
  127.                         count[0] = 0;
  128.                 } else {
  129.                         int n = niza.length;
  130.                         Archive[] tmp = new Archive[n + 1];
  131.                         for (int i = 0; i < n; i++)
  132.                                 tmp[i] = niza[i];
  133.                         tmp[n] = item;
  134.                         tmp[n].setDate(date);
  135.                         niza = tmp;
  136.                         int len = count.length;
  137.                         int[] t = new int[len + 1];
  138.                         for (int i = 0; i < len; i++)
  139.                                 t[i] = count[i];
  140.                         t[len] = 0;
  141.                         count = t;
  142.                         StringBuilder sb = new StringBuilder();
  143.                         sb.append("Item " + niza[n].getId() + " archived at " + date
  144.                                         + "\n");
  145.                         writeInJournal(sb.toString());
  146.                 }
  147.         }
  148.  
  149.         public void openItem(int id, Date date) throws NonExistingItemException {
  150.                 int n = niza.length;
  151.                 short flag = 1;
  152.                 for (int i = 0; i < n; i++) {
  153.                         if (niza[i].getId() == id) {
  154.                                 flag = 0;
  155.                                 if (niza[i] instanceof LockedArchive) {
  156.                                         if (date.compareTo(((LockedArchive) niza[i]).getDateToOpen()) > 0) {
  157.                                                 StringBuilder sb = new StringBuilder();
  158.                                                 sb.append("Item " + niza[i].getId() + " opened at "
  159.                                                                 + date + "\n");
  160.                                                 writeInJournal(sb.toString());
  161.                                         } else {
  162.                                                 StringBuilder sb = new StringBuilder();
  163.                                                 sb.append("Item " + niza[i].getId()
  164.                                                                 + " cannot be opened before " + ((LockedArchive)niza[i]).getDateToOpen() + "\n");
  165.                                                 writeInJournal(sb.toString());
  166.                                         }
  167.                                 } else {
  168.                                         SpecialArchive tmp = (SpecialArchive) niza[i];
  169.                                         if (count[i] + 1 > tmp.maxopen) {
  170.                                                 StringBuilder sb = new StringBuilder();
  171.                                                 sb.append("Item " + niza[i].getId()
  172.                                                                 + " cannot be opened more than "
  173.                                                                 + tmp.maxopen + " times\n");
  174.                                                 writeInJournal(sb.toString());
  175.                                         } else {
  176.                                                 StringBuilder sb = new StringBuilder();
  177.                                                 sb.append("Item " + niza[i].getId() + " opened at "
  178.                                                                 + date + "\n");
  179.                                                 writeInJournal(sb.toString());
  180.                                                 count[i]++;
  181.                                         }
  182.                                 }
  183.                         }
  184.                 }
  185.                 if(flag == 1) {
  186.                         StringBuilder sb = new StringBuilder();
  187.                         sb.append("Item with id " + id + " doesn't exist");
  188.                         throw new NonExistingItemException(sb.toString());
  189.                 }
  190.         }
  191.  
  192.         public void writeInJournal(String s) {
  193.                 if (journal == null) {
  194.                         journal = new String[1];
  195.                         journal[0] = s;
  196.                 } else {
  197.                         int N = journal.length;
  198.                         String[] tp = new String[N + 1];
  199.                         for (int i = 0; i < N; i++)
  200.                                 tp[i] = journal[i];
  201.                         tp[N] = s;
  202.                         journal = tp;
  203.                 }
  204.         }
  205.         public String getLog(){
  206.                 StringBuilder sb = new StringBuilder();
  207.                 int N = journal.length;
  208.                 for(int i = 0; i < N; i++)
  209.                         sb.append(journal[i]);
  210.                 return sb.toString();
  211.         }
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement