Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import java.io.*;
- public class StudentGui
- {
- //Container contents;
- private CardLayout contents = new CardLayout();
- JFrame frame = new JFrame("Student Course Scheduler");
- //Panels for different cards and cardlayout hoster
- JPanel cont = new JPanel();
- JPanel maingui = new JPanel();
- JPanel coursechange = new JPanel();
- //ArrayList for loading and controlling the JList information
- Map<String, course> ccm = new HashMap<>();
- private DefaultListModel<String> mcc = new DefaultListModel<>();
- private DefaultListModel<String> mcsc = new DefaultListModel<>();
- private ArrayList<String> list = new ArrayList<>();
- private int control;
- //GUI components for Main
- private JButton coursechanger,trackreqs;
- private ImageIcon studentphoto;
- private JLabel studentname;
- //GUI components for course change
- private JButton AddCourse;
- private JButton Back;
- private JLabel CourseCat;
- private JList<String> CourseCatalog;
- private JLabel Courseselec;
- private javax.swing.JLabel CurrentCour;
- private javax.swing.JList<String> CurrentCourses;
- private javax.swing.JButton DeleteCourse;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JScrollPane jScrollPane2;
- private JOptionPane getbettergrades;
- private JOptionPane yaho;
- private JOptionPane track;
- //student transitional variable to modify current courses of student temp
- student x;
- public StudentGui(student temp)
- {
- x = temp;
- //adds the card layout
- cont.setLayout(contents);
- //initializes most of the necessary components for the courseselction part of the GUI
- updateJList(temp); //updates the JList according to the student's files
- initComponents();
- //adds the first JPanel
- updateMain(temp);
- //adds the second JPanel
- updatecoursechange();
- //shows the main gui as the first thing you see when launching student gui
- contents.show(cont, "1");
- frame.add(cont);
- frame.pack();
- frame.setVisible(true);
- }
- public void updateJList(student x)
- {
- //adds the course catalog
- String text2;
- try (Scanner courseinit = new Scanner(new File("CurrentCourseList.txt"))) {
- while (courseinit.hasNextLine()) {
- text2 = courseinit.nextLine();
- processLine(text2);
- }
- } catch (IOException exp) {
- exp.printStackTrace();
- }
- ArrayList<course> temp = x.getCurrentcourses();
- for(int i = 0; i < temp.size(); i++)
- {
- mcsc.addElement(temp.get(i).getName());
- }
- }
- //initializes all the J Components etc
- private void initComponents()
- {
- jScrollPane1 = new javax.swing.JScrollPane();
- CourseCatalog = new javax.swing.JList<>(mcc);
- jScrollPane2 = new javax.swing.JScrollPane();
- CurrentCourses = new javax.swing.JList<>(mcsc);
- CourseCat = new javax.swing.JLabel();
- CurrentCour = new javax.swing.JLabel();
- AddCourse = new javax.swing.JButton();
- DeleteCourse = new javax.swing.JButton();
- Courseselec = new javax.swing.JLabel();
- Back = new javax.swing.JButton();
- getbettergrades = new JOptionPane();
- yaho = new JOptionPane();
- track = new JOptionPane();
- frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jScrollPane1.setViewportView(CourseCatalog);
- jScrollPane2.setViewportView(CurrentCourses);
- control = 0;
- CourseCat.setText("CourseCatalog");
- CurrentCour.setText("Current Courses");
- AddCourse.setText("Add Course");
- AddCourse.addActionListener(new MyHandler());
- DeleteCourse.setText("Delete Course");
- DeleteCourse.addActionListener(new MyHandler());
- Courseselec.setText("Course Selector");
- Back.setText("Back");
- Back.addActionListener(new MyHandler());
- }
- //button traffic handler
- private class MyHandler implements ActionListener
- {
- public void actionPerformed(ActionEvent ae)
- {
- //placeholder variables
- int temp;
- String temp2;
- course temp3;
- //back pressed go back to Student Page
- if(ae.getSource() == Back)
- {
- contents.show(cont, "1");
- }
- //if trackreqs is pressed
- if(ae.getSource() == trackreqs)
- {
- track.showMessageDialog(maingui, "Not ready yet");
- }
- //switches to course changer pane
- if(ae.getSource() == coursechanger)
- {
- contents.show(cont, "2");
- }
- //deletes the course
- if(ae.getSource() == DeleteCourse)
- {
- temp = CurrentCourses.getSelectedIndex();
- if(temp == -1)
- {
- //do nothing
- }
- else
- {
- temp2 = mcsc.getElementAt(temp);
- mcsc.remove(temp);
- System.out.print(temp2);
- //deletes the course from the student file
- DeleteCourse(temp2);
- }
- }
- //adds a course
- if(ae.getSource() == AddCourse)
- {
- temp = CourseCatalog.getSelectedIndex();
- temp2 = CourseCatalog.getSelectedValue();
- temp3 = ccm.get(temp2);
- if(temp3 == null)
- {
- //do nothing
- }
- //if it satisfies the course requirements and the student isn't already taking the course
- else if(temp3.getgpareq() <= x.getGpa() && !mcsc.contains(temp2))
- {
- mcsc.addElement(temp2);
- AddCourse(mcc.getElementAt(temp));
- }
- else if (mcsc.contains(temp2))
- {
- yaho.showMessageDialog(coursechange, "You already have one of these courses");
- }
- else
- {
- getbettergrades.showMessageDialog(coursechange, "Get better grades");
- }
- control = 0;
- list.clear();
- }
- }
- }
- //updates first panel and initializes the necessary variables
- public void updateMain(student temp)
- {
- coursechanger = new JButton("Manage your courses");
- coursechanger.addActionListener(new MyHandler());
- maingui.add(coursechanger);
- trackreqs = new JButton("track reqs");
- maingui.add(trackreqs);
- cont.add(maingui,"1");
- }
- //update second panel
- public void updatecoursechange()
- {
- GroupLayout layout = new GroupLayout(coursechange);
- layout.setHorizontalGroup
- (
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(29, 29, 29)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(CourseCat)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(DeleteCourse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(AddCourse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(Back, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGap(31, 31, 31)))
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(CurrentCour))
- .addGap(83, 83, 83))
- .addGroup(layout.createSequentialGroup()
- .addGap(193, 193, 193)
- .addComponent(Courseselec)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup
- (
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addComponent(Courseselec)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(CourseCat)
- .addComponent(CurrentCour))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addComponent(AddCourse)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(DeleteCourse)
- .addGap(48, 48, 48)
- .addComponent(Back)))
- .addGap(49, 49, 49))
- );
- cont.add(coursechange, "2");
- }
- //process the course files to add to the JList line by line
- public void processLine(String text)
- {
- String tempname;
- int tempmax;
- int tempmin;
- double tempgpareg;
- ArrayList<Integer> tempperiodsO = new ArrayList<>();
- Scanner linescan = new Scanner(text);
- tempname = linescan.next();
- tempmax = linescan.nextInt();
- tempmin = linescan.nextInt();
- tempgpareg = linescan.nextDouble();
- while(linescan.hasNextInt())
- {
- tempperiodsO.add(linescan.nextInt());
- }
- course x = new course(tempname, tempmax, tempmin, tempgpareg, tempperiodsO);
- mcc.addElement(x.getName());
- ccm.put(x.getName(), x);
- }
- //method to delete a course from the file
- public void DeleteCourse(String del)
- {
- String charset = "UTF-8";
- String delete = del;
- String newtext = null;
- try
- {
- File file = new File("CurrentStudentList.txt");
- BufferedReader reader = new BufferedReader(new FileReader(file));
- String line = "";
- String txt = "";
- while((line = reader.readLine()) != null)
- {
- txt += line + "\r\n";
- }
- reader.close();
- // replace a word in a file
- if(control == 0)
- {
- newtext = txt.replaceAll(delete, "");
- }
- FileWriter writer = new FileWriter("CurrentStudentList.txt");
- writer.write(newtext);writer.close();
- }
- catch (IOException ioe)
- {
- ioe.printStackTrace();
- }
- }
- //method to add a course into the file
- public void AddCourse(String add)
- {
- String text2;
- BufferedWriter writer = null;
- try (Scanner courseinit = new Scanner(new File("CurrentStudentList.txt")))
- {
- while (courseinit.hasNextLine())
- {
- text2 = courseinit.nextLine();
- //System.out.println(text2);
- processLine2(text2, add);
- }
- writer = new BufferedWriter(new FileWriter("CurrentStudentList.txt"));
- for (int i = 0; i < list.size(); i++) {
- //System.out.println(list.get(i));
- writer.write(list.get(i) + "\r\n");
- }
- writer.close();
- }
- catch (IOException exp)
- {
- exp.printStackTrace();
- }
- }
- public void processLine2(String text, String add)
- {
- Scanner linescan = new Scanner(text);
- Boolean check = false;
- ArrayList<String> temp4 = new ArrayList<>();
- String temp;
- while(linescan.hasNext())
- {
- temp = linescan.next();
- temp4.add(temp);
- if(temp4.contains(x.getname()))
- {
- check = true;
- }
- }
- if(check = true && control == 0)
- {
- temp4.add(add);
- control++;
- check = false;
- }
- String list2 = fixarraytostring(temp4);
- list.add(list2);
- }
- //changes ArrayList to a printable string form or to be used for file scanning
- public String fixarraytostring(ArrayList<String> x)
- {
- String temp = Arrays.toString(x.toArray()).replace("[", "").replace("]", "").replace(",","");
- return temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment