Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. class Archive {
  2.     protected int id;
  3.     protected Date dateArchived;
  4.  
  5.     public Archive(int id) {
  6.         this.id = id;
  7.         this.dateArchived = new Date();
  8.     }
  9.  
  10.     public Date getDateArchived() {
  11.         return dateArchived;
  12.     }
  13. }
  14.  class LockedArchive extends Archive {
  15.     private Date dateToOpen;
  16.  
  17.     public LockedArchive(int id, Date dateToOpen) {
  18.         super(id);
  19.         this.dateToOpen = dateToOpen;
  20.     }
  21.  
  22.     public Date getDateToOpen() {
  23.         return dateToOpen;
  24.     }
  25.  
  26. }
  27. class SpecialArchive extends Archive {
  28.     protected int maxOpen;
  29.     protected int timesOpen;
  30.  
  31.     public SpecialArchive(int id,int maxOpen) {
  32.         super(id);
  33.         this.maxOpen = maxOpen;
  34.         timesOpen=0;
  35.     }
  36.  
  37.     public int getMaxOpen() {
  38.         return maxOpen;
  39.     }
  40.  
  41. }
  42. class ArchiveStore {
  43.     private ArrayList<Archive> files;
  44.     private StringBuilder logFile;
  45.  
  46.     public ArchiveStore() {
  47.         files = new ArrayList<Archive>();
  48.         logFile = new StringBuilder();
  49.     }
  50.  
  51.     public void archiveItem(Archive item, Date date) {
  52.         files.add(item);
  53.         logFile.append("Item " + item.id + " archived at " + item.dateArchived+"\n");
  54.     }
  55.  
  56.     public void openItem(int id, Date date) throws NonExistingItemException {
  57.         boolean isOpen = false;
  58.         Archive itemToArchive = null;
  59.         for (Archive item : files) {
  60.             if (item.id == id) {
  61.                 isOpen = true;
  62.                 item = itemToArchive;
  63.                 break;
  64.             }
  65.         }
  66.         if (isOpen == true) {
  67.             if (itemToArchive instanceof LockedArchive) {
  68.                 if (itemToArchive.getDateArchived().getTime() > date.getTime()) {
  69.                     logFile.append("Item " + id + " cannot be opened before " + itemToArchive.getDateArchived()+"\n");
  70.                 }
  71.                 if (itemToArchive instanceof SpecialArchive) {
  72.                     SpecialArchive o = null;
  73.                     o = (SpecialArchive) itemToArchive;
  74.                     if (o.maxOpen > o.timesOpen) {
  75.                         logFile.append("Item " + id + " cannot be opened more than " + o.maxOpen + " times\n");
  76.                     } else {
  77.                         logFile.append("Item " + id + " opened at " + date + "\n");
  78.                     }
  79.                 }
  80.             }
  81.         } else {
  82.             throw new NonExistingItemException("Item with id " + id + " doesn't exist");
  83.         }
  84.     }
  85.  
  86.     public String getLog() {
  87.         return logFile.toString();
  88.     }
  89. }
  90. @SuppressWarnings("serial")
  91.  class NonExistingItemException extends Exception {
  92.     public NonExistingItemException(String s){
  93.         super(s);
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement