willieshi232

Untitled

May 1st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.22 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5. import java.io.*;
  6.  
  7. public class StudentGui
  8. {
  9. //Container contents;
  10. private CardLayout contents = new CardLayout();
  11.  
  12. JFrame frame = new JFrame("Student Course Scheduler");
  13.  
  14. //Panels for different cards and cardlayout hoster
  15. JPanel cont = new JPanel();
  16. JPanel maingui = new JPanel();
  17. JPanel coursechange = new JPanel();
  18.  
  19. //ArrayList for loading and controlling the JList information
  20. Map<String, course> ccm = new HashMap<>();
  21. private DefaultListModel<String> mcc = new DefaultListModel<>();
  22. private DefaultListModel<String> mcsc = new DefaultListModel<>();
  23. private ArrayList<String> list = new ArrayList<>();
  24. private int control;
  25.  
  26. //GUI components for Main
  27. private JButton coursechanger,trackreqs;
  28. private ImageIcon studentphoto;
  29. private JLabel studentname;
  30.  
  31. //GUI components for course change
  32. private JButton AddCourse;
  33. private JButton Back;
  34. private JLabel CourseCat;
  35. private JList<String> CourseCatalog;
  36. private JLabel Courseselec;
  37. private javax.swing.JLabel CurrentCour;
  38. private javax.swing.JList<String> CurrentCourses;
  39. private javax.swing.JButton DeleteCourse;
  40. private javax.swing.JScrollPane jScrollPane1;
  41. private javax.swing.JScrollPane jScrollPane2;
  42. private JOptionPane getbettergrades;
  43. private JOptionPane yaho;
  44. private JOptionPane track;
  45.  
  46. //student transitional variable to modify current courses of student temp
  47. student x;
  48.  
  49.  
  50.  
  51.  
  52. public StudentGui(student temp)
  53. {
  54. x = temp;
  55. //adds the card layout
  56. cont.setLayout(contents);
  57.  
  58. //initializes most of the necessary components for the courseselction part of the GUI
  59. updateJList(temp); //updates the JList according to the student's files
  60. initComponents();
  61.  
  62. //adds the first JPanel
  63. updateMain(temp);
  64.  
  65. //adds the second JPanel
  66. updatecoursechange();
  67.  
  68. //shows the main gui as the first thing you see when launching student gui
  69. contents.show(cont, "1");
  70. frame.add(cont);
  71. frame.pack();
  72. frame.setVisible(true);
  73.  
  74. }
  75.  
  76. public void updateJList(student x)
  77. {
  78. //adds the course catalog
  79. String text2;
  80. try (Scanner courseinit = new Scanner(new File("CurrentCourseList.txt"))) {
  81. while (courseinit.hasNextLine()) {
  82. text2 = courseinit.nextLine();
  83. processLine(text2);
  84. }
  85. } catch (IOException exp) {
  86. exp.printStackTrace();
  87. }
  88.  
  89. ArrayList<course> temp = x.getCurrentcourses();
  90. for(int i = 0; i < temp.size(); i++)
  91. {
  92. mcsc.addElement(temp.get(i).getName());
  93. }
  94.  
  95.  
  96. }
  97.  
  98. //initializes all the J Components etc
  99. private void initComponents()
  100. {
  101.  
  102. jScrollPane1 = new javax.swing.JScrollPane();
  103. CourseCatalog = new javax.swing.JList<>(mcc);
  104. jScrollPane2 = new javax.swing.JScrollPane();
  105. CurrentCourses = new javax.swing.JList<>(mcsc);
  106. CourseCat = new javax.swing.JLabel();
  107. CurrentCour = new javax.swing.JLabel();
  108. AddCourse = new javax.swing.JButton();
  109. DeleteCourse = new javax.swing.JButton();
  110. Courseselec = new javax.swing.JLabel();
  111. Back = new javax.swing.JButton();
  112. getbettergrades = new JOptionPane();
  113. yaho = new JOptionPane();
  114. track = new JOptionPane();
  115.  
  116. frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  117.  
  118. jScrollPane1.setViewportView(CourseCatalog);
  119.  
  120. jScrollPane2.setViewportView(CurrentCourses);
  121.  
  122. control = 0;
  123.  
  124. CourseCat.setText("CourseCatalog");
  125.  
  126. CurrentCour.setText("Current Courses");
  127.  
  128. AddCourse.setText("Add Course");
  129. AddCourse.addActionListener(new MyHandler());
  130.  
  131.  
  132. DeleteCourse.setText("Delete Course");
  133. DeleteCourse.addActionListener(new MyHandler());
  134.  
  135.  
  136.  
  137. Courseselec.setText("Course Selector");
  138.  
  139. Back.setText("Back");
  140. Back.addActionListener(new MyHandler());
  141.  
  142. }
  143.  
  144. //button traffic handler
  145. private class MyHandler implements ActionListener
  146. {
  147. public void actionPerformed(ActionEvent ae)
  148. {
  149. //placeholder variables
  150. int temp;
  151. String temp2;
  152. course temp3;
  153.  
  154. //back pressed go back to Student Page
  155. if(ae.getSource() == Back)
  156. {
  157. contents.show(cont, "1");
  158. }
  159. //if trackreqs is pressed
  160. if(ae.getSource() == trackreqs)
  161. {
  162. track.showMessageDialog(maingui, "Not ready yet");
  163. }
  164. //switches to course changer pane
  165. if(ae.getSource() == coursechanger)
  166. {
  167. contents.show(cont, "2");
  168. }
  169.  
  170. //deletes the course
  171. if(ae.getSource() == DeleteCourse)
  172. {
  173. temp = CurrentCourses.getSelectedIndex();
  174. if(temp == -1)
  175. {
  176. //do nothing
  177. }
  178. else
  179. {
  180. temp2 = mcsc.getElementAt(temp);
  181. mcsc.remove(temp);
  182.  
  183. System.out.print(temp2);
  184.  
  185. //deletes the course from the student file
  186. DeleteCourse(temp2);
  187. }
  188. }
  189.  
  190. //adds a course
  191. if(ae.getSource() == AddCourse)
  192. {
  193. temp = CourseCatalog.getSelectedIndex();
  194. temp2 = CourseCatalog.getSelectedValue();
  195. temp3 = ccm.get(temp2);
  196.  
  197. if(temp3 == null)
  198. {
  199. //do nothing
  200. }
  201. //if it satisfies the course requirements and the student isn't already taking the course
  202. else if(temp3.getgpareq() <= x.getGpa() && !mcsc.contains(temp2))
  203. {
  204. mcsc.addElement(temp2);
  205. AddCourse(mcc.getElementAt(temp));
  206. }
  207. else if (mcsc.contains(temp2))
  208. {
  209. yaho.showMessageDialog(coursechange, "You already have one of these courses");
  210. }
  211. else
  212. {
  213. getbettergrades.showMessageDialog(coursechange, "Get better grades");
  214. }
  215. control = 0;
  216. list.clear();
  217.  
  218. }
  219.  
  220. }
  221. }
  222. //updates first panel and initializes the necessary variables
  223. public void updateMain(student temp)
  224. {
  225. coursechanger = new JButton("Manage your courses");
  226. coursechanger.addActionListener(new MyHandler());
  227. maingui.add(coursechanger);
  228. trackreqs = new JButton("track reqs");
  229. maingui.add(trackreqs);
  230. cont.add(maingui,"1");
  231.  
  232. }
  233.  
  234. //update second panel
  235. public void updatecoursechange()
  236. {
  237. GroupLayout layout = new GroupLayout(coursechange);
  238. layout.setHorizontalGroup
  239. (
  240. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  241. .addGroup(layout.createSequentialGroup()
  242. .addGap(29, 29, 29)
  243. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  244. .addGroup(layout.createSequentialGroup()
  245. .addComponent(CourseCat)
  246. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  247. .addGroup(layout.createSequentialGroup()
  248. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
  249. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  250. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  251. .addComponent(DeleteCourse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  252. .addComponent(AddCourse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  253. .addComponent(Back, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  254. .addGap(31, 31, 31)))
  255. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  256. .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
  257. .addComponent(CurrentCour))
  258. .addGap(83, 83, 83))
  259. .addGroup(layout.createSequentialGroup()
  260. .addGap(193, 193, 193)
  261. .addComponent(Courseselec)
  262. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  263. );
  264. layout.setVerticalGroup
  265. (
  266. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  267. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  268. .addComponent(Courseselec)
  269. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
  270. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  271. .addComponent(CourseCat)
  272. .addComponent(CurrentCour))
  273. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  274. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  275. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
  276. .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
  277. .addGroup(layout.createSequentialGroup()
  278. .addComponent(AddCourse)
  279. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  280. .addComponent(DeleteCourse)
  281. .addGap(48, 48, 48)
  282. .addComponent(Back)))
  283. .addGap(49, 49, 49))
  284. );
  285. cont.add(coursechange, "2");
  286. }
  287.  
  288. //process the course files to add to the JList line by line
  289. public void processLine(String text)
  290. {
  291. String tempname;
  292. int tempmax;
  293. int tempmin;
  294. double tempgpareg;
  295. ArrayList<Integer> tempperiodsO = new ArrayList<>();
  296.  
  297. Scanner linescan = new Scanner(text);
  298. tempname = linescan.next();
  299. tempmax = linescan.nextInt();
  300. tempmin = linescan.nextInt();
  301. tempgpareg = linescan.nextDouble();
  302. while(linescan.hasNextInt())
  303. {
  304. tempperiodsO.add(linescan.nextInt());
  305. }
  306.  
  307. course x = new course(tempname, tempmax, tempmin, tempgpareg, tempperiodsO);
  308. mcc.addElement(x.getName());
  309. ccm.put(x.getName(), x);
  310.  
  311.  
  312. }
  313.  
  314. //method to delete a course from the file
  315. public void DeleteCourse(String del)
  316. {
  317. String charset = "UTF-8";
  318. String delete = del;
  319. String newtext = null;
  320. try
  321. {
  322. File file = new File("CurrentStudentList.txt");
  323. BufferedReader reader = new BufferedReader(new FileReader(file));
  324. String line = "";
  325. String txt = "";
  326. while((line = reader.readLine()) != null)
  327. {
  328. txt += line + "\r\n";
  329. }
  330. reader.close();
  331. // replace a word in a file
  332. if(control == 0)
  333. {
  334. newtext = txt.replaceAll(delete, "");
  335.  
  336. }
  337.  
  338. FileWriter writer = new FileWriter("CurrentStudentList.txt");
  339. writer.write(newtext);writer.close();
  340. }
  341. catch (IOException ioe)
  342. {
  343. ioe.printStackTrace();
  344. }
  345. }
  346.  
  347. //method to add a course into the file
  348. public void AddCourse(String add)
  349. {
  350. String text2;
  351. BufferedWriter writer = null;
  352. try (Scanner courseinit = new Scanner(new File("CurrentStudentList.txt")))
  353. {
  354. while (courseinit.hasNextLine())
  355. {
  356. text2 = courseinit.nextLine();
  357. //System.out.println(text2);
  358. processLine2(text2, add);
  359. }
  360. writer = new BufferedWriter(new FileWriter("CurrentStudentList.txt"));
  361. for (int i = 0; i < list.size(); i++) {
  362. //System.out.println(list.get(i));
  363. writer.write(list.get(i) + "\r\n");
  364. }
  365. writer.close();
  366. }
  367. catch (IOException exp)
  368. {
  369. exp.printStackTrace();
  370. }
  371.  
  372. }
  373. public void processLine2(String text, String add)
  374. {
  375. Scanner linescan = new Scanner(text);
  376. Boolean check = false;
  377. ArrayList<String> temp4 = new ArrayList<>();
  378. String temp;
  379. while(linescan.hasNext())
  380. {
  381. temp = linescan.next();
  382. temp4.add(temp);
  383. if(temp4.contains(x.getname()))
  384. {
  385. check = true;
  386. }
  387.  
  388. }
  389. if(check = true && control == 0)
  390. {
  391. temp4.add(add);
  392. control++;
  393. check = false;
  394. }
  395. String list2 = fixarraytostring(temp4);
  396. list.add(list2);
  397. }
  398.  
  399. //changes ArrayList to a printable string form or to be used for file scanning
  400. public String fixarraytostring(ArrayList<String> x)
  401. {
  402. String temp = Arrays.toString(x.toArray()).replace("[", "").replace("]", "").replace(",","");
  403. return temp;
  404. }
  405.  
  406.  
  407.  
  408.  
  409.  
  410. }
Advertisement
Add Comment
Please, Sign In to add comment