首先:确定代码雏形框架(原始代码)
先根据以往经验写出一般的电话簿管理系统,先把代码雏形框架确定好。
原始代码运行结果:
原始代码源码:
package bidirectionalDome;
import java.util.ArrayList;
public class BidirectionalDome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Contact Contact1 = new Contact(007,"幸存者letp",true);
Phone Phone1 = new Phone(11,"home",Contact1);
Phone Phone2 = new Phone(12,"person",Contact1);
Phone Phone3 = new Phone(13,"company",Contact1);
ArrayList<Phone> phones = Contact1.getPhoneArray();//获取电话簿
String pername = Contact1.getName();
for(int i = 0;i<phones.size();i++) {
System.out.println(phones.get(i).getPhoneNom()+phones.get(i).getType());
}
System.out.println("电话号码主人为:"+Contact1.getName());
}
}
class Contact{
private int contectID;
private String name;
private boolean isFroqContect;
private ArrayList<Phone> phoneArray;//实现多对一
//带参构造
public Contact(int contectID,String name,boolean isFroqContect) {
this.contectID = contectID;
this.name = name;
this.isFroqContect = isFroqContect;
//定义一个电话簿数组—大小为3
phoneArray = new ArrayList<Phone>(3);
}
//标准方法
public ArrayList<Phone> getPhoneArray() {
return phoneArray;
}
public void setPhoneArray(ArrayList<Phone> phoneArray) {
this.phoneArray = phoneArray;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//将电话号码添加到phoneArray数组中——实现contact与电话号码一对多形式
public void addPhoneToContact(Phone aPhone) {
phoneArray.add(aPhone);
}
}
class Phone{
private int phoneNom;
private String type;
private Contact aContact;//实现一对一
//带参构造
public Phone(int phoneNom,String type,Contact aContact){
this.phoneNom = phoneNom;
this.type = type;
this.aContact = aContact;
aContact.addPhoneToContact(this);
}
//标准方法
public int getPhoneNom() {
return phoneNom;
}
public void setPhoneNom(int phoneNom) {
this.phoneNom = phoneNom;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
其次:按题目要求进行修改(终极代码)
【问题描述】编程实现如图所示的类间关联关系(双向关联)
【输入形式】
输入分4行,
第1行:联系人ID 联系人姓名
第2行:第一个联系电话号码 联系电话类型
第3行:第二个联系电话号码 联系电话类型
第4行:第三个联系电话号码 联系电话类型
【输出形式】
见样例输出
【样例输入】
123 xiaoyu
87542222 office
87558755 home
1397890678 mobile
【样例输出】
ContactName:xiaoyu PhoneNumber:87542222 type:office
ContactName:xiaoyu PhoneNumber:87558755 type:home
ContactName:xiaoyu PhoneNumber:1397890678 type:mobile
Contact of the second phone:xiaoyu
终极代码运行结果:
终极代码源码:
package bidirectionalDome;
import java.util.ArrayList;
import java.util.Scanner;
public class BidirectionalDome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
//输入电话主人基本信息
int ID = in.nextInt();
String name = in.next();
boolean isFroq = in.hasNext();
Contact aContact = new Contact(ID,name,isFroq);
//输入电话号码
for(int i = 0;i<3;i++) {
String phoneNom = in.next();
String type = in.next();
new Phone(phoneNom,type,aContact);//用到类间关联
//Phone aPhone = new Phone(phoneNom,type,aContact);
//aContact.addPhoneToContact(aPhone);
}
ArrayList<Phone> phones = aContact.getPhoneArray();//获取电话簿
String pername = aContact.getName();
for(int i = 0;i<phones.size();i++) {
System.out.println("ContactName:"+pername+" PhoneNumber:"+phones.get(i).getPhoneNom()+" type:"+phones.get(i).getType());
}
System.out.println("Contact of the second phone:"+pername);
//System.out.println(aContact.isFroqContect());//验证isFroqContect()默认值为true
}
}
class Contact{
private int contectID;
private String name;
private boolean isFroqContect;//默认为true
private ArrayList<Phone> phoneArray;//实现多对一
//带参构造
public Contact(int contectID,String name,boolean isFroqContect) {
this.contectID = contectID;
this.name = name;
this.isFroqContect = isFroqContect;
//定义一个电话簿数组—大小为3
phoneArray = new ArrayList<Phone>(3);
}
//标准方法
public ArrayList<Phone> getPhoneArray() {
return phoneArray;
}
public void setPhoneArray(ArrayList<Phone> phoneArray) {
this.phoneArray = phoneArray;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFroqContect() {
return isFroqContect;
}
public void setFroqContect(boolean isFroqContect) {
this.isFroqContect = isFroqContect;
}
//将电话号码添加到phoneArray数组中——实现contact与电话号码一对多形式
public void addPhoneToContact(Phone aPhone) {
phoneArray.add(aPhone);
}
}
class Phone{
private String phoneNom;
private String type;
private Contact aContact;//实现一对一
//带参构造
public Phone(String phoneNom,String type,Contact aContact){
this.phoneNom = phoneNom;
this.type = type;
this.aContact = aContact;
aContact.addPhoneToContact(this); //实现一对多——作用等同于void addPhoneToContact(Phone aPhone)
}
//标准方法
public String getPhoneNom() {
return phoneNom;
}
public void setPhoneNom(String phoneNom) {
this.phoneNom = phoneNom;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Contact getaContact() {
return aContact;
}
public void setaContact(Contact aContact) {
this.aContact = aContact;
}
}
END:放眼远方,聚焦当下。