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

Command and Parser #34

Merged
merged 13 commits into from
Sep 29, 2019
15 changes: 9 additions & 6 deletions src/main/java/seedu/algobase/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import static java.util.Objects.requireNonNull;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_AUTHOR;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_DESCRIPTION;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_DIFFICULTY;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_SOURCE;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_WEBLINK;

Expand All @@ -26,12 +28,13 @@ public class AddCommand extends Command {
+ PREFIX_DESCRIPTION + "DESCRIPTION "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_AUTHOR + "98765432 "
+ PREFIX_WEBLINK + "johnd@example.com "
+ PREFIX_DESCRIPTION + "311, Clementi Ave 2, #02-25 "
+ PREFIX_TAG + "friends "
+ PREFIX_TAG + "owesMoney";
+ PREFIX_NAME + "Sequences "
+ PREFIX_AUTHOR + "Tung Kam Chuen "
+ PREFIX_WEBLINK + "https://open.kattis.com/problems/sequences "
+ PREFIX_DESCRIPTION
+ "Find the sum of the number of inversions of the 2k sequences, modulo 1000000007 (109+7). "
+ PREFIX_DIFFICULTY + "3.0 "
+ PREFIX_SOURCE + "Kattis";

public static final String MESSAGE_SUCCESS = "New Problem added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This Problem already exists in the algobase";
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/algobase/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class EditCommand extends Command {
+ "[" + PREFIX_DESCRIPTION + "DESCRIPTION] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_AUTHOR + "91234567 "
+ PREFIX_WEBLINK + "johndoe@example.com";
+ PREFIX_AUTHOR + "Tung Kam Chuen "
+ PREFIX_WEBLINK + "https://open.kattis.com/problems/sequences";

public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Problem: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
Expand Down
62 changes: 52 additions & 10 deletions src/main/java/seedu/algobase/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_WEBLINK;

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

Expand Down Expand Up @@ -38,21 +39,62 @@ public class AddCommandParser implements Parser<AddCommand> {
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_AUTHOR, PREFIX_WEBLINK, PREFIX_DESCRIPTION,
PREFIX_TAG);
PREFIX_TAG, PREFIX_DIFFICULTY, PREFIX_REMARK, PREFIX_SOURCE);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_DESCRIPTION, PREFIX_AUTHOR, PREFIX_WEBLINK)
if (!arePrefixesPresent(argMultimap, PREFIX_NAME)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Author author = ParserUtil.parseAuthor(argMultimap.getValue(PREFIX_AUTHOR).get());
WebLink webLink = ParserUtil.parseWeblink(argMultimap.getValue(PREFIX_WEBLINK).get());
Description description = ParserUtil.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Difficulty difficulty = ParserUtil.parseDifficulty(argMultimap.getValue(PREFIX_DIFFICULTY).get());
Remark remark = ParserUtil.parseRemark(argMultimap.getValue(PREFIX_REMARK).get());
Source source = ParserUtil.parseSource(argMultimap.getValue(PREFIX_SOURCE).get());

Author author;
if (arePrefixesPresent(argMultimap, PREFIX_AUTHOR)) {
author = ParserUtil.parseAuthor(argMultimap.getValue(PREFIX_AUTHOR).get());
} else {
author = Author.DEFAULT_AUTHOR;
}

WebLink webLink;
if (arePrefixesPresent(argMultimap, PREFIX_AUTHOR)) {
webLink = ParserUtil.parseWeblink(argMultimap.getValue(PREFIX_WEBLINK).get());
} else {
webLink = WebLink.DEFAULT_WEBLINK;
}

Description description;
if (arePrefixesPresent(argMultimap, PREFIX_AUTHOR)) {
description = ParserUtil.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION).get());
} else {
description = Description.DEFAULT_DESCRIPTION;
}

Set<Tag> tagList;
if (arePrefixesPresent(argMultimap, PREFIX_TAG)) {
tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
} else {
tagList = new HashSet<>();
}

Difficulty difficulty;
if (arePrefixesPresent(argMultimap, PREFIX_AUTHOR)) {
difficulty = ParserUtil.parseDifficulty(argMultimap.getValue(PREFIX_DIFFICULTY).get());
} else {
difficulty = Difficulty.DEFAULT_DIFFICULTY;
}

Remark remark;
if (arePrefixesPresent(argMultimap, PREFIX_AUTHOR)) {
remark = ParserUtil.parseRemark(argMultimap.getValue(PREFIX_REMARK).get());
} else {
remark = Remark.DEFAULT_REMARK;
}

Source source;
if (arePrefixesPresent(argMultimap, PREFIX_AUTHOR)) {
source = ParserUtil.parseSource(argMultimap.getValue(PREFIX_SOURCE).get());
} else {
source = Source.DEFAULT_SOURCE;
}

Problem problem = new Problem(name, author, webLink, description, tagList, difficulty, remark, source);

Expand Down
15 changes: 14 additions & 1 deletion src/main/java/seedu/algobase/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import static seedu.algobase.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_AUTHOR;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_DESCRIPTION;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_DIFFICULTY;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_REMARK;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_SOURCE;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_WEBLINK;

Expand Down Expand Up @@ -33,7 +36,7 @@ public EditCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_AUTHOR, PREFIX_WEBLINK, PREFIX_DESCRIPTION,
PREFIX_TAG);
PREFIX_TAG, PREFIX_DIFFICULTY, PREFIX_REMARK, PREFIX_SOURCE);

Index index;

Expand All @@ -58,6 +61,16 @@ public EditCommand parse(String args) throws ParseException {
ParserUtil.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editProblemDescriptor::setTags);
if (argMultimap.getValue(PREFIX_DIFFICULTY).isPresent()) {
editProblemDescriptor.setDifficulty(
ParserUtil.parseDifficulty(argMultimap.getValue(PREFIX_DIFFICULTY).get()));
}
if (argMultimap.getValue(PREFIX_REMARK).isPresent()) {
editProblemDescriptor.setRemark(ParserUtil.parseRemark(argMultimap.getValue(PREFIX_REMARK).get()));
}
if (argMultimap.getValue(PREFIX_SOURCE).isPresent()) {
editProblemDescriptor.setSource(ParserUtil.parseSource(argMultimap.getValue(PREFIX_SOURCE).get()));
}

if (!editProblemDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/seedu/algobase/model/problem/Author.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ public class Author {

public static final String MESSAGE_CONSTRAINTS =
"Author numbers should only contain numbers, and it should be at least 3 digits long";
public static final String VALIDATION_REGEX = "\\d{3,}";
public static final Author DEFAULT_AUTHOR = new Author();

/*
* The first character of the author's name must not be a whitespace,
* otherwise " " (a blank string) becomes a valid input.
*/
public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*";
public final String value;

/**
Expand All @@ -26,6 +32,10 @@ public Author(String author) {
value = author;
}

private Author() {
value = "";
}

/**
* Returns true if a given string is a valid author number.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/algobase/model/problem/Description.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class Description {

public static final String MESSAGE_CONSTRAINTS = "Descriptions can take any values, and it should not be blank";
public static final Description DEFAULT_DESCRIPTION = new Description();

/*
* The first character of the description must not be a whitespace,
Expand All @@ -30,6 +31,10 @@ public Description(String description) {
value = description;
}

private Description() {
value = "";
}

/**
* Returns true if a given string is a valid weblink.
*/
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/algobase/model/problem/Difficulty.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Difficulty {
public static final double DIFFICULTY_LOWER_BOUND = 0.0;
public static final double DIFFICULTY_UPPER_BOUND = 5.0;
public static final Difficulty DEFAULT_DIFFICULTY = new Difficulty();
public static final String VALIDATION_REGEX = "\\d+.\\d+";
public final double value;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/algobase/model/problem/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Name {
"Names should only contain alphanumeric characters and spaces, and it should not be blank";

/*
* The first character of the description must not be a whitespace,
* The first character of the name must not be a whitespace,
* otherwise " " (a blank string) becomes a valid input.
*/
public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*";
Expand Down
35 changes: 17 additions & 18 deletions src/main/java/seedu/algobase/model/problem/WebLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,19 @@
import static java.util.Objects.requireNonNull;
import static seedu.algobase.commons.util.AppUtil.checkArgument;

import java.net.MalformedURLException;
import java.net.URL;

/**
* Represents a Problem's weblink in the algobase.
* Guarantees: immutable; is valid as declared in {@link #isValidWeblink(String)}
*/
public class WebLink {

private static final String SPECIAL_CHARACTERS = "!#$%&'*+/=?`{|}~^.-";
public static final String MESSAGE_CONSTRAINTS = "Weblinks should be of the format local-part@domain "
+ "and adhere to the following constraints:\n"
+ "1. The local-part should only contain alphanumeric characters and these special characters, excluding "
+ "the parentheses, (" + SPECIAL_CHARACTERS + ") .\n"
+ "2. This is followed by a '@' and then a domain name. "
+ "The domain name must:\n"
+ " - be at least 2 characters long\n"
+ " - start and end with alphanumeric characters\n"
+ " - consist of alphanumeric characters, a period or a hyphen for the characters in between, if any.";
// alphanumeric and special characters
private static final String LOCAL_PART_REGEX = "^[\\w" + SPECIAL_CHARACTERS + "]+";
private static final String DOMAIN_FIRST_CHARACTER_REGEX = "[^\\W_]"; // alphanumeric characters except underscore
private static final String DOMAIN_MIDDLE_REGEX = "[a-zA-Z0-9.-]*"; // alphanumeric, period and hyphen
private static final String DOMAIN_LAST_CHARACTER_REGEX = "[^\\W_]$";
public static final String VALIDATION_REGEX = LOCAL_PART_REGEX + "@"
+ DOMAIN_FIRST_CHARACTER_REGEX + DOMAIN_MIDDLE_REGEX + DOMAIN_LAST_CHARACTER_REGEX;
public static final String MESSAGE_CONSTRAINTS = "Weblinks should be parsable by java.net.URL";
public static final String VALIDATION_REGEX =
"<\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]>";
public static final WebLink DEFAULT_WEBLINK = new WebLink();

public final String value;

Expand All @@ -40,11 +30,20 @@ public WebLink(String weblink) {
value = weblink;
}

private WebLink() {
value = "";
}

/**
* Returns if a given string is a valid weblink.
*/
public static boolean isValidWeblink(String test) {
return test.matches(VALIDATION_REGEX);
try {
URL url = new URL(test);
return true;
} catch (MalformedURLException e) {
return false;
}
}

@Override
Expand Down
69 changes: 39 additions & 30 deletions src/main/java/seedu/algobase/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,48 @@
public class SampleDataUtil {
public static Problem[] getSampleProblems() {
return new Problem[] {
new Problem(new Name("Alex Yeoh"), new Author("87438807"), new WebLink("alexyeoh@example.com"),
new Description("Blk 30 Geylang Street 29, #06-40"),
getTagSet("friends"),
new Problem(new Name("Sequences"), new Author("Tung Kam Chuen"),
new WebLink("https://open.kattis.com/problems/sequences"),
new Description("Find the sum of the number of inversions of the 2k sequences, "
+ "modulo 1000000007(109+7)."),
getTagSet(),
Difficulty.DEFAULT_DIFFICULTY,
Remark.DEFAULT_REMARK,
Source.DEFAULT_SOURCE),
new Problem(new Name("Bernice Yu"), new Author("99272758"), new WebLink("berniceyu@example.com"),
new Description("Blk 30 Lorong 3 Serangoon Gardens, #07-18"),
getTagSet("colleagues", "friends"),
Difficulty.DEFAULT_DIFFICULTY,
Remark.DEFAULT_REMARK,
Source.DEFAULT_SOURCE),
new Problem(new Name("Charlotte Oliveiro"), new Author("93210283"), new WebLink("charlotte@example.com"),
new Description("Blk 11 Ang Mo Kio Street 74, #11-04"),
getTagSet("neighbours"),
Difficulty.DEFAULT_DIFFICULTY,
Remark.DEFAULT_REMARK,
Source.DEFAULT_SOURCE),
new Problem(new Name("David Li"), new Author("91031282"), new WebLink("lidavid@example.com"),
new Description("Blk 436 Serangoon Gardens Street 26, #16-43"),
getTagSet("family"),
Difficulty.DEFAULT_DIFFICULTY,
Remark.DEFAULT_REMARK,
Source.DEFAULT_SOURCE),
new Problem(new Name("Irfan Ibrahim"), new Author("92492021"), new WebLink("irfan@example.com"),
new Description("Blk 47 Tampines Street 20, #17-35"),
getTagSet("classmates"),
Difficulty.DEFAULT_DIFFICULTY,
new Source("Kattis")),
new Problem(new Name("Two Sum"), Author.DEFAULT_AUTHOR,
new WebLink("https://leetcode.com/problems/two-sum/"),
new Description("Given an array of integers, "
+ "return indices of the two numbers such that they add up to a specific target."),
getTagSet("array", "hash table", "algorithm"),
new Difficulty("1.0"),
new Remark("You may assume that each input would have exactly one solution, "
+ "and you may not use the same element twice."),
new Source("LeetCode")),
new Problem(new Name("Second Highest Salary"), new Author("LeetCode"),
new WebLink("https://leetcode.com/problems/second-highest-salary/"),
new Description("Write a SQL query to get the second highest salary from the Employee table."),
getTagSet("MySQL", "database"),
new Difficulty("2.0"),
Remark.DEFAULT_REMARK,
Source.DEFAULT_SOURCE),
new Problem(new Name("Roy Balakrishnan"), new Author("92624417"), new WebLink("royb@example.com"),
new Description("Blk 45 Aljunied Street 85, #11-31"),
getTagSet("colleagues"),
new Source("LeetCode")),
new Problem(new Name("Sudoku Solver"), Author.DEFAULT_AUTHOR,
new WebLink("https://leetcode.com/problems/sudoku-solver/"),
Description.DEFAULT_DESCRIPTION,
getTagSet("hash table", "backtracking", "algorithm"),
new Difficulty("5.0"),
new Remark("You may assume that the given Sudoku puzzle will have a single unique solution."),
new Source("LeetCode")),
new Problem(new Name("A. Dawid and Bags of Candies"), Author.DEFAULT_AUTHOR,
new WebLink("https://codeforces.com/problemset/problem/1230/A"),
Description.DEFAULT_DESCRIPTION,
getTagSet("brute force"),
new Difficulty("4.0"),
new Remark("time limit per test1 second\n" + "memory limit per test256 megabytes"),
new Source("CodeForce")),
new Problem(new Name("Factorial"), new Author("Wee Han"), WebLink.DEFAULT_WEBLINK,
new Description("define a function factorial that takes in a number n "
+ "and returns the factorial of the number."),
getTagSet("recursion"),
Difficulty.DEFAULT_DIFFICULTY,
Remark.DEFAULT_REMARK,
Source.DEFAULT_SOURCE)
Expand Down