Skip to content

Commit

Permalink
Merge pull request #90 from Ijaaz01/MohamedIjaaz-Storage
Browse files Browse the repository at this point in the history
Add storage class
  • Loading branch information
IanFH authored Apr 2, 2024
2 parents ae9d470 + d4abf15 commit a71ce66
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ bin/

/text-ui-test/ACTUAL.TXT
text-ui-test/EXPECTED-UNIX.TXT
/florizz-out/logs
/florizz-out/data
Empty file removed FlorizzLogger.xml
Empty file.
5 changes: 4 additions & 1 deletion src/main/java/florizz/core/Florizz.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import florizz.command.Command;
import florizz.logging.MyFormatter;
import florizz.logging.Storage;
import florizz.objects.Bouquet;

import java.io.IOException;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/florizz/logging/Storage.java
Original file line number Diff line number Diff line change
@@ -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<Bouquet> 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<Bouquet> 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<Flower, Integer> tempHashMap = bouquet.getFlowerHashMap();
for (Map.Entry<Flower, Integer> k : tempHashMap.entrySet()) {
Flower flower = k.getKey();
Integer quantity = k.getValue();
w.write("add " + flower.getFlowerName() + " /q " + quantity + " /to " + bouquetName + "\n");
}
}
}

0 comments on commit a71ce66

Please sign in to comment.