forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
383 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.transaction.Transaction; | ||
|
||
/** | ||
* Adds an income to the bank account. | ||
*/ | ||
public class InCommand extends Command { | ||
|
||
public static final String MESSAGE_SUCCESS = "New transaction added: %1$s"; | ||
public static final String COMMAND_WORD = "in"; | ||
|
||
private final Transaction transaction; | ||
|
||
public InCommand(Transaction transaction) { | ||
this.transaction = transaction; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
model.addTransaction(transaction); | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, transaction)); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/seedu/address/logic/commands/SplitCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.transaction.Transaction; | ||
|
||
/** | ||
* Splits an amount into smaller different amounts. | ||
*/ | ||
public class SplitCommand extends Command { | ||
|
||
public static final String MESSAGE_SUCCESS = "Split amount successful"; | ||
public static final String COMMAND_WORD = "split"; | ||
private final Transaction transaction; | ||
|
||
public SplitCommand(Transaction transaction) { | ||
this.transaction = transaction; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
model.addTransaction(transaction); | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, transaction)); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/seedu/address/logic/parser/BankAccountParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.HelpCommand; | ||
import seedu.address.logic.commands.InCommand; | ||
// import seedu.address.logic.commands.SplitCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new SplitCommand or InCommand object | ||
*/ | ||
public class BankAccountParser { | ||
|
||
/** | ||
* Used for initial separation of command word and args. | ||
*/ | ||
private static final Pattern BASIC_COMMAND_FORMAT = Pattern.compile("(?<commandWord>\\S+)(?<arguments>.*)"); | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the SplitCommand or InCommand | ||
* and returns an SplitCommand or InCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public Command parseCommand(String userInput) throws ParseException { | ||
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim()); | ||
if (!matcher.matches()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
final String commandWord = matcher.group("commandWord"); | ||
final String arguments = matcher.group("arguments"); | ||
|
||
switch(commandWord) { | ||
// case SplitCommand.COMMAND_WORD: | ||
// return new SplitCommand(arguments); | ||
case InCommand.COMMAND_WORD: | ||
return new InCommandParser().parse(arguments); | ||
default: | ||
throw new ParseException(MESSAGE_UNKNOWN_COMMAND); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/seedu/address/logic/parser/InCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_AMOUNT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
|
||
import java.util.Date; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.AddCommand; | ||
import seedu.address.logic.commands.InCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
import seedu.address.model.person.Name; | ||
import seedu.address.model.tag.Tag; | ||
import seedu.address.model.transaction.Amount; | ||
import seedu.address.model.transaction.InTransaction; | ||
import seedu.address.model.transaction.Transaction; | ||
|
||
/** | ||
* Parses input arguments and creates a new InCommand object | ||
*/ | ||
public class InCommandParser implements Parser<InCommand> { | ||
@Override | ||
public InCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_AMOUNT, PREFIX_TAG); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_AMOUNT, PREFIX_DATE) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()); | ||
Amount amount = ParserUtil.parseAmount(argMultimap.getValue(PREFIX_AMOUNT).get()); | ||
// Date date = ParserUtil.parseDate(argMultimap.getValue(PREFIX_DATE).get()); | ||
|
||
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); | ||
|
||
Transaction transaction = new InTransaction(amount, new Date(System.currentTimeMillis())); | ||
|
||
return new InCommand(transaction); | ||
|
||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package seedu.address.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import seedu.address.model.transaction.Amount; | ||
import seedu.address.model.transaction.Transaction; | ||
|
||
/** | ||
* Bank account of the user. | ||
*/ | ||
public class BankAccount { | ||
private Amount balance; | ||
private List<Transaction> transactions; | ||
|
||
BankAccount() { | ||
transactions = new ArrayList<>(); | ||
} | ||
|
||
/** | ||
* Adds a transaction to the bank account. | ||
* @param txn Transaction to be added to bank account. | ||
*/ | ||
public void addTransaction(Transaction txn) { | ||
transactions.add(txn); | ||
Amount newBalance = txn.handleBalance(this.balance); | ||
this.balance = newBalance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package seedu.address.model.transaction; | ||
|
||
/** | ||
* Amount in terms of cents | ||
*/ | ||
public class Amount { | ||
|
||
private int amount; | ||
|
||
public Amount(double amount) { | ||
this.amount = (int) Math.floor(amount * 100); | ||
} | ||
|
||
public Amount(int amount) { | ||
this.amount = amount; | ||
} | ||
|
||
/** | ||
* Returns amount in cents | ||
*/ | ||
public int getAmount() { | ||
return this.amount; | ||
} | ||
|
||
/** | ||
* Adds this.amount by amount. | ||
* @param amount Amount to be added. | ||
* @return New amount after addition. | ||
*/ | ||
public Amount addAmount(Amount amount) { | ||
final int newAmount = this.amount + amount.amount; | ||
return new Amount(newAmount); | ||
} | ||
|
||
/** | ||
* Subtracts this.amount by amount. | ||
* @param amount Amount to be subtracted. | ||
* @return New amount after subtraction. | ||
*/ | ||
public Amount subtractAmount(Amount amount) { | ||
final int newAmount = this.amount - amount.amount; | ||
return new Amount(newAmount); | ||
} | ||
|
||
/** | ||
* Divides amount by number of people. | ||
* @param numOfPeople Number of people for amount to be divided. | ||
* @return Equally (TO BE CHANGED) divided amount. | ||
*/ | ||
public Amount divideAmount(int numOfPeople) { | ||
final int newAmount = this.amount / numOfPeople; | ||
return new Amount(newAmount); | ||
} | ||
|
||
public void updateAmount(Amount amount) { | ||
this.amount = amount.amount; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%.2f", amount / 100.0); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/seedu/address/model/transaction/InTransaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package seedu.address.model.transaction; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* Handles in transactions. | ||
*/ | ||
public class InTransaction extends Transaction { | ||
public InTransaction(Amount amount, Date date) { | ||
super(amount, date); | ||
} | ||
|
||
@Override | ||
public Amount handleBalance(Amount balance) { | ||
Amount newBalance = balance.addAmount(super.amount); | ||
return newBalance; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/seedu/address/model/transaction/OutTransaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package seedu.address.model.transaction; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* Handles out transactions. | ||
*/ | ||
public class OutTransaction extends Transaction { | ||
public OutTransaction(Amount amount, Date date) { | ||
super(amount, date); | ||
} | ||
|
||
@Override | ||
public Amount handleBalance(Amount balance) { | ||
Amount newBalance = balance.subtractAmount(super.amount); | ||
return newBalance; | ||
} | ||
} |
Oops, something went wrong.