DamSi

Untitled

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