Skip to content

Commit 15123c0

Browse files
committed
Changed HeartRates.java into a record class and updated comments to be better.
1 parent e82dbfe commit 15123c0

3 files changed

Lines changed: 25 additions & 55 deletions

File tree

src/HeartRateCalculator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ public static void calculateHeartRate(String firstName, String lastName, String
77

88
// Validation and parsing
99
try {
10-
//calls to parseMonth, which is below.
10+
// Calls to parseMonth, which is below.
1111
birthMonthValue = parseMonth(birthMonth);
1212
} catch (IllegalArgumentException ex) {
1313
gui.displayErrorMessage(ex.getMessage());
1414
return;
1515
}
1616

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

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

57-
//I really wanted it to allow you to type the full name of the month instead of just using numbers,
58-
//but I also allowed you to use numbers if you'd like as well.
57+
// I really wanted it to allow you to type the full name of the month instead of just using numbers,
58+
// but I also allowed you to use numbers if you'd like as well.
5959
private static int parseMonth(String input) {
6060
// Check if input contains only digits
6161
boolean isNumeric = input.chars().allMatch(Character::isDigit);

src/HeartRateGUI.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
/*
22
Name: Four.
3-
Date: Apr 16 2024.
3+
Date: originally written on Apr 16 2024.
44
Class: CS 145.
55
Assignment: Lab 2: Target Heart Rate.
6-
Purpose: Creates an application where a user can input info and receive data about their target heart rate.
6+
Purpose: Creates an application where a user can input info and receive data about their target heart rate,
7+
based on data from the American Heart Association.
78
89
WARNING: I MADE THIS PROGRAM WITH JDK VERSION 22. I CANNOT GUARANTEE IT WILL WORK WITH EARLIER VERSIONS.
910
*/
1011
import javax.swing.*;
11-
//awt for the button
12+
// AWT for the button
1213
import java.awt.*;
1314
import java.awt.event.*;
1415

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

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

3131
// Creates the window.
3232
public HeartRateGUI() {
33-
//the name of the window
33+
// The name of the window
3434
super("Heart Rate Calculator");
3535
initComponents();
36+
// 6 rows, 2 cols
3637
setLayout(new GridLayout(6, 2));
3738
addComponents();
3839
setVisible(true);
3940
}
4041

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

54-
//actually put the components we just initialized onto the window.
55+
// Actually put the components we just initialized onto the window.
5556
private void addComponents() {
5657
// Add labels and fields
5758
add(new JLabel("First Name:"));
@@ -69,8 +70,8 @@ private void addComponents() {
6970
JButton calculateButton = new JButton("Calculate");
7071
calculateButton.addActionListener(new ActionListener() {
7172
public void actionPerformed(ActionEvent e) {
72-
//when the button is clicked, calculate the heart rate.
73-
//and pass in everything it needs.
73+
// When the button is clicked, calculate the heart rate.
74+
// And pass in everything it needs.
7475
HeartRateCalculator.calculateHeartRate(
7576
firstNameField.getText(),
7677
lastNameField.getText(),
@@ -84,13 +85,13 @@ public void actionPerformed(ActionEvent e) {
8485
add(calculateButton);
8586
}
8687

87-
//is called from the calculator to open a pane for the final heart rate info
88+
// Is called from the calculator to open a pane for the final heart rate info
8889
public void displayHeartRateInfo(String info) {
8990
JOptionPane.showMessageDialog(this, info, "Heart Rate Information",
9091
JOptionPane.INFORMATION_MESSAGE);
9192
}
9293

93-
//is called from the calculator to send an error message.
94+
// Is called from the calculator to send an error message.
9495
public void displayErrorMessage(String message) {
9596
JOptionPane.showMessageDialog(this, message, "Error",
9697
JOptionPane.ERROR_MESSAGE);

src/HeartRates.java

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,17 @@
11
import java.util.Calendar;
22

3-
public class HeartRates {
4-
private final String firstName;
5-
private final String lastName;
6-
private final int birthMonth;
7-
private final int birthDay;
8-
private final int birthYear;
9-
10-
public HeartRates(String firstName, String lastName, int birthMonth, int birthDay, int birthYear) {
11-
this.firstName = firstName;
12-
this.lastName = lastName;
13-
this.birthMonth = birthMonth;
14-
this.birthDay = birthDay;
15-
this.birthYear = birthYear;
16-
}
17-
18-
// Getter methods
19-
public String firstName() {
20-
return firstName;
21-
}
22-
23-
public String lastName() {
24-
return lastName;
25-
}
26-
27-
public int birthMonth() {
28-
return birthMonth;
29-
}
30-
31-
public int birthDay() {
32-
return birthDay;
33-
}
34-
35-
public int birthYear() {
36-
return birthYear;
37-
}
3+
public record HeartRates(String firstName, String lastName, int birthMonth, int birthDay, int birthYear) {
384

395
// Method to calculate age
406
public int calculateAge() {
7+
8+
// Finds what time it is now, to figure out how old you are.
419
Calendar now = Calendar.getInstance();
4210
int currentYear = now.get(Calendar.YEAR);
4311
int currentMonth = now.get(Calendar.MONTH) + 1;
4412
int currentDay = now.get(Calendar.DAY_OF_MONTH);
4513

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

0 commit comments

Comments
 (0)