Lab#SE00-2: Maven Clinic
Java SE Lab 00
javase
lab
model
composition
Java SE Lab 00, part 2
📘 Linux Lab#SE00-2: Maven Clinic
Create a
Maven/Gradle
Java SE Project with theUML
and classes defined below.Add
Lombok
,JUnit
andFaker
dependenciesRefactor: remove boilerplate code (getters, setters and so on) and work with
Lombok
fromPerson
,MyDate
andClinic
Create
Junit
tests to test objects. UseFaker
to create objects:- Test
createPerson
: check weather the objectPerson
creation works properly. - Test
createMyDate
: check weather the objectCar
creation works properly. - Test
createClinic
: check weather the objectBook
creation works properly.
- Test
Complete these mehtods within
Clinic
class:public boolean isAccepted(Person person) {}
: return true when weightIndex() < lowestWeightIndexpublic void addAsMember(Person person) {}
: add as a member if memberisAccepted(Person person)
public Person personWithHighestWeightIndex() {}
: returnPerson
object with the highestWeightIndex()
Create
Junit
tests to test the previous three new methods created atClinic
class.
1 UML
- The class diagram represents three classes,
Person
,MyDate
, andClinic
.- The
Person
class has four private fields name, age, height, and weight and one field birthMyDate of type MyDate . - The
MyDate
class has three private fields day, month, and year. - The
Clinic
class has two private fields lowestWeightIndex and name, and a field members which is an ArrayList ofPerson
objects.
- The
- The relationship between
Person
andMyDate
is a Composition relationship, where as the relationship betweenClinic
andPerson
is also a Composition relationship but trough an ArraList.
2 Base Classes
2.1 Person Class
Here, the Person
class represents a person with a name, address and others.
Code Person
Person.java
package exemple3;
public class Person {
private String name;
private int age;
private int height;
private int weight;
//composition relationship
private MyDate birthMyDate;
public Person(String name) {
this(name, 0); // run here the other constructor's code and set the age parameter to 0
}
public Person(String name, int age) {
this.name = name;
this.age = age;
this.weight = 0;
this.height = 0;
}
public Person(String name, int height, int weigth) {
this.name = name;
this.age = 0;
this.weight = weigth;
this.height = height;
}
public Person(String name, int day, int month, int year) {
this.name = name;
this.weight = 0;
this.height = 0;
this.birthMyDate = new MyDate(day, month, year);
}
public Person(String name, int age, int day, int month, int year) {
this.name = name;
this.age = age;
this.weight = 0;
this.height = 0;
this.birthMyDate = new MyDate(day, month, year);
}
public void printPerson() {
System.out.println(this.name + " I am " + this.age + " years old");
}
public void becomeOlder() {
this.age++;
}
public void becomeOlder(int years) {
this.age = this.age + years;
}
public boolean isAdult() {
if (this.age < 18) {
return false;
}
return true;
}
public double weightIndex() {
double heightInMeters = this.height / 100.0;
return this.weight / (heightInMeters * heightInMeters);
}
public boolean olderThan(Person compared) {
if (this.age > compared.getAge()) {
return true;
}
return false;
}
public String toString() {
return this.name + " I am " + this.age + " years old, my weight index is " + this.weightIndex() + ", born "
+ this.birthMyDate;
}
//setters and getters
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return this.height;
}
public int getWeight() {
return this.weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
}
2.2 MyDate Class
Here, the MyDate
class represents a date with a day, month and year.
Code MyDate
MyDate.java
package exemple3;
public class MyDate {
private int day;
private int month;
private int year;
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public String toString() {
return this.day + "." + this.month + "." + this.year;
}
public boolean earlier(MyDate compared) {
// first we'll compare years
if (this.year < compared.year) {
return true;
}
// if the years are the same, we'll compare the months
if (this.year == compared.year && this.month < compared.month) {
return true;
}
// years and months the same, we'll compare the days
if (this.year == compared.year && this.month == compared.month && this.day < compared.day) {
return true;
}
return false;
}
}
2.3 Clinic Class
Here, the Clinic
class represents a clinic with a name, lowestWeightIndex and members.
Code Clinic
Clinic.java
package exemple3;
import java.util.ArrayList;
public class Clinic {
private double lowestWeightIndex;
private String name;
private ArrayList<Person> members;
public Clinic(String name, double lowestWeightIndex) {
this.lowestWeightIndex = lowestWeightIndex;
this.name = name;
this.members = new ArrayList<Person>();
}
public boolean isAccepted(Person person) {
// to-do
}
public void addAsMember(Person person) {
// to-do
}
public Person personWithHighestWeightIndex() {
// to-do
}
public String toString() {
String membersAsString = "";
for (Person member : this.members) {
membersAsString += " " + member + "\n";
}
return "Clinic:\n " + this.name + " (" + this.lowestWeightIndex + ") " + " \n members: \n" + membersAsString;
}
//getters and setters
public double getLowestWeightIndex() {
return lowestWeightIndex;
}
public void setLowestWeightIndex(double lowestWeightIndex) {
this.lowestWeightIndex = lowestWeightIndex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Person> getMembers() {
return members;
}
public void setMembers(ArrayList<Person> members) {
this.members = members;
}
}