Guest User

Untitled

a guest
Jul 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.62 KB | None | 0 0
  1. import java.awt.*;
  2. import java.util.*;
  3. import java.io.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. public class test extends JFrame
  8. {
  9. private JPanel nameHolder,colorHolder, option, numHolder,placeHolder,occupationHolder,autoHolder;
  10. private JLabel names,cars,nums,occupations,places, choice;
  11. private JTextField name1,name2,name3,name4;
  12. private JTextField color1,color2,color3,color4;
  13. private JTextField place1,place2,place3,place4;
  14. private JTextField oc1,oc2,oc3,oc4;
  15. private JTextField auto1,auto2,auto3,auto4;
  16. private JTextField num1,num2,num3,num4;
  17. private String m ="M";
  18. private String a ="A";
  19. private String s ="S";
  20. private String h ="H";
  21. private String input1,input2,input3,input4,input5,input6,input7,input8;
  22. private String input9,input10,input11,input12,input13,input14,input15,input16;
  23. private String input17,input18,input19,input20,input21,input22,input23, input24;
  24. private String []array, apple;
  25. private boolean [] bitarray;
  26. private int selectednum;
  27.  
  28. char ch;
  29. private final int val = 7;
  30. private final int val2 = 4;
  31. private JButton Ok, Cancel, Send;
  32. private JRadioButton b1,b2,b3,b4;
  33. private ButtonGroup radioGroup;
  34. // private int B1,B2,B3;
  35. public test()
  36. {
  37. setTitle("M*A*S*H - Can you handle your future?");
  38. setBackground(Color.orange);
  39. setLayout(new GridLayout(2,3));
  40. setPreferredSize(new Dimension(900,400));
  41. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42. buildPanel();
  43. pack();
  44. setVisible(true);
  45. }
  46. private void buildPanel()
  47. {
  48.  
  49. //Panel to respective sides
  50. nameHolder = new JPanel();
  51. autoHolder = new JPanel();
  52. colorHolder = new JPanel();
  53. placeHolder = new JPanel();
  54. numHolder = new JPanel();
  55. occupationHolder = new JPanel();
  56.  
  57. //Panel holds Ok and Cancel button
  58. option = new JPanel();
  59.  
  60. Ok = new JButton("Ok");
  61.  
  62. // Action when pressed Ok button
  63. Ok.addActionListener(new OKButtonListener());
  64. option.add(Ok);
  65.  
  66. Cancel = new JButton("Cancel");
  67. // Action when pressed Cancel button
  68. Cancel.addActionListener(new CancelButtonListener());
  69. option.add(Cancel);
  70.  
  71. choice= new JLabel("Pick a number\t");
  72.  
  73. radioGroup = new ButtonGroup();
  74. b1= new JRadioButton("1");
  75. b2= new JRadioButton("2");
  76. b3= new JRadioButton("3");
  77. b4= new JRadioButton("4");
  78. radioGroup.add(b1);
  79. radioGroup.add(b2);
  80. radioGroup.add(b3);
  81. radioGroup.add(b4);
  82.  
  83. b1.addItemListener(new RadioButtonHandler(1));
  84. b2.addItemListener(new RadioButtonHandler(2));
  85. b3.addItemListener(new RadioButtonHandler(3));
  86. b4.addItemListener(new RadioButtonHandler(4));
  87.  
  88. option.add(choice);
  89.  
  90.  
  91. option.add(b1);
  92. option.add(b2);
  93. option.setBackground(Color.orange);
  94. option.add(b3);
  95. option.add(b4);
  96.  
  97.  
  98. nameHolder();
  99. add(nameHolder);
  100. colorHolder();
  101. add(colorHolder);
  102. autoHolder();
  103. add(autoHolder);
  104. placeHolder();
  105. add(placeHolder);
  106. occupationHolder();
  107. add(occupationHolder);
  108. numHolder();
  109. add(numHolder);
  110. add(option);
  111.  
  112. }
  113.  
  114. public void nameHolder()
  115. {
  116. nameHolder.setBackground(Color.orange);
  117. names = new JLabel("Names");
  118. name1 = new JTextField(15);
  119. name2 = new JTextField(15);
  120. name3 = new JTextField(15);
  121. name4 = new JTextField(15);
  122.  
  123. nameHolder.add(names);
  124. nameHolder.add(name1);
  125. nameHolder.add(name2);
  126. nameHolder.add(name3);
  127. nameHolder.add(name4);
  128.  
  129. }
  130.  
  131. public void colorHolder()
  132. {
  133. colorHolder.setBackground(Color.orange);
  134. JLabel colors = new JLabel("Colors");
  135. color1 = new JTextField(15);
  136. color2 = new JTextField(15);
  137. color3 = new JTextField(15);
  138. color4 = new JTextField(15);
  139. colorHolder.add(colors);
  140. colorHolder.add(color1);
  141. colorHolder.add(color2);
  142. colorHolder.add(color3);
  143. colorHolder.add(color4);
  144. }
  145.  
  146. public void autoHolder()
  147. {
  148. autoHolder.setBackground(Color.orange);
  149. cars = new JLabel(" Cars ");
  150. auto1 = new JTextField(15);
  151. auto2 = new JTextField(15);
  152. auto3 = new JTextField(15);
  153. auto4 = new JTextField(15);
  154. autoHolder.add(cars);
  155. autoHolder.add(auto1);
  156. autoHolder.add(auto2);
  157. autoHolder.add(auto3);
  158. autoHolder.add(auto4);
  159. }
  160. public void occupationHolder()
  161. {
  162. occupationHolder.setBackground(Color.orange);
  163. occupations = new JLabel("Careers");
  164. oc1 = new JTextField(15);
  165. oc2 = new JTextField(15);
  166. oc3 = new JTextField(15);
  167. oc4 = new JTextField(15);
  168. occupationHolder.add(occupations);
  169. occupationHolder.add(oc1);
  170. occupationHolder.add(oc2);
  171. occupationHolder.add(oc3);
  172. occupationHolder.add(oc4);
  173. }
  174. public void numHolder()
  175. {
  176. numHolder.setBackground(Color.orange);
  177. nums = new JLabel("Numbers");
  178. num1 = new JTextField(15);
  179. num2 = new JTextField(15);
  180. num3 = new JTextField(15);
  181. num4 = new JTextField(15);
  182. numHolder.add(nums);
  183. numHolder.add(num1);
  184. numHolder.add(num2);
  185. numHolder.add(num3);
  186. numHolder.add(num4);
  187. }
  188. public void placeHolder()
  189. {
  190. placeHolder.setBackground(Color.orange);
  191. places = new JLabel("Places");
  192. place1= new JTextField(15);
  193. place2= new JTextField(15);
  194. place3= new JTextField(15);
  195. place4= new JTextField(15);
  196. placeHolder.add(places);
  197. placeHolder.add(place1);
  198. placeHolder.add(place2);
  199. placeHolder.add(place3);
  200. placeHolder.add(place4);
  201. }
  202.  
  203. /* public void check(char ch)
  204. {
  205. if(!Character.isLetter(ch))
  206. {
  207. JOptionPane.showMessageDialog(null,"ERROR: Invalid inputs found! \n {Tip: Check inputs in Names,Cars,Careers,Places.}");
  208. }
  209. else if(!Character.isDigit(ch))
  210. {
  211. JOptionPane.showMessageDialog(null,"ERROR:Invalid inputs found! \n Check Numbers section.}");
  212. }
  213. }
  214. */
  215.  
  216. public void addValue()
  217. {
  218.  
  219. input5 = name1.getText();
  220.  
  221. input6 = name2.getText();
  222.  
  223. input7 = name3.getText();
  224.  
  225. input8 = name4.getText();
  226.  
  227. input1 = auto1.getText();
  228.  
  229. input2 = auto2.getText();
  230.  
  231. input3 = auto3.getText();
  232.  
  233. input4 = auto4.getText();
  234.  
  235. input9 = color1.getText();
  236.  
  237. input10 = color2.getText();
  238.  
  239. input11 = color3.getText();
  240.  
  241. input12 = color4.getText();
  242.  
  243. input13 = oc1.getText();
  244.  
  245. input14 = oc2.getText();
  246.  
  247. input15 = oc3.getText();
  248.  
  249. input16 = oc4.getText();
  250.  
  251. input17 = num1.getText();
  252.  
  253. input18 = num2.getText();
  254.  
  255. input19 = num3.getText();
  256.  
  257. input20 = num4.getText();
  258.  
  259. input21 = place1.getText();
  260.  
  261. input22 = place2.getText();
  262.  
  263. input23 = place3.getText();
  264.  
  265. input24 = place4.getText();
  266.  
  267. /* ArrayList list = new ArrayList();
  268. list.add(input20);
  269. list.add(input21);
  270. list.add(input22);
  271. list.add(input23);
  272. list.add(input24);
  273. */
  274. String []info = {m,a,s,h,input1,input2,input3,input4,input5,input6,input7,input8,
  275. input9,input10,input11,input12,input13,input14,input15,input16,
  276. input17,input18,input19,input20,input21,input22,input23, input24};
  277.  
  278. bitarray = new boolean[24];
  279. for(int i=0;i<24;i++)
  280. {
  281. bitarray[i] = false;
  282. }
  283.  
  284. int index = -1;
  285. for(int i=0;i<18;i++)
  286. {
  287. for(int j=selectednum;j>0;j--)
  288. {
  289. do
  290. {
  291. index++;
  292. if(index==24)
  293. index=0;
  294. }while(bitarray[index]==false);
  295. }
  296. bitarray[index]=true;
  297. for(int j=0;j<6;j++)
  298. {
  299. int totaleliminated = 0;
  300. if(bitarray[j*4]==true)
  301. totaleliminated++;
  302. if(bitarray[j*4+1]==true)
  303. totaleliminated++;
  304. if(bitarray[j*4+2]==true)
  305. totaleliminated++;
  306. if(bitarray[j*4+3]==true)
  307. totaleliminated++;
  308. if(totaleliminated==3)
  309. {
  310. if(bitarray[j*4]==false)
  311. {
  312. // extract answer for group
  313. bitarray[j*4]=true;
  314. }
  315. if(bitarray[j*4+1]==false)
  316. {
  317. // extract answer for group
  318. bitarray[j*4+1]=true;
  319. }
  320. if(bitarray[j*4+2]==false)
  321. {
  322. // extract answer for group
  323. bitarray[j*4+2]=true;
  324. }
  325. if(bitarray[j*4+3]==false)
  326. {
  327. // extract answer for group
  328. // example: answerarray[j]=info[j*4+3];
  329. bitarray[j*4+3]=true;
  330. }
  331. }
  332. }
  333.  
  334. }
  335.  
  336. }
  337. public class RadioButtonHandler implements ItemListener
  338. {
  339. private int skipnum;
  340.  
  341. public RadioButtonHandler(int n)
  342. {
  343. skipnum = n;
  344. }
  345.  
  346. public void itemStateChanged( ItemEvent event)
  347. {
  348. selectednum = skipnum;
  349. }
  350. }
  351. public class CancelButtonListener implements ActionListener
  352. {
  353. public void actionPerformed(ActionEvent e)
  354. {
  355. JOptionPane.showMessageDialog(null,"Bye");
  356. System.exit(0);
  357. }
  358. }
  359. public class OKButtonListener implements ActionListener
  360. {
  361. public void actionPerformed(ActionEvent e)
  362. {
  363. //add inputs into array
  364. addValue();
  365. }
  366. }
  367.  
  368. public static void main(String[]args)
  369. {
  370. // Scanner input = new Scanner(System.in);
  371.  
  372. //System.out.print("enter number");
  373. //int nw = input.nextInt();
  374.  
  375.  
  376. new test();
  377.  
  378. }
  379. }
Add Comment
Please, Sign In to add comment