Skip to content
Snippets Groups Projects
Commit 1fa0e4b2 authored by zabiulla.ahmadi's avatar zabiulla.ahmadi
Browse files

update last changes

parents
No related branches found
No related tags found
No related merge requests found
.vscode
.idea
target
# contacts_manager
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://gitedu.hesge.ch/java_oop/contacts_manager.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitedu.hesge.ch/java_oop/contacts_manager/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
[{"name":"acima","lastname":["BBBB"],"address":"sude ksdjf ldsaflksa flkds fldsa fdsaf","telelphoneNumbers":["+4234234234234234"],"emailAddresses":["@.com.sdfd"],"socialAcounts":["https://"],"profession":"student","familyRelation":"brother","type":"Family"},{"name":"agnon","lastname":["burteshi"],"address":"rue du valais 21","telelphoneNumbers":["+9294324234234"],"emailAddresses":["burteshi@gmail.com"],"socialAcounts":["https://hello.com"],"profession":"developeeur","type":"Professional","professionalRelation":"Collegue"},{"name":"agnon","lastname":["kurteshi"],"address":"rue du rhone 4, 1203 Genève","telelphoneNumbers":["+41 78 741 22 11"],"emailAddresses":["agnon@gmail.com"],"socialAcounts":["https://hello.com/?=name"],"profession":"student","type":"Friend","friendsSince":"2002.12.12"}]
\ No newline at end of file
pom.xml 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>contacts.hepia</groupId>
<artifactId>contacts_manager_maven</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>Main</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
\ No newline at end of file
package Application;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.function.Consumer;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import Contacts.Contacts;
import Family.Family;
import Friends.Friends;
import Helper.Helper;
import Professional.Professional;
public class Application extends Helper {
List<Contacts> contactsList = new ArrayList<>();
public void saveContactList(List<Contacts> contactsList) {
ObjectMapper objectMapper = new ObjectMapper();
try {
objectMapper.writeValue(new File("db/contacts.json"), contactsList);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
private void parseContact(Object contact) {
JSONObject info = ((JSONObject) contact);
String name = (String) info.get("name");
List<String> lastname = Arrays.asList(info.get("lastname").toString()
.replace("[", "")
.replace("]", "")
.trim()
.replace("\"", "").split(","));
String address = info.get("address").toString().trim();
List<String> telelphoneNumbers = Arrays.asList(info.get("telelphoneNumbers").toString()
.replace("[", "")
.replace("]", "")
.replace("\"", "")
.trim()
.split(","));
List<String> emailAddresses = Arrays.asList(info.get("emailAddresses").toString()
.replace("[", "")
.replace("]", "")
.replace("\"", "")
.trim()
.split(","));
List<String> socialAcounts = Arrays.asList(info.get("socialAcounts").toString()
.replace("[", "")
.replace("]", "")
.replace("\\", "")
.replace("\"", "")
.trim()
.split(","));
String profession = info.get("profession").toString().trim();
Contacts new_contact = null;
String type = info.get("type").toString();
switch (type) {
case "Family":
String familyRelation = (info.get("familyRelation") != null)
? info.get("familyRelation").toString().trim()
: "";
new_contact = new Family(name, lastname, address, telelphoneNumbers, emailAddresses,
socialAcounts, profession, familyRelation);
break;
case "Friend":
String friendsSince = (info.get("friendsSince") != null) ? info.get("friendsSince").toString().trim()
: "";
new_contact = new Friends(name, lastname, address, telelphoneNumbers, emailAddresses,
socialAcounts, profession, friendsSince);
break;
case "Professional":
String ProfessionRelation = (info.get("ProfessionRelation") != null)
? info.get("ProfessionRelation").toString().trim()
: "";
new_contact = new Friends(name, lastname, address, telelphoneNumbers, emailAddresses,
socialAcounts, profession, ProfessionRelation);
break;
default:
break;
}
addToContactList(new_contact);
}
public void loadDatabase() {
// JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("db/contacts.json")) {
// Read JSON file
Object obj = jsonParser.parse(reader);
var contactList = (JSONArray) obj;
contactList.forEach(contact -> parseContact(contact));
} catch (IOException | org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
}
public void addToContactList(Contacts newContact) {
contactsList.add(newContact);
// sort
sortContactList();
}
public Contacts createContact() {
System.out.println("Enter the type of contact: []");
System.out.println("[1] for friend ");
System.out.println("[2] for family ");
System.out.println("[3] for professional");
System.out.println("Enter your choice [] :");
Scanner scanner = new Scanner(System.in);
int type = scanner.nextInt();
while (type < 1 || type > 3) {
System.out.format("%s %s %s\n", RED, "ERREUR: PLEASE CHOOSE THE CORRECT TYPE", RED);
System.out.format("%s", RESET);
System.out.println("Enter your choice [] :");
type = scanner.nextInt();
}
Contacts new_contact = null;
if (type == 1) {
new_contact = new Friends()
.askContact();
}
else if (type == 2) {
new_contact = new Family().askContact();
}
else if (type == 3) {
new_contact = new Professional()
.askContact();
} else {
PrintErrorAndReturn();
}
// scanner.close();
return new_contact;
}
public boolean update(int id) {
int elementId = id - 1;
if (!contactsList.isEmpty() && contactsList.get(elementId) != null) {
contactsList.get(elementId).updateContact();
sortContactList();
return true;
}
return false;
}
public List<Contacts> search() {
List<Contacts> result = new ArrayList<Contacts>();
Scanner scanner = new Scanner(System.in);
System.out.println("CHOOSE BY WHICH FIELD YOU WANT TO SEARCH");
System.out.println("[1] NAME ");
System.out.println("[2] LASTNAME ");
System.out.println("[3] EMAIL");
System.out.println("[4] BY TYPE:");
System.out.printf("%s %s %s : []%n", BLUE, "YOUR OPTION ", BLUE);
System.out.printf("%s", RESET);
int option = scanner.nextInt();
while (option < 1 || option > 4) {
System.out.printf("%s %s %s%n", RED, "ERREUR: PLEASE CHOOSE THE CORRECT OPTION", RED);
System.out.printf("%s %s %s : [] %n", BLUE, "YOUR OPTION ", BLUE);
System.out.printf("%s", RESET);
option = scanner.nextInt();
}
if (option == 1) {
System.out.printf("%s %s %s : [] %n", BLUE, "ENTER A NAME TO SEARCH ", BLUE);
System.out.printf("%s", RESET);
Scanner sc = new Scanner(System.in);
String nameToSearch = sc.nextLine();
while (nameToSearch.length() < 3) {
System.out.printf("%s %s %s%n", RED, "NAME SHOULD CONTAINS AT LEAST 3 CHARACTER LENGTH", RED);
System.out.printf("%s %s %s : [] %n", BLUE, "ENTER A NAME TO SEARCH ", BLUE);
System.out.printf("%s", RESET);
nameToSearch = sc.nextLine();
}
final String searchKey = nameToSearch;
result = contactsList.stream().filter(e -> e.getName().contains(searchKey)).toList();
}
if (option == 2)
{
int j = 0;
System.out.printf("%s %s %s : [] %n", BLUE, "ENTER A LASTNAME TO SEARCH ", BLUE);
System.out.printf("%s", RESET);
Scanner sc = new Scanner(System.in);
String lastNameToSerach = sc.nextLine();
while (lastNameToSerach.length() < 3) {
System.out.printf("%s %s %s%n", RED, "LASTNAME SHOULD CONTAINS AT LEAST 3 CHARACTER LENGTH", RED);
System.out.printf("%s %s %s : [] %n", BLUE, "ENTER A LASTNAME TO SEARCH ", BLUE);
System.out.printf("%s", RESET);
lastNameToSerach = sc.nextLine();
}
final String searchKey = lastNameToSerach;
result = contactsList.stream().filter(e -> e.getLastname().contains(searchKey)).toList();
} else if (option == 3) {
int j = 0;
System.out.printf("%s %s %s : [] %n", BLUE, "ENTER A EMAIL TO SEARCH ", BLUE);
System.out.printf("%s", RESET);
Scanner sc = new Scanner(System.in);
String emailToSearch = sc.nextLine();
while (emailToSearch.length() < 3) {
System.out.printf("%s %s %s\n%n", RED, "LASTNAME SHOULD CONTAINS AT LEAST 3 CHARACTER LENGTH", RED);
System.out.printf("%s %s %s : [] %n", BLUE, "ENTER A EMAIL TO SEARCH", BLUE);
System.out.printf("%s", RESET);
emailToSearch = sc.nextLine();
}
final String searchKey = emailToSearch;
result = contactsList.stream().filter(e -> e.getEmailAddresses().contains(searchKey)).toList();
} else if (option == 4) {
int j = 0;
System.out.printf("%s %s %s : [] %n", BLUE, "ENTER A TYPE TO SEARCH ", BLUE);
System.out.printf("%s", RESET);
Scanner sc = new Scanner(System.in);
String typeToSearch = sc.nextLine();
while (typeToSearch.length() < 5) {
System.out.printf("%s %s %s\n%n", RED, "TYPE SHOULD CONTAINS AT LEAST 5 CHARACTER LENGTH", RED);
System.out.printf("%s %s %s : [] %n", BLUE, "ENTER A TYPE TO SEARCH ", BLUE);
System.out.printf("%s", RESET);
typeToSearch = sc.nextLine();
}
final String searchKey = typeToSearch;
result = contactsList.stream().filter(e -> e.getType().contains(searchKey)).toList();
}
return result;
}
public boolean delete(int id) {
int elementId = id - 1;
if (elementId >= 0 && elementId < contactsList.size()) {
// delete
contactsList.remove(elementId);
// sort
sortContactList();
return true;
} else {
return false;
}
}
public void sortContactList() {
Comparator<Contacts> compareByName = Comparator
.comparing(Contacts::getName)
.thenComparing(contacts -> contacts.getLastname().get(0));
Collections.sort(contactsList, compareByName);
}
public void showContacts() {
clearConsoleScreen();
PrintInColor(" ==================================================================================\n", YELLOW);
PrintInColor(String.format("SHOW CONTACTS \t\t\t\t\t\t\tTOTAL [%s %d %s]\n", WHITE,
(!contactsList.isEmpty()) ? contactsList.size() : 0, CYAN), CYAN);
PrintInColor("==================================================================================\n\n", YELLOW);
if (contactsList.isEmpty()) {
System.out.println(" No contacts yet !\n");
} else {
contactsList.forEach(e -> e.showContact());
}
}
public void run() {
loadDatabase();
sortContactList();
int option = ASK_USER_INPUT;
while (true) {
Scanner scanner = new Scanner(System.in);
if (option == ASK_USER_INPUT) {
PrintHeader();
PrintInColor("Enter your option : ", WHITE);
option = scanner.nextInt();
if (option == EXIT_CONSOLE) {
saveContactList(contactsList); // save
System.exit(0);
}
if (option == ADD_CONTACTS) {
addToContactList(createContact());
saveContactList(contactsList); // save
option = ASK_USER_INPUT;
continue;
}
if (option == SHOW_CONTACTS) {
showContacts();
printFooter();
PrintInColor("Enter your option : ", WHITE);
option = scanner.nextInt();
option = populateErrorOrReturnHome(option);
continue;
}
if (option == SEARCH_CONTACT) {
if (!contactsList.isEmpty()) {
List<Contacts> result = search();
if (!result.isEmpty()) {
result.forEach(res -> res.showContact());
printFooter();
option = scanner.nextInt();
option = populateErrorOrReturnHome(option);
} else {
System.out.println("---------------------------------------------------");
System.out.println("no contact found");
System.out.println("---------------------------------------------------");
printFooter();
option = scanner.nextInt();
option = populateErrorOrReturnHome(option);
}
} else {
showContacts();
printFooter();
option = scanner.nextInt();
option = populateErrorOrReturnHome(option);
}
continue;
}
// update contact
if (option == UPDATE_CONTACTS) {
showContacts();
if (!contactsList.isEmpty()) {
System.out.format("%s %s %s : [] ", BLUE, "ENTER A CONTACT ID TO UPDATE: ", BLUE);
int id = scanner.nextInt();
while (id < 1 || id > contactsList.size() + 1) {
System.out.format("%s %s %s\n", RED, "ERREUR: PLEASE CHOOSE THE CORRECT ID", RED);
System.out.format("%s %s %s : [] ", BLUE, "ENTER A CONTACT ID TO UPDATE: ", BLUE);
id = scanner.nextInt();
}
Boolean updated = update(id);
if (updated) {
// save to database
saveContactList(contactsList);
option = ASK_USER_INPUT;
} else {
System.out.printf(
" Contact with id %d doesn't exists%n", id);
PrintErrorAndReturn();
option = scanner.nextInt();
option = populateErrorOrReturnHome(option);
}
} else {
showContacts();
printFooter();
option = scanner.nextInt();
option = populateErrorOrReturnHome(option);
}
continue;
}
// delete contact
if (option == DELETE_CONTACT) {
showContacts();
if (!contactsList.isEmpty()) {
deleteOption();
System.out.print("Enter contact id to delete: ");
int id = scanner.nextInt();
boolean deleted = delete(id);
if (deleted) {
option = ASK_USER_INPUT;
// save to database
saveContactList(contactsList);
} else {
System.out.printf(
" Contact with id %d doesn't exists%n", id);
PrintErrorAndReturn();
option = scanner.nextInt();
option = populateErrorOrReturnHome(option);
}
} else {
printFooter();
option = scanner.nextInt();
option = populateErrorOrReturnHome(option);
}
continue;
}
}
if (option < 1 || option > 4) {
clearConsoleScreen();
PrintErrorAndReturn();
option = scanner.nextInt();
option = populateErrorOrReturnHome(option);
}
scanner.close();// close scanner
}
}
public List<Contacts> getContactList() {
return this.contactsList;
}
}
package Contacts;
import Helper.Helper;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public abstract class Contacts extends Helper {
protected String name;
protected List<String> lastname;
protected String address;
protected List<String> telelphoneNumbers;
protected List<String> emailAddresses;
protected List<String> socialAcounts;
protected String profession;
public Contacts() {
}
public Contacts(String name, List<String> lastname, String address, List<String> telelphoneNumbers,
List<String> emailAddresses,
List<String> socialAcounts, String profession) {
this.name = name;
this.lastname = lastname;
this.address = address;
this.telelphoneNumbers = telelphoneNumbers;
this.emailAddresses = emailAddresses;
this.socialAcounts = socialAcounts;
this.profession = profession;
}
public String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
public List<String> getLastname() {
return lastname;
}
public void setLastname(List<String> lastname) {
this.lastname = lastname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<String> getTelelphoneNumbers() {
return telelphoneNumbers;
}
public void setTelelphoneNumbers(List<String> telelphoneNumbers) {
this.telelphoneNumbers = telelphoneNumbers;
}
public List<String> getEmailAddresses() {
return emailAddresses;
}
public void setEmailAddresses(List<String> emailAddresses) {
this.emailAddresses = emailAddresses;
}
public List<String> getSocialAcounts() {
return socialAcounts;
}
public void setSocialAcounts(List<String> socialAcounts) {
this.socialAcounts = socialAcounts;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public void askName() {
Scanner sc = new Scanner(System.in);
System.out.print(" Enter a name [] : ");
String name = sc.nextLine();
while (name.length() < 3) {
System.out
.println(
String.format("\n %sName should contain at least 3 character %s%s",
RED,
RED,
RESET));
System.out.println();
System.out.print(" Enter a name [] : ");
name = sc.nextLine();
}
setName(name);
// sc.close();
}
public void askLastName() {
Scanner sc = new Scanner(System.in);
System.out.print(" Enter one or more last name separated by , [] : ");
String lastName = sc.nextLine();
// validate
while (lastName.length() < 3) {
System.out
.println(
String.format("\n %slast name should contain at least 3 character %s%s",
RED,
RED,
RESET));
System.out.println();
System.out.print(" Enter one or more last name separated by , [] : ");
name = sc.nextLine();
}
setLastname(Arrays.asList(lastName.split(",")));
// sc.close();
}
public void askAdress() {
Scanner sc = new Scanner(System.in);
System.out.print(" Enter Address [] : ");
String address = sc.nextLine();
while (address.length() < 15) {
System.out
.println(
String.format("\n %sAddress should at least contain 15 character length%s%s",
RED,
RED,
RESET));
System.out.println();
System.out.print(" Enter Address [] : ");
address = sc.nextLine();
}
setAddress(address);
// sc.close();
}
public void askEmail() {
Scanner sc = new Scanner(System.in);
System.out.print(" Enter one or more email separated by , [] : ");
String email = sc.nextLine();
while (email.length() < 10 || !email.contains("@") || !email.contains(".")) {
System.out
.println(
String.format("\n %sPlease Insert a correct Email !%s%s", RED,
RED,
RESET));
System.out.println();
System.out.print(" Enter one or more email separated by , [] : ");
email = sc.nextLine();
}
setEmailAddresses(Arrays.asList(email.split(",")));
// sc.close();
}
public void askTelephoneNumber() {
Scanner sc = new Scanner(System.in);
System.out.print(" Enter one or more telephone number separated by , [] : ");
String telephonNumber = sc.nextLine();
while (telephonNumber.length() < 12 || !telephonNumber.contains("+")
|| telephonNumber.replaceAll(" ", telephonNumber).length() < 12) { // +4178 223 22 44
System.out.println(
String.format("\n %sPLEASE INSERT A CORRECT TELEPHONE NUMBER !%s%s",
RED,
RED,
RESET));
System.out.println();
System.out.print(" Enter one or more telephone number separated by , [] : ");
telephonNumber = sc.nextLine();
}
setTelelphoneNumbers(Arrays.asList(telephonNumber.split(",")));
}
public void askSocialAcount() {
Scanner sc = new Scanner(System.in);
System.out.print(" Enter one or more social acount URL separated by , [] :");
String socialAcount = sc.nextLine();
while (!socialAcount.contains("https://")) {
System.out
.println(
String.format("\n %sPLEASE INSERT A CORRECT URL PREFIXED BY HTTPS:// !%s%s",
RED,
RED,
RESET));
System.out.println();
System.out.print(" Enter one or more social acount URL separated by , [] :");
socialAcount = sc.nextLine();
}
setSocialAcounts(Arrays.asList(socialAcount.split(",")));
}
public void askProfession() {
Scanner sc = new Scanner(System.in);
System.out.print(" Enter Your Contact Profession [] : ");
String profession = sc.nextLine();
while (profession.length() < 5) {
System.out
.println(
String.format("\n %sPLEASE INSERT A CORRECT PROFESSION !%s%s",
RED,
RED,
RESET));
System.out.println();
System.out.print(" Enter Your Contact Profession [] : ");
profession = sc.nextLine();
}
setProfession(profession);
}
public void displayRequestHeader() {
clearConsoleScreen();
PrintInColor(" ==================================================================================\n", GREEN);
PrintInColor(String.format("%s\n", "CREATE A CONTACT"), BLUE);
PrintInColor("==================================================================================\n", GREEN);
}
public abstract String getType();
public abstract void setType(String type);
public abstract void showContact();
public abstract void updateContact();
}
package Family;
import java.util.List;
import java.util.Scanner;
import Contacts.Contacts;
import Helper.Helper;
public class Family extends Contacts {
private String Type;
private String familyRelation;
public Family() {
super();
this.Type = "Family";
}
public Family(String name, List<String> lastname, String address, List<String> telelphoneNumbers, List<String> emailAddresses,
List<String> socialAcounts, String profession, String familyRelation) {
super(name, lastname, address, telelphoneNumbers, emailAddresses, socialAcounts, profession);
Type = "Family";
setFamilyRelation(familyRelation);
}
@Override
public String getType() {
return Type;
}
@Override
public void setType(String type) {
Type = type;
}
public void setFamilyRelation(String familyRelation) {
this.familyRelation = familyRelation;
}
public String getFamilyRelation() {
return this.familyRelation;
}
public void askFamilyRelation() {
Scanner sc = new Scanner(System.in);
System.out.print(" Enter Your relation with contact owner [father, mother, brother, sister, ...] : ");
String famRelation = sc.nextLine();
while (famRelation.length() < 5) {
System.out
.println(
String.format("\n %sPLEASE INSERT A CORRECT FAMILY RELATION !%s%s",
RED,
RED,
RESET));
System.out.println();
System.out.print(" Enter Your relation with contact owner [father, mother, brother, sister, ...] : ");
famRelation = sc.nextLine();
}
setFamilyRelation(famRelation);
}
public Family askContact() {
clearConsoleScreen();
PrintInColor(" ==================================================================================\n", GREEN);
PrintInColor(String.format("%s\n", "CREATE A CONTACT [Family]"), BLUE);
PrintInColor("==================================================================================\n", GREEN);
askName();
askLastName();
askEmail();
//
askFamilyRelation();
askTelephoneNumber();
askAdress();
askSocialAcount();
askProfession();
return this;
}
public void updateContact(){
clearConsoleScreen();
PrintInColor(" ==================================================================================\n", GREEN);
PrintInColor(String.format("%s\n", "UPDATE CONTACT [Family]"), BLUE);
PrintInColor("==================================================================================\n", GREEN);
System.out.println("CHOOSE WHICH FIELD YOU WANT TO UPDATE");
System.out.println("[1] NAME ");
System.out.println("[2] LASTNAME ");
System.out.println("[3] EMAIL");
System.out.println("[4] FAMILY RELATION:");
System.out.println("[5] TELEPHONE NUMBER:");
System.out.println("[6] ADDRESS:");
System.out.println("[7] SOCIAL ACOUNT:");
System.out.println("[8] PROFESSION:");
System.out.format("%s %s %s : [] ", BLUE, "YOUR OPTION ", BLUE);
Scanner sc = new Scanner(System.in);
int option = sc.nextInt();
while(option < 1 || option > 8){
System.out.format("%s %s %s\n", RED, "ERREUR: PLEASE CHOOSE THE CORRECT OPTION", RED);
System.out.format("%s", RESET);
System.out.format("%s %s %s : [] ", BLUE, "YOUR OPTION ", BLUE);
option = sc.nextInt();
}
switch (option){
case 1:
askName();
break;
case 2:
askLastName();
break;
case 3:
askEmail();
break;
case 4:
askFamilyRelation();
break;
case 5:
askTelephoneNumber();
break;
case 6:
askAdress();
break;
case 7:
askSocialAcount();
break;
case 8:
askProfession();
break;
}
}
@Override
public void showContact() {
System.out.println(
"----------------------------------------------------------------------------------");
System.out.println();
System.out.println(String.format(" %sName : %s", BLUE, BLUE)
+ String.format("\t\t\t%s" + getName(), RESET));
System.out.println();
String lastnames = String.join(",", getLastname());
System.out.println(String.format(" %sLast Name : %s", BLUE,
BLUE)
+ String.format("\t\t\t%s" + lastnames, RESET));
System.out.println();
System.out.println(String.format(" %sFamily Relation : %s", BLUE,
BLUE)
+ String.format("\t\t%s" + getFamilyRelation(), RESET));
System.out.println();
System.out.println(String.format(" %sAddress : %s", BLUE, BLUE)
+ String.format("\t\t\t%s" + getAddress(), RESET));
System.out.println();
String emails = String.join(", ", getEmailAddresses());
System.out.println(String.format(" %sEmail : %s", BLUE, BLUE)
+ String.format("\t\t\t%s" + emails, RESET));
System.out.println();
String telephone_numbers = String.join(", ", getTelelphoneNumbers());
System.out.println(String.format(" %sTelephone Number : %s", BLUE,
BLUE)
+ String.format("\t\t%s" + telephone_numbers, RESET));
System.out.println();
String social_acounts = String.join(", ", getSocialAcounts());
System.out.println(String.format(" %sSocial Acount : %s", BLUE,
BLUE)
+ String.format("%s" + social_acounts, RESET));
System.out.println();
System.out.println(String.format(" %sProfession : %s", BLUE,
BLUE)
+ String.format("\t\t\t%s" + getProfession(), RESET));
System.out.println();
System.out.println(
"----------------------------------------------------------------------------------\n");
}
}
package Friends;
import Contacts.Contacts;
import java.util.List;
import java.util.Scanner;
public class Friends extends Contacts {
private String Type;
private String firendsSince;
public Friends() {
super();
Type = "Friend";
}
public Friends(String name, List<String> lastname, String address, List<String> telelphoneNumbers,
List<String> emailAddresses,
List<String> socialAcounts, String profession, String firendsSince) {
super(name, lastname, address, telelphoneNumbers, emailAddresses, socialAcounts, profession);
Type = "Friend";
setFriendsSince(firendsSince);
}
@Override
public String getType() {
return Type;
}
@Override
public void setType(String type) {
Type = type;
}
public void setFriendsSince(String str) {
this.firendsSince = str;
}
public String getFriendsSince() {
return this.firendsSince;
}
public void askFriendShipDate() {
Scanner sc = new Scanner(System.in);
System.out.print(this.name + " is Your friend since : ");
String friendshipeDate = sc.nextLine();
while (friendshipeDate.length() < 5) {
System.out
.println(
String.format("\n %sPLEASE INSERT A CORRECT FRIENDSHIP DATE !%s%s",
RED,
RED,
RESET));
System.out.println();
System.out.print(this.name + " is Your friend since : ");
friendshipeDate = sc.nextLine();
}
setFriendsSince(friendshipeDate);
}
public Contacts askContact() {
clearConsoleScreen();
PrintInColor(" ==================================================================================\n", GREEN);
PrintInColor(String.format("%s\n", "CREATE A CONTACT [Friends]"), BLUE);
PrintInColor("==================================================================================\n", GREEN);
askName();
askLastName();
askEmail();
//
askFriendShipDate();
askTelephoneNumber();
askAdress();
askSocialAcount();
askProfession();
return this;
}
public void updateContact() {
clearConsoleScreen();
PrintInColor(" ==================================================================================\n", GREEN);
PrintInColor(String.format("%s\n", "UPDATE CONTACT [FRIENDS]"), BLUE);
PrintInColor("==================================================================================\n", GREEN);
System.out.println("CHOOSE WHICH FIELD YOU WANT TO UPDATE");
System.out.println("[1] NAME ");
System.out.println("[2] LASTNAME ");
System.out.println("[3] EMAIL");
System.out.println("[4] FRIENDSHIP DATE:");
System.out.println("[5] TELEPHONE NUMBER:");
System.out.println("[6] ADDRESS:");
System.out.println("[7] SOCIAL ACOUNT:");
System.out.println("[8] PROFESSION:");
System.out.format("%s %s %s : [] ", BLUE, "YOUR OPTION ", BLUE);
Scanner sc = new Scanner(System.in);
int option = sc.nextInt();
while (option < 1 || option > 8) {
System.out.format("%s %s %s\n", RED, "ERREUR: PLEASE CHOOSE THE CORRECT OPTION", RED);
System.out.format("%s", RESET);
System.out.format("%s %s %s : [] ", BLUE, "YOUR OPTION ", BLUE);
option = sc.nextInt();
}
switch (option) {
case 1:
askName();
break;
case 2:
askLastName();
break;
case 3:
askEmail();
break;
case 4:
askFriendShipDate();
break;
case 5:
askTelephoneNumber();
break;
case 6:
askAdress();
break;
case 7:
askSocialAcount();
break;
case 8:
askProfession();
break;
}
}
@Override
public void showContact() {
System.out.println(
"----------------------------------------------------------------------------------");
System.out.println();
System.out.println(String.format(" %sName : %s", BLUE, BLUE)
+ String.format("\t\t\t%s" + getName(), RESET));
System.out.println();
String lastnames = String.join(",", getLastname());
System.out.println(String.format(" %sLast Name : %s", BLUE,
BLUE)
+ String.format("\t\t\t%s" + lastnames, RESET));
System.out.println();
System.out.println(String.format(" %sFriendship since : %s", BLUE,
BLUE)
+ String.format("\t\t%s" + getFriendsSince(), RESET));
System.out.println();
System.out.println(String.format(" %sAddress : %s", BLUE, BLUE)
+ String.format("\t\t\t%s" + getAddress(), RESET));
System.out.println();
String emails = String.join(", ", getEmailAddresses());
System.out.println(String.format(" %sEmail : %s", BLUE, BLUE)
+ String.format("\t\t\t%s" + emails, RESET));
System.out.println();
String telephone_numbers = String.join(", ", getTelelphoneNumbers());
System.out.println(String.format(" %sTelephone Number : %s", BLUE,
BLUE)
+ String.format("\t\t%s" + telephone_numbers, RESET));
System.out.println();
String social_acounts = String.join(", ", getSocialAcounts());
System.out.println(String.format(" %sSocial Acount : %s", BLUE,
BLUE)
+ String.format("\t\t%s" + social_acounts, RESET));
System.out.println();
System.out.println(String.format(" %sProfession : %s", BLUE,
BLUE)
+ String.format("\t\t\t%s" + getProfession(), RESET));
System.out.println();
System.out.println(
"----------------------------------------------------------------------------------\n");
}
}
package Helper;
public abstract class Helper {
// text color
protected final String RED = "\u001B[31m";
protected final String BLACK = "\u001B[30m";
protected final String GREEN = "\u001B[32m";
protected final String BLUE = "\u001B[34m";
protected final String RESET = "\u001B[0m";
protected final String PURPLE = "\u001B[35m";
protected final String CYAN = "\u001B[36m";
protected final String YELLOW = "\u001B[33m";
protected final String WHITE = "\u001B[37m";
protected final String YELLOW_BACKGROUND = "\u001B[43m";
protected final String BLUE_BACKGROUND = "\u001B[44m";
protected final String BLACK_BACKGROUND = "\u001B[40m";
protected final String PURPLE_BACKGROUND = "\u001B[45m";
protected final String CYAN_BACKGROUND = "\u001B[46m";
protected final String GREEN_BACKGROUND = "\u001B[42m";
protected final String WHITE_BACKGROUND = "\u001B[47m";
protected final int ASK_USER_INPUT = 110;
protected final int SHOW_CONTACTS = 1;
protected final int ADD_CONTACTS = 2;
protected final int SEARCH_CONTACT = 3;
protected final int UPDATE_CONTACTS = 4;
protected final int DELETE_CONTACT = 5;
protected final int EXIT_CONSOLE = 6;
// clear console screen
protected void clearConsoleScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
// print in color a given text or change background color
protected void PrintInColor(String text, String color) {
System.out.format("%s %s %s", color, text, color);
System.out.format("%s", RESET);
}
// Print header of our console application
protected void PrintHeader() {
clearConsoleScreen();
PrintInColor(" ==================================================================================\n",
YELLOW);
PrintInColor("WELLCOME TO CONTACT MANAGEMENT SYSTEM \n", GREEN);
PrintInColor("==================================================================================\n",
YELLOW);
PrintInColor("[1]: \tshow contacts\t\t\t\t\n", CYAN);
PrintInColor("[2]: \tadd contact\t\t\t\t\n", CYAN);
PrintInColor("[3]: \tsearch contact\t\t\t\t\n", CYAN);
PrintInColor("[4]: \tupdate a contact\t\t\t\t\n", CYAN);
PrintInColor("[5]: \tdelete a contact\t\t\t\t\n", CYAN);
PrintInColor("[6]: \texit\t\t\t\t\n", CYAN);
PrintInColor("==================================================================================\n",
YELLOW);
}
// print OPTION ERROR
protected void PrintErrorAndReturn() {
PrintInColor(" ----------------------------------------------------------------------------------\n",
YELLOW);
PrintInColor("ERROR: WRONG OPTION ID \n", RED);
PrintInColor("----------------------------------------------------------------------------------\n",
YELLOW);
PrintInColor("[1]: \treturn to main menu\t\t\t\t\n", CYAN);
PrintInColor("[2]: \texit\t\t\t\t\n", CYAN);
PrintInColor("----------------------------------------------------------------------------------\n",
YELLOW);
}
protected void printFooter() {
PrintInColor(" ----------------------------------------------------------------------------------\n",
YELLOW);
PrintInColor("RETURN TO HOME \n", CYAN);
PrintInColor("----------------------------------------------------------------------------------\n",
YELLOW);
PrintInColor("[1]: \treturn to main menu\t\t\t\t\n", CYAN);
PrintInColor("[2]: \texit\t\t\t\t\n", CYAN);
PrintInColor("----------------------------------------------------------------------------------\n",
YELLOW);
}
protected void deleteOption() {
PrintInColor(" ==================================================================================\n",
YELLOW);
PrintInColor("DELETE A CONCTACT\n", CYAN);
PrintInColor("==================================================================================\n",
YELLOW);
}
protected int populateErrorOrReturnHome(int option) {
if (option == 1) {
return ASK_USER_INPUT;
}
if (option == 2) {
System.exit(0);
}
return 0;
}
}
import Application.Application;
public class Main {
public static void main(String[] args) {
new Application().run();
}
}
\ No newline at end of file
package Professional;
import java.util.List;
import java.util.Scanner;
import Contacts.Contacts;
import Family.Family;
public class Professional extends Contacts {
private String Type;
private String ProfessionRelation;
public Professional() {
super();
Type = "Professional";
}
public Professional(String name, List<String> lastname, String address, List<String> telelphoneNumbers,
List<String> emailAddresses, List<String> socialAcounts, String profession, String professionRelation) {
super(name, lastname, address, telelphoneNumbers, emailAddresses, socialAcounts, profession);
setProfessionRelation(professionRelation);
Type = "Professional";
}
@Override
public String getType() {
return Type;
}
@Override
public void setType(String type) {
Type = type;
}
public void setProfessionRelation(String professionRelation) {
ProfessionRelation = professionRelation;
}
public String getProfessionalRelation() {
return this.ProfessionRelation;
}
public void askProfessionalRelation() {
Scanner sc = new Scanner(System.in);
System.out.print(" Enter Your relation with contact owner [Boss, Collegue, Aprentice,collaborator, ...] : ");
String proRelation = sc.nextLine();
while (proRelation.length() < 5) {
System.out
.println(
String.format("\n %sPLEASE INSERT A CORRECT PROFESSIONAL RELATION !%s%s",
RED,
RED,
RESET));
System.out.println();
System.out
.print(" Enter Your relation with contact owner [Boss, Collegue, Aprentice,collaborator, ...] : ");
proRelation = sc.nextLine();
}
setProfessionRelation(proRelation);
}
public Professional askContact() {
clearConsoleScreen();
PrintInColor(" ==================================================================================\n", GREEN);
PrintInColor(String.format("%s\n", "CREATE A CONTACT [Professional]"), BLUE);
PrintInColor("==================================================================================\n", GREEN);
askName();
askLastName();
askEmail();
//
askProfessionalRelation();
askTelephoneNumber();
askAdress();
askSocialAcount();
askProfession();
return this;
}
public void updateContact(){
clearConsoleScreen();
PrintInColor(" ==================================================================================\n", GREEN);
PrintInColor(String.format("%s\n", "UPDATE CONTACT [PROFESSIONAL]"), BLUE);
PrintInColor("==================================================================================\n", GREEN);
System.out.println("CHOOSE WHICH FIELD YOU WANT TO UPDATE");
System.out.println("[1] NAME ");
System.out.println("[2] LASTNAME ");
System.out.println("[3] EMAIL");
System.out.println("[4] PROFESSIONAL RELATION:");
System.out.println("[5] TELEPHONE NUMBER:");
System.out.println("[6] ADDRESS:");
System.out.println("[7] SOCIAL ACOUNT:");
System.out.println("[8] PROFESSION:");
System.out.format("%s %s %s : [] ", BLUE, "YOUR OPTION ", BLUE);
Scanner sc = new Scanner(System.in);
int option = sc.nextInt();
while(option < 1 || option > 8){
System.out.format("%s %s %s\n", RED, "ERREUR: PLEASE CHOOSE THE CORRECT OPTION", RED);
System.out.format("%s", RESET);
System.out.format("%s %s %s : [] ", BLUE, "YOUR OPTION ", BLUE);
option = sc.nextInt();
}
switch (option){
case 1:
askName();
break;
case 2:
askLastName();
break;
case 3:
askEmail();
break;
case 4:
askProfessionalRelation();
break;
case 5:
askTelephoneNumber();
break;
case 6:
askAdress();
break;
case 7:
askSocialAcount();
break;
case 8:
askProfession();
break;
}
}
@Override
public void showContact() {
System.out.println(
"----------------------------------------------------------------------------------");
System.out.println();
System.out.println(String.format(" %sName : %s", BLUE, BLUE)
+ String.format("\t\t\t%s" + getName(), RESET));
System.out.println();
String lastnames = String.join(",", getLastname());
System.out.println(String.format(" %sLast Name : %s", BLUE,
BLUE)
+ String.format("\t\t\t%s" + lastnames, RESET));
System.out.println();
System.out.println(String.format(" %sProfessional Relation : %s", BLUE,
BLUE)
+ String.format("\t\t%s" + getProfessionalRelation(), RESET));
System.out.println();
System.out.println(String.format(" %sAddress : %s", BLUE, BLUE)
+ String.format("\t\t\t%s" + getAddress(), RESET));
System.out.println();
String emails = String.join(", ", getEmailAddresses());
System.out.println(String.format(" %sEmail : %s", BLUE, BLUE)
+ String.format("\t\t\t%s" + emails, RESET));
System.out.println();
String telephone_numbers = String.join(", ", getTelelphoneNumbers());
System.out.println(String.format(" %sTelephone Number : %s", BLUE,
BLUE)
+ String.format("\t\t%s" + telephone_numbers, RESET));
System.out.println();
String social_acounts = String.join(", ", getSocialAcounts());
System.out.println(String.format(" %sSocial Acount : %s", BLUE,
BLUE)
+ String.format("\t\t%s" + social_acounts, RESET));
System.out.println();
System.out.println(String.format(" %sProfession : %s", BLUE,
BLUE)
+ String.format("\t\t\t%s" + getProfession(), RESET));
System.out.println();
System.out.println(
"----------------------------------------------------------------------------------\n");
}
}
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import Application.Application;
import Contacts.Contacts;
import Friends.Friends;
public class MainTest {
@Test
public void insertContactTest() {
Application app = new Application();
String name = "agnon";
List<String> lastName = List.of("kurteshi", "burteshi");
String address = "rue du rhone 4, 1203 Genève";
List<String> telephoneNumber = List.of("+41 77 444 33 22");
List<String> email = List.of("agnon@gmail.com");
List<String> socialAcount = List.of("https://hello.com/?name=agnon");
String profession = "student";
String friendSince = "2022/02/02";
Contacts contact = new Friends(name, lastName, address, email, telephoneNumber, socialAcount, profession,
friendSince);
app.addToContactList(contact);
assertEquals(app.getContactList().size(), 1);
assertEquals(app.getContactList().get(0).getName(), name);
assertEquals(app.getContactList().get(0).getType(), contact.getType());
System.out.println("INSERTION TEST PASSED");
}
@Test
public void DeleteContactTest() {
Application app = new Application();
String name = "agnon";
List<String> lastName = List.of("kurteshi", "burteshi");
String address = "rue du rhone 4, 1203 Genève";
List<String> telephoneNumber = List.of("+41 77 444 33 22");
List<String> email = List.of("agnon@gmail.com");
List<String> socialAcount = List.of("https://hello.com/?name=agnon");
String profession = "student";
String friendSince = "2022/02/02";
Contacts contact = new Friends(name, lastName, address, email, telephoneNumber, socialAcount, profession,
friendSince);
app.addToContactList(contact);
app.delete(1);
// after delete size should be 0
assertEquals(app.getContactList().size(), 0);
System.out.println("DELETION TEST PASSED");
}
@Test
public void sort() {
Application app = new Application();
String name = "agnon";
List<String> lastName = List.of("kurteshi", "burteshi");
String address = "rue du rhone 4, 1203 Genève";
List<String> telephoneNumber = List.of("+41 77 444 33 22");
List<String> email = List.of("agnon@gmail.com");
List<String> socialAcount = List.of("https://hello.com/?name=agnon");
String profession = "student";
String friendSince = "2022/02/02";
Contacts contact1 = new Friends(name, lastName, address, email, telephoneNumber, socialAcount, profession,
friendSince);
String name2 = "AAAAAA";
List<String> lastName2 = List.of("BBBBBBBBBBBBBBB", "BOURRRRRIS");
Contacts contact2 = new Friends(name2, lastName2, address, email, telephoneNumber, socialAcount, profession,
friendSince);
app.addToContactList(contact1);
app.addToContactList(contact2);
// if sort is correct then "AAAAAA" should be at index 0
assertEquals(app.getContactList().get(0).getName(), name2);
System.out.println("SORT TEST PASSED");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment