Skip to content

Commit 98039dd

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#79 from yuanxing-y/budget-commands
Add showBudget and setBudget commands
2 parents c7cdc6d + 3d16e8e commit 98039dd

File tree

12 files changed

+177
-13
lines changed

12 files changed

+177
-13
lines changed

docs/UserGuide.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ Examples:
145145

146146
Set the budget for UniSave. Default zero budget. Pop up for input when first launched.
147147

148-
Format: `setBudget a/AMOUNT`
148+
Format: `setBudget AMOUNT`
149149

150150
Example:
151-
`setBudget a/1000`: Set the budget to 1000.
151+
`setBudget 1000`: Set the budget to 1000.
152152

153153
### Show budget : `showBudget`
154154

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package seedu.address.logic.commands;
2+
3+
import seedu.address.logic.commands.exceptions.CommandException;
4+
import seedu.address.model.Model;
5+
6+
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
7+
8+
public class SetBudgetCommand extends Command {
9+
public static final String COMMAND_WORD = "setBudget";
10+
public static final String MESSAGE_SET_BUDGET_SUCCESS = "Successful! Te budget is now S$ %.2f.\n";
11+
public static final String MESSAGE_SET_BUDGET_FAIL = "Set Budget failed, please enter a valid budget.\n";
12+
13+
private final double budget;
14+
15+
public SetBudgetCommand(double budget) {
16+
requireAllNonNull(budget);
17+
this.budget = budget;
18+
}
19+
20+
@Override
21+
public CommandResult execute(Model model) throws CommandException {
22+
model.setExpenseBookBudget(budget);
23+
String msg = String.format(MESSAGE_SET_BUDGET_SUCCESS, budget);
24+
return new CommandResult(msg);
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package seedu.address.logic.commands;
2+
3+
import seedu.address.logic.commands.exceptions.CommandException;
4+
import seedu.address.model.Model;
5+
6+
public class ShowBudgetCommand extends Command {
7+
8+
public static final String COMMAND_WORD = "showBudget";
9+
public static final String MESSAGE_BUDGET = "Current budget is: S$ %.2f\n";
10+
public static final String MESSAGE_REMAINING = "Remaining budget is: S$ %.2f\n";
11+
public static final String MESSAGE_SETNEW = "Your current budget is S$0.00, \n"
12+
+ "please set a new budget with command: setBudget AMOUNT\n";
13+
14+
@Override
15+
public CommandResult execute(Model model) throws CommandException {
16+
if (model.getExpenseBookBudget() == 0) {
17+
return new CommandResult(MESSAGE_SETNEW);
18+
} else {
19+
String budgetMsg = String.format(MESSAGE_BUDGET, model.getExpenseBookBudget());
20+
String remainingMsg = String.format(MESSAGE_REMAINING, model.getExpenseBookRemaining());
21+
22+
return new CommandResult(budgetMsg + remainingMsg);
23+
}
24+
}
25+
}

src/main/java/seedu/address/logic/parser/ExpenseBookParser.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import java.util.regex.Matcher;
77
import java.util.regex.Pattern;
88

9-
import seedu.address.logic.commands.Command;
10-
import seedu.address.logic.commands.DescriptionCommand;
11-
import seedu.address.logic.commands.HelpCommand;
9+
import seedu.address.logic.commands.*;
1210
import seedu.address.logic.parser.exceptions.ParseException;
1311

1412
/**
@@ -44,6 +42,14 @@ public Command parseCommand(String userInput) throws ParseException {
4442
.COMMAND_WORD:
4543
return new DescriptionCommandParser().parse(arguments);
4644

45+
case ShowBudgetCommand
46+
.COMMAND_WORD:
47+
return new ShowBudgetCommandParser().parse(arguments);
48+
49+
case SetBudgetCommand
50+
.COMMAND_WORD:
51+
return new SetBudgetCommandParser().parse(arguments);
52+
4753
default:
4854
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
4955
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package seedu.address.logic.parser;
2+
3+
import seedu.address.commons.core.index.Index;
4+
import seedu.address.logic.commands.DeleteCommand;
5+
import seedu.address.logic.commands.SetBudgetCommand;
6+
import seedu.address.logic.parser.exceptions.ParseException;
7+
8+
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
9+
10+
public class SetBudgetCommandParser implements Parser<SetBudgetCommand> {
11+
@Override
12+
public SetBudgetCommand parse(String userInput) throws ParseException {
13+
try {
14+
//needs oop later
15+
double budget = Double.parseDouble(userInput.split(" ")[1]);
16+
return new SetBudgetCommand(budget);
17+
} catch (Exception pe) {
18+
throw new ParseException(
19+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SetBudgetCommand.MESSAGE_SET_BUDGET_FAIL), pe);
20+
}
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package seedu.address.logic.parser;
2+
3+
import seedu.address.logic.commands.DescriptionCommand;
4+
import seedu.address.logic.commands.ShowBudgetCommand;
5+
import seedu.address.logic.parser.exceptions.ParseException;
6+
import seedu.address.model.person.Description;
7+
8+
public class ShowBudgetCommandParser implements Parser<ShowBudgetCommand> {
9+
10+
@Override
11+
public ShowBudgetCommand parse(String userInput) throws ParseException {
12+
return new ShowBudgetCommand();
13+
}
14+
}

src/main/java/seedu/address/model/ExpenseBook.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,19 @@ public boolean equals(Object other) {
125125
public int hashCode() {
126126
return expenses.hashCode();
127127
}
128+
129+
//yuanxing edited
130+
public void setBudget(double budget) {
131+
expenses.setBudget(budget);
132+
}
133+
134+
public double getBudget() {
135+
return expenses.getBudget();
136+
}
137+
138+
public double getRemainingBudget() {
139+
return expenses.getRemainingBudget();
140+
}
141+
//end of yuanxing edits
142+
128143
}

src/main/java/seedu/address/model/ExpenseModelManager.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,21 @@ public boolean equals(Object obj) {
163163
&& filteredExpenses.equals(other.filteredExpenses);
164164
}
165165

166+
//yuanxing edited
167+
@Override
168+
public double getExpenseBookBudget() {
169+
return expenseBook.getBudget();
170+
}
171+
172+
@Override
173+
public double getExpenseBookRemaining() {
174+
return expenseBook.getRemainingBudget();
175+
}
176+
177+
@Override
178+
public void setExpenseBookBudget(double budget) {
179+
expenseBook.setBudget(budget);
180+
}
181+
182+
166183
}

src/main/java/seedu/address/model/Model.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,10 @@ public interface Model {
9696
* @param predicate
9797
*/
9898
void updateFilteredExpenseList(Predicate<Expense> predicate);
99+
100+
double getExpenseBookBudget();
101+
102+
double getExpenseBookRemaining();
103+
104+
void setExpenseBookBudget(double budget);
99105
}

src/main/java/seedu/address/model/ModelManager.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,24 @@ public void updateFilteredPersonList(Predicate<Person> predicate) {
174174
public void updateFilteredExpenseList(Predicate<Expense> predicate) {
175175
}
176176

177+
//dummy
178+
@Override
179+
public double getExpenseBookBudget() {
180+
return 0;
181+
}
182+
183+
//dummy
184+
@Override
185+
public double getExpenseBookRemaining() {
186+
return 0;
187+
}
188+
189+
//dummy
190+
@Override
191+
public void setExpenseBookBudget(double budget) {
192+
193+
}
194+
177195
@Override
178196
public boolean equals(Object obj) {
179197
// short circuit if same object

0 commit comments

Comments
 (0)