CODE is here
class PartnerOfSomeoneWhoNeedOnePartner {
private final String nameOfTheOneWhoNeedOnePartner;
private final String nameOfThePartner;
private static PartnerOfSomeoneWhoNeedOnePartner partner;
private PartnerOfSomeoneWhoNeedOnePartner(String nameOfTheOneWhoNeedOnePartner, String nameOfThePartner) {
this.nameOfTheOneWhoNeedOnePartner = nameOfTheOneWhoNeedOnePartner;
this.nameOfThePartner = nameOfThePartner;
}
public static PartnerOfSomeoneWhoNeedOnePartner helpSomeOneWhoNeedOneParter(String nameOfTheOneWhoNeedOnePartner) {
if (nameOfTheOneWhoNeedOnePartner == null) {
return null;
}
if (partner == null || partner.nameOfTheOneWhoNeedOnePartner.equals(nameOfTheOneWhoNeedOnePartner)) {
partner = new PartnerOfSomeoneWhoNeedOnePartner(nameOfTheOneWhoNeedOnePartner, null);
} else if (partner.nameOfThePartner == null) {
partner = new PartnerOfSomeoneWhoNeedOnePartner(nameOfTheOneWhoNeedOnePartner, partner.nameOfTheOneWhoNeedOnePartner);
} else {
partner = null;
}
return partner;
}
@Override
public String toString() {
if (nameOfThePartner == null) {
return nameOfTheOneWhoNeedOnePartner + " is alone.";
}
return nameOfThePartner + " and " + nameOfTheOneWhoNeedOnePartner + " is not alone.";
}
public static void main(String[] args) {
PartnerOfSomeoneWhoNeedOnePartner luRenJia = PartnerOfSomeoneWhoNeedOnePartner.helpSomeOneWhoNeedOneParter("路人甲");
System.out.println(luRenJia);
PartnerOfSomeoneWhoNeedOnePartner luRenYi = PartnerOfSomeoneWhoNeedOnePartner.helpSomeOneWhoNeedOneParter("路人乙");
System.out.println(luRenYi);
}
}