yoga1290

Java Assignment#3 w/out print

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