在此我尝试了Model View Control体系结构中的注册表单。我无法将用户数据存储在另一个类中。因为注册字段必须存储在注册数据库类中,而联系信息则必须存储在联系信息类中。
那我应该如何构造Main方法
public class RegistrationData {
private String username;
private String password;
private Contact contact;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public RegistrationData(String username, String password, Contact contact) {
this.username = username;
this.password = password;
this.contact = contact;
}
}
public class Contact {
private String name;
private String phoneNumber;
private String emailId;
public Contact(String name, String phoneNumber, String emailId) {
this.name = name;
this.phoneNumber = phoneNumber;
this.emailId = emailId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
public class Registration {
Scanner scanner = new Scanner(System.in);
void Registration(RegistrationData registrationData,Contact contact){
System.out.println("Username : ");registrationData.setUsername(scanner.next());
System.out.println("Password : ");registrationData.setPassword(scanner.next());
System.out.println("Name : ");contact.setName(scanner.next());
System.out.println("PhoneNumber : ");contact.setPhoneNumber(scanner.next());
System.out.println("Email id :");contact.setEmailId(scanner.next());
}
}
//Data Base Class
import java.util.ArrayList;
import java.util.List;
public class RegistrationDatabase {
List<RegistrationData> registrationData = new ArrayList<>();
public void storeData(RegistrationData registrationData){
this.registrationData.add(registrationData);
System.out.println(this.registrationData);
}
}
import java.util.LinkedList;
import java.util.List;
public class ContactDatabase {
List<Contact> contacts = new LinkedList<>();
public void addContact(Contact contact){
this.contacts.add(contact);
}
}
答案 0 :(得分:0)
纠正我,如果我在解释您的问题时错了
假设1: 您希望将联系信息类和RegistrationInformation类中的注册数据存储到RegistrationInformation表中
解决方案1: 正如我所看到的,Contact和注册之间存在一对一的关系,因此您可以在ContactInformation类中添加一个关联,也可以在RegisterInformation类中添加关联
public class Contact {
//one to one relation
private RegistrationData registrationData;
.
.
.
.
并使用它来存储来自联系人的注册
假设2: 您拥有用于联系和注册的单独数据库
解决方案2:
在那种情况下,为注册表单创建一个POJO类(与RegistrationData相同,但是将用于从视图中捕获数据),然后创建两个类RegistrationData和Contact类,然后使用POJO来检索和存储这两个信息数据库。