diff --git a/build.gradle b/build.gradle index 7f6261f2c6..134ee3c202 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,7 @@ javafx { dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.5.0' implementation 'com.google.code.gson:gson:2.8.6' + compile group: 'com.opencsv', name: 'opencsv', version: '4.1' } application { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4b7e1f3d38..2ce03230ad 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Fri Nov 08 15:37:50 SGT 2019 +distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-bin.zip -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/src/main/java/executor/command/CommandExport.java b/src/main/java/executor/command/CommandExport.java new file mode 100644 index 0000000000..8dae937415 --- /dev/null +++ b/src/main/java/executor/command/CommandExport.java @@ -0,0 +1,68 @@ +package executor.command; + +import com.opencsv.CSVWriter; +import duke.exception.DukeException; +import storage.StorageManager; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Scanner; + +public class CommandExport extends Command { + + private String filePath; + + public CommandExport(String userInput) { + this.userInput = userInput; + this.commandType = CommandType.EXPORT; + this.description = "Exports txt into CSV" + + "FORMAT : export "; + } + + @Override + public void execute(StorageManager storageManager) { + + } + + public void convertTxtToCsv(String data) throws DukeException { + if(data.toLowerCase().equals("wallet")){ + this.filePath = "savedWallet.txt"; + } else { + this.filePath = "savedTask.txt"; + } + try{ + File file = new File(this.filePath); + Scanner scanner = new Scanner(file); + while (scanner.hasNextLine()) { + String loadedInput = scanner.nextLine(); + if (loadedInput.equals("")) { + break; + } + writeCSV(loadedInput); + } + } catch (Exception e) { + throw new DukeException("No Previously Saved Wallet Data."); + } + } + + + private void writeCSV(String line) throws DukeException { + // first create file object for file placed at location + // specified by filepath + File file = new File("data.csv"); + try { + // create FileWriter object with file as parameter + FileWriter outputFile = new FileWriter(file); + // create CSVWriter object filewriter object as parameter + CSVWriter writer = new CSVWriter(outputFile); + // add data to csv + String[] eachRowOfData = {line.replace(" ", ",")}; + writer.writeNext(eachRowOfData); + // closing writer connection + writer.close(); + } + catch (IOException e) { + throw new DukeException("Unable to convert txt into csv"); + } + } +} diff --git a/src/main/java/executor/command/CommandType.java b/src/main/java/executor/command/CommandType.java index 5d5a96c847..0f91c2f987 100644 --- a/src/main/java/executor/command/CommandType.java +++ b/src/main/java/executor/command/CommandType.java @@ -18,6 +18,7 @@ public enum CommandType { OUT(CommandAddSpendingReceipt.class), SETBALANCE(CommandUpdateBalance.class), BUDGET(CommandBudget.class), + EXPORT(CommandExport.class), EXPENSES(CommandDisplayExpenditure.class), HELP(CommandHelp.class), DEADLINE(CommandNewTask.class),