diff --git a/tp/src/main/java/com/example/tpfx/Contact.java b/tp/src/main/java/com/example/tpfx/Contact.java index 19dbfc6d468189c589314b0ddf1edc9a032843a0..f3fa9e5e81d8454b1c1e7f1aa0f0d924c74faa60 100644 --- a/tp/src/main/java/com/example/tpfx/Contact.java +++ b/tp/src/main/java/com/example/tpfx/Contact.java @@ -28,10 +28,10 @@ public class Contact implements Comparable<Contact>{ } //Getter - public String get_type() { return type;} - public String getLast_name() { return last_name;} + public String getType() { return type;} + public String getLastName() { return last_name;} - public List<String> getFirst_name() { return first_name; } + public List<String> getFirstName() { return first_name; } public String getAddr() { return addr; } @@ -57,9 +57,9 @@ public class Contact implements Comparable<Contact>{ //Test //Setter - public void set_type(String type){this.type = type;} - public void setFirst_name(List<String> first_name){this.first_name = first_name;} - public void setLast_name(String last_name){this.last_name = last_name;} + public void setType(String type){this.type = type;} + public void setFirstName(List<String> first_name){this.first_name = first_name;} + public void setLastName(String last_name){this.last_name = last_name;} public void setAddr(String addr) { this.addr = addr; } @@ -85,26 +85,26 @@ public class Contact implements Comparable<Contact>{ //Methodes //Type cannot be edited - public void EditFirstName(List<String> newFirstName){ - this.first_name = newFirstName; + public void editFirstName(List<String> new_first_name){ + this.first_name = new_first_name; } - public void EditLastName(String newLastName){ - this.last_name = newLastName; + public void editLastName(String new_last_name){ + this.last_name = new_last_name; } - public void EditAddress(String newAddrr){ - this.addr = newAddrr; + public void editAddress(String new_addrr){ + this.addr = new_addrr; } - public void EditPhone(List<String> newPhone){ - this.phone = newPhone; + public void editPhone(List<String> new_phone){ + this.phone = new_phone; } - public void EditEmail(List<String> newEmail){ - this.email = newEmail; + public void editEmail(List<String> new_email){ + this.email = new_email; } - public void EditSocial(List<String> newSocial){ - this.social = newSocial; + public void editSocial(List<String> new_social){ + this.social = new_social; } - public void EditJob(String newJob){ - this.job = newJob; + public void editJob(String new_job){ + this.job = new_job; } @Override public String toString(){ diff --git a/tp/src/main/java/com/example/tpfx/Family.java b/tp/src/main/java/com/example/tpfx/Family.java index 0242b5543bd15bd7336d4d205a4f0f07ce7ec662..4f64069667f292bc44e8923580c5eef3b1aadc96 100644 --- a/tp/src/main/java/com/example/tpfx/Family.java +++ b/tp/src/main/java/com/example/tpfx/Family.java @@ -12,8 +12,8 @@ public class Family extends Contact{ return relation; } - public void EditRelation(String newRelation){ - this.relation = newRelation; + public void editRelation(String new_relation){ + this.relation = new_relation; } @Override @@ -36,7 +36,7 @@ public class Family extends Contact{ w_social += elem + " "; } - return String.valueOf(id) + " - " + w_names + "- " + this.last_name + " - " + this.addr + " - " + w_phone + return id + " - " + w_names + "- " + this.last_name + " - " + this.addr + " - " + w_phone + "- " + w_email + "- " + w_social + "- " + this.job + " - " + this.relation; } } diff --git a/tp/src/main/java/com/example/tpfx/HelloController.java b/tp/src/main/java/com/example/tpfx/HelloController.java index 92266597be694be8e66edf02befe6f3b4572612d..341d18ac1b3607c6afce4d08ecbf533d0848750e 100644 --- a/tp/src/main/java/com/example/tpfx/HelloController.java +++ b/tp/src/main/java/com/example/tpfx/HelloController.java @@ -14,8 +14,9 @@ import java.util.regex.Pattern; public class HelloController { static ArrayList<Contact> contactArray = new ArrayList<Contact>(); int selectedID = 0; - boolean imported = false; + boolean imported = false; //Used to know whenever we should enable or disable button save/edit ObservableList<String> ctType = FXCollections.observableArrayList("Friend", "Family", "Professional"); + //Fx fields @FXML private TextField txtFirst; @FXML @@ -54,6 +55,10 @@ public class HelloController { private ListView LstCt; @FXML private ComboBox cbxType; + + //Fx functions + //Form start Up - base contact type is friend + //Hide unwanted fields @FXML protected void initialize() { cbxType.setValue("Friend"); @@ -64,9 +69,10 @@ public class HelloController { btnDelete.setDisable(true); btnAdd.setDisable(true); } + //Add button click @FXML protected void AddCt() { - switch (cbxType.getValue().toString()){ + switch (cbxType.getValue().toString()){ //Use the combo box value case "Friend": AddPal(); break; @@ -78,6 +84,7 @@ public class HelloController { break; } } + //Relation ComboBox Text Value edit @FXML protected void ChangeType(){ switch (cbxType.getValue().toString()){ @@ -92,11 +99,12 @@ public class HelloController { break; case "Professional": lblOther.setVisible(true); - lblOther.setText("Job"); + lblOther.setText("Work"); txtOther.setVisible(true); break; } } + //Btn Load Click @FXML protected void ImportCt() { LstCt.getItems().clear(); @@ -108,7 +116,7 @@ public class HelloController { while((line = reader.readLine()) != null){ switch (line){ case "Friend": - checker = 1; + checker = 1; //checker is used to know which type of contact it is to it to the list break; case "Family": checker = 2; @@ -128,13 +136,14 @@ public class HelloController { e.printStackTrace(); } } + //Btn Save CLick @FXML protected void ExportCt() { try{ BufferedWriter writer = new BufferedWriter(new FileWriter("contact_save.csv")); for (Contact c: contactArray){ List<String> str_array = SplitLine(c.toString(), " - "); - writer.write(c.get_type() + "\n"); + writer.write(c.getType() + "\n"); //First line is contact type (for easier usage during import) writer.write(str_array.get(0) + "/"); writer.write(str_array.get(1) + "/"); writer.write(str_array.get(2) + "/"); @@ -143,7 +152,7 @@ public class HelloController { writer.write(str_array.get(5) + "/"); writer.write(str_array.get(6) + "/"); writer.write(str_array.get(7)); - if(c.get_type().equals("Family") || c.get_type().equals("Professional")){ + if(c instanceof Family || c instanceof Professional){ writer.write("/" + str_array.get(8) + "\n"); } else{ writer.write("/" + "\n"); @@ -154,12 +163,13 @@ public class HelloController { e.printStackTrace(); } } + //Btn Search Click @FXML protected void Search() { Pattern pat = Pattern.compile(txtSearch.getText()); LstCt.getItems().clear(); for(Contact c: contactArray){ - for(String n: c.getFirst_name()){ + for(String n: c.getFirstName()){ Matcher match = pat.matcher(n); if(match.find()){ LstCt.getItems().add(c); @@ -167,6 +177,7 @@ public class HelloController { } } } + //Select Line in ListView @FXML protected void Selected() { lblOther.setVisible(true); @@ -187,14 +198,14 @@ public class HelloController { if(data.size() > 8){ txtOther.setVisible(true); lblOther.setVisible(true); - txtOther.setText(data.get(7)); - if(contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).get_type().equals("Family")){ + txtOther.setText(data.get(8)); + if(contactArray.get(LstCt.getSelectionModel().getSelectedIndex()) instanceof Family){ //contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).EditRelation(other); lblOther.setVisible(true); lblOther.setText("Relation"); txtOther.setVisible(true); cbxType.setValue("Family"); - } else if(contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).get_type().equals("Professional")){ + } else if(contactArray.get(LstCt.getSelectionModel().getSelectedIndex()) instanceof Professional){ lblOther.setVisible(true); lblOther.setText("Work"); txtOther.setVisible(true); @@ -209,11 +220,13 @@ public class HelloController { imported = true; TestFields(); } + //Btn Delete Click @FXML protected void Delete() { contactArray.remove(selectedID); ShowAll(); } + //Btn edit Click @FXML protected void Edit() { List<String> firstName = SplitLine(txtFirst.getText(), " "); @@ -225,41 +238,48 @@ public class HelloController { String job = txtJob.getText(); String other = txtOther.getText(); - contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).EditFirstName(firstName); - contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).EditLastName(lastName); - contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).EditAddress(address); - contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).EditPhone(phone); - contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).EditEmail(emails); - contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).EditSocial(social); - contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).EditJob(job); - if(contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).get_type().equals("Family")){ - //contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).EditRelation(other); - } else if(contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).get_type().equals("Professional")){ + contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).editFirstName(firstName); + contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).editLastName(lastName); + contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).editAddress(address); + contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).editPhone(phone); + contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).editEmail(emails); + contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).editSocial(social); + contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).editJob(job); + if(contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).getType().equals("Family")){ + //contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).editRelation(other); + Family fam = (Family)contactArray.get(LstCt.getSelectionModel().getSelectedIndex()); + } else if(contactArray.get(LstCt.getSelectionModel().getSelectedIndex()).getType().equals("Professional")){ } ShowAll(); } + //edit text in TextBox for First Name, Last Name and Email @FXML public void TestFields(){ - Pattern pat = Pattern.compile(txtEmail.getText()); - Matcher match = pat.matcher("^(.+)@(.+)$*"); + Pattern pat = Pattern.compile("^(.+)@(.+)\\.(.+)$", Pattern.CASE_INSENSITIVE); + Matcher match = pat.matcher(txtEmail.getText()); if(txtFirst.getText() != "" && txtLast.getText() != "" && match.find()){ - if(imported){ + if(imported){ //Selected from list, can both save and edit btnEdit.setDisable(false); - btnAdd.setDisable(false); - } else{ + btnAdd.setDisable(true); + } else{ //New contact, can't edit btnEdit.setDisable(true); btnAdd.setDisable(false); } - } else { + } else { //No contact, can't save nor edit btnEdit.setDisable(true); btnAdd.setDisable(true); } } + //Btn All Click + //Also used for form reset @FXML protected void ShowAll() { int cpt = 0; + imported = false; + btnEdit.setDisable(true); + btnAdd.setDisable(true); for(Contact cont: contactArray){ cont.setId(cpt); cpt++; @@ -277,8 +297,8 @@ public class HelloController { txtJob.setText(""); txtOther.setText(""); cbxType.setDisable(false); - imported = false; } + //Add a friend public void AddPal(){ String firstName = txtFirst.getText(); String lastName = txtLast.getText(); @@ -296,6 +316,7 @@ public class HelloController { Friend pal = new Friend(first_name_list, lastName, address, phoneList, emailList, socialList, job); Add(pal); } + //Add a family member public void AddFam(){ String firstName = txtFirst.getText(); String lastName = txtLast.getText(); @@ -314,6 +335,7 @@ public class HelloController { Family fam = new Family(first_name_list, lastName, address, phoneList, emailList, socialList, job, relation); Add(fam); } + //Add a professional public void AddPro(){ String firstName = txtFirst.getText(); String lastName = txtLast.getText(); @@ -332,52 +354,57 @@ public class HelloController { Professional pro = new Professional(first_name_list, lastName, address, phoneList, emailList, socialList, job, wp); Add(pro); } + //Add a contact public void Add(Contact c){ contactArray.add(c); Collections.sort(contactArray); ShowAll(); } + //Import a friend public void ImportFriend(List<String> str_array){ Friend f = new Friend( - SplitArray(str_array.get(1)), + SplitLine(str_array.get(1), " "), str_array.get(2), str_array.get(3), - SplitArray(str_array.get(4)), - SplitArray(str_array.get(5)), - SplitArray(str_array.get(6)), + SplitLine(str_array.get(4), " "), + SplitLine(str_array.get(5), " "), + SplitLine(str_array.get(6), " "), str_array.get(7) ); f.setId(Integer.parseInt(str_array.get(0))); Add(f); } + //Import a family member public void ImportFamily(List<String> str_array){ Family f = new Family( - SplitArray(str_array.get(1)), + SplitLine(str_array.get(1), " "), str_array.get(2), str_array.get(3), - SplitArray(str_array.get(4)), - SplitArray(str_array.get(5)), - SplitArray(str_array.get(6)), + SplitLine(str_array.get(4), " "), + SplitLine(str_array.get(5), " "), + SplitLine(str_array.get(6), " "), str_array.get(7), str_array.get(8) ); f.setId(Integer.parseInt(str_array.get(0))); Add(f); } + //Import a Professional public void ImportProfessional(List<String> str_array){ Professional f = new Professional( - SplitArray(str_array.get(1)), + SplitLine(str_array.get(1), " "), str_array.get(2), str_array.get(3), - SplitArray(str_array.get(4)), - SplitArray(str_array.get(5)), - SplitArray(str_array.get(6)), + SplitLine(str_array.get(4), " "), + SplitLine(str_array.get(5), " "), + SplitLine(str_array.get(6), " "), str_array.get(7), str_array.get(8) ); f.setId(Integer.parseInt(str_array.get(0))); Add(f); } + //Add a contact (any type) to the list public void AddContactToList(List<String> str_array, int checker){ switch (checker){ case 1: @@ -394,29 +421,22 @@ public class HelloController { break; } } + //Split a String into String array + //@param str: list to split, splitter: string used as separator public List<String> SplitLine(String str, String splitter){ List<String> data = Arrays.asList(str.split(splitter)); return data; } - public List<String> SplitArray(String str){ - String new_str = ""; - for(char c: str.toCharArray()){ - if(c !='[' && c !=']'){ - new_str += c; - } - } - List<String> names = Arrays.asList(new_str.split(" ")); - return names; - } //TODO /* - Add comments + Unit Test, + edit relation/job? + Test contact exist - Unit Test - Edit relation/job? + Label warning */ } \ No newline at end of file diff --git a/tp/src/main/java/com/example/tpfx/Professional.java b/tp/src/main/java/com/example/tpfx/Professional.java index ba5c4d07e2ece4b4f78ef3d16c4cfeefc9912521..ca51384ac985925442d1672c5a03198a3733bc1e 100644 --- a/tp/src/main/java/com/example/tpfx/Professional.java +++ b/tp/src/main/java/com/example/tpfx/Professional.java @@ -7,12 +7,12 @@ public class Professional extends Contact{ super("Professional", first_name, last_name, addr, phone, email, social, job); this.work_place = work_place; } - public String getWork_place() { + public String getWorkPlace() { return work_place; } - public void EditWork(String newWork){ - this.work_place = newWork; + public void editWork(String new_work){ + this.work_place = new_work; } @Override public String toString(){ @@ -34,7 +34,7 @@ public class Professional extends Contact{ w_social += elem + " "; } - return String.valueOf(id) + " - " + w_names + "- " + last_name + " - " + this.addr + " - " + w_phone + "- " + return id + " - " + w_names + "- " + last_name + " - " + this.addr + " - " + w_phone + "- " + w_email + "- " + w_social + "- " + this.job + " - " + this.work_place; } } diff --git a/tp/src/main/resources/com/example/tpfx/hello-view.fxml b/tp/src/main/resources/com/example/tpfx/hello-view.fxml index d1a72c2c8503758fdc1c384688ea6e32d826a020..90c6bd971c67745d524370da47dd468d16a668de 100644 --- a/tp/src/main/resources/com/example/tpfx/hello-view.fxml +++ b/tp/src/main/resources/com/example/tpfx/hello-view.fxml @@ -46,22 +46,22 @@ <children> <ListView fx:id="LstCt" layoutX="427.0" layoutY="38.0" onMouseClicked="#Selected" prefHeight="489.0" prefWidth="412.0" /> <Button fx:id="btnAdd" layoutX="229.0" layoutY="51.0" mnemonicParsing="false" onAction="#AddCt" prefHeight="24.0" prefWidth="62.0" text="Add" /> - <TextField fx:id="txtFirst" layoutX="32.0" layoutY="52.0" onInputMethodTextChanged="#TestFields" onKeyReleased="#TestFields" /> + <TextField fx:id="txtFirst" layoutX="32.0" layoutY="52.0" onAction="#TestFields" onInputMethodTextChanged="#TestFields" onKeyPressed="#TestFields" onKeyReleased="#TestFields" onKeyTyped="#TestFields" /> <Label layoutX="230.0" layoutY="300.0" text="First Name" /> - <TextField fx:id="txtLast" layoutX="32.0" layoutY="107.0" onInputMethodTextChanged="#TestFields" onKeyReleased="#TestFields" /> + <TextField fx:id="txtLast" layoutX="32.0" layoutY="107.0" onAction="#TestFields" onInputMethodTextChanged="#TestFields" onKeyPressed="#TestFields" onKeyReleased="#TestFields" onKeyTyped="#TestFields" /> <Label layoutX="32.0" layoutY="85.0" text="Last Name" /> <TextField fx:id="txtAdd" layoutX="32.0" layoutY="162.0" /> <Label layoutX="32.0" layoutY="140.0" text="Address" /> <TextField fx:id="txtPhone" layoutX="32.0" layoutY="216.0" /> <Label layoutX="32.0" layoutY="194.0" text="Phones" /> - <TextField fx:id="txtEmail" layoutX="32.0" layoutY="270.0" onInputMethodTextChanged="#TestFields" onKeyReleased="#TestFields" /> + <TextField fx:id="txtEmail" layoutX="32.0" layoutY="270.0" onAction="#TestFields" onInputMethodTextChanged="#TestFields" onKeyPressed="#TestFields" onKeyReleased="#TestFields" onKeyTyped="#TestFields" /> <Label layoutX="32.0" layoutY="248.0" text="Emails" /> <TextField fx:id="txtSocial" layoutX="32.0" layoutY="322.0" /> <Label layoutX="32.0" layoutY="300.0" text="Socials" /> <TextField fx:id="txtJob" layoutX="32.0" layoutY="374.0" /> <Label layoutX="32.0" layoutY="352.0" text="Job" /> <TextField fx:id="txtOther" layoutX="32.0" layoutY="491.0" /> - <Label fx:id="lblOther" layoutX="32.0" layoutY="467.0" prefHeight="16.0" prefWidth="78.0" text="Depends" /> + <Label fx:id="lblOther" layoutX="32.0" layoutY="467.0" prefHeight="16.0" prefWidth="171.0" text="Depends" /> <Label layoutX="37.0" layoutY="412.0" text="Type" /> <Button fx:id="btnSave" layoutX="229.0" layoutY="88.0" mnemonicParsing="false" onAction="#ExportCt" prefHeight="24.0" prefWidth="62.0" text="Save" /> <Button fx:id="btnLoad" layoutX="229.0" layoutY="127.0" mnemonicParsing="false" onAction="#ImportCt" prefHeight="24.0" prefWidth="62.0" text="Load" />