Skip to content

Commit

Permalink
Merge 9f2d76e into c0e6e77
Browse files Browse the repository at this point in the history
  • Loading branch information
wallacelim committed Oct 13, 2019
2 parents c0e6e77 + 9f2d76e commit ad6bdd1
Show file tree
Hide file tree
Showing 16 changed files with 383 additions and 4 deletions.
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ asciidoctor {
idprefix: '', // for compatibility with GitHub preview
idseparator: '-',
'site-root': "${sourceDir}", // must be the same as sourceDir, do not modify
'site-name': 'AddressBook-Level3',
'site-githuburl': 'https://github.com/se-edu/addressbook-level3',
'site-seedu': true, // delete this line if your project is not a fork (not a SE-EDU project)
'site-name': 'PalPay',
'site-githuburl': 'https://github.com/AY1920S1-CS2103T-W12-3/main',
]

options['template_dirs'].each {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/seedu/address/logic/commands/InCommand.java
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 src/main/java/seedu/address/logic/commands/SplitCommand.java
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 src/main/java/seedu/address/logic/parser/BankAccountParser.java
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);
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class CliSyntax {
public static final Prefix PREFIX_PHONE = new Prefix("p/");
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_AMOUNT = new Prefix("$/");
public static final Prefix PREFIX_TAG = new Prefix("t/");

public static final Prefix PREFIX_DATE = new Prefix("d/");
}
56 changes: 56 additions & 0 deletions src/main/java/seedu/address/logic/parser/InCommandParser.java
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());
}
}
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;
import seedu.address.model.transaction.Amount;

/**
* Contains utility methods used for parsing strings in the various *Parser classes.
Expand Down Expand Up @@ -121,4 +122,8 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
}
return tagSet;
}

public static Amount parseAmount(String s) {
return new Amount(Double.parseDouble(s));
}
}
29 changes: 29 additions & 0 deletions src/main/java/seedu/address/model/BankAccount.java
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;
}
}
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.model.person.Person;
import seedu.address.model.transaction.Transaction;

/**
* The API of the Model component.
Expand Down Expand Up @@ -84,4 +85,10 @@ public interface Model {
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredPersonList(Predicate<Person> predicate);

/**
* Adds the given transaction.
* {@code transaction} must not already exist in the bank account.
*/
void addTransaction(Transaction transaction);
}
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.person.Person;
import seedu.address.model.transaction.Transaction;

/**
* Represents the in-memory model of the address book data.
Expand All @@ -22,6 +23,8 @@ public class ModelManager implements Model {
private final AddressBook addressBook;
private final UserPrefs userPrefs;
private final FilteredList<Person> filteredPersons;
// TODO: integrate bank account into modelManager, remove stub
private final BankAccount bankAccount = new BankAccount();

/**
* Initializes a ModelManager with the given addressBook and userPrefs.
Expand All @@ -37,6 +40,7 @@ public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs
filteredPersons = new FilteredList<>(this.addressBook.getPersonList());
}


public ModelManager() {
this(new AddressBook(), new UserPrefs());
}
Expand Down Expand Up @@ -129,6 +133,12 @@ public void updateFilteredPersonList(Predicate<Person> predicate) {
filteredPersons.setPredicate(predicate);
}

//=========== Bank Account =============================================================
@Override
public void addTransaction(Transaction transaction) {
bankAccount.addTransaction(transaction);
}

@Override
public boolean equals(Object obj) {
// short circuit if same object
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/seedu/address/model/transaction/Amount.java
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 src/main/java/seedu/address/model/transaction/InTransaction.java
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 src/main/java/seedu/address/model/transaction/OutTransaction.java
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;
}
}
Loading

0 comments on commit ad6bdd1

Please sign in to comment.