From d4abf158eecc5d6f9058146d94a4529542bcc6de Mon Sep 17 00:00:00 2001 From: "LAPTOP-6F19PDN9\\Ijaaz" Date: Tue, 2 Apr 2024 22:57:42 +0800 Subject: [PATCH] Add storage class --- .gitignore | 2 + FlorizzLogger.xml | 0 src/main/java/florizz/core/Florizz.java | 5 +- src/main/java/florizz/logging/Storage.java | 60 ++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) delete mode 100644 FlorizzLogger.xml create mode 100644 src/main/java/florizz/logging/Storage.java diff --git a/.gitignore b/.gitignore index 2873e189e1..e1c79ebc51 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ bin/ /text-ui-test/ACTUAL.TXT text-ui-test/EXPECTED-UNIX.TXT +/florizz-out/logs +/florizz-out/data diff --git a/FlorizzLogger.xml b/FlorizzLogger.xml deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/main/java/florizz/core/Florizz.java b/src/main/java/florizz/core/Florizz.java index c06087ba1b..4841052840 100644 --- a/src/main/java/florizz/core/Florizz.java +++ b/src/main/java/florizz/core/Florizz.java @@ -2,6 +2,7 @@ import florizz.command.Command; import florizz.logging.MyFormatter; +import florizz.logging.Storage; import florizz.objects.Bouquet; import java.io.IOException; @@ -33,6 +34,7 @@ public static void main(String[] args) { // Set up logger LogManager.getLogManager().reset(); + Storage storage = new Storage(); logger.setLevel(Level.ALL); // Set up console handler @@ -42,7 +44,7 @@ public static void main(String[] args) { // Set up file handler try { - FileHandler fh = new FileHandler("./FlorizzLogger.xml", 2000, 1); + FileHandler fh = new FileHandler("./florizz-out/logs/FlorizzLogger.xml", 2000, 1); fh.setFormatter(new MyFormatter()); fh.setLevel(Level.ALL); logger.addHandler(fh); @@ -56,6 +58,7 @@ public static void main(String[] args) { String input = ui.getInput(); Command command = Parser.parse(input); isRunning = command.execute(tempBouquetList, ui); + storage.trySaveAllBouquets(tempBouquetList); } catch(FlorizzException error){ ui.printError(error); } diff --git a/src/main/java/florizz/logging/Storage.java b/src/main/java/florizz/logging/Storage.java new file mode 100644 index 0000000000..83f00e33da --- /dev/null +++ b/src/main/java/florizz/logging/Storage.java @@ -0,0 +1,60 @@ +package florizz.logging; + +import florizz.objects.Bouquet; +import florizz.objects.Flower; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Paths; +import java.nio.file.Files; +import java.util.HashMap; +import java.util.ArrayList; +import java.util.Map; + +public class Storage { + private File bouquetStorage; + private final String storagePath = "./florizz-out/data/FlorizzBouquets.txt"; + + + public Storage() { + this.bouquetStorage = new File(storagePath); + if (!bouquetStorage.exists()) { + try { + Files.createDirectories(Paths.get("./florizz-out/data/")); + Files.createDirectory(Paths.get("./florizz-out/logs/")); + Files.createFile(Paths.get(storagePath)); + } catch (IOException e) { + System.out.println("File not created"); + } + } + } + + public void trySaveAllBouquets(ArrayList bouquetList) { + try { + FileWriter bouquetStorageWriter = new FileWriter(storagePath); + saveAllBouquets(bouquetStorageWriter, bouquetList); + bouquetStorageWriter.close(); + } catch (IOException e) { + System.out.println("File does not exist"); + } + } + + public void saveAllBouquets(FileWriter w, ArrayList list) throws IOException { + for (Bouquet bouquet : list) { + String bouquetName = bouquet.getBouquetName(); + w.write("new " + bouquetName + "\n"); + saveBouquet(bouquet, w); + } + } + + public void saveBouquet(Bouquet bouquet, FileWriter w) throws IOException{ + String bouquetName = bouquet.getBouquetName(); + HashMap tempHashMap = bouquet.getFlowerHashMap(); + for (Map.Entry k : tempHashMap.entrySet()) { + Flower flower = k.getKey(); + Integer quantity = k.getValue(); + w.write("add " + flower.getFlowerName() + " /q " + quantity + " /to " + bouquetName + "\n"); + } + } +}