master
  1/**
  2 * Assignment 2, COMP268 Class: Person.java
  3 *
  4 * @description A class used to shame a person by calculating a BMI which has no value whatsoever.
  5 * @author: mo khan Student ID: 3431709
  6 * @date Jul 19, 2019
  7 * @version 1.0
  8 */
  9package Q7;
 10
 11import java.util.*;
 12
 13public class Person {
 14  private double bmi;
 15  private double height;
 16  private double weight;
 17  private String name;
 18
 19  /**
 20   * Create an instance of a person and calculates their BMI.
 21   *
 22   * @param name the name of the person
 23   * @param weight the weight of the person
 24   * @param height the height of the person
 25   */
 26  public Person(String name, double weight, double height) {
 27    this.name = name;
 28    this.weight = weight;
 29    this.height = height;
 30    this.updateBMI();
 31  }
 32
 33  /**
 34   * Returns the category the person is categorized as based on BMI.
 35   *
 36   * @return A category of "Underweight", "Normal", "Overweight" or "Obese"
 37   */
 38  public String getCategory() {
 39    return this.getCategory(this.bmi);
 40  }
 41
 42  /**
 43   * Returns the category for the BMI.
 44   *
 45   * @param bmi the BMI to determine a category for.
 46   * @return A category of "Underweight", "Normal", "Overweight" or "Obese"
 47   */
 48  public String getCategory(double bmi) {
 49    if (bmi < 18.5) return "Underweight";
 50    else if (bmi < 25.0) return "Normal";
 51    else if (bmi < 30.0) return "Overweight";
 52    return "Obese";
 53  }
 54
 55  /**
 56   * Returns the persons name
 57   *
 58   * @return the persons name
 59   */
 60  public String getName() {
 61    return this.name;
 62  }
 63
 64  /**
 65   * Returns the persons BMI
 66   *
 67   * @return the persons BMI
 68   */
 69  public double getBMI() {
 70    return this.bmi;
 71  }
 72
 73  /**
 74   * Returns the persons height
 75   *
 76   * @return the persons height
 77   */
 78  public double getHeight() {
 79    return this.height;
 80  }
 81
 82  /**
 83   * Returns the persons weight
 84   *
 85   * @return the persons weight
 86   */
 87  public double getWeight() {
 88    return this.weight;
 89  }
 90
 91  /**
 92   * Change the BMI
 93   *
 94   * @param bmi the new BMI
 95   */
 96  private void setBMI(double bmi) {
 97    this.bmi = bmi;
 98  }
 99
100  /**
101   * Change the Height
102   *
103   * @param height the new height
104   */
105  public void setHeight(double height) {
106    this.height = height;
107    this.updateBMI();
108  }
109
110  /**
111   * Change the name
112   *
113   * @param name the new name
114   */
115  public void setName(String name) {
116    this.name = name;
117  }
118
119  /**
120   * Change the weight
121   *
122   * @param weight the new weight
123   */
124  public void setWeight(double weight) {
125    this.weight = weight;
126    this.updateBMI();
127  }
128
129  private void updateBMI() {
130    this.setBMI((this.weight * 703) / Math.pow(height, 2));
131  }
132
133  /**
134   * The entry point to the console application.
135   *
136   * @param args the argument vector passed to the program.
137   */
138  public static void main(String[] args) {
139    ArrayList<Person> people = new ArrayList<Person>();
140    people.add(new Person("Andrew", 125.5, 55.1));
141    people.add(new Person("Boyd", 150.0, 67.0));
142    people.add(new Person("Cathy", 135.0, 72.3));
143    people.add(new Person("Donna", 190.0, 64.0));
144
145    System.out.println(String.format("%-20s Weight Height BMI Category", "Name"));
146    System.out.println("-----------------------------------------------");
147    for (Person person : people) {
148      System.out.println(
149          String.format(
150              "%-20s %+5.1f %+6.1f %+3.0f %s",
151              person.getName(),
152              person.getWeight(),
153              person.getHeight(),
154              person.getBMI(),
155              person.getCategory()));
156    }
157  }
158}