Skip to content

Commit

Permalink
Changed HeartRates.java into a record class and updated comments to b…
Browse files Browse the repository at this point in the history
…e better.
  • Loading branch information
feedmepaperr committed May 6, 2024
1 parent e82dbfe commit 15123c0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 55 deletions.
10 changes: 5 additions & 5 deletions src/HeartRateCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public static void calculateHeartRate(String firstName, String lastName, String

// Validation and parsing
try {
//calls to parseMonth, which is below.
// Calls to parseMonth, which is below.
birthMonthValue = parseMonth(birthMonth);
} catch (IllegalArgumentException ex) {
gui.displayErrorMessage(ex.getMessage());
return;
}

try {
//since I only expect number values for birth day, I don't need another method.
// Since I only expect number values for birth day, I don't need another method.
birthDayValue = Integer.parseInt(birthDay);
if (birthDayValue < 1 || birthDayValue > 31) {
gui.displayErrorMessage("Birth day must be between 1 and 31");
Expand All @@ -28,7 +28,7 @@ public static void calculateHeartRate(String firstName, String lastName, String

try {
birthYearValue = Integer.parseInt(birthYear);
//I am assuming here the user is not a baby or long since dead. I feel like this is safe to assume.
// I am assuming here the user is not a baby or long since dead. I feel like this is safe to assume.
if (birthYearValue < 1900 || birthYearValue > 2023) {
gui.displayErrorMessage("Please enter your real birth year.");
return;
Expand All @@ -54,8 +54,8 @@ public static void calculateHeartRate(String firstName, String lastName, String
);
}

//I really wanted it to allow you to type the full name of the month instead of just using numbers,
//but I also allowed you to use numbers if you'd like as well.
// I really wanted it to allow you to type the full name of the month instead of just using numbers,
// but I also allowed you to use numbers if you'd like as well.
private static int parseMonth(String input) {
// Check if input contains only digits
boolean isNumeric = input.chars().allMatch(Character::isDigit);
Expand Down
31 changes: 16 additions & 15 deletions src/HeartRateGUI.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/*
Name: Four.
Date: Apr 16 2024.
Date: originally written on Apr 16 2024.
Class: CS 145.
Assignment: Lab 2: Target Heart Rate.
Purpose: Creates an application where a user can input info and receive data about their target heart rate.
Purpose: Creates an application where a user can input info and receive data about their target heart rate,
based on data from the American Heart Association.
WARNING: I MADE THIS PROGRAM WITH JDK VERSION 22. I CANNOT GUARANTEE IT WILL WORK WITH EARLIER VERSIONS.
*/
import javax.swing.*;
//awt for the button
// AWT for the button
import java.awt.*;
import java.awt.event.*;

//I'm still learning how to use Swing.
//but from what I know, this extends the JFrame class to create our own custom "frame" or window,
//then we just need to set certain parameters.
// I'm still learning how to use Swing.
// but from what I know, this extends the JFrame class to create our own custom "frame" or window,
// then we just need to set certain parameters.
public class HeartRateGUI extends JFrame {
private JTextField firstNameField;
private JTextField lastNameField;
Expand All @@ -23,22 +24,22 @@ public class HeartRateGUI extends JFrame {
private JTextField birthYearField;

public static void main(String[] args) {
//I know you said keep main as small as possible, but this is a little silly.
//this creates a new window for the HeartRateGUI.
// Creates an object to run the program
new HeartRateGUI();
}

// Creates the window.
public HeartRateGUI() {
//the name of the window
// The name of the window
super("Heart Rate Calculator");
initComponents();
// 6 rows, 2 cols
setLayout(new GridLayout(6, 2));
addComponents();
setVisible(true);
}

//initiates components for the window, such as fields (text boxes) and the size.
// Initiates components for the window, such as fields (text boxes) and the size.
private void initComponents() {
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Expand All @@ -51,7 +52,7 @@ private void initComponents() {
birthYearField = new JTextField();
}

//actually put the components we just initialized onto the window.
// Actually put the components we just initialized onto the window.
private void addComponents() {
// Add labels and fields
add(new JLabel("First Name:"));
Expand All @@ -69,8 +70,8 @@ private void addComponents() {
JButton calculateButton = new JButton("Calculate");
calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//when the button is clicked, calculate the heart rate.
//and pass in everything it needs.
// When the button is clicked, calculate the heart rate.
// And pass in everything it needs.
HeartRateCalculator.calculateHeartRate(
firstNameField.getText(),
lastNameField.getText(),
Expand All @@ -84,13 +85,13 @@ public void actionPerformed(ActionEvent e) {
add(calculateButton);
}

//is called from the calculator to open a pane for the final heart rate info
// Is called from the calculator to open a pane for the final heart rate info
public void displayHeartRateInfo(String info) {
JOptionPane.showMessageDialog(this, info, "Heart Rate Information",
JOptionPane.INFORMATION_MESSAGE);
}

//is called from the calculator to send an error message.
// Is called from the calculator to send an error message.
public void displayErrorMessage(String message) {
JOptionPane.showMessageDialog(this, message, "Error",
JOptionPane.ERROR_MESSAGE);
Expand Down
39 changes: 4 additions & 35 deletions src/HeartRates.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,17 @@
import java.util.Calendar;

public class HeartRates {
private final String firstName;
private final String lastName;
private final int birthMonth;
private final int birthDay;
private final int birthYear;

public HeartRates(String firstName, String lastName, int birthMonth, int birthDay, int birthYear) {
this.firstName = firstName;
this.lastName = lastName;
this.birthMonth = birthMonth;
this.birthDay = birthDay;
this.birthYear = birthYear;
}

// Getter methods
public String firstName() {
return firstName;
}

public String lastName() {
return lastName;
}

public int birthMonth() {
return birthMonth;
}

public int birthDay() {
return birthDay;
}

public int birthYear() {
return birthYear;
}
public record HeartRates(String firstName, String lastName, int birthMonth, int birthDay, int birthYear) {

// Method to calculate age
public int calculateAge() {

// Finds what time it is now, to figure out how old you are.
Calendar now = Calendar.getInstance();
int currentYear = now.get(Calendar.YEAR);
int currentMonth = now.get(Calendar.MONTH) + 1;
int currentDay = now.get(Calendar.DAY_OF_MONTH);

// Checks to make sure the user has or has not had their birthday yet this year
int age = currentYear - birthYear;
if (currentMonth < birthMonth || (currentMonth == birthMonth && currentDay < birthDay)) {
age--;
Expand Down

0 comments on commit 15123c0

Please sign in to comment.