Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add save command #95

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ bin/
text-ui-test/EXPECTED-UNIX.TXT
/florizz-out/logs
/florizz-out/data
/florizz-out/saved
54 changes: 54 additions & 0 deletions src/main/java/florizz/command/SaveCommand.java
Original file line number Diff line number Diff line change
@@ -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<Bouquet> 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 <BOUQUETNAME>'");
}
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;
}
}
1 change: 1 addition & 0 deletions src/main/java/florizz/core/FuzzyLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/florizz/core/Parser.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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!");
}
Expand All @@ -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 {
Expand All @@ -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();
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/florizz/core/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <occasion> - 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 <bouquetName> - Saves a bouquet to an external <bouquetName>.txt file");
System.out.println("11. bye - Exits the programme");
printBreakLine();
}

Expand Down Expand Up @@ -326,4 +327,10 @@ public void printFuzzyInputDetection (String userInput, String bestMatch) {
public void printIOError() {
System.out.println("ERROR: IO Error Encountered Xd");
}

public void printSaveSuccess(String bouquetName) {
System.out.println("Successfully saved " + bouquetName + ". You can find it at 'florizz-out/saved/"
+ bouquetName + ".txt'");
printBreakLine();
}
Comment on lines +335 to +339
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor change but might want to consider adding JavaDoc

}
20 changes: 15 additions & 5 deletions src/main/java/florizz/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -56,6 +57,7 @@ public void trySaveAllBouquets(ArrayList<Bouquet> 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
Expand All @@ -64,26 +66,34 @@ public void saveAllBouquets(FileWriter bouquetStorageWriter, ArrayList<Bouquet>
for (Bouquet bouquet : bouquetList) {
String bouquetName = bouquet.getBouquetName();
bouquetStorageWriter.write("new " + bouquetName + "\n");
saveBouquet(bouquet, bouquetStorageWriter);
HashMap<Flower, Integer> tempHashMap = bouquet.getFlowerHashMap();
for (Map.Entry<Flower, Integer> 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
*/
public void saveBouquet(Bouquet bouquet, FileWriter bouquetStorageWriter) throws IOException{
String bouquetName = bouquet.getBouquetName();
HashMap<Flower, Integer> tempHashMap = bouquet.getFlowerHashMap();
double totalPrice = 0;
bouquetStorageWriter.write(bouquetName + " :\n");
for (Map.Entry<Flower, Integer> 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));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/florizz/unused/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
27 changes: 27 additions & 0 deletions text-ui-test/ACTUAL.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Hello from

__ _ _
/ _| | (_)
| |_| | ___ _ __ _ ________
| _| |/ _ \| '__| |_ /_ /
| | | | (_) | | | |/ / / /
|_| |_|\___/|_| |_/___/___|


What can I do for you?
Here are the list of commands you can use:
1. new <bouquetName> - Add a bouquet
2. delete <bouquetName> - Delete a bouquets
3. mybouquets - List current saved bouquets
4. info <flowerName> - Provide information on chosen flower
5. add <flowerName> /q <quantity> /to <bouquetName> - add flower to a bouquet.
6. remove <flowerName> /q <quantity> /from <bouquetName> - remove flower from a bouquet.
7. flowers - Shows a list of flowers that can be added into mybouquets
8. flowers <occasion> - Shows a list of flowers associated with said occasion
9. occasion - Shows a list of occasions associated with available flowers.
10. save <bouquetName> - Saves a bouquet to an external <bouquetName>.txt file
11. bye - Exits the programme
____________________________________________________________
What can I do for you?
Enjoy your bouquet! Thank you for using Florizz
____________________________________________________________
14 changes: 14 additions & 0 deletions text-ui-test/EXPECTED-UNIX.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Hello from

__ _ _
/ _| | (_)
| |_| | ___ _ __ _ ________
| _| |/ _ \| '__| |_ /_ /
| | | | (_) | | | |/ / / /
|_| |_|\___/|_| |_/___/___|



What can I do for you?
Enjoy your bouquet! Thank you for using Florizz
____________________________________________________________
3 changes: 2 additions & 1 deletion text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -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 <occasion> - 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 <bouquetName> - Saves a bouquet to an external <bouquetName>.txt file
11. bye - Exits the programme
____________________________________________________________
What can I do for you?
Enjoy your bouquet! Thank you for using Florizz
Expand Down
Loading