CI 2019 von Daniel, Eugen und Michael
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
12 KiB

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyAdapter;
  8. import java.awt.event.KeyEvent;
  9. public class FitnessTest_M extends JFrame {
  10. JFrame frame = null;
  11. JMenuItem menuItem_1 = null;
  12. JMenuBar menuBar = null;
  13. JMenu menu = null;
  14. JLabel height_label = null;
  15. JTextField height_tf = null;
  16. JLabel weight_label = null;
  17. JTextField weight_tf = null;
  18. JLabel height_ungueltig = null;
  19. JLabel weight_ungueltig = null;
  20. JButton bmi_rechnen = null;
  21. JLabel question_label1 = null;
  22. JLabel question_label2 = null;
  23. JLabel question_label3 = null;
  24. JLabel question_label4 = null;
  25. JTextField question_tf1 = null;
  26. JTextField question_tf2 = null;
  27. JTextField question_tf3 = null;
  28. JTextField question_tf4 = null;
  29. JButton bmi_confirm = null;
  30. JButton question1_confirm = null;
  31. JButton question2_confirm = null;
  32. JButton question3_confirm = null;
  33. JButton question4_confirm = null;
  34. int points = 0;
  35. public FitnessTest_M() {
  36. frame = new JFrame("Fitness Test");
  37. frame.setLayout(null);
  38. frame.setSize(1920, 1080);
  39. menuBar = new JMenuBar();
  40. frame.setJMenuBar(menuBar);
  41. menu = new JMenu("Option");
  42. menuItem_1 = new JMenuItem("Exit");
  43. menuBar.add(menu);
  44. menu.add(menuItem_1);
  45. menuItem_1.addActionListener(new ActionListener() {
  46. public void actionPerformed(ActionEvent a) {
  47. if(a.getSource() == menuItem_1) {
  48. System.exit(0);
  49. }
  50. }
  51. });
  52. height_ungueltig = new JLabel();
  53. height_ungueltig.setForeground(Color.red);
  54. height_label = new JLabel("Geben sie hier ihre Koerpergroesse ein (in Meter)");
  55. height_label.setBounds(10, 10, 280, 30);
  56. height_tf = new JTextField(40);
  57. height_tf.setBounds(10, 40, 280, 30);
  58. height_tf.addKeyListener(new KeyAdapter() {
  59. @Override
  60. public void keyPressed(KeyEvent e) {
  61. try {
  62. float height = Float.parseFloat(height_tf.getText());
  63. height_ungueltig.setText("");
  64. } catch (NumberFormatException e1) {
  65. height_ungueltig.setText("Ungueltige Nummer");
  66. height_ungueltig.setBounds(10, 70, 280, 20);
  67. }
  68. }
  69. });
  70. weight_ungueltig = new JLabel();
  71. weight_ungueltig.setForeground(Color.red);
  72. weight_label = new JLabel("Geben sie hier ihr Gewicht ein (in Kilogramm)");
  73. weight_label.setBounds(320, 10, 300, 30);
  74. weight_tf = new JTextField(40);
  75. weight_tf.setBounds(320, 40, 300, 30);
  76. weight_tf.addKeyListener(new KeyAdapter() {
  77. @Override
  78. public void keyPressed(KeyEvent w) {
  79. try {
  80. float weight = Float.parseFloat(weight_tf.getText());
  81. weight_ungueltig.setText("");
  82. } catch (NumberFormatException w1) {
  83. weight_ungueltig.setText("Ungueltige Nummer");
  84. weight_ungueltig.setBounds(320, 70, 300, 20);
  85. }
  86. }
  87. });
  88. question_label1 = new JLabel("Trainieren sie: \n A: Regelmässig B: Ab und zu C: Gar nicht");
  89. question_label1.setBounds(660, 10, 500, 60);
  90. question_label2 = new JLabel("Essen sie: \n A: 1-2 mal am Tag B: 3-4 mal am Tag C: 5+ mal am Tag");
  91. question_label2.setBounds(1170, 10, 500, 60);
  92. question_label3 = new JLabel("Rauchen sie?: \n A: Sehr oft B: Ab und zu C: Nie");
  93. question_label3.setBounds(510, 150, 500, 60);
  94. question_label4 = new JLabel("Trinken sie regelmässig Alkohol?: \n A: Sehr oft B: Ab und zu C: Nie");
  95. question_label4.setBounds(10, 150, 500, 60);
  96. question_tf1 = new JTextField(10);
  97. question_tf1.setBounds(660, 70, 500, 30);
  98. question_tf2 = new JTextField(10);
  99. question_tf2.setBounds(1170, 70, 500, 30);
  100. question_tf3 = new JTextField(10);
  101. question_tf3.setBounds(520, 210, 500, 30);
  102. question_tf4 = new JTextField(10);
  103. question_tf4.setBounds(10, 210, 500, 30);
  104. question1_confirm = new JButton("Bestätigen");
  105. question1_confirm.setBounds(660, 100, 150, 30);
  106. question1_confirm.addActionListener(new ActionListener() {
  107. public void actionPerformed(ActionEvent e) {
  108. if (e.getSource() == question1_confirm) {
  109. if(question_tf1.getText().startsWith("A") || question_tf1.getText().startsWith("a")) {
  110. points += 10;
  111. }
  112. if(question_tf1.getText().startsWith("B") || question_tf1.getText().startsWith("b")) {
  113. points += 5;
  114. }
  115. if(question_tf1.getText().startsWith("C") || question_tf1.getText().startsWith("c")) {
  116. points += 0;
  117. }
  118. }
  119. }
  120. });
  121. question2_confirm = new JButton("Bestätigen");
  122. question2_confirm.setBounds(1170, 100, 150, 30);
  123. question2_confirm.addActionListener(new ActionListener() {
  124. public void actionPerformed(ActionEvent e) {
  125. if (e.getSource() == question2_confirm) {
  126. if(question_tf2.getText().startsWith("A") || question_tf2.getText().startsWith("a")) {
  127. points += 10;
  128. }
  129. if(question_tf2.getText().startsWith("B") || question_tf2.getText().startsWith("b")) {
  130. points += 5;
  131. }
  132. if(question_tf2.getText().startsWith("C") || question_tf2.getText().startsWith("c")) {
  133. points += 0;
  134. }
  135. }
  136. }
  137. });
  138. question3_confirm = new JButton("Bestätigen");
  139. question3_confirm.setBounds(520, 240, 150, 30);
  140. question3_confirm.addActionListener(new ActionListener() {
  141. public void actionPerformed(ActionEvent e) {
  142. if (e.getSource() == question3_confirm) {
  143. if(question_tf3.getText().startsWith("A") || question_tf3.getText().startsWith("a")) {
  144. points += 0;
  145. }
  146. if(question_tf3.getText().startsWith("B") || question_tf3.getText().startsWith("b")) {
  147. points += 4;
  148. }
  149. if(question_tf3.getText().startsWith("C") || question_tf3.getText().startsWith("c")) {
  150. points += 10;
  151. }
  152. }
  153. }
  154. });
  155. question4_confirm = new JButton("Bestätigen");
  156. question4_confirm.setBounds(10, 240, 150, 30);
  157. question4_confirm.addActionListener(new ActionListener() {
  158. public void actionPerformed(ActionEvent e) {
  159. if (e.getSource() == question4_confirm) {
  160. if(question_tf4.getText().startsWith("A") || question_tf4.getText().startsWith("a")) {
  161. points += 0;
  162. }
  163. if(question_tf4.getText().startsWith("B") || question_tf4.getText().startsWith("b")) {
  164. points += 4;
  165. }
  166. if(question_tf4.getText().startsWith("C") || question_tf4.getText().startsWith("c")) {
  167. points += 10;
  168. }
  169. }
  170. }
  171. });
  172. frame.add(height_label);
  173. frame.add(height_tf);
  174. frame.add(height_ungueltig);
  175. frame.add(weight_label);
  176. frame.add(weight_tf);
  177. frame.add(weight_ungueltig);
  178. frame.add(question_label1);
  179. frame.add(question_label2);
  180. frame.add(question_label3);
  181. frame.add(question_label4);
  182. frame.add(question_tf1);
  183. frame.add(question_tf2);
  184. frame.add(question_tf3);
  185. frame.add(question_tf4);
  186. frame.add(question1_confirm);
  187. frame.add(question2_confirm);
  188. frame.add(question3_confirm);
  189. frame.add(question4_confirm);
  190. bmi_rechnen = new JButton("BMI Berechnen");
  191. bmi_rechnen.setBounds(100, 90, 150, 30);
  192. bmi_rechnen.addActionListener(new ActionListener() {
  193. public void actionPerformed(ActionEvent b) {
  194. if(b.getSource() == bmi_rechnen) {
  195. float height = Float.parseFloat(height_tf.getText());
  196. float weight = Float.parseFloat(weight_tf.getText());
  197. int bmi = calculateBmi(height, weight);
  198. if(bmi < 19) {
  199. JOptionPane.showMessageDialog(null,
  200. "Ihr BMI: " + bmi + " \nUntergewicht!",
  201. "Ergebnis",
  202. JOptionPane.WARNING_MESSAGE);
  203. }
  204. if(bmi >= 19 && bmi <= 25) {
  205. JOptionPane.showMessageDialog(null,
  206. "Ihr BMI: " + bmi + " \nNormalgewicht!",
  207. "Ergebnis",
  208. JOptionPane.WARNING_MESSAGE);
  209. }
  210. if(bmi > 26 && bmi <= 30) {
  211. JOptionPane.showMessageDialog(null,
  212. "Ihr BMI: " + bmi + " \nLeichtes Übergewicht!",
  213. "Ergebnis",
  214. JOptionPane.WARNING_MESSAGE);
  215. }
  216. if(bmi >= 31) {
  217. JOptionPane.showMessageDialog(null,
  218. "Ihr BMI: " + bmi + " \nÜbergewicht!",
  219. "Ergebnis",
  220. JOptionPane.WARNING_MESSAGE);
  221. }
  222. }
  223. }
  224. });
  225. bmi_confirm = new JButton("BMI Bestätigen");
  226. bmi_confirm.setBounds(400, 90, 150, 30);
  227. bmi_confirm.addActionListener(new ActionListener() {
  228. public void actionPerformed(ActionEvent e) {
  229. if (e.getSource() == bmi_confirm) {
  230. float height = Float.parseFloat(height_tf.getText());
  231. float weight = Float.parseFloat(weight_tf.getText());
  232. int bmi = calculateBmi(height, weight);
  233. if(bmi <= 15) points += 0;
  234. if(bmi == 16) points += 1;
  235. if(bmi == 17) points += 2;
  236. if(bmi == 18) points += 3;
  237. if(bmi == 19) points += 6;
  238. if(bmi == 20) points += 8;
  239. if(bmi == 21) points += 9;
  240. if(bmi == 22) points += 10;
  241. if(bmi == 23) points += 10;
  242. if(bmi == 24) points += 9;
  243. if(bmi == 25) points += 8;
  244. if(bmi == 26) points += 7;
  245. if(bmi == 27) points += 6;
  246. if(bmi == 28) points += 5;
  247. if(bmi == 29) points += 4;
  248. if(bmi == 30) points += 4;
  249. if(bmi == 31) points += 3;
  250. if(bmi == 32) points += 2;
  251. if(bmi == 33) points += 1;
  252. if(bmi >= 34) points += 0;
  253. }
  254. }
  255. });
  256. frame.add(bmi_rechnen);
  257. frame.add(bmi_confirm);
  258. frame.setVisible(true);
  259. }
  260. public static void main(String[] args) {
  261. FitnessTest_M M = new FitnessTest_M();
  262. }
  263. public int calculateBmi(float height, float weight) {
  264. return (int) (weight / (height*height));
  265. }
  266. }