This code is just some exercise code printing questions and waiting for answers. It is working perfectly well as far as I can tell. I would like to know if this code could be considered acceptable or how it could be improved.
import java.util.ArrayList;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
ArrayList<Citizens> list = new ArrayList<Citizens>();
Citizens p1 = new Portuguese();
list.add(p1);
p1.addName();
p1.addAge();
p1.addAdress();
Citizens p2 = new German();
list.add(p2);
p2.addName();
p2.addAge();
p2.addAdress();
boolean addCitizen = true;
while (addCitizen) {
Scanner input = new Scanner(System.in);
System.out.println("Do you want to add another citizen?Y or N?");
String answer = input.next();
if (answer.equals("y")) {
System.out.println("Do you want to add a Portuguese or a German citizen?PT or GER?");
answer = input.next();
if (answer.equals("pt")) {
Citizens p3 = new Portuguese();
list.add(p3);
p3.addName();
p3.addAge();
p3.addAdress();
} else if (answer.equals("ger")) {
Citizens p3 = new German();
list.add(p3);
p3.addName();
p3.addAge();
p3.addAdress();
} else if (!answer.equals("pt") || (!answer.equals("ger"))) {
System.out.println("Please choose PT or GER!");
}
} else if (answer.equals("n")) {
System.out.println("You´re not gonna add a new citizen!");
addCitizen = false;
} else {
System.out.println("Please enter y or n");
}
}
System.out.println("The end");
}
}
class Citizens
import java.util.Scanner;
public class Citizens {
private String name;
private int age;
private String adress;
String answer;
public int answernr;
boolean afirmativeanswer = true;
Scanner input = new Scanner(System.in);
public void addName() {
System.out.println("Do you want to add the citizen name?Y or N?");
answer = input.nextLine();
while (!answer.equals("y") || (!answer.equals("n"))) {
if (answer.equals("y")) {
System.out.println("Please give the citizen´s name !");
String giveName = input.nextLine();
this.setName(giveName);
break;
} else if (answer.equals("n")) {
System.out.println("Not adding a name!");
break;
} else {
System.out.println("Please enter y or n");
answer = input.nextLine();
}
}
}
public void addAge() {
System.out.println("Do you want to add " + this.getName() + "´s age? Y or N?");
while (afirmativeanswer) {
answer = input.nextLine();
if (answer.equals("y")) {
System.out.println("Please enter " + this.getName() + "´s age!");
answernr = input.nextInt();
this.setAge(answernr);
afirmativeanswer = false;
} else if (answer.equals("n")) {
System.out.println("You choose not to add " + this.getName() + "´s age!");
afirmativeanswer = false;
} else {
System.out.println("Please enter y or n");
}
}
}
public void addAdress() {
System.out.println("Do you want to add " + this.getName() + "´s Adress?Y or N?");
afirmativeanswer = true;
while (afirmativeanswer) {
answer = input.next();
if (answer.equals("y")) {
System.out.println("Enter " + this.getName() + "´s Adress!");
answer = input.next();
this.setAdress(answer);
afirmativeanswer = false;
} else if (answer.equals("n")) {
System.out.println("You choose not to add " + this.getName() + "´s Adress!");
afirmativeanswer = false;
} else {
System.out.println("Please choose Y or N!");
}
}
}
public void setName(String s) {
this.name = s;
}
public String getName() {
return name;
}
public void setAge(int i) {
this.age = i;
}
public int getAge() {
return age;
}
public void setAdress(String c) {
this.adress = c;
}
public String getAdress() {
return adress;
}
public String toString() {
return name;
}
}
The Portuguese and German classes have just one constructor each.