Skip to content

Commit 78efa0f

Browse files
author
“A1-4U2T1NN”
committed
feat: added .person to the package; added <String> to ArrayList innstances; added isEmpty to .getHairColor function to check for null and empty arrays and change the hair color to bald; added '|' to seperate information in .getPersonInfo function; created PersonTest, imported nessesary data, and added PersonTest set up; added tests for all .get and .set functions; added exception testing for .getHairColor;
1 parent ad7ec4c commit 78efa0f

File tree

3 files changed

+315
-155
lines changed

3 files changed

+315
-155
lines changed
Lines changed: 120 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,126 @@
1-
package com.codedifferently.lesson16;
1+
package com.codedifferently.lesson16.person;
22

33
import java.util.ArrayList;
44

55
public class Person {
6-
7-
private String name;
8-
private String sex;
9-
private String race;
10-
private int age;
11-
private boolean alive;
12-
private double height;
13-
private ArrayList<String> hairColor;
14-
15-
16-
17-
public Person(String name, String sex, String race, int age, double height, ArrayList hairColor, boolean alive){
18-
this.name = name;
19-
this.sex = sex;
20-
this.race = race;
21-
this.age = age;
22-
this.height = height;
23-
this.hairColor = hairColor;
24-
this.alive = alive;
25-
}
26-
27-
public String getName() {
28-
return name;
29-
}
30-
31-
public void setName(String name) {
32-
this.name = name;
33-
}
34-
35-
public String getSex() {
36-
return sex;
37-
}
38-
39-
public void setSex(String sex) {
40-
this.sex = sex;
41-
}
42-
43-
public String getRace() {
44-
return race;
45-
}
46-
47-
public void setRace(String race) {
48-
this.race = race;
49-
}
50-
51-
public int getAge() {
52-
return age;
53-
}
54-
55-
public void setAge(int age) {
56-
this.age = age;
57-
}
58-
59-
public double getHeight() {
60-
return height;
61-
}
62-
63-
public void setHeight(double height) {
64-
this.height = height;
65-
}
66-
67-
public ArrayList<String> getHairColor() {
68-
if (hairColor == null) {
69-
try {
70-
throw new IllegalArgumentException("No hair color? This person must be bald!");
71-
} catch (IllegalArgumentException e) {
72-
hairColor.add("Bald");
73-
}
74-
}
75-
return hairColor;
76-
}
77-
78-
public void setHairColor(ArrayList hairColor) {
79-
this.hairColor = hairColor;
80-
}
81-
82-
public ArrayList<String> getNaturalHairColor() {
83-
ArrayList<String> naturalHairColor = new ArrayList<>();
84-
ArrayList<String> possibleNaturalColor = new ArrayList<>();
85-
possibleNaturalColor.add("Black");
86-
possibleNaturalColor.add("Brown");
87-
possibleNaturalColor.add("Blonde");
88-
possibleNaturalColor.add("Ginger");
89-
possibleNaturalColor.add("Auburn");
90-
possibleNaturalColor.add("Albino");
91-
possibleNaturalColor.add("Grey");
92-
93-
for (String color : hairColor) {
94-
if (possibleNaturalColor.contains(color)) {
95-
naturalHairColor.add(color);
96-
break;
97-
}
98-
}
99-
return naturalHairColor;
100-
}
101-
102-
public String getLifeStatus() {
103-
if (alive == true) {
104-
return "Alive";
105-
} else {
106-
return "Deceased";
107-
}
108-
}
109-
110-
public void setLifeStatus(boolean alive) {
111-
this.alive = alive;
112-
}
1136

114-
public String getPersonInfo() {
115-
return "Name: " + name +
116-
"Gender Identity: " + sex +
117-
"Race: " + race +
118-
"Age: " + age +
119-
"Height(In meters): " + height +
120-
"Hair Color: " + hairColor +
121-
"Status: " + getLifeStatus();
122-
}
7+
private String name;
8+
private String sex;
9+
private String race;
10+
private int age;
11+
private boolean alive;
12+
private double height;
13+
private ArrayList<String> hairColor;
14+
15+
public Person(String name, String sex, String race, int age, double height, ArrayList<String> hairColor, boolean alive) {
16+
this.name = name;
17+
this.sex = sex;
18+
this.race = race;
19+
this.age = age;
20+
this.height = height;
21+
this.hairColor = hairColor;
22+
this.alive = alive;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public String getSex() {
34+
return sex;
35+
}
36+
37+
public void setSex(String sex) {
38+
this.sex = sex;
39+
}
40+
41+
public String getRace() {
42+
return race;
43+
}
44+
45+
public void setRace(String race) {
46+
this.race = race;
47+
}
48+
49+
public int getAge() {
50+
return age;
51+
}
52+
53+
public void setAge(int age) {
54+
this.age = age;
55+
}
56+
57+
public double getHeight() {
58+
return height;
59+
}
60+
61+
public void setHeight(double height) {
62+
this.height = height;
63+
}
64+
65+
public ArrayList<String> getHairColor() {
66+
if (hairColor == null || hairColor.isEmpty()) {
67+
this.hairColor = new ArrayList<>();
68+
hairColor.add("Bald");
69+
throw new IllegalArgumentException("No hair color? This person must be bald!");
70+
}
71+
return hairColor;
72+
}
73+
74+
public void setHairColor(ArrayList<String> hairColor) {
75+
this.hairColor = hairColor;
76+
}
77+
78+
public ArrayList<String> getNaturalHairColor() {
79+
ArrayList<String> naturalHairColor = new ArrayList<>();
80+
ArrayList<String> possibleNaturalColor = new ArrayList<>();
81+
possibleNaturalColor.add("Black");
82+
possibleNaturalColor.add("Brown");
83+
possibleNaturalColor.add("Blonde");
84+
possibleNaturalColor.add("Ginger");
85+
possibleNaturalColor.add("Auburn");
86+
possibleNaturalColor.add("Albino");
87+
possibleNaturalColor.add("Grey");
88+
89+
for (String color : hairColor) {
90+
if (possibleNaturalColor.contains(color)) {
91+
naturalHairColor.add(color);
92+
break;
93+
}
94+
}
95+
return naturalHairColor;
96+
}
97+
98+
public String getLifeStatus() {
99+
if (alive == true) {
100+
return "Alive";
101+
} else {
102+
return "Deceased";
103+
}
104+
}
105+
106+
public void setLifeStatus(boolean alive) {
107+
this.alive = alive;
108+
}
109+
110+
public String getPersonInfo() {
111+
return "Name: "
112+
+ name
113+
+ "| Gender Identity: "
114+
+ sex
115+
+ "| Race: "
116+
+ race
117+
+ "| Age: "
118+
+ age
119+
+ "| Height(In meters): "
120+
+ height
121+
+ "| Hair Color: "
122+
+ hairColor
123+
+ "| Status: "
124+
+ getLifeStatus();
125+
}
123126
}

lesson_16/objects/objects_app/src/test/java/Person/PersonTest.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)