forked from AY2324S1-CS2103T-T09-2/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request AY2324S1-CS2103T-T09-2#120 from ncmathan/Update-im…
…port Update import command for developers and clients
- Loading branch information
Showing
9 changed files
with
279 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/seedu/address/logic/commands/imports/ImportClientCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package seedu.address.logic.commands.imports; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.ImportCommand; | ||
import seedu.address.logic.commands.TabIndex; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.client.Client; | ||
import seedu.address.model.developer.Developer; | ||
|
||
import java.util.ArrayList; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class ImportClientCommand extends Command { | ||
public static final String COMMAND_WORD = "import-client"; | ||
//Name name, Phone phone, Email email, Address address, Role role, Set<String> projects, | ||
// Name organisation, Document document | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Imports clients from csv file.\n" | ||
+ "Column titles should follow this format strictly:\n" | ||
+ "Name, Contact Number, Email, Address, Role, Organisation, Document, Projects"; | ||
public static final String MESSAGE_SUCCESS = "New client added: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_CLIENT = " is a client that already exists in the address book\n"; | ||
|
||
private final ArrayList<Client> toAddList; | ||
|
||
/** | ||
* Creates an AddClientCommand to add the specified {@code Client} | ||
*/ | ||
public ImportClientCommand(ArrayList<Client> clientList) { | ||
requireNonNull(clientList); | ||
for(Client i: clientList) { | ||
requireNonNull(i); | ||
} | ||
toAddList = clientList; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
String output = ""; | ||
for(Client toAdd: toAddList) { | ||
if (model.hasClient(toAdd)) { | ||
output += toAdd.getName().fullName + MESSAGE_DUPLICATE_CLIENT; | ||
} | ||
|
||
model.addClient(toAdd); | ||
output += String.format(MESSAGE_SUCCESS, Messages.format(toAdd)); | ||
output += "\n"; | ||
} | ||
return new CommandResult(output, TabIndex.Client); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof ImportCommand)) { | ||
return false; | ||
} | ||
|
||
ImportClientCommand otherImportCommand = (ImportClientCommand) other; | ||
return toAddList.equals(otherImportCommand.toAddList); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("toAddList", toAddList) | ||
.toString(); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/seedu/address/logic/commands/imports/ImportDeveloperCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package seedu.address.logic.commands.imports; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.ImportCommand; | ||
import seedu.address.logic.commands.TabIndex; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.developer.Developer; | ||
|
||
import java.util.ArrayList; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class ImportDeveloperCommand extends Command { | ||
public static final String COMMAND_WORD = "import-developer"; | ||
//Name name, Phone phone, Email email, Address address, Role role, Set<String> projects, | ||
// Salary salary, Date dateJoined, GithubId githubId, Rating rating | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Imports developers from csv file.\n" | ||
+ "Column titles should follow this format strictly:\n" | ||
+ "Name, Contact Number, Email, Address, Date Joined, Role, Salary, GithubId, Rating, Projects"; | ||
public static final String MESSAGE_SUCCESS = "New developer added: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_DEVELOPER = " is a developer that already exists in the address book\n"; | ||
|
||
private final ArrayList<Developer> toAddList; | ||
|
||
/** | ||
* Creates an AddDeveloperCommand to add the specified {@code Developer} | ||
*/ | ||
public ImportDeveloperCommand(ArrayList<Developer> developerList) { | ||
requireNonNull(developerList); | ||
for(Developer i: developerList) { | ||
requireNonNull(i); | ||
} | ||
toAddList = developerList; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
String output = ""; | ||
for(Developer toAdd: toAddList) { | ||
if (model.hasDeveloper(toAdd)) { | ||
output += toAdd.getName().fullName + MESSAGE_DUPLICATE_DEVELOPER; | ||
} | ||
|
||
model.addDeveloper(toAdd); | ||
output += String.format(MESSAGE_SUCCESS, Messages.format(toAdd)); | ||
output += "\n"; | ||
} | ||
return new CommandResult(output, TabIndex.Developer); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof ImportCommand)) { | ||
return false; | ||
} | ||
|
||
ImportDeveloperCommand otherImportCommand = (ImportDeveloperCommand) other; | ||
return toAddList.equals(otherImportCommand.toAddList); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("toAddList", toAddList) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/seedu/address/logic/parser/imports/ImportClientCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package seedu.address.logic.parser.imports; | ||
import seedu.address.logic.commands.imports.ImportClientCommand; | ||
import seedu.address.logic.parser.Parser; | ||
import seedu.address.logic.parser.ParserUtil; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.client.Client; | ||
import seedu.address.model.client.Document; | ||
import seedu.address.model.commons.Name; | ||
import seedu.address.model.person.Address; | ||
import seedu.address.model.person.Email; | ||
import seedu.address.model.person.Phone; | ||
import seedu.address.model.person.Role; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Set; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_FILE; | ||
|
||
public class ImportClientCommandParser implements Parser<ImportClientCommand> { | ||
|
||
@Override | ||
public ImportClientCommand parse(String fileName) throws ParseException { | ||
try { | ||
FileWriter myWriter = new FileWriter("filename.txt"); | ||
myWriter.write("Files in Java might be tricky, but it is fun enough!"); | ||
myWriter.close(); | ||
fileName = fileName.trim(); | ||
BufferedReader br = new BufferedReader(new FileReader(fileName)); | ||
String line = ""; | ||
String splitBy = ","; | ||
|
||
// Check if the CSV file contains valid column names | ||
boolean isValid = checkColumnNames(br.readLine()); | ||
if (!isValid) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ImportClientCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
ArrayList<Client> toAddList = new ArrayList<>(); | ||
while ((line = br.readLine()) != null) { | ||
String[] clientData = line.split(splitBy); | ||
|
||
// Parse client data from CSV columns | ||
Name name = ParserUtil.parseName(clientData[0]); | ||
Phone phone = ParserUtil.parsePhone(clientData[1]); | ||
Email email = ParserUtil.parseEmail(clientData[2]); | ||
Address address = ParserUtil.parseAddress(clientData[3]); | ||
Role role = ParserUtil.parseRole(clientData[4]); | ||
Name organisation = ParserUtil.parseName(clientData[5]); | ||
Document document = ParserUtil.parseDocument(clientData[6]); | ||
ArrayList<String> projects = new ArrayList<>(); | ||
for(int i=7;i< clientData.length;i++) { | ||
projects.add(clientData[i]); | ||
} | ||
Set<String> projectList = ParserUtil.parseProjectsWithCheck(projects); | ||
|
||
// Create a Client object | ||
Client client = new Client(name, phone, email, address, role, projectList, organisation, document); | ||
|
||
// Add the client to the list | ||
toAddList.add(client); | ||
} | ||
|
||
return new ImportClientCommand(toAddList); | ||
} catch (FileNotFoundException ex) { | ||
throw new ParseException(MESSAGE_INVALID_FILE); | ||
} catch (IOException e) { | ||
throw new ParseException("Error reading line from file " + fileName); | ||
} | ||
} | ||
|
||
// Define a method to check if the CSV file contains valid column names | ||
//Name name, Phone phone, Email email, Address address, Role role, Set<String> projects, | ||
// Name organisation, Document document | ||
private boolean checkColumnNames(String line) { | ||
String[] columnNames = line.split(","); | ||
return columnNames.length == 8 && columnNames[0].contains("Name") && | ||
columnNames[1].contains("Contact Number") && columnNames[2].contains("Email") && | ||
columnNames[3].contains("Address") && columnNames[4].contains("Role") && | ||
columnNames[5].contains("Organisation") && columnNames[6].contains("Document") && | ||
columnNames[7].contains("Projects"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.