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.

319 lines
12 KiB

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