diff --git a/.gitignore b/.gitignore index e1c79ebc51..0d4f3e6668 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ bin/ text-ui-test/EXPECTED-UNIX.TXT /florizz-out/logs /florizz-out/data +/florizz-out/saved diff --git a/docs/UserGuide.md b/docs/UserGuide.md index 0e669d21e4..14805e23e8 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -17,10 +17,11 @@ Here are the list of commands you can use: 4. info - Provide information on chosen flower 5. add /q /to - add flower to a bouquet. 6. remove /q /from - remove flower from a bouquet. -7. flower - Shows a list of flowers that can be added into mybouquets -8. flower - Shows a list of flowers associated with said occasion +7. flowers - Shows a list of flowers that can be added into mybouquets +8. flowers - Shows a list of flowers associated with said occasion 9. occasion - Shows a list of occasions associated with available flowers. -10. bye - Exits the programme +10. save - Saves a bouquet to an external .txt file +11. bye - Exits the programme ``` ### Create a new bouquet: `new` @@ -155,6 +156,21 @@ valentines wedding ``` +### Save a bouquet to device: save +Saves chosen bouquet, if it exists, locally to the users device + +Format: `save ` + +- Bouquet must exist before it can be saved + +Examples: +- `save moms bouquet` + +Expected Output: +``` +Successfully saved moms bouquet. You can find it at 'florizz-out/saved/moms bouquet.txt' +``` + ### Exit programme: exit Exits the program. diff --git a/src/main/java/florizz/command/SaveCommand.java b/src/main/java/florizz/command/SaveCommand.java new file mode 100644 index 0000000000..c40e8937f4 --- /dev/null +++ b/src/main/java/florizz/command/SaveCommand.java @@ -0,0 +1,54 @@ +package florizz.command; + +import florizz.core.FlorizzException; +import florizz.core.Ui; +import florizz.objects.Bouquet; +import florizz.storage.Storage; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; + +public class SaveCommand extends Command{ + + private final String bouquetName; + private final String savePath; + private final File saveFile; + + public SaveCommand(String bouquetName) { + this.bouquetName = bouquetName.trim(); + this.savePath = "./florizz-out/saved/" + bouquetName.trim() + ".txt"; + this.saveFile = new File(savePath); + } + + /** + * Executes command to save a bouquet externally + * @param bouquetList list of bouquets to be manipulated by the command + * @param ui ui class for printing + * @return True if command is successfully executed + * @throws FlorizzException If the bouquet does not exist + */ + @Override + public boolean execute(ArrayList bouquetList, Ui ui) throws FlorizzException { + int bouquetIdx = bouquetList.indexOf(new Bouquet(bouquetName)); + if (bouquetIdx == -1) { + throw new FlorizzException("This bouquet does not exist. Create it by typing 'new '"); + } + Storage storage = new Storage(); + try { + Bouquet bouquetToAdd = bouquetList.get(bouquetIdx); + if (!saveFile.exists()) { + Files.createFile(Paths.get(savePath)); + } + FileWriter bouquetSaver = new FileWriter(savePath); + storage.saveBouquet(bouquetToAdd, bouquetSaver); + bouquetSaver.close(); + } catch (IOException e) { + System.out.println("File not found"); + } + ui.printSaveSuccess(bouquetName); + return true; + } +} diff --git a/src/main/java/florizz/core/FuzzyLogic.java b/src/main/java/florizz/core/FuzzyLogic.java index f8a43fe8a7..3f2337544a 100644 --- a/src/main/java/florizz/core/FuzzyLogic.java +++ b/src/main/java/florizz/core/FuzzyLogic.java @@ -16,6 +16,7 @@ public class FuzzyLogic { private static final Logger logger = Logger.getLogger(FuzzyLogic.class.getName()); static { + ITEMS.put("save", "Command: Save a bouquet to device"); ITEMS.put("new", "Command: Add a new bouquet"); ITEMS.put("delete", "Command: Delete a bouquet"); ITEMS.put("mybouquets", "Command: List all bouquets"); diff --git a/src/main/java/florizz/core/Parser.java b/src/main/java/florizz/core/Parser.java index 167846c8ab..6576a18d90 100644 --- a/src/main/java/florizz/core/Parser.java +++ b/src/main/java/florizz/core/Parser.java @@ -1,18 +1,19 @@ package florizz.core; -import florizz.command.AddBouquetCommand; -import florizz.command.AddFlowerCommand; -import florizz.command.BackCommand; -import florizz.command.Command; -import florizz.command.DeleteBouquetCommand; -import florizz.command.ExitCommand; -import florizz.command.FlowerCommand; -import florizz.command.HelpCommand; import florizz.command.InfoCommand; -import florizz.command.ListBouquetCommand; +import florizz.command.HelpCommand; +import florizz.command.SaveCommand; +import florizz.command.FlowerCommand; +import florizz.command.ExitCommand; +import florizz.command.RemoveFlowerCommand; import florizz.command.ListOccasionCommand; +import florizz.command.ListBouquetCommand; +import florizz.command.DeleteBouquetCommand; +import florizz.command.AddFlowerCommand; +import florizz.command.Command; +import florizz.command.AddBouquetCommand; +import florizz.command.BackCommand; import florizz.command.NextCommand; -import florizz.command.RemoveFlowerCommand; import florizz.objects.Bouquet; import java.util.logging.Level; @@ -77,6 +78,9 @@ public static Command parse (String input, boolean enableUi) throws FlorizzExcep case ("remove"): command = handleRemoveFlower(decodedInput[1]); break; + case ("save"): + command = new SaveCommand(decodedInput[1]); + break; default: throw new FlorizzException("Unidentified input, type help to get a list of all commands!"); } @@ -97,7 +101,6 @@ public static Command parse (String input, boolean enableUi) throws FlorizzExcep * @param input * @return String[] output; output[0] = item ; output[1] = argument(s) */ - public static String[] commandHandler(String input) throws FlorizzException { String[] outputs = new String[2]; try { @@ -106,6 +109,7 @@ public static String[] commandHandler(String input) throws FlorizzException { if (firstWhitespace != -1) { outputs[0] = FuzzyLogic.detectItem(trimmedInput.substring(0,firstWhitespace).toLowerCase()); switch (outputs[0]) { + case ("save"): case ("delete"): // Fallthrough case ("new"): outputs[1] = trimmedInput.substring(firstWhitespace).trim(); diff --git a/src/main/java/florizz/core/Ui.java b/src/main/java/florizz/core/Ui.java index d95505d654..255b6a5d84 100644 --- a/src/main/java/florizz/core/Ui.java +++ b/src/main/java/florizz/core/Ui.java @@ -112,7 +112,8 @@ public void printHelpMessage() { System.out.println("7. flowers - Shows a list of flowers that can be added into mybouquets"); System.out.println("8. flowers - Shows a list of flowers associated with said occasion"); System.out.println("9. occasion - Shows a list of occasions associated with available flowers."); - System.out.println("10. bye - Exits the programme"); + System.out.println("10. save - Saves a bouquet to an external .txt file"); + System.out.println("11. bye - Exits the programme"); printBreakLine(); } @@ -326,4 +327,14 @@ public void printFuzzyInputDetection (String userInput, String bestMatch) { public void printIOError() { System.out.println("ERROR: IO Error Encountered Xd"); } + + /** + * Prints out to user when bouquet has been successfully saved + * @param bouquetName Bouquet to save externally + */ + public void printSaveSuccess(String bouquetName) { + System.out.println("Successfully saved " + bouquetName + ". You can find it at 'florizz-out/saved/" + + bouquetName + ".txt'"); + printBreakLine(); + } } diff --git a/src/main/java/florizz/storage/Storage.java b/src/main/java/florizz/storage/Storage.java index a99fa7a50c..460638b344 100644 --- a/src/main/java/florizz/storage/Storage.java +++ b/src/main/java/florizz/storage/Storage.java @@ -32,6 +32,7 @@ public Storage() { try { Files.createDirectories(Paths.get("./florizz-out/data/")); Files.createDirectory(Paths.get("./florizz-out/logs/")); + Files.createDirectory(Paths.get("./florizz-out/saved/")); Files.createFile(Paths.get(storagePath)); } catch (IOException e) { System.out.println("File not created"); @@ -56,6 +57,7 @@ public void trySaveAllBouquets(ArrayList bouquetList) { /** * Saves all bouquets in the bouquetList to a txt file + * text is formatted beforehand to allow the bouquets to be added back next time florizz is run * @param bouquetStorageWriter A FileWriter that writes into the storage txt file * @param bouquetList An ArrayList that contains all the bouquets added during run time * @throws IOException Thrown when the file to write to does not exist @@ -64,13 +66,18 @@ public void saveAllBouquets(FileWriter bouquetStorageWriter, ArrayList for (Bouquet bouquet : bouquetList) { String bouquetName = bouquet.getBouquetName(); bouquetStorageWriter.write("new " + bouquetName + "\n"); - saveBouquet(bouquet, bouquetStorageWriter); + HashMap tempHashMap = bouquet.getFlowerHashMap(); + for (Map.Entry k : tempHashMap.entrySet()) { + Flower flower = k.getKey(); + Integer quantity = k.getValue(); + bouquetStorageWriter.write("add " + flower.getFlowerName() + + " /q " + quantity + " /to " + bouquetName + "\n"); + } } } /** - * Saves the flower, quantity and target bouquet to a txt file - * text is formatted beforehand to allow the bouquets to be added back next time florizz is run + * Saves a bouquet to a txt file * @param bouquet Target bouquet to be added to txt file * @param bouquetStorageWriter FileWriter that writes to a txt file * @throws IOException Thrown when the target file to write to does not exist @@ -78,12 +85,15 @@ public void saveAllBouquets(FileWriter bouquetStorageWriter, ArrayList public void saveBouquet(Bouquet bouquet, FileWriter bouquetStorageWriter) throws IOException{ String bouquetName = bouquet.getBouquetName(); HashMap tempHashMap = bouquet.getFlowerHashMap(); + double totalPrice = 0; + bouquetStorageWriter.write(bouquetName + " :\n"); for (Map.Entry k : tempHashMap.entrySet()) { Flower flower = k.getKey(); Integer quantity = k.getValue(); - bouquetStorageWriter.write("add " + flower.getFlowerName() + - " /q " + quantity + " /to " + bouquetName + "\n"); + bouquetStorageWriter.write(" - " + quantity + " x " + flower.getFlowerName() + "\n"); + totalPrice += (flower.getPrice() * quantity); } + bouquetStorageWriter.write(" Total estimated price = $" + String.format("%.2f", (double) totalPrice)); } /** diff --git a/src/main/java/florizz/unused/StorageManager.java b/src/main/java/florizz/unused/StorageManager.java index 3ecd6446aa..c0744c603b 100644 --- a/src/main/java/florizz/unused/StorageManager.java +++ b/src/main/java/florizz/unused/StorageManager.java @@ -10,13 +10,13 @@ //@@ author IanFH-unused public class StorageManager { - private final String URL = "jdbc:sqlite:flowers.db"; + private final String url = "jdbc:sqlite:flowers.db"; private Connection connection; private Statement statement; public void loadDatabase() throws FlorizzException { try { - this.connection = DriverManager.getConnection(URL); + this.connection = DriverManager.getConnection(url); Statement statement = connection.createStatement(); } catch (SQLException e) { throw new FlorizzException("ERROR: unable to make connection with database"); diff --git a/text-ui-test/ACTUAL.TXT b/text-ui-test/ACTUAL.TXT new file mode 100644 index 0000000000..638d3f74f3 --- /dev/null +++ b/text-ui-test/ACTUAL.TXT @@ -0,0 +1,27 @@ +Hello from + + __ _ _ + / _| | (_) + | |_| | ___ _ __ _ ________ + | _| |/ _ \| '__| |_ /_ / + | | | | (_) | | | |/ / / / + |_| |_|\___/|_| |_/___/___| + + +What can I do for you? +Here are the list of commands you can use: +1. new - Add a bouquet +2. delete - Delete a bouquets +3. mybouquets - List current saved bouquets +4. info - Provide information on chosen flower +5. add /q /to - add flower to a bouquet. +6. remove /q /from - remove flower from a bouquet. +7. flowers - Shows a list of flowers that can be added into mybouquets +8. flowers - Shows a list of flowers associated with said occasion +9. occasion - Shows a list of occasions associated with available flowers. +10. save - Saves a bouquet to an external .txt file +11. bye - Exits the programme +____________________________________________________________ +What can I do for you? +Enjoy your bouquet! Thank you for using Florizz +____________________________________________________________ diff --git a/text-ui-test/EXPECTED-UNIX.TXT b/text-ui-test/EXPECTED-UNIX.TXT new file mode 100644 index 0000000000..2c01364d45 --- /dev/null +++ b/text-ui-test/EXPECTED-UNIX.TXT @@ -0,0 +1,14 @@ +Hello from + + __ _ _ + / _| | (_) + | |_| | ___ _ __ _ ________ + | _| |/ _ \| '__| |_ /_ / + | | | | (_) | | | |/ / / / + |_| |_|\___/|_| |_/___/___| + + + +What can I do for you? +Enjoy your bouquet! Thank you for using Florizz +____________________________________________________________ diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 547670c757..638d3f74f3 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -19,7 +19,8 @@ Here are the list of commands you can use: 7. flowers - Shows a list of flowers that can be added into mybouquets 8. flowers - Shows a list of flowers associated with said occasion 9. occasion - Shows a list of occasions associated with available flowers. -10. bye - Exits the programme +10. save - Saves a bouquet to an external .txt file +11. bye - Exits the programme ____________________________________________________________ What can I do for you? Enjoy your bouquet! Thank you for using Florizz