Skip to content

Commit

Permalink
Merge pull request #130 from maxxyx96/master
Browse files Browse the repository at this point in the history
Implemented budget in GUI
  • Loading branch information
maxxyx96 authored Oct 16, 2019
2 parents d77bfe5 + ead2f59 commit 435b193
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 50 deletions.
2 changes: 2 additions & 0 deletions data/budget.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
20.0
0.0
2 changes: 1 addition & 1 deletion src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ public void run() {
|| cmd instanceof SetPriorityCommand) {
cmd.execute(items, priorityList, ui);
} else if (cmd instanceof BackupCommand) {
ui.showBeforeBackupMsg();
priorityStorage.write(priorityList);
budgetStorage.write(budgetList);
contactStorage.write(contactList);
storage.write(items);
cmd.execute(items, ui);
cmd.executeStorage(items, ui, storage);
} else if (cmd instanceof AddContactsCommand) {
cmd.execute(items, contactList, ui);
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/duke/command/AddBudgetCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import java.io.IOException;

public class AddBudgetCommand extends Command {

protected BudgetList budgetList;
protected Ui ui = new Ui();
protected float amount;

/**
* Adds the amount specified into the budgetList.
Expand All @@ -19,34 +20,35 @@ public class AddBudgetCommand extends Command {
* @param amount amount to be updated in the user's budget.
*/
public AddBudgetCommand(BudgetList budgetList, float amount) {
budgetList.addToBudget(amount);
ui.showBudget(budgetList.getBudget());
this.budgetList = budgetList;
this.amount = amount;
}


/**
* Executes a command with task list and ui.
* (not used)
* Executes the command to add a certain amount to the existing budget.
*
* @param items The task list that contains a list of tasks.
* @param ui To tell the user that it is executed successfully.
*/
@Override
public void execute(TaskList items, Ui ui) {

ui.showAddBudget(amount, budgetList.getBudget());
budgetList.addToBudget(amount);
ui.showBudget(budgetList.getBudget());
}

/**
* Executes a command with task list and ui (GUI).
* (not used)
* Executes the command to add a certain amount to the existing budget.
*
* @param items The task list that contains a list of tasks.
* @param ui To tell the user that it is executed successfully.
* @return String to be outputted to the user.
*/
@Override
public String executeGui(TaskList items, Ui ui) {
return null;
budgetList.addToBudget(amount);
return ui.showAddBudgetGui(amount, budgetList.getBudget()) + "\n" + ui.showBudgetGui(budgetList.getBudget());
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/duke/command/BackupCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public static void openBackupFolder() throws IOException {
*/
@Override
public void execute(TaskList items, Ui ui) {
ui.showBackupMessage();
ui.showBackupFolderMessage();
}

/**
Expand All @@ -47,7 +49,7 @@ public void execute(TaskList items, Ui ui) {
*/
@Override
public String executeGui(TaskList items, Ui ui) {
return null;
return ui.showBackupMessageGui();
}

/**
Expand All @@ -61,7 +63,6 @@ public String executeGui(TaskList items, Ui ui) {
@Override
public void executeStorage(TaskList items, Ui ui, Storage storage) throws IOException {
openBackupFolder();
ui.showAfterBackupMsg();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,21 @@

import java.io.IOException;

public class UpdateBudgetCommand extends Command {
public class ResetBudgetCommand extends Command {
protected BudgetList budgetList;
protected Ui ui = new Ui();
protected float amount;


/**
* Updates the current budget with the input amount, and prompts the user to confirm
* his/her actions before carrying on with the action.
* Obtaining the parameters of budget from budget corner.
*
* @param budgetList The list of budget that is stored by Duke Manager.
* @param amount The amount to be updated.
*/
public UpdateBudgetCommand(BudgetList budgetList, float amount) {
if (ui.isBudgetResetTrue()) {
budgetList.resetBudget(amount);
ui.showBudget(budgetList.getBudget());
} else {
ui.rejectBudgetResetMessage();
}
public ResetBudgetCommand(BudgetList budgetList, float amount) {
this.budgetList = budgetList;
this.amount = amount;
}


Expand All @@ -37,7 +34,9 @@ public UpdateBudgetCommand(BudgetList budgetList, float amount) {
*/
@Override
public void execute(TaskList items, Ui ui) {

ui.showResetBudget(budgetList.getBudget());
budgetList.resetBudget(amount);
ui.showBudget(budgetList.getBudget());
}

/**
Expand All @@ -50,7 +49,9 @@ public void execute(TaskList items, Ui ui) {
*/
@Override
public String executeGui(TaskList items, Ui ui) {
return null;
Float previousBudget = budgetList.getBudget();
budgetList.resetBudget(amount);
return ui.showResetBudgetGui(previousBudget) + "\n" + ui.showBudgetGui(budgetList.getBudget());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/duke/command/ViewBudgetCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
public class ViewBudgetCommand extends Command {

protected Ui ui = new Ui();
protected BudgetList budgetList;

/**
* Command that allows the user to view the budget that he/she currently has.
*
* @param budgetList The list of budget that is stored by Duke Manager.
*/
public ViewBudgetCommand(BudgetList budgetList) {
ui.showBudget(budgetList.getBudget());
this.budgetList = budgetList;
}


/**
* Executes a command with task list and ui.
* (not used)
Expand All @@ -31,7 +31,7 @@ public ViewBudgetCommand(BudgetList budgetList) {
*/
@Override
public void execute(TaskList items, Ui ui) {

ui.showBudget(budgetList.getBudget());
}

/**
Expand All @@ -44,7 +44,7 @@ public void execute(TaskList items, Ui ui) {
*/
@Override
public String executeGui(TaskList items, Ui ui) {
return null;
return ui.showBudgetGui(budgetList.getBudget());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import duke.command.AddContactsCommand;
import duke.command.ListContactsCommand;
import duke.command.AddBudgetCommand;
import duke.command.UpdateBudgetCommand;
import duke.command.ResetBudgetCommand;
import duke.command.ViewBudgetCommand;

import duke.task.TaskList;
Expand All @@ -33,7 +33,6 @@
import duke.task.Contacts;
import duke.task.BudgetList;

import java.text.ParseException;
import java.util.ArrayList;

/**
Expand Down Expand Up @@ -403,7 +402,7 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi
try {
String budgetAmount = budgetCommandString.split(" ", 2)[1];
if (budgetCommand.trim().equals("new") || budgetCommand.trim().equals("reset")) {
return new UpdateBudgetCommand(budgetList, Float.parseFloat(budgetAmount));
return new ResetBudgetCommand(budgetList, Float.parseFloat(budgetAmount));
} else if (budgetCommand.trim().equals("add") || budgetCommand.trim().equals("+")) {
return new AddBudgetCommand(budgetList, Float.parseFloat(budgetAmount));
} else if (budgetCommand.trim().equals("minus") || budgetCommand.trim().equals("-")) {
Expand Down
81 changes: 62 additions & 19 deletions src/main/java/duke/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,16 @@ public void showDuplicateMsg() {
/**
* Outputs a message to the user to let it know that it is updating.
*/
public void showBeforeBackupMsg() {
out.println(" Updating duke.txt...");
public void showBackupMessage() {
out.println(" Duke Manager has been backed up!");
}

/**
* Outputs a message to the user to let it know that it has finished updating,
* and the file is shown in a folder.
*/
public void showAfterBackupMsg() {
out.println(" Updated " + BACKUP_FILENAME + " with the current items in Duke Manager!");
out.println(" Directory of the file opened in explorer!");
public void showBackupFolderMessage() {
out.println(" Duke has opened the backup file location in file explorer!");
}

public String showBackupMessageGui() {
return " Duke Manager has been backed up!";
}

/**
Expand Down Expand Up @@ -381,26 +380,70 @@ public void showAddedContact(ContactList contactList) {
}
}

/**
* Shows the current budget of the user.
*
* @param amount the budget the user currently has.
*/
public void showBudget(float amount) {
out.println(" Your budget is : $" + amount);
}

public void rejectBudgetResetMessage() {
out.println(" Budget reset cancelled.");
/**
* Shows the current budget of the user.
*
* @param amount the budget the user currently has.
* @return the message to be shown to the user.
*/
public String showBudgetGui(float amount) {
return " Your current Budget is : $" + amount;
}

/**
* Checks if the user is certain about resetting the budget by prompting the user to confirm his/her actions.
* Shows the user the amount that is added/subtracted to the existing budget.
*
* @return returns true if user pressed Y, and false otherwise.
* @param amount The amount that is to be added/subtracted.
* @param budget The existing budget of the user.
*/
public boolean isBudgetResetTrue() {
out.println(" You have an existing budget, are you sure you want to do this? Y/N");
String choice = readCommand();
if (choice.equals("Y") || choice.equals("y")) {
return true;
public void showAddBudget(float amount, float budget) {
if (amount > 0) {
out.println(" You are adding $" + amount + " to your current budget of $" + budget);
} else {
return false;
out.println(" You are subtracting $" + -amount + " from your current budget of $" + budget);
}
}

/**
* Shows the user the amount that is added/subtracted to the existing budget. (GUI)
*
* @param amount The amount that is to be added/subtracted.
* @param budget The existing budget of the user.
* @return String of the text to show user.
*/
public String showAddBudgetGui(float amount, float budget) {
if (amount > 0) {
return " You are adding $" + amount + " to your current budget of $" + budget;
} else {
return " You are subtracting $" + -amount + " from your current budget of $" + budget;
}
}

/**
* Shows the budget that the user has before the changes.
*
* @param budget The budget of the user.
*/
public void showResetBudget(float budget) {
out.println(" Your previous budget of $" + budget + " has been reset.");
}

/**
* Shows the budget that the user has before the changes.
*
* @param budget The budget of the user.
* @return String of the reset message.
*/
public String showResetBudgetGui(float budget) {
return " Your previous budget of " + budget + " has been reset.";
}
}

0 comments on commit 435b193

Please sign in to comment.