Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error messages for payment class and refactored code #163

Merged
merged 4 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ public class PaymentAddCommand extends PaymentCommand {

public static final String COMMAND_WORD = "payment";

public static final String BASIC_USAGE = COMMAND_WORD + " TUTEE_INDEX " + PREFIX_LESSON + "LESSON_INDEX\n";

public static final String MESSAGE_USAGE = "Obtains tutee identified "
+ "by the index number used in the displayed tutee list and lesson identified in tutee's lesson list. "
+ "Fees of the indexed lesson are then added to the payment value owed by tutee.\n"
+ "Required Parameters: TUTEE_INDEX (must be a positive integer), "
+ "LESSON_INDEX (must be a positive integer)\n"
+ "Example: payment 1 " + PREFIX_LESSON + "1\n\n";

public static final String UPDATE_TUTEE_PAYMENT_SUCCESS = "Updated Payment details of %s:\n%s";

public static final String MESSAGE_LESSON_INDEX_OUT_OF_BOUNDS = "Lesson index provided is out of bounds.";

public static final String MESSAGE_LESSON_INDEX_OUT_OF_BOUNDS = "Lesson index provided is invalid.";

private final Index targetIndex;
private final Index lessonIndex;
Expand All @@ -48,7 +47,36 @@ public PaymentAddCommand(Index targetIndex, Index lessonIndex) {
}

/**
* Creates a duplicate tutee with the updated payment value to replace the existing tutee
* Obtains the lesson fees and adds it to existing payment value.
*
* @param lessonIndex Index of lesson in tutee's lesson list
* @param tutee Current tutee
* @return New payment value owed by tutee
* @throws CommandException If an error occurs during command execution.
*/
public static String addLessonCostToValue(Index lessonIndex, Tutee tutee) throws CommandException {

Payment existingPayment = tutee.getPayment();
String existingPaymentValue = existingPayment.getValue();

List<Lesson> lessonList = tutee.getLessons();

if (lessonIndex.getZeroBased() >= lessonList.size()) {
throw new CommandException(MESSAGE_LESSON_INDEX_OUT_OF_BOUNDS);
}

// Gets the indexed lesson in tutee's lesson list
Lesson lessonRetrieved = lessonList.get(lessonIndex.getZeroBased());
Double lessonCost = lessonRetrieved.getCost();
Double updatedPaymentVal = Double.parseDouble(existingPaymentValue) + lessonCost;
String updatedPaymentAsString = String.format("%.2f", updatedPaymentVal);

return updatedPaymentAsString;
}


/**
* Adds the fees of a lesson to tutee's existing fees.
*
* @param model {@code Model} which the command should operate on.
* @return feedback message of the operation result for display
Expand All @@ -66,21 +94,12 @@ public CommandResult execute(Model model) throws CommandException {

Tutee tuteeToGet = lastShownList.get(targetIndex.getZeroBased());
Payment existingPayment = tuteeToGet.getPayment();
String existingPaymentValue = existingPayment.getValue();
LocalDate existingPayByDate = existingPayment.getPayByDate();

// Gets the indexed lesson in tutee's lesson list
List<Lesson> lessonList = tuteeToGet.getLessons();
String updatedPaymentAsString = addLessonCostToValue(lessonIndex, tuteeToGet);

if (lessonIndex.getZeroBased() >= lessonList.size()) {
throw new CommandException(MESSAGE_LESSON_INDEX_OUT_OF_BOUNDS);
}

Lesson lessonRetrieved = lessonList.get(lessonIndex.getZeroBased());
Double lessonCost = lessonRetrieved.getCost();
Double updatedPaymentVal = Double.parseDouble(existingPaymentValue) + lessonCost;
String updatedPaymentAsString = String.format("%.2f", updatedPaymentVal);
Tutee editedTutee = createEditedPaymentDetailsTutee(tuteeToGet, updatedPaymentAsString, existingPayByDate);
Tutee editedTutee = createEditedPaymentDetailsTutee(tuteeToGet, updatedPaymentAsString, existingPayByDate,
null);

model.setTutee(tuteeToGet, editedTutee);
model.updateFilteredTuteeList(PREDICATE_SHOW_ALL_TUTEES);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package seedu.address.logic.commands.paymentcommand;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_LESSON;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PAYMENT_AMOUNT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PAYMENT_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PAYMENT_RECEIVED_DATE;

import java.time.LocalDate;
import java.util.List;
Expand Down Expand Up @@ -35,10 +31,20 @@ public class PaymentCommand extends Command {

public static final String COMMAND_WORD = "payment";

// Default usage of payment allows you to view payment details of current tutee
public static final String MESSAGE_DEFAULT_USAGE = COMMAND_WORD
+ ": View payment details of the tutee identified by the index number used in the displayed tutee list.\n"
+ "Required Parameters: TUTEE_INDEX (must be a positive integer)\n" + "Example: payment 1\n\n";

// Basic usage on the extensions for payment command
public static final String MESSAGE_BASIC_USAGE_ALL = PaymentAddCommand.BASIC_USAGE
+ PaymentSetAmountCommand.BASIC_USAGE
+ PaymentSetDateCommand.BASIC_USAGE
+ PaymentReceiveCommand.BASIC_USAGE
+ "\n"
+ "For more details on payment commands: payment";

// Elaborated usage on the extensions for payment command
public static final String MESSAGE_USAGE_ALL = "Payment command has the following functionalities and"
+ " is to only include up to 1 parameter:\n\n"
+ MESSAGE_DEFAULT_USAGE
Expand All @@ -48,18 +54,15 @@ public class PaymentCommand extends Command {
+ PaymentReceiveCommand.MESSAGE_USAGE
+ "\n";

public static final String MESSAGE_PAYMENT_MANAGEMENT_USAGE =
COMMAND_WORD + " TUTEE_INDEX " + PREFIX_LESSON + "LESSON_INDEX\n"
+ COMMAND_WORD + " TUTEE_INDEX " + PREFIX_PAYMENT_AMOUNT + "PAYMENT_AMOUNT\n"
+ COMMAND_WORD + " TUTEE_INDEX " + PREFIX_PAYMENT_DATE + "PAYMENT_DATE\n"
+ COMMAND_WORD + " TUTEE_INDEX " + PREFIX_PAYMENT_RECEIVED_DATE + "[DATE_RECEIVED]\n\n"
+ "For more details on payment commands: payment";

// Separator to showcase basic extensions on payment command
public static final String SEPARATOR_TITLE = "Command usages to manage the payment details of tutee:\n";

// Final payment details formatting including basic payment extension command usage
public static final String MESSAGE_VIEW_TUTEE_PAYMENT_SUCCESS = "Payment details of %s:\n%s%s\n"
+ SEPARATOR_TITLE
+ MESSAGE_PAYMENT_MANAGEMENT_USAGE;
+ MESSAGE_BASIC_USAGE_ALL;

public static final String UPDATE_TUTEE_PAYMENT_SUCCESS = "Updated Payment details of %s:\n%s";

private final Index targetIndex;

Expand Down Expand Up @@ -93,49 +96,20 @@ public CommandResult execute(Model model) throws CommandException {
}

Tutee tuteeToGet = lastShownList.get(targetIndex.getZeroBased());
List<Lesson> lessons = tuteeToGet.getLessons();

//Edit this portion to link payment details instead of tutee
String tuteePaymentDetails = getPaymentDetailsMessage(tuteeToGet);
return new CommandResult(tuteePaymentDetails);
}


/**
* Uses the information of an existing tutee and creates a new tutee with the updated payment details.
*
* @param tuteeToEdit Existing tutee
* @param payment Payment amount to set
* @param payByDate Date that tutee is to pay amount by
* @return A new tutee object with the updated payment details
*/

public static Tutee createEditedPaymentDetailsTutee(Tutee tuteeToEdit, String payment, LocalDate payByDate) {
assert tuteeToEdit != null;

Name updatedName = tuteeToEdit.getName();
Phone updatedPhone = tuteeToEdit.getPhone();
School updatedSchool = tuteeToEdit.getSchool();
Level updatedLevel = tuteeToEdit.getLevel();
Address updatedAddress = tuteeToEdit.getAddress();
Payment existingPayment = tuteeToEdit.getPayment();
Payment updatedPayment = new Payment(payment, payByDate);
updatedPayment.copyPaymentHistory(existingPayment.paymentHistory);
Remark updatedRemark = tuteeToEdit.getRemark(); // edit command does not allow editing remarks
Set<Tag> updatedTags = tuteeToEdit.getTags();
List<Lesson> updatedLessons = tuteeToEdit.getLessons(); // edit command does not allow editing lessons

return new Tutee(updatedName, updatedPhone, updatedSchool, updatedLevel, updatedAddress,
updatedPayment, updatedRemark, updatedTags, updatedLessons);
}

/**
* Uses the information of an existing tutee and creates a new tutee with the updated payment details.
* Uses the information of an existing tutee and creates a new tutee with the updated payment details and
* updates the last paid date of tutee (only when the receive command is used). In cases where other commands are
* used, lastPaidDate will be null.
*
* @param tuteeToEdit Existing tutee
* @param payment Payment amount to set
* @param payByDate Date that tutee is to pay amount by
* @param lastPaidDate Date that tutee paid
* @param lastPaidDate Date that tutee paid, to be only initialized when receive command is used
* @return
*/
public static Tutee createEditedPaymentDetailsTutee(Tutee tuteeToEdit, String payment, LocalDate payByDate,
Expand All @@ -147,8 +121,14 @@ public static Tutee createEditedPaymentDetailsTutee(Tutee tuteeToEdit, String pa
School updatedSchool = tuteeToEdit.getSchool();
Level updatedLevel = tuteeToEdit.getLevel();
Address updatedAddress = tuteeToEdit.getAddress();
Payment existingPayment = tuteeToEdit.getPayment();
Payment updatedPayment = new Payment(payment, payByDate);
updatedPayment.paymentHistory.add(lastPaidDate);
if (lastPaidDate != null) {
updatedPayment.paymentHistory.add(lastPaidDate);
} else {
List<String> existingPaymentHist = existingPayment.paymentHistory;
updatedPayment.copyPaymentHistory(existingPaymentHist);
}
Remark updatedRemark = tuteeToEdit.getRemark(); // edit command does not allow editing remarks
Set<Tag> updatedTags = tuteeToEdit.getTags();
List<Lesson> updatedLessons = tuteeToEdit.getLessons(); // edit command does not allow editing lessons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class PaymentReceiveCommand extends PaymentCommand {

public static final String COMMAND_WORD = "payment";

public static final String BASIC_USAGE = COMMAND_WORD + " TUTEE_INDEX "
+ PREFIX_PAYMENT_RECEIVED_DATE + "[DATE_RECEIVED]\n";

public static final String MESSAGE_USAGE = "Sets payment value owed by the tutee identified "
+ "by the index number used in the displayed tutee list to 0 and "
+ "has an optional date field to update the date to make next payment by. If no date is set, "
Expand All @@ -28,14 +31,14 @@ public class PaymentReceiveCommand extends PaymentCommand {
+ "Optional Parameters: PAY_BY_DATE (in the format of dd-mm-yyyy)\n"
+ "Example: payment 1 " + PREFIX_PAYMENT_RECEIVED_DATE + "15-10-2021\n\n";

public static final String UPDATE_TUTEE_PAYMENT_SUCCESS = "Updated Payment details of %s:\n%s";

public static final String MESSAGE_NO_CHANGE_IN_PAYMENT_VALUE = "Payment value owed by tutee "
public static final String MESSAGE_NO_CHANGE_IN_PAYMENT_VALUE = "Current payment value owed by tutee "
+ "is already 0 and date to make payment by had no change.";


private static final String ZERO_PAYMENT_VAL = "0";

private final Index targetIndex;
private final LocalDate newPayByDate;
private final String zeroPaymentVal = "0";
private final LocalDate nullPayByDate = null;


/**
Expand Down Expand Up @@ -71,23 +74,25 @@ public CommandResult execute(Model model) throws CommandException {
Payment existingPayment = tuteeToGet.getPayment();
String existingPaymentValue = existingPayment.getValue();
LocalDate existingPayByDate = existingPayment.getPayByDate();
Tutee editedTutee;

if (zeroPaymentVal.equals(existingPaymentValue) && newPayByDate == null && existingPayByDate == null) {
boolean hasZeroPaymentValue = Double.parseDouble(existingPaymentValue) == 0;
boolean hasNullExistingDate = existingPayByDate == null;
boolean hasNullNewPayByDate = newPayByDate == null;
boolean hasSameExistingAndNewDate = false;

if (!hasNullNewPayByDate) {
hasSameExistingAndNewDate = newPayByDate.equals(existingPayByDate);
}
// Cases where current payment value is currently zero and dates have no change
if (hasZeroPaymentValue && hasNullNewPayByDate && hasNullExistingDate) {
throw new CommandException(MESSAGE_NO_CHANGE_IN_PAYMENT_VALUE);
} else if (zeroPaymentVal.equals(existingPaymentValue) && newPayByDate == null) {
editedTutee = createEditedPaymentDetailsTutee(tuteeToGet, zeroPaymentVal,
nullPayByDate, TODAY_DATE_AS_STRING);
} else if (zeroPaymentVal.equals(existingPaymentValue) && newPayByDate.equals(existingPayByDate)) {
} else if (hasZeroPaymentValue && hasSameExistingAndNewDate) {
throw new CommandException(MESSAGE_NO_CHANGE_IN_PAYMENT_VALUE);
} else if (newPayByDate == null && existingPayByDate != null) {
editedTutee = createEditedPaymentDetailsTutee(tuteeToGet, zeroPaymentVal,
nullPayByDate, TODAY_DATE_AS_STRING);
} else {
editedTutee = createEditedPaymentDetailsTutee(tuteeToGet, zeroPaymentVal,
newPayByDate, TODAY_DATE_AS_STRING);
}

Tutee editedTutee = createEditedPaymentDetailsTutee(tuteeToGet, ZERO_PAYMENT_VAL,
newPayByDate, TODAY_DATE_AS_STRING);

model.setTutee(tuteeToGet, editedTutee);
model.updateFilteredTuteeList(PREDICATE_SHOW_ALL_TUTEES);
Payment newPaymentDetails = editedTutee.getPayment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ public class PaymentSetAmountCommand extends PaymentCommand {

public static final String COMMAND_WORD = "payment";

public static final String BASIC_USAGE = COMMAND_WORD + " TUTEE_INDEX "
+ PREFIX_PAYMENT_AMOUNT + "PAYMENT_AMOUNT\n";

public static final String MESSAGE_USAGE = "Update payment value owed by the tutee identified "
+ "by the index number used in the displayed tutee list to new specified value.\n"
+ "Required Parameters: TUTEE_INDEX (must be a positive integer), "
+ "PAYMENT_VALUE (must be a positive value up to 2 decimal places)\n"
+ "Example: payment 1 " + PREFIX_PAYMENT_AMOUNT + "150\n\n";

public static final String UPDATE_TUTEE_PAYMENT_SUCCESS = "Updated Payment details of %s:\n%s";

public static final String MESSAGE_NO_CHANGE_IN_PAYMENT_VALUE = "Payment value provided is the same"
+ " as the existing payment value of tutee.";

Expand Down Expand Up @@ -72,7 +73,7 @@ public CommandResult execute(Model model) throws CommandException {
Payment existingPayment = tuteeToGet.getPayment();
String existingPaymentValue = existingPayment.getValue();
LocalDate existingPayByDate = existingPayment.getPayByDate();
Tutee editedTutee = createEditedPaymentDetailsTutee(tuteeToGet, paymentValueToSet, existingPayByDate);
Tutee editedTutee = createEditedPaymentDetailsTutee(tuteeToGet, paymentValueToSet, existingPayByDate, null);

// If existing value is same as input value
if (paymentValueToSet.equals(existingPaymentValue)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public class PaymentSetDateCommand extends PaymentCommand {

public static final String COMMAND_WORD = "payment";

public static final String BASIC_USAGE = COMMAND_WORD + " TUTEE_INDEX " + PREFIX_PAYMENT_DATE + "PAYMENT_DATE\n";

public static final String MESSAGE_USAGE = "Update the date to pay by for tutee identified "
+ "by the index number used in the displayed tutee list to new specified date.\n"
+ "Required Parameters: TUTEE_INDEX (must be a positive integer), "
+ "PAY_BY_DATE (in the format of dd-mm-yyyy)\n"
+ "Example: payment 1 " + PREFIX_PAYMENT_DATE + "15-10-2021\n\n";

public static final String UPDATE_TUTEE_PAYMENT_SUCCESS = "Updated Payment details of %s:\n%s";

public static final String MESSAGE_NO_CHANGE_IN_PAYMENT_DATE = "Payment date owed by tutee "
+ "is the same as existing payment date";

Expand Down Expand Up @@ -68,14 +68,13 @@ public CommandResult execute(Model model) throws CommandException {
Payment existingPayment = tuteeToGet.getPayment();
String existingPaymentValue = existingPayment.getValue();
LocalDate existingPayByDate = existingPayment.getPayByDate();
Tutee editedTutee;

// If existing pay by value is same as input date value
if (newPayByDate.equals(existingPayByDate)) {
throw new CommandException(MESSAGE_NO_CHANGE_IN_PAYMENT_DATE);
}

editedTutee = createEditedPaymentDetailsTutee(tuteeToGet, existingPaymentValue, newPayByDate);
Tutee editedTutee = createEditedPaymentDetailsTutee(tuteeToGet, existingPaymentValue, newPayByDate, null);

model.setTutee(tuteeToGet, editedTutee);
model.updateFilteredTuteeList(PREDICATE_SHOW_ALL_TUTEES);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ParserUtil {
*/
public static Index parseIndex(String oneBasedIndex) throws ParseException {
String trimmedIndex = oneBasedIndex.trim();

if (!StringUtil.isNonZeroUnsignedInteger(trimmedIndex)) {
throw new ParseException(MESSAGE_INVALID_INDEX);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/tutee/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public String getOverdueStatus() {
} else if (payByDateAsString.equals("-")) {
return "No (Pay-by date not set)";
} else {
return "No (by " + payByDateAsString + ")";
return "No (Next payment date by: " + payByDateAsString + ")";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will clear the confusion that our testers had 😅

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static DeleteLessonCommand parse(Index tuteeIndex, Index lessonIndex) thr
}
}

private static class LessonMock {
public static class LessonMock {
public static Lesson getLesson() throws ParseException {
Subject subject = ParserUtil.parseSubject(VALID_LESSON_SUBJECT_BOB);
DayOfWeek dayOfWeek = ParserUtil.parseDayOfWeek(VALID_LESSON_DAY_OF_WEEK_BOB);
Expand Down
Loading