Skip to content

Commit

Permalink
adding ability to export csv form of either task or wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
priyan-coder committed Nov 8, 2019
1 parent 34d36d1 commit 05d83bd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions src/main/java/executor/command/CommandExport.java
Original file line number Diff line number Diff line change
@@ -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 <wallet or task>";
}

@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");
}
}
}
1 change: 1 addition & 0 deletions src/main/java/executor/command/CommandType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 05d83bd

Please sign in to comment.