Skip to content

Commit

Permalink
Merge pull request #133 from limyuhching/master
Browse files Browse the repository at this point in the history
Update error messages
  • Loading branch information
limyuhching authored Nov 1, 2023
2 parents 4400e4e + 1f63dee commit 82c6594
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public void run() {
protected void loadData() {
try {
get.getFromTextFile(incomes, expenses);
exchangeRateFileHandler.load();
if(!exchangeRateFileHandler.load()) {
Ui.showLineDivider();
}
} catch (FileNotFoundException e) {
System.out.println("\tOOPS!!! File not found.");
} catch (KaChinnnngException e) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/duke/commands/EditExpenseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.ArrayList;

public class EditExpenseCommand extends Command {
private static final String EXPECTED_FORMAT =
"edit expense <index> /cat <category> /type <type> /de <description> " +
"/date <DD/MM/YYYY> /amt [currency] <amount>";
ArrayList<Expense> expenses;
String fullCommand;
int index;
Expand Down Expand Up @@ -63,7 +66,7 @@ private int getIndex(String fullCommand) throws KaChinnnngException {
assert tokens.length >= 4 : "Command must have at least 4 tokens";
return Integer.parseInt(tokens[2])-1;
} catch (ArrayIndexOutOfBoundsException e) {
throw new KaChinnnngException("You're missing an argument");
throw new KaChinnnngException("You're missing an argument.\nExpected: "+EXPECTED_FORMAT);
} catch (NullPointerException | NumberFormatException e) {
throw new KaChinnnngException("Oops! An integer index is expected");
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/duke/commands/EditIncomeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

public class EditIncomeCommand extends Command {

private static final String EXPECTED_FORMAT =
"edit income <index> /de <description> /date <DD/MM/YYYY> /amt [currency] <amount>";

ArrayList<Income> incomes;
String fullCommand;

Expand Down Expand Up @@ -64,7 +67,7 @@ private int getIndex(String fullCommand) throws KaChinnnngException {
assert tokens.length >= 4 : "Command must have at least 4 tokens";
return Integer.parseInt(tokens[2])-1;
} catch (ArrayIndexOutOfBoundsException e) {
throw new KaChinnnngException("You're missing an argument");
throw new KaChinnnngException("You're missing an argument.\nExpected: "+EXPECTED_FORMAT);
} catch (NullPointerException | NumberFormatException e) {
throw new KaChinnnngException("Oops! An integer index is expected");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.Arrays;

public class UpdateExchangeRateCommand extends Command {

private static final String EXPECTED_FORMAT =
"update exchange rate <supported_currency> <rate>";
ExchangeRateFileHandler exchangeRateFileHandler;
String currency;
double rate;
Expand All @@ -17,7 +20,7 @@ public UpdateExchangeRateCommand(String fullCommand, ExchangeRateFileHandler exc
currency = args[0];
rate = Double.parseDouble(args[1]);
} catch (NumberFormatException | NullPointerException e) {
throw new KaChinnnngException("Invalid command");
throw new KaChinnnngException("Invalid command.\nExpected: "+EXPECTED_FORMAT);
}
}

Expand All @@ -32,7 +35,7 @@ public void execute() throws KaChinnnngException {
private static String[] parse(String fullCommand) throws KaChinnnngException {
String[] args = fullCommand.split(" ");
if (args.length != 5) {
throw new KaChinnnngException("Invalid command");
throw new KaChinnnngException("Invalid command.\nExpected: "+EXPECTED_FORMAT);
}
return Arrays.copyOfRange(args,3, 5);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void updateExchangeRate(String currency, double rate) throws KaChinnnngEx
if (!SUPPORTED_CURRENCIES.contains(currency)) {
throw new KaChinnnngException("This currency is not supported");
}
if (rate > 3000000.00 || rate <= 0.001) {
if (rate > 3000000.00 || rate < 0.001) {
throw new KaChinnnngException("Fail to update exchange rate, " +
"the exchange rate is not between 0.001 and 3000000");
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/seedu/duke/storage/ExchangeRateFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
import java.util.Scanner;

public class ExchangeRateFileHandler {
private static final String WRONG_FORMAT_ERROR = "The format of this line is wrong";
private static final String WRONG_FORMAT_ERROR = "Invalid row in exchange rate file";
private String filePath;

public ExchangeRateFileHandler(String filePath) {
this.filePath = filePath;
}
public void load() throws KaChinnnngException, FileNotFoundException {
public boolean load() throws KaChinnnngException, FileNotFoundException {
boolean loadedAllRows = true;
ExchangeRateManager exchangeRateManager = ExchangeRateManager.getInstance();
if (createFile(filePath)) {
return; // File not found, created new file and return empty HashMap
return true; // File not found, created new file
}
File file = new File(filePath);
Scanner s = new Scanner(file); // Create a Scanner using the File as the source
Expand All @@ -35,8 +36,10 @@ public void load() throws KaChinnnngException, FileNotFoundException {
} catch (IndexOutOfBoundsException | NumberFormatException | NullPointerException e) {
// Skip line if rate is not a double
System.out.println(WRONG_FORMAT_ERROR + " : " + textLine);
loadedAllRows = false;
}
}
return loadedAllRows;
}
public void save(HashMap<String, Double> exchangeRates) throws KaChinnnngException {
try (FileWriter fw = new FileWriter(filePath)) {
Expand Down

0 comments on commit 82c6594

Please sign in to comment.