/Users/19826/Downloads/IA - Brian/src/ia/brian/ReadTutorAndTutee.java
  1 package ia.brian;
  2 
  3 
  4 
  5 import java.io.File;
  6 import java.io.FileNotFoundException;
  7 import java.util.Scanner;
  8 import java.util.StringTokenizer;
  9 
 10 public class ReadTutorAndTutee {
 11 
 12     public static Tutor[] readingTutorFile(Tutor[] tutors, int tutorCounter) throws FileNotFoundException {
 13         //Pre-condition: 1. Tutors array is received and current value of tutorCounter is received. 
 14         //2. File named "PeerTutoring.csv" exists in the root folder.
 15         //Post-condition: Array of tutors is returned that combines the existing tutors array received and 
 16         //new tutors added while importing data from the file
 17         
 18         Scanner scanner = new Scanner(new File("PeerTutoring.csv"));
 19         
 20         //Initialization of variables
 21         String tutorName = "not set yet";
 22         int grade = -9;
 23         String subjectsString = "not set yet";
 24         String tutorID = "not set yet";
 25         String tutorTypeString = "not set yet";
 26         String availableDaysString = "not set yet";
 27         TimeTable[] timeTableArray = {new TimeTable(), new TimeTable(), new TimeTable(), new TimeTable()};
 28         
 29         
 30         boolean isMSOnCall = false;
 31         boolean isCurrentlyAvailable = true;
 32         while (scanner.hasNextLine()) {
 33             //initialization of more local variables
 34             String line = scanner.nextLine();
 35             StringTokenizer st = new StringTokenizer(line, ",");
 36             String[] subjects = new String[17];  
 37                 
 38             String[] tutorType = new String[4];
 39             for(int b =0; b< tutorType.length; b++){
 40                 tutorType[b] = "not set yet";
 41             }
 42             for(int k =0; k<subjects.length; k++){
 43                 subjects[k] = "not set yet";
 44             }
 45             //Only rows with either 9, 7, 6 tokens will have tokens. Information of tutors is imported from rows 
 46             //with those token numbers
 47             while(st.countTokens() == 9 || st.countTokens() == 6 || st.countTokens() == 7){
 48                 
 49                 if(st.countTokens() == 9){
 50                     tutorName = st.nextToken();
 51                     grade = Integer.parseInt(st.nextToken());
 52                     subjectsString = st.nextToken();
 53                     tutorID = st.nextToken().substring(0, 5);
 54                     tutorTypeString = st.nextToken();
 55                     st.nextToken();
 56                     st.nextToken();
 57                     availableDaysString = st.nextToken();
 58                     isCurrentlyAvailable = false;
 59 
 60                 }
 61                 else if(st.countTokens() == 6){
 62                     tutorName = st.nextToken();
 63                     grade = Integer.parseInt(st.nextToken());
 64                     subjectsString = st.nextToken();
 65                     tutorID = st.nextToken().substring(0, 5);
 66                     tutorTypeString = st.nextToken();
 67                     availableDaysString = st.nextToken();   
 68                     isCurrentlyAvailable = true;
 69                 }
 70                 else if(st.countTokens() == 7){
 71                     tutorName = st.nextToken();
 72                     grade = Integer.parseInt(st.nextToken());
 73                     subjectsString = st.nextToken();
 74                     tutorID = st.nextToken().substring(0, 5);
 75                     tutorTypeString = st.nextToken();
 76                     availableDaysString = st.nextToken(); 
 77                     isMSOnCall = true;
 78                     isCurrentlyAvailable = true;
 79 
 80                 }
 81                 else{
 82 
 83                 }
 84                 //intermediary variables are converted to correct type
 85                 String[] subjectsTemp = subjectsString.split("/");
 86                 String[] tutorTypeTemp = tutorTypeString.split("/");
 87                 
 88                 for(int k =0; k< subjectsTemp.length; k++){
 89                     subjects[k] = subjectsTemp[k];
 90                 }
 91                 for(int k =0; k< tutorTypeTemp.length; k++){
 92                     tutorType[k] = tutorTypeTemp[k];
 93                 }
 94                 //new tutors are added
 95                 tutors[tutorCounter] = new Tutor(tutorName, tutorID, grade, subjects, tutorType, timeTableArray, isCurrentlyAvailable);
 96                 tutorCounter++;
 97                 System.out.println(tutorType[0]);
 98             }
 99             
100 
101         }
102 
103         return tutors;
104     }
105         
106     /**
107      *
108      * @return
109      * @throws java.io.FileNotFoundException
110      */
111     public static Tutee[] readingTuteeFile(Tutee[] tutees, int tuteeCounter) throws FileNotFoundException {
112         //Pre-condition: 1. Tutees array is received and current value of tuteeCounter is received. 
113         //2. File named "PeerTutoring.csv" exists in the root folder.
114         //Post-condition: Array of tutees is returned that combines the existing tutees array received and 
115         //new tutees added while importing data from the file
116         
117         //Method works similar to readingTutorFile
118         Scanner scanner = new Scanner(new File("PeerTutoring.csv"));
119         String tuteeName = "not set yet";
120         int grade = -9;
121         String subjectsString = "not set yet";
122         String tutorID = "not set yet";
123         String availableDaysString = "not set yet";
124         TimeTable[] timeTableArray = {new TimeTable(), new TimeTable(), new TimeTable(), new TimeTable()};
125         
126         
127         boolean isMSOnCall = false;
128         boolean isCurrentlyAvailable = true;
129         while (scanner.hasNextLine()) {
130             String line = scanner.nextLine();
131             StringTokenizer st = new StringTokenizer(line, ",");
132             String[] subjects = new String[17]; 
133             for(int k =0; k<subjects.length; k++){
134                 subjects[k] = "not set yet";
135             }
136             while(st.countTokens() == 9 || st.countTokens() == 4){
137                 
138                 if(st.countTokens() == 9){
139                     st.nextToken();
140                     st.nextToken();
141                     st.nextToken();
142                     st.nextToken();
143                     st.nextToken();
144                     tuteeName = st.nextToken();
145                     tutorID = st.nextToken().substring(0, 5);
146                     availableDaysString = st.nextToken();
147                     subjectsString = st.nextToken();
148                     isCurrentlyAvailable = false;
149 
150                 }
151                 else if(st.countTokens() == 4){
152                     tuteeName = st.nextToken();
153                     tutorID = st.nextToken().substring(0, 5);
154                     availableDaysString = st.nextToken(); 
155                     subjectsString = st.nextToken();
156                     isCurrentlyAvailable = true;
157                 }
158                 else{
159 
160                 }
161                 String[] subjectsTemp = subjectsString.split("/");
162                 
163                 for(int k =0; k< subjectsTemp.length; k++){
164                     subjects[k] = subjectsTemp[k];
165                 }
166                 tutees[tuteeCounter] = new Tutee(tuteeName, tutorID, grade, subjects, timeTableArray, isCurrentlyAvailable);
167                 tuteeCounter++;
168             }
169             
170 
171         }
172         System.out.println(tuteeCounter);
173         return tutees;
174     }
175 }