Skip to content

Commit

Permalink
Fix In/Out Transactions to take in duplicates. Fix JsonAdaptedTransac…
Browse files Browse the repository at this point in the history
…tions to read negative amounts
  • Loading branch information
Berttwm committed Oct 23, 2019
1 parent b49543d commit 2a527bc
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public UpdateTransactionDescriptor() {
* Copy constructor.
* A defensive copy of {@code categories} is used internally.
*/
public UpdateTransactionDescriptor(UpdateCommand.UpdateTransactionDescriptor toCopy) {
public UpdateTransactionDescriptor(UpdateTransactionDescriptor toCopy) {
setAmount(toCopy.amount);
setDate(toCopy.date);
setCategories(toCopy.categories);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/model/transaction/Amount.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public Amount makeNegative() {
return new Amount(newAmount);
}

public boolean isNegative() {
return this.amount < 0;
}

/**
* Create new Amount of 0
* @return Amount of 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ public boolean contains(BankAccountOperation toCheck) {
*/
public void add(BankAccountOperation toAdd) {
requireNonNull(toAdd);
if (contains(toAdd)) {
throw new DuplicatePersonException();
}
internalList.add(toAdd);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public BankAccountOperation toModelType() throws IllegalValueException {

final Set<Category> modelCategories = new HashSet<>(transactionCategories);

if((Double.parseDouble(amount)) < 0){
if ((Double.parseDouble(amount)) < 0) {
/* if amount is negative */
return new OutTransaction(new Amount(Double.parseDouble(amount)).makeNegative(), new Date(date), modelCategories);
return new OutTransaction(new Amount(Double.parseDouble(amount))
.makeNegative(), new Date(date), modelCategories);
} else {
/* if amount is positive */
return new InTransaction(new Amount(Double.parseDouble(amount)), new Date(date), modelCategories);
Expand Down
159 changes: 159 additions & 0 deletions src/test/java/seedu/address/logic/commands/UpdateCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package seedu.address.logic.commands;

/**
* Contains integration tests (interaction with the Model,
* UndoCommand and RedoCommand) and unit tests for UpdateCommand.
*/
public class UpdateCommandTest {
/*
private Model model = new ModelManager(getTypicalBankAccount(), new UserPrefs());
private String typeTransaction = "t";
private String typeBudget = "b";
@Test
public void execute_allFieldsSpecifiedUnfilteredList_success() {
BankAccountOperation editedTransaction = new TransactionBuilder().build();
UpdateTransactionDescriptor descriptor = new UpdateTransactionDescriptorBuilder(editedTransaction).build();
UpdateCommand updateCommand = new UpdateCommand(typeTransaction ,INDEX_FIRST_TRANSACTION, descriptor);
String expectedMessage = String.format(UpdateCommand.MESSAGE_UPDATE_TRANSACTION_SUCCESS, editedTransaction);
Model expectedModel = new ModelManager(new BankAccount(model.getBankAccount()), new UserPrefs());
BankAccountOperation txn = model.getFilteredTransactionList().get(0);
expectedModel.setTransaction(txn, editedTransaction);
assertCommandSuccess(updateCommand, model, expectedMessage, expectedModel);
}
@Test
public void execute_someFieldsSpecifiedUnfilteredList_success() {
Index indexLastTransaction = Index.fromOneBased(model.getFilteredTransactionList().size());
Transaction lastTransaction = model.getFilteredTransactionList().get(indexLastTransaction.getZeroBased());
TransactionBuilder transactionInList = new TransactionBuilder(lastTransaction);
// TODO : FIX
Transaction editedTransaction = transactionInList.withAmount("1").withDate("1")
.withCategories(VALID_TAG_HUSBAND).build();
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB)
.withPhone(VALID_PHONE_BOB).withCategories(VALID_TAG_HUSBAND).build();
EditCommand editCommand = new EditCommand(indexLastTransaction, descriptor);
String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, editedTransaction);
Model expectedModel = new ModelManager(new BankAccount(model.getBankAccount()), new UserPrefs());
expectedModel.setTransaction(lastTransaction, editedTransaction);
assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
}
@Test
public void execute_noFieldSpecifiedUnfilteredList_success() {
EditCommand editCommand = new EditCommand(INDEX_FIRST_TRANSACTION, new EditPersonDescriptor());
Transaction editedTransaction = model.getFilteredTransactionList().get(INDEX_FIRST_TRANSACTION.getZeroBased());
String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, editedTransaction);
Model expectedModel = new ModelManager(new BankAccount(model.getBankAccount()), new UserPrefs());
assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
}
@Test
public void execute_filteredList_success() {
showPersonAtIndex(model, INDEX_FIRST_TRANSACTION);
Transaction transactionInFilteredList = model
.getFilteredTransactionList()
.get(INDEX_FIRST_TRANSACTION.getZeroBased());
// TODO: FIX
Transaction editedTransaction = new TransactionBuilder(transactionInFilteredList).withAmount("1").build();
EditCommand editCommand = new EditCommand(INDEX_FIRST_TRANSACTION,
new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build());
String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, editedTransaction);
Model expectedModel = new ModelManager(new BankAccount(model.getBankAccount()), new UserPrefs());
expectedModel.setTransaction(model.getFilteredTransactionList().get(0), editedTransaction);
assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
}
@Test
public void execute_duplicateTransactionUnfilteredList_failure() {
Transaction firstTransaction = model.getFilteredTransactionList().get(INDEX_FIRST_TRANSACTION.getZeroBased());
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(firstTransaction).build();
EditCommand editCommand = new EditCommand(INDEX_SECOND_TRANSACTION, descriptor);
assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
}
@Test
public void execute_duplicateTransactionFilteredList_failure() {
showPersonAtIndex(model, INDEX_FIRST_TRANSACTION);
// edit person in filtered list into a duplicate in address book
Transaction transactionInList = model
.getBankAccount().getTransactionHistory().get(INDEX_SECOND_TRANSACTION.getZeroBased());
EditCommand editCommand = new EditCommand(INDEX_FIRST_TRANSACTION,
new EditPersonDescriptorBuilder(transactionInList).build());
assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
}
@Test
public void execute_invalidTransactionIndexUnfilteredList_failure() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredTransactionList().size() + 1);
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build();
EditCommand editCommand = new EditCommand(outOfBoundIndex, descriptor);
assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
/**
* Edit filtered list where index is larger than size of filtered list,
* but smaller than size of address book
*/
/*
@Test
public void execute_invalidTransactionIndexFilteredList_failure() {
showPersonAtIndex(model, INDEX_FIRST_TRANSACTION);
Index outOfBoundIndex = INDEX_SECOND_TRANSACTION;
// ensures that outOfBoundIndex is still in bounds of address book list
assertTrue(outOfBoundIndex.getZeroBased() < model.getBankAccount().getTransactionHistory().size());
EditCommand editCommand = new EditCommand(outOfBoundIndex,
new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build());
assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
@Test
public void equals() {
final EditCommand standardCommand = new EditCommand(INDEX_FIRST_TRANSACTION, DESC_AMY);
// same values -> returns true
EditPersonDescriptor copyDescriptor = new EditPersonDescriptor(DESC_AMY);
EditCommand commandWithSameValues = new EditCommand(INDEX_FIRST_TRANSACTION, copyDescriptor);
assertTrue(standardCommand.equals(commandWithSameValues));
// same object -> returns true
assertTrue(standardCommand.equals(standardCommand));
// null -> returns false
assertFalse(standardCommand.equals(null));
// different types -> returns false
assertFalse(standardCommand.equals(new ClearCommand()));
// different index -> returns false
assertFalse(standardCommand.equals(new EditCommand(INDEX_SECOND_TRANSACTION, DESC_AMY)));
// different descriptor -> returns false
assertFalse(standardCommand.equals(new EditCommand(INDEX_FIRST_TRANSACTION, DESC_BOB)));
}
*/

}
15 changes: 11 additions & 4 deletions src/test/java/seedu/address/testutil/TransactionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import seedu.address.model.transaction.Amount;
import seedu.address.model.transaction.BankAccountOperation;
import seedu.address.model.transaction.InTransaction;
import seedu.address.model.transaction.OutTransaction;
import seedu.address.model.transaction.Transaction;
import seedu.address.model.util.Date;
import seedu.address.model.util.SampleDataUtil;
Expand All @@ -18,6 +19,7 @@ public class TransactionBuilder {

public static final String DEFAULT_AMOUNT = "1";
public static final String DEFAULT_DATE = "10102019";
public static final String DEFAULT_CATEGORY = "category";

public static final String DEFAULT_NAME = "Alice Pauline";
public static final String DEFAULT_PHONE = "85355255";
Expand All @@ -32,6 +34,7 @@ public TransactionBuilder() {
amount = new Amount(Double.parseDouble(DEFAULT_AMOUNT));
date = new Date(DEFAULT_DATE);
categories = new HashSet<>();
categories.add(new Category(DEFAULT_CATEGORY));
}

/**
Expand Down Expand Up @@ -68,10 +71,14 @@ public TransactionBuilder withDate(String date) {
return this;
}


// TODO: Change constructor
/**
* Builds BankOperation of InTransaction or OutTransaction
*/
public BankAccountOperation build() {
return new InTransaction(amount, date, categories);
if (amount.isNegative()) {
return new OutTransaction(amount, date, categories);
} else {
return new InTransaction(amount, date, categories);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package seedu.address.testutil;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import seedu.address.logic.commands.UpdateCommand.UpdateTransactionDescriptor;
import seedu.address.model.category.Category;
import seedu.address.model.transaction.Amount;
import seedu.address.model.transaction.BankAccountOperation;
import seedu.address.model.util.Date;

/**
* A utility class to help with building UpdateTransactionDescriptor objects.
*/
public class UpdateTransactionDescriptorBuilder {

private UpdateTransactionDescriptor descriptor;

public UpdateTransactionDescriptorBuilder() {
descriptor = new UpdateTransactionDescriptor();
}

public UpdateTransactionDescriptorBuilder(UpdateTransactionDescriptor descriptor) {
this.descriptor = new UpdateTransactionDescriptor(descriptor);
}

/**
* Returns an {@code UpdateTransactionDescriptor} with fields containing
* {@code BankAccountOperation}'s details
*/
public UpdateTransactionDescriptorBuilder(BankAccountOperation transaction) {
descriptor = new UpdateTransactionDescriptor();
descriptor.setAmount(transaction.getAmount());
descriptor.setDate(transaction.getDate());
descriptor.setCategories(transaction.getCategories());
}

/**
* Sets the {@code Amount} of the {@code UpdateTransactionDescriptor} that we are building.
*/
public UpdateTransactionDescriptorBuilder withAmount(String amount) {
descriptor.setAmount(new Amount(Double.parseDouble(amount)));
return this;
}

/**
* Sets the {@code Date} of the {@code UpdateTransactionDescriptor} that we are building.
*/
public UpdateTransactionDescriptorBuilder withDate(String date) {
descriptor.setDate(new Date(date));
return this;
}

/**
* Parses the {@code categories} into a {@code Set<Category>} and set it to the {@code UpdateTransactionDescriptor}
* that we are building.
*/
public UpdateTransactionDescriptorBuilder withCategories(String... categories) {
Set<Category> categorySet = Stream.of(categories).map(Category::new).collect(Collectors.toSet());
descriptor.setCategories(categorySet);
return this;
}

public UpdateTransactionDescriptor build() {
return descriptor;
}

}

0 comments on commit 2a527bc

Please sign in to comment.