Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. //////////////////
  2.  
  3. import java.io.PrintWriter;
  4.  
  5. public abstract class Student {
  6.  
  7. // private member
  8. private String name;
  9. private int id;
  10. private float gpa;
  11.  
  12. public Student(){
  13. this.name="";
  14. this.id=-1;
  15. this.gpa=0;
  16. }
  17. public Student(String n,int i,float g){
  18. this.name=n;
  19. this.id=i;
  20. this.gpa=g;
  21. }
  22.  
  23. // print student in console
  24. void printStudent(){
  25. System.out.println("Name : "+this.name);
  26. System.out.println("ID : "+this.id);
  27. System.out.println("GPA : "+this.gpa);
  28. }
  29.  
  30. // return id
  31. public int getID(){
  32. return(this.id);
  33. }
  34.  
  35. // write in file
  36. public void printStudent(PrintWriter output){
  37. output.print(this.name);
  38. output.print(","+this.id);
  39. output.print(","+this.gpa);
  40. }
  41. }
  42.  
  43. /////////////
  44.  
  45. import java.io.PrintWriter;
  46.  
  47. public class UndergradStudent extends Student {
  48. private boolean isTransfer;
  49. public UndergradStudent(){
  50. // call super class
  51. super();
  52. this.isTransfer=false;
  53. }
  54.  
  55. // constructor with parameter
  56. public UndergradStudent(String m,int i,float g,boolean b){
  57.  
  58. // call super class
  59. super(m,i,g);
  60. this.isTransfer=b;
  61. }
  62.  
  63. // print in console
  64. void printStudent(){
  65. super.printStudent();
  66. System.out.println("Under Graduate");
  67. System.out.println("isTransfer : "+this.isTransfer);
  68. }
  69.  
  70. // write in file
  71. public void printStudent(PrintWriter output){
  72. super.printStudent(output);
  73. output.print(",undergraduate");
  74. output.println(","+this.isTransfer);
  75. }
  76. }
  77.  
  78. //////////////////////
  79.  
  80. import java.io.PrintWriter;
  81.  
  82. public class GradStudent extends Student {
  83. private String college;
  84. public GradStudent(){
  85. // call super class
  86. super();
  87. this.college="";
  88. }
  89.  
  90. // constructor
  91. public GradStudent(String m,int i,float g,String c){
  92.  
  93. // call super class
  94. super(m,i,g);
  95. this.college=c;
  96. }
  97. // print in console
  98. void printStudent(){
  99. super.printStudent();
  100. System.out.println("Graduate");
  101. System.out.println("College : "+this.college);
  102. }
  103.  
  104. // write in file
  105. public void printStudent(PrintWriter output){
  106. super.printStudent(output);
  107. output.print(",graduate");
  108. output.println(","+this.college);
  109. }
  110. }
  111.  
  112. //////////////////
  113.  
  114.  
  115. import java.io.BufferedReader;
  116. import java.io.FileNotFoundException;
  117. import java.io.FileReader;
  118. import java.io.FileWriter;
  119. import java.io.IOException;
  120. import java.io.PrintWriter;
  121. import java.util.ArrayList;
  122.  
  123.  
  124. public class StdentDriver {
  125. public static void main(String[] args) throws FileNotFoundException, IOException {
  126.  
  127.  
  128. if(args.length!=2){
  129. System.out.println("Error in input\n<input file name> <output file name>");
  130. return;
  131. }
  132.  
  133.  
  134.  
  135. FileReader f = new FileReader(args[0]);
  136. BufferedReader b = new BufferedReader(f);
  137. String line="";
  138. ArrayList<Student> students=new ArrayList<Student>();
  139.  
  140. // input from file
  141. while((line=b.readLine())!=null){
  142. String [] spt=line.split(",");
  143.  
  144. if(spt[3].equals("graduate")==true){
  145. students.add(new GradStudent(spt[0],Integer.parseInt(spt[1]),Float.parseFloat(spt[2]),spt[4]));
  146. }
  147. if(spt[3].equals("undergraduate")==true){
  148. students.add(new UndergradStudent(spt[0],Integer.parseInt(spt[1]),Float.parseFloat(spt[2]),Boolean.parseBoolean(spt[4])));
  149. }
  150. }
  151. b.close();
  152.  
  153.  
  154.  
  155. System.out.println("Readding Input Complete");
  156. System.out.println("Print in file and console");
  157.  
  158. // output to file
  159. FileWriter writer= new FileWriter(args[1]);
  160. PrintWriter outWriter = new PrintWriter(writer);
  161.  
  162. for(int i=students.size()-1;i>=0;i--){
  163. System.out.println("\nRecord "+(students.size()-i));
  164.  
  165. // get instance of object
  166. if(students.get(i) instanceof UndergradStudent){
  167. UndergradStudent s=(UndergradStudent) students.get(i);
  168. s.printStudent();
  169. s.printStudent(outWriter);
  170. }
  171. if(students.get(i) instanceof GradStudent){
  172. GradStudent s=(GradStudent) students.get(i);
  173. s.printStudent();
  174. s.printStudent(outWriter);
  175. }
  176. }
  177.  
  178. outWriter.close();
  179. }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement