Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- import java.util.Scanner;
- public class ArchiveStoreTest {
- public static void main(String[] args) {
- ArchiveStore store = new ArchiveStore();
- @SuppressWarnings("deprecation")
- Date date = new Date(113, 10, 7);
- Scanner scanner = new Scanner(System.in);
- scanner.nextLine();
- int n = scanner.nextInt();
- scanner.nextLine();
- scanner.nextLine();
- int i;
- for (i = 0; i < n; ++i) {
- int id = scanner.nextInt();
- long days = scanner.nextLong();
- Date dateToOpen = new Date(date.getTime()
- + (days * 24 * 60 * 60 * 1000));
- LockedArchive lockedArchive = new LockedArchive(id, dateToOpen);
- store.archiveItem(lockedArchive, date);
- }
- scanner.nextLine();
- scanner.nextLine();
- n = scanner.nextInt();
- scanner.nextLine();
- scanner.nextLine();
- for (i = 0; i < n; ++i) {
- int id = scanner.nextInt();
- int maxOpen = scanner.nextInt();
- SpecialArchive specialArchive = new SpecialArchive(id, maxOpen);
- store.archiveItem(specialArchive, date);
- }
- scanner.nextLine();
- scanner.nextLine();
- while (scanner.hasNext()) {
- int open = scanner.nextInt();
- try {
- store.openItem(open, date);
- } catch (NonExistingItemException e) {
- System.out.println(e.getMessage());
- }
- }
- System.out.println(store.getLog());
- }
- }
- class NonExistingItemException extends Exception {
- public NonExistingItemException(String string) {
- super(string);
- }
- }
- abstract class Archive {
- int id;
- Date date;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- public Archive(int id) {
- this.id = id;
- }
- }
- class LockedArchive extends Archive {
- Date dateToOpen;
- public LockedArchive(int id, Date dateToOpen) {
- super(id);
- // this.id = id;
- // this.date = date;
- this.dateToOpen = dateToOpen;
- }
- public Date getDateToOpen() {
- return dateToOpen;
- }
- }
- class SpecialArchive extends Archive {
- int maxopen;
- public SpecialArchive(int id, int maxopen) {
- super(id);
- // this.id = id;
- // this.date = date;
- this.maxopen = maxopen;
- }
- }
- class ArchiveStore {
- Archive[] niza;
- int[] count;
- String[] journal;
- public ArchiveStore() {
- niza = null;
- journal = null;
- }
- public void archiveItem(Archive item, Date date) {
- if (niza == null) {
- niza = new Archive[1];
- niza[0] = item;
- niza[0].setDate(date);
- StringBuilder sb = new StringBuilder();
- sb.append("Item " + niza[0].getId() + " archived at " + date
- + "\n");
- writeInJournal(sb.toString());
- count = new int[1];
- count[0] = 0;
- } else {
- int n = niza.length;
- Archive[] tmp = new Archive[n + 1];
- for (int i = 0; i < n; i++)
- tmp[i] = niza[i];
- tmp[n] = item;
- tmp[n].setDate(date);
- niza = tmp;
- int len = count.length;
- int[] t = new int[len + 1];
- for (int i = 0; i < len; i++)
- t[i] = count[i];
- t[len] = 0;
- count = t;
- StringBuilder sb = new StringBuilder();
- sb.append("Item " + niza[n].getId() + " archived at " + date
- + "\n");
- writeInJournal(sb.toString());
- }
- }
- public void openItem(int id, Date date) throws NonExistingItemException {
- int n = niza.length;
- short flag = 1;
- for (int i = 0; i < n; i++) {
- if (niza[i].getId() == id) {
- flag = 0;
- if (niza[i] instanceof LockedArchive) {
- if (date.compareTo(((LockedArchive) niza[i]).getDateToOpen()) > 0) {
- StringBuilder sb = new StringBuilder();
- sb.append("Item " + niza[i].getId() + " opened at "
- + date + "\n");
- writeInJournal(sb.toString());
- } else {
- StringBuilder sb = new StringBuilder();
- sb.append("Item " + niza[i].getId()
- + " cannot be opened before " + ((LockedArchive)niza[i]).getDateToOpen() + "\n");
- writeInJournal(sb.toString());
- }
- } else {
- SpecialArchive tmp = (SpecialArchive) niza[i];
- if (count[i] + 1 > tmp.maxopen) {
- StringBuilder sb = new StringBuilder();
- sb.append("Item " + niza[i].getId()
- + " cannot be opened more than "
- + tmp.maxopen + " times\n");
- writeInJournal(sb.toString());
- } else {
- StringBuilder sb = new StringBuilder();
- sb.append("Item " + niza[i].getId() + " opened at "
- + date + "\n");
- writeInJournal(sb.toString());
- count[i]++;
- }
- }
- }
- }
- if(flag == 1) {
- StringBuilder sb = new StringBuilder();
- sb.append("Item with id " + id + " doesn't exist");
- throw new NonExistingItemException(sb.toString());
- }
- }
- public void writeInJournal(String s) {
- if (journal == null) {
- journal = new String[1];
- journal[0] = s;
- } else {
- int N = journal.length;
- String[] tp = new String[N + 1];
- for (int i = 0; i < N; i++)
- tp[i] = journal[i];
- tp[N] = s;
- journal = tp;
- }
- }
- public String getLog(){
- StringBuilder sb = new StringBuilder();
- int N = journal.length;
- for(int i = 0; i < N; i++)
- sb.append(journal[i]);
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement