summaryrefslogtreecommitdiff
path: root/src/Q7/Person.java
blob: aaca8af60cc2914f99fa9ffc6543603927461d23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
 * Assignment 2, COMP268 Class: Person.java
 *
 * @description A class used to shame a person by calculating a BMI which has no value whatsoever.
 * @author: mo khan Student ID: 3431709
 * @date Jul 19, 2019
 * @version 1.0
 */
package Q7;

import java.util.*;

public class Person {
  private double bmi;
  private double height;
  private double weight;
  private String name;

  /**
   * Create an instance of a person and calculates their BMI.
   *
   * @param name the name of the person
   * @param weight the weight of the person
   * @param height the height of the person
   */
  public Person(String name, double weight, double height) {
    this.name = name;
    this.weight = weight;
    this.height = height;
    this.updateBMI();
  }

  /**
   * Returns the category the person is categorized as based on BMI.
   *
   * @return A category of "Underweight", "Normal", "Overweight" or "Obese"
   */
  public String getCategory() {
    return this.getCategory(this.bmi);
  }

  /**
   * Returns the category for the BMI.
   *
   * @param bmi the BMI to determine a category for.
   * @return A category of "Underweight", "Normal", "Overweight" or "Obese"
   */
  public String getCategory(double bmi) {
    if (bmi < 18.5) return "Underweight";
    else if (bmi < 25.0) return "Normal";
    else if (bmi < 30.0) return "Overweight";
    return "Obese";
  }

  /**
   * Returns the persons name
   *
   * @return the persons name
   */
  public String getName() {
    return this.name;
  }

  /**
   * Returns the persons BMI
   *
   * @return the persons BMI
   */
  public double getBMI() {
    return this.bmi;
  }

  /**
   * Returns the persons height
   *
   * @return the persons height
   */
  public double getHeight() {
    return this.height;
  }

  /**
   * Returns the persons weight
   *
   * @return the persons weight
   */
  public double getWeight() {
    return this.weight;
  }

  /**
   * Change the BMI
   *
   * @param bmi the new BMI
   */
  private void setBMI(double bmi) {
    this.bmi = bmi;
  }

  /**
   * Change the Height
   *
   * @param height the new height
   */
  public void setHeight(double height) {
    this.height = height;
    this.updateBMI();
  }

  /**
   * Change the name
   *
   * @param name the new name
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * Change the weight
   *
   * @param weight the new weight
   */
  public void setWeight(double weight) {
    this.weight = weight;
    this.updateBMI();
  }

  private void updateBMI() {
    this.setBMI((this.weight * 703) / Math.pow(height, 2));
  }

  public static void main(String[] args) {
    ArrayList<Person> people = new ArrayList<Person>();
    people.add(new Person("Andrew", 125.5, 55.1));
    people.add(new Person("Boyd", 150.0, 67.0));
    people.add(new Person("Cathy", 135.0, 72.3));
    people.add(new Person("Donna", 190.0, 64.0));

    System.out.println(String.format("%-20s Weight Height BMI Category", "Name"));
    System.out.println("-----------------------------------------------");
    for (Person person : people) {
      System.out.println(
          String.format(
              "%-20s %+5.1f %+6.1f %+3.0f %s",
              person.getName(),
              person.getWeight(),
              person.getHeight(),
              person.getBMI(),
              person.getCategory()));
    }
  }
}