由于某种原因,我的getName()
方法和addMemberList()
无法正常工作。
我已经尝试过和一位朋友一起进行所有的解析和调试,但是似乎没有任何效果。
主要
public static void main(String[] args) throws FileNotFoundException {
initialUserGuide();
Scanner sc = new Scanner(new File("familytree.txt"));
Family fr = new Family(sc);
fr.displayAllMembersList();
processUserInput(fr);
}
static void initialUserGuide(){
System.out.println("The input file consists of names of all Humans");
System.out.println("Enter the Human name from below list, to display his/her maternal line, Paternal line and children");
}
static void processUserInput(Family fr)
{
Scanner input = new Scanner(System.in);
Person person;
System.out.println("newt Human ( enter told " + "quit)?");
String nextLine = input.nextLine();
while(nextLine.length() > 0){
person = fr.find(nextLine);
if(person == null)
System.out.println("NoMatch Occured");
else
{
displayMaternalLine(person);
displayPaternalLine(person);
displayChildren(person);
}
System.out.println("");
System.out.println("next Human (enter to quit)?");
nextLine = input.nextLine();
}
}
static void displayMaternalLine(Person mother)
{
System.out.println("Maternal Line");
int i = 0;
int count = 1;
while(mother != null)
{
for(i = 0;i < count; i++){
System.out.println("");
}
System.out.println(mother.getName());
mother = mother.getMother();
count++;
}
}
static void displayPaternalLine(Person father)
{
System.out.println("Paternal Line");
int i = 0;
int count = 1;
while(father != null)
{
for(i = 0;i < count; i++){
System.out.println("");
}
System.out.println(father.getName());
father = father.getMother();
count++;
}
}
static void displayChildren(Person person)
{
System.out.println("Children");
ArrayList ptr = person.getChildren();
if(ptr.size()==0)
{
System.out.println(" none");
}
for(int i = 0; i < ptr.size(); i++)
{
System.out.println("\t" + ptr.get(i).getName());
}
}
}
家庭班
package assignment7;
import java.util.ArrayList;
import java.util.Scanner;
public class Family{
private ArrayList allMembersList;
public Family(Scanner sc){
allMembersList = new ArrayList();
addAllMembersList(sc);
makeParentChildRelation(sc);
}
Person find(String name)
{
for(int i = 0; i < allMembersList.size();i++)
{
if(allMembersList.get(i).getName().equalsIgnoreCase(name))
return (Person) allMembersList.get(i);
}
return null;
}
public void addAllMembersList(Scanner sc){
String line = sc.nextLine();
while(line.length() != 0)
{
Person person = new Person(line);
addMembersList.add(person);
line = sc.nextLine();
}
}
public void makeParentChildRelation(Scanner sc)
{
String nextLine;
String name,motherName,fatherName;
while(sc.hasNextLine())
{
name = sc.nextLine();
motherName = sc.nextLine();
fatherName = sc.nextLine();
Person personn = find(name);
if(!motherName.equals("unknown"))
{
Person mother = find(motherName);
personn.setMother(mother);
mother.addChildren(personn);
}
if(!fatherName.equals("unknown"))
{
Person father = find(fatherName);
personn.setFather(father);
father.addChildren(personn);
}
}
}
public void displayAllMembersList()
{
for(int i = 0; i < allMembersList.size(); i++)
{
Person person = (Person) allMembersList.get(i);
System.out.println(person.getName());
}
}
}
人员班
package assignment7;
import java.util.ArrayList;
public class Person {
private String name;
private Person mother;
private Person father;
private ArrayList children;
public Person(String name)
{
this.name = name;
children = new ArrayList();
}
public void setName(String name)
{
this.name = name;
}
public void setFather(Person Father){
this.father = Father;
}
public void setMother(Person Mother){
this.mother = Mother;
}
public void addChildren(Person Child)
{
children.add(Child);
}
public String getName()
{
return this.name;
}
public Person getFather()
{
return this.father;
}
public Person getMother()
{
return this.mother;
}
public ArrayList getChildren(){
return this.children;
}
}
familytree.txt
母线: 亨利六世 约克的伊丽莎白 父系: 亨利八世 亨利七世 儿童: 玛丽一世 伊丽莎白一世 爱德华六世
任何帮助将不胜感激,这是原始问题。
编写一个家庭数据库程序。创建一个类来代表一个人,并存储对该人的母亲,父亲和该人所拥有的任何孩子的引用。读取名称文件以初始化每个Person的名称和父子关系。将人员的总体列表存储为Arraylist。编写一个整体的主要用户界面,要求输入姓名并打印该人的父母家庭关系。
答案 0 :(得分:1)
问题是您正在使用通用ArrayList来存储Person对象的List。您应该使用List<Person>
。
另外,addMembersList
应该是allMembersList
(必须是错字)。
这是工作代码。
//main
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
initialUserGuide();
Scanner sc = new Scanner(new File("familytree.txt"));
Family fr = new Family(sc);
fr.displayAllMembersList();
processUserInput(fr);
}
static void initialUserGuide() {
System.out.println("The input file consists of names of all Humans");
System.out.println("Enter the Human name from below list, to display his/her maternal line, Paternal line and children");
}
static void processUserInput(Family fr) {
Scanner input = new Scanner(System.in);
Person person;
System.out.println("newt Human ( enter told " + "quit)?");
String nextLine = input.nextLine();
while (nextLine.length() > 0) {
person = fr.find(nextLine);
if (person == null)
System.out.println("NoMatch Occured");
else {
displayMaternalLine(person);
displayPaternalLine(person);
displayChildren(person);
}
System.out.println("");
System.out.println("next Human (enter to quit)?");
nextLine = input.nextLine();
}
}
static void displayMaternalLine(Person mother) {
System.out.println("Maternal Line");
int i = 0;
int count = 1;
while (mother != null) {
for (i = 0; i < count; i++) {
System.out.println("");
}
System.out.println(mother.getName());
mother = mother.getMother();
count++;
}
}
static void displayPaternalLine(Person father) {
System.out.println("Paternal Line");
int i = 0;
int count = 1;
while (father != null) {
for (i = 0; i < count; i++) {
System.out.println("");
}
System.out.println(father.getName());
father = father.getMother();
count++;
}
}
static void displayChildren(Person person) {
System.out.println("Children");
List<Person> ptr = person.getChildren();
if (ptr.size() == 0) {
System.out.println(" none");
}
for (int i = 0; i < ptr.size(); i++) {
System.out.println("\t" + ptr.get(i).getName());
}
}
}
//Family
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Family {
private List<Person> allMembersList;
public Family(Scanner sc) {
allMembersList = new ArrayList<>();
addAllMembersList(sc);
makeParentChildRelation(sc);
}
Person find(String name) {
for (int i = 0; i < allMembersList.size(); i++) {
if (allMembersList.get(i).getName().equalsIgnoreCase(name))
return (Person) allMembersList.get(i);
}
return null;
}
public void addAllMembersList(Scanner sc) {
String line = sc.nextLine();
while (line.length() != 0) {
Person person = new Person(line);
allMembersList.add(person);
line = sc.nextLine();
}
}
public void makeParentChildRelation(Scanner sc) {
String nextLine;
String name, motherName, fatherName;
while (sc.hasNextLine()) {
name = sc.nextLine();
motherName = sc.nextLine();
fatherName = sc.nextLine();
Person personn = find(name);
if (!motherName.equals("unknown")) {
Person mother = find(motherName);
personn.setMother(mother);
mother.addChildren(personn);
}
if (!fatherName.equals("unknown")) {
Person father = find(fatherName);
personn.setFather(father);
father.addChildren(personn);
}
}
}
public void displayAllMembersList() {
for (int i = 0; i < allMembersList.size(); i++) {
Person person = (Person) allMembersList.get(i);
System.out.println(person.getName());
}
}
}
//Person
import java.util.ArrayList;
import java.util.List;
public class Person {
private String name;
private Person mother;
private Person father;
private List<Person> children;
public Person(String name) {
this.name = name;
children = new ArrayList<>();
}
public void setName(String name) {
this.name = name;
}
public void setFather(Person Father) {
this.father = Father;
}
public void setMother(Person Mother) {
this.mother = Mother;
}
public void addChildren(Person Child) {
children.add(Child);
}
public String getName() {
return this.name;
}
public Person getFather() {
return this.father;
}
public Person getMother() {
return this.mother;
}
public List<Person> getChildren() {
return this.children;
}
}