yoga1290

Java semester6 TODO!

May 30th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.81 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package javaproject;
  6.  
  7. import java.awt.BorderLayout;
  8. import java.awt.Container;
  9. import java.awt.GridBagLayout;
  10. import java.awt.GridLayout;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.io.FileInputStream;
  14. import java.io.PrintWriter;
  15. import java.util.Arrays;
  16. import java.util.Comparator;
  17. import java.util.Scanner;
  18. import javax.swing.JButton;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JPanel;
  22. import javax.swing.JScrollBar;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.JTextArea;
  25. import javax.swing.JTextField;
  26.  
  27. /**
  28.  *
  29.  * @author youssefgamil
  30.  */
  31. class Course
  32. {
  33.     private int credit;
  34.     private String name,id;
  35.     public int getCredit()
  36.     {
  37.         return credit;
  38.     }
  39.     public String getName()
  40.     {
  41.         return name;
  42.     }
  43.     public String getId()
  44.     {
  45.         return id;
  46.     }
  47.     public void setName(String p){name=p;}
  48.     public void setId(String p){id=p;}
  49.     public void setCredit(int p){credit=p;}
  50.     public Course(){}
  51.     public Course(String name,String id,int cr)
  52.     {
  53.         this.name=name;
  54.         this.id=id;
  55.         this.credit=cr;
  56.     }
  57.     public void setCourselnfo(String name,String id,int cr)
  58.     {
  59.         this.name=name;
  60.         this.id=id;
  61.         this.credit=cr;
  62.     }
  63.     public void setlnfo(String name,String id,int cr)
  64.     {
  65.         this.name=name;
  66.         this.id=id;
  67.         this.credit=cr;
  68.     }
  69.    
  70.     @Override
  71.     public String toString()
  72.     {
  73.         System.out.println("Course Name: "+name+
  74.                             "\nCourse ID: "+id+
  75.                             "\nCredit Hours: "+credit);
  76.         return this.toString();
  77.     }
  78. }
  79. class CourseComparator implements Comparator<Course>
  80. {
  81.     @Override
  82.     public int compare(Course o1, Course o2) {
  83.      return o1.getName().compareTo(o2.getName());  
  84.     }
  85. }
  86. class Person
  87. {
  88.     protected String firstName,lastName,id;
  89. }
  90. class Student extends Person
  91. {
  92.     private int numberOfCourses;
  93.     private static double tuitionRate=0;
  94.     private boolean isTuitionPaid=false;
  95.     private Course Courses[]=new Course[6];
  96.     private char   grades[]=new char[6];
  97.     //firstName,LastName,id,isTuitionPaid,courses,grades as in the given example testcase
  98.     public void setInfo(String firstName,String lastName,String id ,int numberOfCourses,boolean isTuitionPaid,Course[] c,char[] grades)
  99.     {
  100.         super.firstName=firstName;
  101.         super.lastName=lastName;
  102.         this.id=id;
  103.         this.isTuitionPaid=isTuitionPaid;
  104.         this.Courses=c;
  105.         this.numberOfCourses=numberOfCourses;
  106.         this.grades=grades;
  107.         this.tuitionRate=tuitionRate;
  108.     }
  109.     public void setInfo(String firstName,String lastName,int id,int numberOfCourses ,boolean isTuitionPaid,Course[] c,char[] grades)
  110.     {
  111.         super.firstName=firstName;
  112.         super.lastName=lastName;
  113.         this.id=id+"";
  114.         this.isTuitionPaid=isTuitionPaid;
  115.         this.Courses=c;
  116.         this.numberOfCourses=numberOfCourses;
  117.         this.grades=grades;
  118.         this.tuitionRate=tuitionRate;
  119.     }
  120.     public String getName()
  121.     {
  122.         return super.firstName+" "+super.lastName;
  123.     }
  124.     public String getID()
  125.     {
  126.         return id;
  127.     }
  128.     public boolean isTuitionPaid()
  129.     {
  130.         return isTuitionPaid;
  131.     }
  132.     public double getTuitionRate()
  133.     {
  134.         return tuitionRate;
  135.     }
  136.     public int getNumberOfCourses()
  137.     {
  138.         return numberOfCourses;
  139.     }
  140.    
  141.     public void setFirstName(String p)
  142.     {
  143.         super.firstName=p;
  144.     }
  145.     public void setLastName(String p)
  146.     {
  147.         super.lastName=p;
  148.     }
  149.     public static void setTuitionRate(double p)
  150.     {
  151.         tuitionRate=p;
  152.     }
  153.     public void setID(String p)
  154.     {
  155.         id=p;
  156.     }
  157.     public void setTuitionPaid(boolean p)
  158.     {
  159.         isTuitionPaid=p;
  160.     }
  161.     public void setNumberOfCourses(int N)
  162.     {
  163.         numberOfCourses=N;
  164.     }
  165.    
  166.     public void setCoursesEnrolled(Course c[],char grades[])
  167.     {
  168.         Courses=c;
  169.         this.grades=grades;
  170.     }
  171.     public char getGrade(int i)
  172.     {
  173.         if(i>=6 || i<0) return '?';
  174.         return grades[i];
  175.     }
  176.     public Course getCourse(int i)
  177.     {
  178.         if(i>=6 || i<0) return new Course("??", "??", -1);
  179.         return Courses[i];
  180.     }
  181.     public int getHoursEnrolled()
  182.     {
  183.         int res=0;
  184.         for (Course i:Courses)
  185.             res+=(  i!=null ? i.getCredit():0    );
  186.         return res;
  187.     }
  188.     public double getGPA()
  189.     {
  190.         String g="ABCDF";
  191.         int gp[]=new int[]{4,3,2,1,0};
  192.         double res=0;
  193.         for(int i=0;i<numberOfCourses;i++)
  194.             if(g.indexOf(   grades[i]    +"")>-1)
  195.                 res+=(gp[g.indexOf(grades[i]+"")] *Courses[i].getCredit() );
  196.  
  197.         res/=(0.0+getHoursEnrolled());
  198.         return res;
  199.     }
  200.     public void sortCourses()
  201.     {
  202.         Arrays.sort(Courses,new CourseComparator());
  203.     }
  204.     @Override
  205.     public String toString()
  206.     {//%4$-10s
  207.         String res=String.format("%1$-10s \t%2$-10s \t%3$-10s \t%4$-10s \n", "Course No","Course Name","Credit","Grade");;
  208.         for(int i=0;i<numberOfCourses;i++)
  209.                 res+=String.format("%1$-10s \t%2$-10s \t%3$-10s \t%4$-10s \n", Courses[i].getId(),Courses[i].getName(),Courses[i].getCredit(),(isTuitionPaid ? grades[i]:"***"));
  210.         return "Student Name:"+super.firstName+" "+super.lastName+"\n"+
  211.                "Student ID:"+id+"\n"+
  212.                 "Number Of Courses:"+numberOfCourses+"\n\n"+res+"\n"+
  213.                 "Total number of credit hours:"+this.getHoursEnrolled()+"\n"+
  214.                 "Midsemester GPA:"+String.format("%2.2f", getGPA())+"\n"+(isTuitionPaid ? "":"Grades are Being held for not paying the tuition Amount Due:$"+(tuitionRate*getHoursEnrolled())+"\n");
  215.     }
  216. }
  217.  
  218. class GradeRepotProgram
  219. {
  220.     public static void getStudentData(Scanner inpFile, Student studentList[],double tuitionRate) throws Exception
  221.     {
  222.        int M;
  223.        inpFile.nextLine();
  224.        String tmp[],tmp2[];
  225.        for(Student i:studentList)
  226.        {
  227.            tmp=inpFile.nextLine().split(" ");
  228.            M=Integer.parseInt(tmp[4]);
  229.            Course c[]=new Course[M];
  230.            char g[]=new char[M];
  231.            for(int j=0;j<M;j++)
  232.            {
  233.                tmp2=inpFile.nextLine().split(" ");
  234.                c[j]=new Course(tmp2[0],tmp2[1], Integer.parseInt(tmp2[2]));
  235.                g[j]=tmp2[3].charAt(0); //grade
  236.            }
  237.            
  238.            i.setInfo(tmp[0], tmp[1],tmp[2], M,  tmp[3].indexOf("Y")>-1 , c, g);
  239.        //    Student.setTuitionRate(tuitionRate);
  240.            //I still don't get it!..how useful is this parameter since,it wouldn't make any difference!
  241.        }
  242.     }
  243.     public static void printGradeReport(PrintWriter outpFile, Student studentList[])
  244.     {
  245.         for(Student i:studentList)
  246.             outpFile.println(i+"\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
  247.         outpFile.close();
  248.     }
  249. }
  250. /*
  251. public class JavaS6Project1 {
  252.     private Student[] studentList;
  253.     private int noOfStudents;
  254.     private double tuitionRate;
  255.     Scanner inFile;
  256.     PrintWriter outFile;
  257.    
  258.    
  259.     public static void main(String[] args) throws Exception {
  260.        
  261.         Scanner inFile=new Scanner(new FileInputStream("sData.txt")) ;      
  262.         PrintWriter outFile=new PrintWriter("sDataOut.txt");
  263.         int noOfStudents=inFile.nextInt();
  264.         double tuitionRate=inFile.nextDouble();
  265.         Student[] studentList=new Student[noOfStudents];
  266.        
  267.        
  268.         Student.setTuitionRate(tuitionRate);
  269.         for(int i=0;i<noOfStudents;i++)
  270.             studentList[i]=new Student();
  271.        
  272.         GradeRepotProgram.getStudentData(inFile, studentList, tuitionRate);
  273.         GradeRepotProgram.printGradeReport(outFile, studentList);
  274.        
  275.     //    /* Example Test Case in Assignment#1:
  276.         Course course1= new Course();
  277.         Course course2= new Course();
  278.         Course course3= new Course();
  279.         Course course4= new Course();
  280.         course1.setCourselnfo("ComputerSci", "CSC478", 3);
  281.         course2.setCourselnfo("History", "H1S356", 3);
  282.         course3.setCourselnfo("Mathematics", "MTH345",  4);
  283.         course4.setCourselnfo("Physics", "PHY357 ", 3);
  284.  
  285.  
  286.         Student student1=new Student();
  287.         Student.setTuitionRate(70); //This Example shows it's static,right?!
  288.         Course [] courses={course1,course2,course3,course4};
  289.         char [] cGrades={'B','A','A','B'};
  290.         student1.setInfo("Ahmed", "Aly", 890238, 4, true, courses, cGrades);
  291.         System.out.println(student1);
  292.     }
  293.  
  294. }
  295. */
  296. class StudentFrame extends JFrame implements ActionListener
  297. {
  298.     private JTextField studentName,courseNumber,studentID,credit,gpa;
  299.     private JButton first,previous,next,last,exit,print;
  300.     private JTextArea textArea;
  301.    
  302.     private Student[] studentList;
  303.     private int noOfStudents;
  304.     private double tuitionRate;
  305.     private Scanner inFile;
  306.     private PrintWriter outFile;
  307.     private boolean init=false;
  308.     public StudentFrame()
  309.     {
  310.         super("Student Information");
  311.         setSize(600,600);
  312.         Container c=getContentPane();
  313.         c.setLayout(new BorderLayout());
  314.        
  315.             JPanel upperNorth=new JPanel();
  316.             upperNorth.setLayout(new GridLayout(3,1)); //3 rows
  317.            
  318.             upperNorth.add(new JLabel("STUDENT RECORD",JLabel.CENTER));
  319.        
  320.                 JPanel northPanel1=new JPanel();
  321.                 northPanel1.setLayout(new GridLayout(1,6));//6 col
  322.                
  323.                     northPanel1.add(new JLabel("Student Name",JLabel.CENTER));
  324.                     studentName=new JTextField();
  325.                     studentName.setEnabled(false);
  326.                     northPanel1.add(studentName);
  327.                    
  328.                     northPanel1.add(new JLabel("number Of Courses",JLabel.CENTER));
  329.                     courseNumber=new JTextField();
  330.                     courseNumber.setEnabled(false);
  331.                     northPanel1.add(courseNumber);
  332.                    
  333.                     northPanel1.add(new JLabel("student ID",JLabel.CENTER));
  334.                     studentID=new JTextField();
  335.                     studentID.setEnabled(false);
  336.                     northPanel1.add(studentID);
  337.                    
  338.             upperNorth.add(northPanel1);
  339.                 JPanel northPanel2=new JPanel();
  340.                 northPanel2.setLayout(new BorderLayout());
  341.                     JPanel northPanel3=new JPanel();
  342.                     northPanel3.setLayout(new GridLayout(1,4)); //4 cols
  343.                        
  344.                         northPanel3.add(new JLabel("credit Hours",JLabel.CENTER));
  345.                         credit=new JTextField();
  346.                         credit.setEnabled(false);
  347.                         credit.setHorizontalAlignment(JTextField.LEFT);
  348.                         northPanel3.add(credit);
  349.                        
  350.                         northPanel3.add(new JLabel("midSemester GPA",JLabel.CENTER));
  351.                         gpa=new JTextField();
  352.                         gpa.setEnabled(false);
  353.                         gpa.setHorizontalAlignment(JTextField.LEFT);
  354.                         northPanel3.add(gpa);
  355.                        
  356.                 northPanel2.add(northPanel3,BorderLayout.CENTER);        
  357.                
  358.             upperNorth.add(northPanel2);
  359.            
  360.         c.add(upperNorth,BorderLayout.NORTH);
  361.        
  362.         textArea=new JTextArea("");
  363.        
  364.         JScrollPane scroll=new JScrollPane(textArea);
  365.         c.add(scroll,BorderLayout.CENTER);
  366.        
  367.             JPanel lowerPanel=new JPanel();
  368.             lowerPanel.setLayout(new BorderLayout());
  369.                 JPanel lowerPanel2=new JPanel();
  370.                 lowerPanel2.setLayout(new GridLayout(1,6));
  371.                
  372.                 first=new JButton("First");
  373.                 first.addActionListener(this);
  374.                 lowerPanel2.add(first);
  375.                
  376.                 previous=new JButton("Previous");
  377.                 previous.addActionListener(this);
  378.                 lowerPanel2.add(previous);
  379.                
  380.                 next=new JButton("Next");
  381.                 next.addActionListener(this);
  382.                 lowerPanel2.add(next);
  383.                
  384.                 last=new JButton("Last");
  385.                 last.addActionListener(this);
  386.                 lowerPanel2.add(last);
  387.                
  388.                 exit=new JButton("Exit");
  389.                 exit.addActionListener(this);
  390.                 lowerPanel2.add(exit);
  391.                
  392.                 print=new JButton("Print");
  393.                 print.addActionListener(this);
  394.                 lowerPanel2.add(print);
  395.                
  396.             lowerPanel.add(lowerPanel2,BorderLayout.CENTER);    
  397.         c.add(lowerPanel2,BorderLayout.SOUTH);
  398.        
  399.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  400.         setVisible(true);
  401.     }
  402.  
  403.     @Override
  404.     public void actionPerformed(ActionEvent e) {
  405.         if(e.getSource()==exit)
  406.             System.exit(0);
  407.         if(!init)
  408.         {
  409.             try{
  410.                 Scanner inFile=new Scanner(new FileInputStream("sData.txt")) ;      
  411.                 PrintWriter outFile=new PrintWriter("sDataOut.txt");
  412.                 int noOfStudents=inFile.nextInt();
  413.                 double tuitionRate=inFile.nextDouble();
  414.                 Student[] studentList=new Student[noOfStudents];
  415.                
  416.             }catch(Exception ex){}
  417.         }
  418.     }
  419. }
  420. public class JavaProject {
  421.  
  422.     /**
  423.      * @param args the command line arguments
  424.      */
  425.     public static void main(String[] args) {
  426.         new StudentFrame();
  427.         // TODO code application logic here
  428.     }
  429. }
Advertisement
Add Comment
Please, Sign In to add comment