Question
Create a Java BMI Data Retrieval service program based on the following code. This program works for collecting individual BMI data and displaying it back
Create a Java BMI Data Retrieval service program based on the following code.
This program works for collecting individual BMI data and displaying it back to the user but I can't figure out how to make it so that the user can search for a name and the program would return the BMI data pertaining to that name. I'm also having trouble figuring out how to make it so that the program displays everyone's data at the end instead of displaying an individual's data after the user inputs it.
Also would appreciate it if the program only uses GUI output, not any console output.
import javax.swing.*;
public class BMIProgram{
String name, comment; int w, h, underWeight, normalWeight, overWeight, obese; double BMI;
public void getBMIData() { try { String numberOfPeople = JOptionPane.showInputDialog("Enter the " + "amount of people whose BMI will be recorded. (Max 10)"); int bmiCount = Integer.parseInt(numberOfPeople); if (bmiCount > 10) { JOptionPane.showMessageDialog(null, "Error: Inputted number " + "exceeds maximum. Shutting down."); System.exit(0); } for (int i = 1; i <= bmiCount; i++) { JTextField f1 = new JTextField(10); //for name JTextField f2 = new JTextField(10); //for height JTextField f3 = new JTextField(10); //for weight JPanel BMIPanel = new JPanel(); BMIPanel.add(new JLabel("Enter user name: ")); BMIPanel.add(f1); BMIPanel.add(new JLabel("Enter weight in pounds: ")); BMIPanel.add(f2); BMIPanel.add(new JLabel("Enter height in inches: ")); BMIPanel.add(f3); int result = JOptionPane.showConfirmDialog(null, BMIPanel, "Enter the Following Values:", JOptionPane.OK_CANCEL_OPTION); name = f1.getText(); w = Integer.parseInt(f2.getText()); h = Integer.parseInt(f3.getText()); if (w < 2 || w > 800) { JOptionPane.showMessageDialog(null, "ERROR: Invalid weight." + " Shutting down."); System.exit(0); } if (h < 10 || h > 108) { JOptionPane.showMessageDialog(null, "ERROR: Invalid height." + " Shutting down."); System.exit(0); } BMI = (w * 703) / (h * h); if (BMI < 18.5) { comment = "You are underweight."; underWeight++; } else if (BMI < 25) { comment = "You are at a normal weight."; normalWeight++; } else if (BMI < 30) { comment = "You are overweight."; overWeight++; } else { comment = "You are obese."; obese++; } if (result == JOptionPane.OK_OPTION) { JOptionPane.showMessageDialog(null, "Name: " + name + " Weight: " + w + " Height: " + h + " BMI: " + BMI + " " + comment); } } JOptionPane.showMessageDialog(null, "# Underweight: " + underWeight + " # Normal Weight: " + normalWeight + " # Overweight: " + overWeight + " # Obese: " + obese); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(null, "FATAL ERROR: Input is not " + "numeric. Shutting down."); System.exit(0); } }
public static void main(String[] args) { new BMIProgram().getBMIData(); JOptionPane.showMessageDialog(null, "Thank you for using my program." + " End of job.");
System.exit(0); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started