Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaproject;
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.FileInputStream;
- import java.io.PrintWriter;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.Scanner;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollBar;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- /**
- *
- * @author youssefgamil
- */
- class Course
- {
- private int credit;
- private String name,id;
- public int getCredit()
- {
- return credit;
- }
- public String getName()
- {
- return name;
- }
- public String getId()
- {
- return id;
- }
- public void setName(String p){name=p;}
- public void setId(String p){id=p;}
- public void setCredit(int p){credit=p;}
- public Course(){}
- public Course(String name,String id,int cr)
- {
- this.name=name;
- this.id=id;
- this.credit=cr;
- }
- public void setCourselnfo(String name,String id,int cr)
- {
- this.name=name;
- this.id=id;
- this.credit=cr;
- }
- public void setlnfo(String name,String id,int cr)
- {
- this.name=name;
- this.id=id;
- this.credit=cr;
- }
- @Override
- public String toString()
- {
- System.out.println("Course Name: "+name+
- "\nCourse ID: "+id+
- "\nCredit Hours: "+credit);
- return this.toString();
- }
- }
- class CourseComparator implements Comparator<Course>
- {
- @Override
- public int compare(Course o1, Course o2) {
- return o1.getName().compareTo(o2.getName());
- }
- }
- class Person
- {
- protected String firstName,lastName,id;
- }
- class Student extends Person
- {
- private int numberOfCourses;
- private static double tuitionRate=0;
- private boolean isTuitionPaid=false;
- private Course Courses[]=new Course[6];
- private char grades[]=new char[6];
- //firstName,LastName,id,isTuitionPaid,courses,grades as in the given example testcase
- public void setInfo(String firstName,String lastName,String id ,int numberOfCourses,boolean isTuitionPaid,Course[] c,char[] grades)
- {
- super.firstName=firstName;
- super.lastName=lastName;
- this.id=id;
- this.isTuitionPaid=isTuitionPaid;
- this.Courses=c;
- this.numberOfCourses=numberOfCourses;
- this.grades=grades;
- this.tuitionRate=tuitionRate;
- }
- public void setInfo(String firstName,String lastName,int id,int numberOfCourses ,boolean isTuitionPaid,Course[] c,char[] grades)
- {
- super.firstName=firstName;
- super.lastName=lastName;
- this.id=id+"";
- this.isTuitionPaid=isTuitionPaid;
- this.Courses=c;
- this.numberOfCourses=numberOfCourses;
- this.grades=grades;
- this.tuitionRate=tuitionRate;
- }
- public String getName()
- {
- return super.firstName+" "+super.lastName;
- }
- public String getID()
- {
- return id;
- }
- public boolean isTuitionPaid()
- {
- return isTuitionPaid;
- }
- public double getTuitionRate()
- {
- return tuitionRate;
- }
- public int getNumberOfCourses()
- {
- return numberOfCourses;
- }
- public void setFirstName(String p)
- {
- super.firstName=p;
- }
- public void setLastName(String p)
- {
- super.lastName=p;
- }
- public static void setTuitionRate(double p)
- {
- tuitionRate=p;
- }
- public void setID(String p)
- {
- id=p;
- }
- public void setTuitionPaid(boolean p)
- {
- isTuitionPaid=p;
- }
- public void setNumberOfCourses(int N)
- {
- numberOfCourses=N;
- }
- public void setCoursesEnrolled(Course c[],char grades[])
- {
- Courses=c;
- this.grades=grades;
- }
- public char getGrade(int i)
- {
- if(i>=6 || i<0) return '?';
- return grades[i];
- }
- public Course getCourse(int i)
- {
- if(i>=6 || i<0) return new Course("??", "??", -1);
- return Courses[i];
- }
- public int getHoursEnrolled()
- {
- int res=0;
- for (Course i:Courses)
- res+=( i!=null ? i.getCredit():0 );
- return res;
- }
- public double getGPA()
- {
- String g="ABCDF";
- int gp[]=new int[]{4,3,2,1,0};
- double res=0;
- for(int i=0;i<numberOfCourses;i++)
- if(g.indexOf( grades[i] +"")>-1)
- res+=(gp[g.indexOf(grades[i]+"")] *Courses[i].getCredit() );
- res/=(0.0+getHoursEnrolled());
- return res;
- }
- public void sortCourses()
- {
- Arrays.sort(Courses,new CourseComparator());
- }
- @Override
- public String toString()
- {//%4$-10s
- String res=String.format("%1$-10s \t%2$-10s \t%3$-10s \t%4$-10s \n", "Course No","Course Name","Credit","Grade");;
- for(int i=0;i<numberOfCourses;i++)
- 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]:"***"));
- return "Student Name:"+super.firstName+" "+super.lastName+"\n"+
- "Student ID:"+id+"\n"+
- "Number Of Courses:"+numberOfCourses+"\n\n"+res+"\n"+
- "Total number of credit hours:"+this.getHoursEnrolled()+"\n"+
- "Midsemester GPA:"+String.format("%2.2f", getGPA())+"\n"+(isTuitionPaid ? "":"Grades are Being held for not paying the tuition Amount Due:$"+(tuitionRate*getHoursEnrolled())+"\n");
- }
- }
- class GradeRepotProgram
- {
- public static void getStudentData(Scanner inpFile, Student studentList[],double tuitionRate) throws Exception
- {
- int M;
- inpFile.nextLine();
- String tmp[],tmp2[];
- for(Student i:studentList)
- {
- tmp=inpFile.nextLine().split(" ");
- M=Integer.parseInt(tmp[4]);
- Course c[]=new Course[M];
- char g[]=new char[M];
- for(int j=0;j<M;j++)
- {
- tmp2=inpFile.nextLine().split(" ");
- c[j]=new Course(tmp2[0],tmp2[1], Integer.parseInt(tmp2[2]));
- g[j]=tmp2[3].charAt(0); //grade
- }
- i.setInfo(tmp[0], tmp[1],tmp[2], M, tmp[3].indexOf("Y")>-1 , c, g);
- // Student.setTuitionRate(tuitionRate);
- //I still don't get it!..how useful is this parameter since,it wouldn't make any difference!
- }
- }
- public static void printGradeReport(PrintWriter outpFile, Student studentList[])
- {
- for(Student i:studentList)
- outpFile.println(i+"\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
- outpFile.close();
- }
- }
- /*
- public class JavaS6Project1 {
- private Student[] studentList;
- private int noOfStudents;
- private double tuitionRate;
- Scanner inFile;
- PrintWriter outFile;
- public static void main(String[] args) throws Exception {
- Scanner inFile=new Scanner(new FileInputStream("sData.txt")) ;
- PrintWriter outFile=new PrintWriter("sDataOut.txt");
- int noOfStudents=inFile.nextInt();
- double tuitionRate=inFile.nextDouble();
- Student[] studentList=new Student[noOfStudents];
- Student.setTuitionRate(tuitionRate);
- for(int i=0;i<noOfStudents;i++)
- studentList[i]=new Student();
- GradeRepotProgram.getStudentData(inFile, studentList, tuitionRate);
- GradeRepotProgram.printGradeReport(outFile, studentList);
- // /* Example Test Case in Assignment#1:
- Course course1= new Course();
- Course course2= new Course();
- Course course3= new Course();
- Course course4= new Course();
- course1.setCourselnfo("ComputerSci", "CSC478", 3);
- course2.setCourselnfo("History", "H1S356", 3);
- course3.setCourselnfo("Mathematics", "MTH345", 4);
- course4.setCourselnfo("Physics", "PHY357 ", 3);
- Student student1=new Student();
- Student.setTuitionRate(70); //This Example shows it's static,right?!
- Course [] courses={course1,course2,course3,course4};
- char [] cGrades={'B','A','A','B'};
- student1.setInfo("Ahmed", "Aly", 890238, 4, true, courses, cGrades);
- System.out.println(student1);
- }
- }
- */
- class StudentFrame extends JFrame implements ActionListener
- {
- private JTextField studentName,courseNumber,studentID,credit,gpa;
- private JButton first,previous,next,last,exit,print;
- private JTextArea textArea;
- private Student[] studentList;
- private int noOfStudents;
- private double tuitionRate;
- private Scanner inFile;
- private PrintWriter outFile;
- private boolean init=false;
- public StudentFrame()
- {
- super("Student Information");
- setSize(600,600);
- Container c=getContentPane();
- c.setLayout(new BorderLayout());
- JPanel upperNorth=new JPanel();
- upperNorth.setLayout(new GridLayout(3,1)); //3 rows
- upperNorth.add(new JLabel("STUDENT RECORD",JLabel.CENTER));
- JPanel northPanel1=new JPanel();
- northPanel1.setLayout(new GridLayout(1,6));//6 col
- northPanel1.add(new JLabel("Student Name",JLabel.CENTER));
- studentName=new JTextField();
- studentName.setEnabled(false);
- northPanel1.add(studentName);
- northPanel1.add(new JLabel("number Of Courses",JLabel.CENTER));
- courseNumber=new JTextField();
- courseNumber.setEnabled(false);
- northPanel1.add(courseNumber);
- northPanel1.add(new JLabel("student ID",JLabel.CENTER));
- studentID=new JTextField();
- studentID.setEnabled(false);
- northPanel1.add(studentID);
- upperNorth.add(northPanel1);
- JPanel northPanel2=new JPanel();
- northPanel2.setLayout(new BorderLayout());
- JPanel northPanel3=new JPanel();
- northPanel3.setLayout(new GridLayout(1,4)); //4 cols
- northPanel3.add(new JLabel("credit Hours",JLabel.CENTER));
- credit=new JTextField();
- credit.setEnabled(false);
- credit.setHorizontalAlignment(JTextField.LEFT);
- northPanel3.add(credit);
- northPanel3.add(new JLabel("midSemester GPA",JLabel.CENTER));
- gpa=new JTextField();
- gpa.setEnabled(false);
- gpa.setHorizontalAlignment(JTextField.LEFT);
- northPanel3.add(gpa);
- northPanel2.add(northPanel3,BorderLayout.CENTER);
- upperNorth.add(northPanel2);
- c.add(upperNorth,BorderLayout.NORTH);
- textArea=new JTextArea("");
- JScrollPane scroll=new JScrollPane(textArea);
- c.add(scroll,BorderLayout.CENTER);
- JPanel lowerPanel=new JPanel();
- lowerPanel.setLayout(new BorderLayout());
- JPanel lowerPanel2=new JPanel();
- lowerPanel2.setLayout(new GridLayout(1,6));
- first=new JButton("First");
- first.addActionListener(this);
- lowerPanel2.add(first);
- previous=new JButton("Previous");
- previous.addActionListener(this);
- lowerPanel2.add(previous);
- next=new JButton("Next");
- next.addActionListener(this);
- lowerPanel2.add(next);
- last=new JButton("Last");
- last.addActionListener(this);
- lowerPanel2.add(last);
- exit=new JButton("Exit");
- exit.addActionListener(this);
- lowerPanel2.add(exit);
- print=new JButton("Print");
- print.addActionListener(this);
- lowerPanel2.add(print);
- lowerPanel.add(lowerPanel2,BorderLayout.CENTER);
- c.add(lowerPanel2,BorderLayout.SOUTH);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource()==exit)
- System.exit(0);
- if(!init)
- {
- try{
- Scanner inFile=new Scanner(new FileInputStream("sData.txt")) ;
- PrintWriter outFile=new PrintWriter("sDataOut.txt");
- int noOfStudents=inFile.nextInt();
- double tuitionRate=inFile.nextDouble();
- Student[] studentList=new Student[noOfStudents];
- }catch(Exception ex){}
- }
- }
- }
- public class JavaProject {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- new StudentFrame();
- // TODO code application logic here
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment