Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Scanner;
- class NonExistingItemException extends Exception {
- int id;
- public NonExistingItemException(int id) {
- this.id = id;
- }
- @Override
- public String getMessage() {
- return String.format("Item with id %d doesn't exist", id);
- }
- }
- class Archive {
- private int id;
- private Date dateArchived;
- public Archive(int id) {
- this.id = id;
- this.dateArchived = null;
- }
- public int getId() {
- return id;
- }
- public Date getDateArchived() {
- return dateArchived;
- }
- public void setDateArchived(Date dateArchived) {
- this.dateArchived = (Date) dateArchived.clone();
- }
- public String getType() {
- return "A";
- }
- public boolean equals(Archive that) {
- if (this.id != that.id) {
- return false;
- }
- return true;
- }
- @Override
- public String toString() {
- return String.format("Item %d opened at %s", id, dateArchived);
- }
- }
- class LockedArchive extends Archive {
- private Date dateToOpen;
- public LockedArchive(int id, Date dateToOpen) {
- super(id);
- this.dateToOpen = (Date) dateToOpen.clone();
- }
- public String getType() {
- return "L";
- }
- @Override
- public String toString() {
- if (getDateArchived().before(dateToOpen)) {
- return String.format("Item %d cannot be opened before %s", getId(), dateToOpen);
- }
- return super.toString();
- }
- public boolean equals(Archive that) {
- return super.equals(that);
- }
- @Override
- public int hashCode() {
- return dateToOpen != null ? dateToOpen.hashCode() : 0;
- }
- }
- class SpecialArchive extends Archive {
- int maxOpen;
- int opened;
- public SpecialArchive(int id, int maxOpen) {
- super(id);
- this.maxOpen = maxOpen;
- this.opened = 0;
- }
- public String getType() {
- return "S";
- }
- public boolean equals(Archive that) {
- return super.equals(that);
- }
- @Override
- public String toString() {
- if (opened == maxOpen) {
- return String.format("Item %d cannot be opened more than %d times", getId(), maxOpen);
- }
- opened++;
- return super.toString();
- }
- }
- class ArchiveStore {
- ArrayList<Archive> archives;
- public ArchiveStore() {
- this.archives = new ArrayList<>();
- }
- public void archiveItem(Archive item, Date date) throws IOException {
- item.setDateArchived(date);
- archives.add(item);
- /* Writing to log file */
- String temp = String.format("Item %d archived at %s", item.getId(), date.toString());
- appendToFile(temp);
- }
- public void openItem(int id, Date date) throws NonExistingItemException, IOException {
- //System.out.println("Open Item");
- //System.out.println(archives);
- boolean found = false;
- Archive temp = new Archive(id);
- for (int i = 0; i < archives.size(); i++) {
- /*System.out.println(i + ": " + archives.get(i));
- System.out.println("TEMP: " + temp);*/
- if (archives.get(i).equals(temp)) {
- temp = archives.get(i);
- found = true;
- break;
- }
- }
- if (!found) {
- throw new NonExistingItemException(id);
- }
- /* Appending to file */
- appendToFile(temp.toString());
- }
- public void appendToFile(String line) throws IOException {
- File fileName = new File("archive.txt");
- //System.out.println("Writing to file");
- fileName.createNewFile();
- try (PrintWriter pw = new PrintWriter(new FileWriter(fileName, true))) {
- pw.println(line);
- pw.close();
- }
- }
- public void clearFile() throws IOException {
- File fileName = new File("archive.txt");
- //System.out.println("Clearing file");
- fileName.createNewFile();
- try (PrintWriter pw = new PrintWriter(new FileWriter(fileName))) {
- pw.print("");
- pw.close();
- }
- }
- public String getLog() throws IOException {
- String line = null;
- StringBuffer sb = new StringBuffer();
- //System.out.println("Reading new file");
- /* Reading from file */
- File fileName = new File("archive.txt");
- if (fileName.exists()) {
- try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
- while ((line = br.readLine()) != null) {
- sb.append(line);
- sb.append("\n");
- }
- br.close();
- }
- }
- return sb.toString().replace("GMT", "UTC");
- }
- }
- public class ArchiveStoreTest {
- public static void main(String[] args) throws IOException {
- ArchiveStore store = new ArchiveStore();
- store.clearFile();
- 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("\nLOG\n");
- System.out.println(store.getLog());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment