Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Archive {
- protected int id;
- protected Date dateArchived;
- public Archive(int id) {
- this.id = id;
- this.dateArchived = new Date();
- }
- public Date getDateArchived() {
- return dateArchived;
- }
- }
- class LockedArchive extends Archive {
- private Date dateToOpen;
- public LockedArchive(int id, Date dateToOpen) {
- super(id);
- this.dateToOpen = dateToOpen;
- }
- public Date getDateToOpen() {
- return dateToOpen;
- }
- }
- class SpecialArchive extends Archive {
- protected int maxOpen;
- protected int timesOpen;
- public SpecialArchive(int id,int maxOpen) {
- super(id);
- this.maxOpen = maxOpen;
- timesOpen=0;
- }
- public int getMaxOpen() {
- return maxOpen;
- }
- }
- class ArchiveStore {
- private ArrayList<Archive> files;
- private StringBuilder logFile;
- public ArchiveStore() {
- files = new ArrayList<Archive>();
- logFile = new StringBuilder();
- }
- public void archiveItem(Archive item, Date date) {
- files.add(item);
- logFile.append("Item " + item.id + " archived at " + item.dateArchived+"\n");
- }
- public void openItem(int id, Date date) throws NonExistingItemException {
- boolean isOpen = false;
- Archive itemToArchive = null;
- for (Archive item : files) {
- if (item.id == id) {
- isOpen = true;
- item = itemToArchive;
- break;
- }
- }
- if (isOpen == true) {
- if (itemToArchive instanceof LockedArchive) {
- if (itemToArchive.getDateArchived().getTime() > date.getTime()) {
- logFile.append("Item " + id + " cannot be opened before " + itemToArchive.getDateArchived()+"\n");
- }
- if (itemToArchive instanceof SpecialArchive) {
- SpecialArchive o = null;
- o = (SpecialArchive) itemToArchive;
- if (o.maxOpen > o.timesOpen) {
- logFile.append("Item " + id + " cannot be opened more than " + o.maxOpen + " times\n");
- } else {
- logFile.append("Item " + id + " opened at " + date + "\n");
- }
- }
- }
- } else {
- throw new NonExistingItemException("Item with id " + id + " doesn't exist");
- }
- }
- public String getLog() {
- return logFile.toString();
- }
- }
- @SuppressWarnings("serial")
- class NonExistingItemException extends Exception {
- public NonExistingItemException(String s){
- super(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement