Cześć, w ramach nauki robię sobie program, który wpisuje do bazy danych kontakty. Składa się z trzech encji: "Contact", "Email" i "Group". Do kontaktu chcę przypisać jeden email i wiele grup. Również do każdej grupy chcę przypisać wiele kontaktów. Jest to aplikacja konsolowa. Zaciąłem się podczas pisania metody dzięki której będę mógł dodać nowy kontakt wraz z grupami do których należy. Po dodaniu pierwszej grupy, chcę wyświetlić pytanie, czy chcę dodać koleją grupę i na podstawie wyporu utworzyć nowy obiekt grupy i dopisać go do listy w encji "Group". Tutaj pojawia się problem, bo nie wiem jak utworzyć nowy obiekt przypisany do zmiennej, której nazwa zmieni się za każdą iteracją (przypuszczam, że tak powinienem to zrobić). Być może powinienem zastosować w tym przypadku jakiś wzorzec projektowy, jeśli tak, to poproszę o podpowiedź.
Kod programu:
encja "Contact"
package domain;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "contacts")
public class Contact {
@Id
@GeneratedValue
private int contactId;
private String firstName;
private String lastName;
@OneToOne(targetEntity = Email.class)
private Email email;
@ManyToMany(targetEntity = Group.class)
private List<Group> groups = new ArrayList<Group>();
// Constructor
public Contact(){}
public Contact(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return "Contact{" +
"contactId=" + contactId +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email=" + email +
", groups=" + groups +
'}';
}
// Getters and Setters
public int getContactId() {
return contactId;
}
public void setContactId(int contactId) {
this.contactId = contactId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Email getEmail() {
return email;
}
public void setEmail(Email email) {
this.email = email;
}
public List<Group> getGroups() {
return groups;
}
public void setGroups(List<Group> groups) {
this.groups = groups;
}
}
encja "Email"
package domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "emails")
public class Email {
@Id
@GeneratedValue
private int emailId;
private String email;
// Constructor
public Email(String email) {
this.email = email;
}
// Getters and Setters
public int getEmailId() {
return emailId;
}
public void setEmailId(int emailId) {
this.emailId = emailId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
encja "Group"
package domain;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "groups")
public class Group {
@Id
@GeneratedValue
private int groupId;
private String groupName;
@ManyToMany
private List<Contact> contacts = new ArrayList<Contact>();
// Constructors
public Group(){}
public Group(String groupName) {
this.groupName = groupName;
}
@Override
public String toString() {
return "Group{" +
"groupId=" + groupId +
", groupName='" + groupName + '\'' +
", contacts=" + contacts +
'}';
}
// Getters and Setters
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
}
metoda dodająca nowy kontakt:
package controller;
import domain.Contact;
import domain.ContactRepository;
import domain.Email;
import domain.Group;
import java.util.Scanner;
public class Controller {
Scanner scanner = new Scanner(System.in);
public void addNewContact(){
System.out.println("Add New Contact");
System.out.println("===============");
System.out.println("");
System.out.println("First Name: ");
String firstName = scanner.next();
System.out.println("Last Name: ");
String lastName = scanner.next();
Contact contact = new Contact(firstName, lastName);
System.out.println("Email: ");
String email_ = scanner.next();
Email email = new Email(email_);
contact.setEmail(email);
boolean groupRun = true;
do{
System.out.println("Group name: ");
String groupName = scanner.next();
Group group = new Group(groupName);
contact.getGroups().add(group);
group.getContacts().add(contact);
System.out.println("Would you like to add another group? yes: 1 no: 2");
int choice = scanner.nextInt();
switch (choice){
case 1:
break;
case 2:
groupRun = false;
break;
}
}while(groupRun);
}
}