News:

Please note these forums are mostly a testing ground for my SMF work and I don't really use them otherwise.

Main Menu

Paste-1204954299:v:use_geshi-1:v:type-php

Started by JayBachatero, Mar 08, 2008, 05:31 AM

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

JayBachatero

// A few libraries that we need.
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JFrame;
import java.awt.BorderLayout;

public class StudentTest
{
   public static void main(String args[])
   {
      // Integers.
      int totalStudents = 0;
      // Input, input, input, name.
      String input, name;
      // Options for the menu.
      int option = 0;
      // Lets keep track of the number of students added.
      int studentsAdded = 0;
      // We get the grades in an array.
      int grades[] = new int[3];

      JFrame frame = new JFrame();
      JFrame frame1 = new JFrame();
      JTextArea textArea = new JTextArea(10, 70);
      //JScrollPane scrollPanel = new JScrollPane(textArea);
      // Can't edit it.
      textArea.setEditable(false);

      // We want a nice menu.
      MenuController menu = new MenuController();
      // Nice headers.
      menu.addMenuHeader("Welcome to BMCC Student Grades Management Center");
      // This is what we are allowed to do.
      menu.addMenuItem("Add a Student");
      menu.addMenuItem("Students Grades Report");
      menu.addMenuItem("Students Highest Grades Report");
      menu.addMenuExitItem("Exit");

      // Before anything else lets just find out how many students we want to add.
      do
      {
         input = JOptionPane.showInputDialog("How many students do you want to manage?");
//         /JOptionPane.showMessageDialog(null, input);
         if (input.equals(""))
            JOptionPane.showMessageDialog(null, "You must enter a valid number.");
         else
            totalStudents = Integer.parseInt(input);
      }
      while (input.equals(""));
      
      // We want students.
      Student stu[] = new Student[totalStudents];

      do
      {
         // Lets tell them how many students they have added.
         menu.addMenuFooter("You have added " + studentsAdded + " out of " + totalStudents + ".");
         // Lets get the input.
         input = JOptionPane.showInputDialog(menu.getMenuItemsString());
         if (input == null)
            break;
         else
            option = Integer.parseInt(input);

         // Lets add the student.
         if (option == 1)
         {
            if (studentsAdded >= totalStudents)
            {
               JOptionPane.showMessageDialog(null, "You can't add anymore students.");
               continue;
            }

            name = readName();
            grades = readGrades();
            stu[studentsAdded++] = new Student(name, grades);
         }
         else if (option == 2)
         {
            // Get the students.
            //textArea.setText("");
            //textArea.append(getStudentsReport(stu, studentsAdded));
            JScrollPane scrollPanel = new JScrollPane(getStudentsReport(stu, studentsAdded));
            // Some settings for the frame.
            frame.add(scrollPanel, BorderLayout.CENTER);
            frame.setSize(600, 200);
            frame.setVisible(true);
            frame.setTitle("Students Grades by Juan Hernandez");
            //JOptionPane.showMessageDialog(null, scrollPanel);
         }
         else if (option == 3)
         {
            // Get the students.
            //textArea.setText("");
            //textArea.append(getStudentsHighestGradeReport(stu, studentsAdded));
            //JOptionPane.showMessageDialog(null, scrollPanel);
            JScrollPane scrollPanel1 = new JScrollPane(getStudentsHighestGradeReport(stu, studentsAdded));
            // Some settings for the frame.
            frame1.add(scrollPanel1, BorderLayout.CENTER);
            frame1.setSize(400, 200);
            frame1.setVisible(true);
            frame1.setAlwaysOnTop(true);
            frame1.setTitle("Students Highest Grades by Juan Hernandez");
         }
         else if (option != 4)
            JOptionPane.showMessageDialog(null, "Please select a valid option.");
         
      }
      while (option != menu.getExitStep());
   }

   /**
    * Reads and returns the name from the keyboard.
    * @return
    */
   public static String readName()
   {
      return JOptionPane.showInputDialog("Enter the name of the student: ");
   }

   /**
    * Gets 3 grades for the students.
    * @return the grades in an array.
    */
   public static int[] readGrades()
   {
      // We store in an array to return in an array.
      int grades[] = new int[3];

      for (int i = 0; i < grades.length; i++)
         grades = Integer.parseInt(JOptionPane.showInputDialog("Enter grade: " + (i + 1) + ": "));

      return grades;
   }

   /**
    * Generates a report of grades for the students.
    * @param stu
    * @param studentsAdded
    * @return
    */
   public static JTable getStudentsReport(Student stu[], int studentsAdded)
   {
      // The objects for the table.
      Object columnNames[] = {"Name", "Grade 1", "Grade 2", "Grade 3", "Average", "Performance"};
      // Lets do some work to just render the rows used.
      Object rowData[][] = new Object[stu.length > studentsAdded ? studentsAdded : stu.length][columnNames.length];

      for (int i = 0; i < stu.length; i++)
      {
         // If we want to check the reports before we add all the students,
         // then we need to exit before the end of the array.
         if (i == studentsAdded && i < stu.length)
            break;

         // Add the data.
         rowData[0] = stu.getName();
         rowData[1] = stu.getGrades(0);
         rowData[2] = stu.getGrades(1);
         rowData[3] = stu.getGrades(2);
         rowData[4] = stu.findAverage();
         rowData[5] = stu.whatLetterGrade(stu.findAverage());
      }

      // Table time.
      JTable table = new JTable(rowData, columnNames);
      return table;
   }

   public static JTable getStudentsHighestGradeReport(Student stu[], int studentsAdded)
   {
      //String students = "\tSTUDENTS HIGHEST GRADES by Juan Hernandez";

      //students += "\nNAME\t\tGRADE";
      Object columnNames1[] = {"Name", "Grade"};
      Object rowData1[][] = new Object[stu.length > studentsAdded ? studentsAdded : stu.length][columnNames1.length];

      for (int i = 0; i < stu.length; i++)
      {
         // If we want to check the reports before we add all the students,
         // then we need to exit before the end of the array.
         if (i == studentsAdded && i < stu.length)
            break;

         rowData1[0] = stu.getName();
         rowData1[1] = stu.getHighestGrade(stu.getGrades());
      }
   
      JTable table1 = new JTable(rowData1, columnNames1);
      return table1;
   }
}