Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////
- import java.io.PrintWriter;
- public abstract class Student {
- // private member
- private String name;
- private int id;
- private float gpa;
- public Student(){
- this.name="";
- this.id=-1;
- this.gpa=0;
- }
- public Student(String n,int i,float g){
- this.name=n;
- this.id=i;
- this.gpa=g;
- }
- // print student in console
- void printStudent(){
- System.out.println("Name : "+this.name);
- System.out.println("ID : "+this.id);
- System.out.println("GPA : "+this.gpa);
- }
- // return id
- public int getID(){
- return(this.id);
- }
- // write in file
- public void printStudent(PrintWriter output){
- output.print(this.name);
- output.print(","+this.id);
- output.print(","+this.gpa);
- }
- }
- /////////////
- import java.io.PrintWriter;
- public class UndergradStudent extends Student {
- private boolean isTransfer;
- public UndergradStudent(){
- // call super class
- super();
- this.isTransfer=false;
- }
- // constructor with parameter
- public UndergradStudent(String m,int i,float g,boolean b){
- // call super class
- super(m,i,g);
- this.isTransfer=b;
- }
- // print in console
- void printStudent(){
- super.printStudent();
- System.out.println("Under Graduate");
- System.out.println("isTransfer : "+this.isTransfer);
- }
- // write in file
- public void printStudent(PrintWriter output){
- super.printStudent(output);
- output.print(",undergraduate");
- output.println(","+this.isTransfer);
- }
- }
- //////////////////////
- import java.io.PrintWriter;
- public class GradStudent extends Student {
- private String college;
- public GradStudent(){
- // call super class
- super();
- this.college="";
- }
- // constructor
- public GradStudent(String m,int i,float g,String c){
- // call super class
- super(m,i,g);
- this.college=c;
- }
- // print in console
- void printStudent(){
- super.printStudent();
- System.out.println("Graduate");
- System.out.println("College : "+this.college);
- }
- // write in file
- public void printStudent(PrintWriter output){
- super.printStudent(output);
- output.print(",graduate");
- output.println(","+this.college);
- }
- }
- //////////////////
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- public class StdentDriver {
- public static void main(String[] args) throws FileNotFoundException, IOException {
- if(args.length!=2){
- System.out.println("Error in input\n<input file name> <output file name>");
- return;
- }
- FileReader f = new FileReader(args[0]);
- BufferedReader b = new BufferedReader(f);
- String line="";
- ArrayList<Student> students=new ArrayList<Student>();
- // input from file
- while((line=b.readLine())!=null){
- String [] spt=line.split(",");
- if(spt[3].equals("graduate")==true){
- students.add(new GradStudent(spt[0],Integer.parseInt(spt[1]),Float.parseFloat(spt[2]),spt[4]));
- }
- if(spt[3].equals("undergraduate")==true){
- students.add(new UndergradStudent(spt[0],Integer.parseInt(spt[1]),Float.parseFloat(spt[2]),Boolean.parseBoolean(spt[4])));
- }
- }
- b.close();
- System.out.println("Readding Input Complete");
- System.out.println("Print in file and console");
- // output to file
- FileWriter writer= new FileWriter(args[1]);
- PrintWriter outWriter = new PrintWriter(writer);
- for(int i=students.size()-1;i>=0;i--){
- System.out.println("\nRecord "+(students.size()-i));
- // get instance of object
- if(students.get(i) instanceof UndergradStudent){
- UndergradStudent s=(UndergradStudent) students.get(i);
- s.printStudent();
- s.printStudent(outWriter);
- }
- if(students.get(i) instanceof GradStudent){
- GradStudent s=(GradStudent) students.get(i);
- s.printStudent();
- s.printStudent(outWriter);
- }
- }
- outWriter.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement