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

Edit Claim Issue #63

Merged
merged 13 commits into from
Oct 9, 2023
32 changes: 32 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="bin/main" path="src/main/java">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/main" path="src/main/resources">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/java">
<attributes>
<attribute name="gradle_scope" value="test"/>
<attribute name="gradle_used_by_scope" value="test"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/resources">
<attributes>
<attribute name="gradle_scope" value="test"/>
<attribute name="gradle_used_by_scope" value="test"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
34 changes: 34 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>tp</name>
<comment>Project tp created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1696599672119</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
13 changes: 13 additions & 0 deletions .settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/usr/local/Cellar/openjdk/17.0.1_1/libexec/openjdk.jdk/Contents/Home
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
4 changes: 4 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.source=11
6 changes: 4 additions & 2 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public class Messages {
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";
public static final String MESSAGE_DUPLICATE_FIELDS = "Multiple values specified for the "
+ "following single-valued field(s): ";
public static final String MESSAGE_OVER_CLAIM = "Claim is REJECTED as the amount is greater than "
+ "the funds the Employee currently has!";
public static final String MESSAGE_EMPTY_DEPARTMENT_FILTER = "department name cannot be empty!";

public static final String MESSAGE_LIST_SUCCESS = "Listed all employees (%1$d)";
Expand Down
118 changes: 118 additions & 0 deletions src/main/java/seedu/address/logic/commands/ClaimCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Money;
import seedu.address.model.person.Person;

/**
* Performs claims for each employee and alters respective claim budgets.
*/
public class ClaimCommand extends Command {

public static final String COMMAND_WORD = "claim";
public static final String MESSAGE_EMPTY = "Kindly state the index and amount that the employee hopes to process!";
public static final String AMOUNT_EMPTY = "Kindly state the amount that the employee hopes to process!";
public static final String CLAIM_SUCCESS = "Claim has been successfully processed!\n";

private final Index index;
private final Boolean isSubtract;
private final long amount;

/**
* Initialises a ClaimCommand Objects with three variables being index, isSubtract and amount.
*
* @param index Index Object representing the position of individual within the list of employees.
* @param isSubtract Boolean Object where True represents deduction and False represents addition.
* @param amount Long Object representing the claim amount user is submitting.
*/
public ClaimCommand(Index index, Boolean isSubtract, long amount) {
this.index = index;
this.isSubtract = isSubtract;
this.amount = amount;
}

/**
* Returns CommandResult Object after successfully updating user's claim budget.
* Updating the Person Object in the list.
*
* @param model {@code Model} which the command should operate on.
* @return CommandResult which highlights the new claim budget the individual has.
* @throws CommandException Exception thrown if index input from HR is beyond the pre-existing max list index.
*/
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);

Check warning on line 56 in src/main/java/seedu/address/logic/commands/ClaimCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/ClaimCommand.java#L56

Added line #L56 was not covered by tests
}

Person personToEdit = lastShownList.get(index.getZeroBased());
long prevClaimBudget = Integer.parseInt(personToEdit.getClaimBudget().amount);
Money claimBudget = calculateNewClaimBudget(prevClaimBudget);
Person editedPerson = postClaimPerson(personToEdit, claimBudget);

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(String.format("%s Remaining claim %s has: %s",
CLAIM_SUCCESS, editedPerson.getName(), claimBudget));
}

/**
* Returns a Money Object which represents the new amount the user has after the claiming process is completed.
*
* @param prevClaimBudget long object on user's claim budget before the claim process.
* @return Money Object that highlights the new claim budget the user has.
* @throws CommandException Exception if the subtracted claim amount is more the user's claim budget.
*/
public Money calculateNewClaimBudget(long prevClaimBudget) throws CommandException {
if (this.isSubtract && (this.amount > prevClaimBudget)) {
throw new CommandException(Messages.MESSAGE_OVER_CLAIM);
}
long newClaimBudget;
if (this.isSubtract) {
newClaimBudget = prevClaimBudget - this.amount;
} else {
newClaimBudget = prevClaimBudget + this.amount;
}
return new Money(String.valueOf(newClaimBudget));
}

/**
* Returns a Person object which contains the new claim budget.
* Other variables of the person remains unchanged.
*
* @param personToEdit Person object (Old).
* @param claimBudget Money object which reflects the new claim budget user has.
* @return Person Object that contains the new claim budget whilst other variables remain unchanged.
*/
public Person postClaimPerson(Person personToEdit, Money claimBudget) {
return new Person(personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(),
personToEdit.getAddress(), personToEdit.getSalary(), claimBudget,
personToEdit.getDepartment(), personToEdit.getDob());
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true; // Both are the same instance
}
if (other == null || getClass() != other.getClass()) {
return false; // Other is not an instance of ClaimCommand
}
ClaimCommand that = (ClaimCommand) other;
return index.equals(that.index)
&& isSubtract.equals(that.isSubtract)
&& amount == that.amount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.ClaimCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
Expand Down Expand Up @@ -77,6 +78,9 @@
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case ClaimCommand.COMMAND_WORD:
return new ClaimCommandParser().parse(arguments);

Check warning on line 82 in src/main/java/seedu/address/logic/parser/AddressBookParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/AddressBookParser.java#L82

Added line #L82 was not covered by tests

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/seedu/address/logic/parser/ClaimCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CLAIM_AMOUNT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.ClaimCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Claim;


/**
* Parses input arguments and creates a new ClaimCommand object.
*/
public class ClaimCommandParser implements Parser<ClaimCommand> {

/**
* Returns ClaimCommand which contains the employee-of-interest's index,
* boolean to represent subtraction/addition as well as the claim amount.
* Parser to parse out the index as well as claim amount
* (containing both symbol [+/-] and amount) based on delimiter of "$/".
* Checks are in place to ensure the index is inputted and claim amount consists of only digits.
*
* @param args String object which represents the user's input.
* @return ClaimCommand which consists of employee's index, subtraction/addition boolean indicator and claim amount.
* @throws ParseException Exception thrown either when index is not inputted or claim amount contains non-digits.
*/
public ClaimCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_CLAIM_AMOUNT);

Index index;
Claim claim;

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ClaimCommand.MESSAGE_EMPTY), pe);
}

if (argMultimap.getValue(PREFIX_CLAIM_AMOUNT).isPresent()) {
claim = ParserUtil.parseClaim(argMultimap.getValue(PREFIX_CLAIM_AMOUNT).get());
} else {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ClaimCommand.AMOUNT_EMPTY));
}

return new ClaimCommand(index, claim.isSubtract, claim.amount);
}
}
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public class CliSyntax {
public static final Prefix PREFIX_DEPARTMENT = new Prefix("d/");
public static final Prefix PREFIX_DOB = new Prefix("dob/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_CLAIM_AMOUNT = new Prefix("$/");
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public EditCommand parse(String args) throws ParseException {
if (argMultimap.getValue(PREFIX_DOB).isPresent()) {
editPersonDescriptor.setDob(ParserUtil.parseDob(argMultimap.getValue(PREFIX_DOB).get()));
}

if (!editPersonDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Birthday;
import seedu.address.model.person.Claim;
import seedu.address.model.person.Department;
import seedu.address.model.person.Email;
import seedu.address.model.person.Money;
Expand Down Expand Up @@ -143,6 +144,26 @@ public static Birthday parseDob(String dob) throws ParseException {
return new Birthday(trimmed);
}

/**
* Returns a Claim Object upon successful checks.
* Checks consist of ensuring user inputted either + or - before the claim amount.
* Checks also consist of ensuring the claim amount contains only digits.
*
* @param claimAmount String Object which is parsed out from the user's command line input. Example: +500
* @return Claim Object which stores claim amount as well as boolean to indicate addition/subtraction.
* @throws ParseException Exception when no symbols were inputted or amount consists of non-digits.
*/
public static Claim parseClaim(String claimAmount) throws ParseException {
requireNonNull(claimAmount);
String trimmed = claimAmount.trim();
if (!Claim.comtainsSymbol(trimmed)) {
throw new ParseException(Claim.NO_SYMBOLS_ERROR);
} else if (!Claim.isCorrectAmountType(trimmed)) {
throw new ParseException(Claim.ALPHABETS_ERROR);
}
return new Claim(trimmed);
}

/**
* Parses a {@code String tag} into a {@code Tag}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/seedu/address/model/person/Claim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package seedu.address.model.person;

/**
* Represents a Person's Claim in the address book.
* Ensures boolean variable and amount is provided.
*/
public class Claim {
public static final String NO_SYMBOLS_ERROR = "Kindly state whether the employee wants to deduct/add "
+ "the claim amount! Provide either +/- in front of the amount!";
public static final String ALPHABETS_ERROR = "Kindly only input integers for the claim amount!";

public final boolean isSubtract;
public final int amount;

/**
* Returns a Claim Object which contains of two variables, isSubtract and amount.
* isSubtract variable represents whether this claim is either addition/subtraction of claim budget.
* amount variable represents whether the claim amount employee is hoping to claim.
*
* @param numStr String Object of user's input.
*/
public Claim(String numStr) {
char firstChar = numStr.charAt(0);
if (firstChar == '+') {
this.isSubtract = false;
} else {
this.isSubtract = true;

Check warning on line 27 in src/main/java/seedu/address/model/person/Claim.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Claim.java#L27

Added line #L27 was not covered by tests
}
this.amount = Integer.parseInt(numStr.substring(1));
}

/**
* Returns a boolean object where True if symbol (+/-) is provided before claim amount else False.
*
* @param args String Object that contains parsed out portions from user's CLI input.
* @return Boolean where True represents correct claim input format else False.
*/
public static boolean comtainsSymbol(String args) {
char firstChar = args.charAt(0);
return firstChar == '+' || firstChar == '-';
}

/**
* Returns boolean object where True if claim amount only contains of digits else False.
* This portion represents post symbol portion. For example, if args == "+500", we are obly care about "500".
*
* @param args String object that contains parsed out partions from user's CLI input.
* @return Boolean where True represents claim amount was given in correct format else False.
*/
public static boolean isCorrectAmountType(String args) {
String amount = args.substring(1);
return amount.matches("\\d+");
}
}
Loading
Loading