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

Improve code quality - ZH #506

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
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Messages {
public static final String MESSAGE_CUSTOMERS_MATCHED_LISTED = "%1$d customers listed!";
public static final String MESSAGE_DELIVERY_LISTED_OVERVIEW = "%1$d deliveries listed!";
public static final String MESSAGE_DUPLICATE_FIELDS = "Multiple values specified for the following"
+ "single-valued field(s): ";
+ " single-valued field(s): ";
public static final String MESSAGE_USER_NOT_AUTHENTICATED = "Access denied! You are currently not logged in.";
public static final String MESSAGE_WELCOME = "Welcome to HomeBoss!\n\n" + "You are currently logged out.\n"
+ "Login or register to start using HomeBoss.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;

import java.util.logging.Logger;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.CommandResult;
Expand Down Expand Up @@ -35,6 +37,7 @@ public class CustomerAddCommand extends CustomerCommand {

public static final String MESSAGE_SUCCESS = "New customer added:\n\n%1$s";
public static final String MESSAGE_DUPLICATE_CUSTOMER = "This customer already exists in HomeBoss";
private static final Logger logger = Logger.getLogger(CustomerAddCommand.class.getName());

private final Customer toAdd;

Expand All @@ -49,17 +52,24 @@ public CustomerAddCommand(Customer customer) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
logger.info("Executing CustomerAddCommand: name "
+ toAdd.getName() + ", phone "
+ toAdd.getPhone() + ", email "
+ toAdd.getEmail() + ", address "
+ toAdd.getAddress());

// User cannot perform this operation before logging in
if (!model.getUserLoginStatus()) {
// reset the customer count to the previous value
Customer.setCustomerCount(toAdd.getCustomerId() - 1);
Customer.resetPrevCustomerCount();
logger.warning("User is not logged in!");
throw new CommandException(MESSAGE_USER_NOT_AUTHENTICATED);
}

if (model.hasCustomer(toAdd)) {
// reset the customer count to the previous value
Customer.setCustomerCount(toAdd.getCustomerId() - 1);
Customer.resetPrevCustomerCount();
logger.warning("Duplicate customer found!");
throw new CommandException(MESSAGE_DUPLICATE_CUSTOMER);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_USER_NOT_AUTHENTICATED;

import java.util.logging.Logger;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.Command;
Expand All @@ -24,6 +26,8 @@ public class CustomerFindCommand extends Command {
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";

private static final Logger logger = Logger.getLogger(CustomerFindCommand.class.getName());

private final NameContainsKeywordsPredicate predicate;

public CustomerFindCommand(NameContainsKeywordsPredicate predicate) {
Expand All @@ -33,16 +37,19 @@ public CustomerFindCommand(NameContainsKeywordsPredicate predicate) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
logger.info("Executing CustomerFindCommand: keyword "
+ predicate.getKeywordsAsString());

// User cannot perform this operation before logging in
if (!model.getUserLoginStatus()) {
logger.warning("User is not logged in!");
throw new CommandException(MESSAGE_USER_NOT_AUTHENTICATED);
}

model.updateFilteredCustomerList(predicate);
return new CommandResult(
String.format(Messages.MESSAGE_CUSTOMERS_MATCHED_LISTED,
model.getFilteredCustomerList().size()), true);
model.getFilteredCustomerListSize()), true);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PASSWORD;
import static seedu.address.logic.parser.CliSyntax.PREFIX_USER;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_CUSTOMERS;

import java.util.logging.Logger;

import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
Expand Down Expand Up @@ -31,6 +32,7 @@ public class UserLoginCommand extends Command {

public static final String MESSAGE_NO_REGISTERED_ACCOUNT_FOUND = "No registered account found.\n\n"
+ "Please register an account first.\n\n" + UserRegisterCommand.MESSAGE_USAGE;
private static final Logger logger = Logger.getLogger(UserLoginCommand.class.getName());

private final User user;

Expand All @@ -52,24 +54,28 @@ public UserLoginCommand(User user) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
logger.info("Executing UserLoginCommand: user " + user.getUsername());

// Check if there is a stored user
if (model.getStoredUser() == null) {
logger.warning("No registered account found!");
throw new CommandException(MESSAGE_NO_REGISTERED_ACCOUNT_FOUND);
}

// Logged in user cannot login again
if (model.getUserLoginStatus()) {
logger.warning("User is already logged in!");
throw new CommandException(MESSAGE_ALREADY_LOGGED_IN);
}

// Check if the user matches the user loaded in model
if (!model.userMatches(user)) {
logger.warning("User credentials does not match!");
throw new CommandException(MESSAGE_WRONG_CREDENTIALS);
}

model.setLoginSuccess();
model.updateFilteredCustomerList(PREDICATE_SHOW_ALL_CUSTOMERS);
model.showAllFilteredCustomerList();
return new CommandResult(MESSAGE_SUCCESS, true);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package seedu.address.logic.commands.user;

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

import java.util.logging.Logger;

import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
Expand All @@ -17,22 +17,25 @@ public class UserLogoutCommand extends Command {
public static final String COMMAND_WORD = "logout";
public static final String MESSAGE_SUCCESS = "Bye!";
public static final String MESSAGE_ALREADY_LOGGED_OUT = "You are already logged out!";
private static final Logger logger = Logger.getLogger(UserLogoutCommand.class.getName());


@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
logger.info("Executing UserLogoutCommand");

// Logged out user cannot log out again
if (!model.getUserLoginStatus()) {
logger.warning("User is already logged out!");
throw new CommandException(MESSAGE_ALREADY_LOGGED_OUT);
}

// Set status in model to logged out, and update filtered lists
model.setLogoutSuccess();
model.updateFilteredCustomerList(PREDICATE_SHOW_NO_CUSTOMERS);
model.updateFilteredDeliveryList(PREDICATE_SHOW_NO_DELIVERIES);
// Display the updated empty list
model.setUiListCustomer();
model.clearFilteredDeliveryList();
model.clearFilteredCustomerList();
Comment on lines +36 to +37

Choose a reason for hiding this comment

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

Good one


return new CommandResult(MESSAGE_SUCCESS, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.Objects;
import java.util.Optional;
import java.util.logging.Logger;

import seedu.address.commons.util.CollectionUtil;
import seedu.address.commons.util.ToStringBuilder;
Expand Down Expand Up @@ -45,6 +46,8 @@ public class UserUpdateCommand extends Command {
public static final String MESSAGE_PASSWORD_MISMATCH = "Passwords do not match. Please try again.";
public static final String MESSAGE_QUESTION_OR_ANSWER_MISSING = "Secret Question and Answer have to be "
+ "either all present or all absent. Try again.";
private static final Logger logger = Logger.getLogger(UserUpdateCommand.class.getName());

private final UserUpdateDescriptor userUpdateDescriptor;

/**
Expand All @@ -65,9 +68,11 @@ public UserUpdateCommand(UserUpdateDescriptor userUpdateDescriptor) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
logger.info("Executing UserUpdateCommand");

// User cannot perform this operation before logging in
if (!model.getUserLoginStatus()) {
logger.warning("User is not logged in!");
throw new CommandException(MESSAGE_USER_NOT_AUTHENTICATED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;

import java.util.logging.Logger;
import java.util.stream.Stream;

import seedu.address.logic.commands.customer.CustomerAddCommand;
Expand All @@ -22,13 +23,15 @@
import seedu.address.model.customer.Phone;

/**
* Parses input arguments and creates a new AddCommand object
* Parses input arguments and creates a new CustomerAddCommand object
*/
public class CustomerAddCommandParser implements Parser<CustomerAddCommand> {

private static final Logger logger = Logger.getLogger(CustomerAddCommandParser.class.getName());

/**
* Parses the given {@code String} of arguments in the context of the AddCommand
* and returns an AddCommand object for execution.
* Parses the given {@code String} of arguments in the context of the CustomerAddCommand
* and returns a CustomerAddCommand object for execution.
*
* @throws ParseException if the user input does not conform the expected format
*/
Expand All @@ -38,6 +41,7 @@ public CustomerAddCommand parse(String args) throws ParseException {

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
logger.severe("Could not parse command");
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, CustomerAddCommand.MESSAGE_USAGE));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PASSWORD;
import static seedu.address.logic.parser.CliSyntax.PREFIX_USER;

import java.util.logging.Logger;
import java.util.stream.Stream;

import seedu.address.logic.commands.user.UserLoginCommand;
Expand All @@ -21,6 +22,9 @@
* Parses input arguments and creates a new UserLoginCommand object
*/
public class UserLoginCommandParser implements Parser<UserLoginCommand> {

private static final Logger logger = Logger.getLogger(UserLoginCommandParser.class.getName());

/**
* Parses the given {@code String} of arguments in the context of the UserLoginCommand
* and returns an UserLoginCommand object for execution.
Expand All @@ -33,6 +37,7 @@ public UserLoginCommand parse(String args) throws ParseException {

if (!arePrefixesPresent(argMultimap, PREFIX_USER, PREFIX_PASSWORD)
|| !argMultimap.getPreamble().isEmpty()) {
logger.severe("Could not parse command");
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UserLoginCommand.MESSAGE_USAGE));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_SECRET_QUESTION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_USER;

import java.util.logging.Logger;

import seedu.address.logic.commands.user.UserUpdateCommand;
import seedu.address.logic.commands.user.UserUpdateCommand.UserUpdateDescriptor;
import seedu.address.logic.parser.ArgumentMultimap;
Expand All @@ -20,6 +22,9 @@
* Parses input arguments and creates a new UserUpdateCommand object
*/
public class UserUpdateCommandParser implements Parser<UserUpdateCommand> {

private static final Logger logger = Logger.getLogger(UserUpdateCommandParser.class.getName());

/**
* Parses the given {@code String} of arguments in the context of the UserUpdateCommand
* and returns a UserUpdateCommand object for execution.
Expand All @@ -37,6 +42,7 @@ public UserUpdateCommand parse(String args) throws ParseException {
UserUpdateDescriptor userUpdateDescriptor = new UserUpdateDescriptor();

if (!argMultimap.getPreamble().isEmpty()) {
logger.severe("Could not parse command");
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UserUpdateCommand.MESSAGE_USAGE));
}

Expand All @@ -49,6 +55,7 @@ public UserUpdateCommand parse(String args) throws ParseException {
userUpdateDescriptor = parseSecretQuestionAndAnswer(argMultimap, userUpdateDescriptor);

if (!userUpdateDescriptor.isAnyFieldEdited()) {
logger.warning("No fields provided");
throw new ParseException(
String.format(UserUpdateCommand.MESSAGE_MISSING_FIELDS, UserUpdateCommand.MESSAGE_USAGE));
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public interface Model {
*/
void showAllFilteredCustomerList();

/**
* Resets the customer list to show no customers.
*/
void clearFilteredCustomerList();

/**
* Returns the number of customers in the filtered customer list.
*
Expand Down Expand Up @@ -225,6 +230,11 @@ public interface Model {
*/
void showAllFilteredDeliveryList();

/**
* Resets the delivery list to show no deliveries.
*/
void clearFilteredDeliveryList();

/**
* Returns the number of deliveries in the filtered delivery list.
*
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ public void showAllFilteredCustomerList() {
updateFilteredCustomerList(PREDICATE_SHOW_ALL_CUSTOMERS);
}

/**
* Resets the customer list to show no customers.
*/
@Override
public void clearFilteredCustomerList() {
updateFilteredCustomerList(PREDICATE_SHOW_NO_CUSTOMERS);
}

/**
* Returns the number of customers in the filtered customer list.
*
Expand Down Expand Up @@ -463,6 +471,14 @@ public void showAllFilteredDeliveryList() {
updateFilteredDeliveryList(PREDICATE_SHOW_ALL_DELIVERIES);
}

/**
* Resets the delivery list to show no deliveries.
*/
@Override
public void clearFilteredDeliveryList() {
updateFilteredDeliveryList(PREDICATE_SHOW_NO_DELIVERIES);
}

/**
* Returns the number of deliveries in the filtered delivery list.
*
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/seedu/address/model/customer/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,21 @@ public static void setCustomerCount(int count) {

/**
* Returns current customerCount.
* Used by {@code CustomerBuilder} to create customer with expected customerId.
* To be used by {@code CustomerBuilder} to create customer with expected customerId.
*
* @return customerCount
*/
public static int getCustomerCount() {
return Customer.customerCount;
}

/**
* Decrements customerCount by 1.
*/
public static void resetPrevCustomerCount() {
Customer.customerCount -= 1;
}

/**
* Returns true if both Customers have the same customerId or {@code Phone}.
* This defines a weaker notion of equality between two customers.
Expand Down
Loading
Loading