Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class NonExistingItemException extends Exception {
- private static final long serialVersionUID = 1L;
- public NonExistingItemException(String message) {
- super(message);
- }
- }
- abstract class Archive {
- int id;
- Date dateArchived;
- public Archive(int id) {
- this.id = id;
- this.dateArchived = null;
- }
- public abstract void openItem(Date date, StringBuilder s);
- }
- class LockedArchive extends Archive {
- Date dateToOpen;
- public LockedArchive(int id, Date dateToOpen) {
- super(id);
- this.dateToOpen = dateToOpen;
- }
- @Override
- public void openItem(Date date, StringBuilder s) {
- if (date.before(dateToOpen))
- s.append(String.format("Item %d cannot be opened before %s\n", id,
- dateToOpen.toString()));
- else
- s.append(String.format("Item %d opened at %s\n", id,
- date.toString()));
- }
- }
- class SpecialArchive extends Archive {
- int maxOpen;
- private int counter;
- public SpecialArchive(int id, int maxOpen) {
- super(id);
- this.maxOpen = maxOpen;
- this.counter = 0;
- }
- @Override
- public void openItem(Date date, StringBuilder s) {
- if (counter >= maxOpen)
- s.append(String.format(
- "Item %d cannot be opened more than %d times\n", id,
- maxOpen));
- else {
- s.append(String.format("Item %d opened at %s\n", id,
- date.toString()));
- counter++;
- }
- }
- }
- class ArchiveStore {
- private ArrayList<Archive> archives;
- private StringBuilder s;
- public ArchiveStore() {
- archives = new ArrayList<Archive>();
- s = new StringBuilder();
- }
- public void archiveItem(Archive item, Date date) {
- item.dateArchived = date;
- archives.add(item);
- s.append(String.format("Item %d archived at %s\n", item.id,
- date.toString()));
- }
- public void openItem(int id, Date date) throws NonExistingItemException {
- for (int i = 0; i < archives.size(); i++) {
- if (archives.get(i).id == id) {
- archives.get(i).openItem(date, s);
- return;
- }
- }
- throw new NonExistingItemException(String.format(
- "Item with id %d doesn't exist", id));
- }
- public String getLog() {
- return s.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment