Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.util.ArrayList;
  9.  
  10. /**
  11. * Project #2
  12. * CS 2334, Section 010
  13. * 2/27/2015
  14. * <P>
  15. * This is the driver class, it contains methods that interact with the Date and Person class
  16. * as well as reads and writes from text files and runs the UserInterface class.
  17. * </P>
  18. * @version 1.0
  19. */
  20. public class Driver {
  21.  
  22. private static ArrayList<Place> place = new ArrayList<Place>();
  23. private static ArrayList<Person> people = new ArrayList<Person>();
  24. public static String fileName;
  25.  
  26. /**
  27. * Reads lines from file and copies them into an ArrayList
  28. * @throws IOException To keep the compiler from complaining
  29. */
  30. public static void readFile() throws IOException
  31. {
  32. FileReader fr = new FileReader(fileName);
  33. BufferedReader br = new BufferedReader(fr);
  34. String nextline = br.readLine();
  35.  
  36. //Loop fills ArrayList with values from file
  37. while (nextline != null) {
  38.  
  39. String[] lineSplit = nextline.split(",");//splits each line by the commas
  40. String[] nameSplit = lineSplit[0].split(" ");
  41.  
  42. if(!Character.isDigit(lineSplit[1].charAt(0)))
  43. {
  44. lineSplit[3] = lineSplit[2];
  45. lineSplit[2] = lineSplit[1];
  46. lineSplit[1] = ("00/00/0000");
  47. }
  48.  
  49. String middleNames = null;
  50.  
  51. for(int i = 1; i < nameSplit.length -1; i++)
  52. {
  53. middleNames = (nameSplit[i] + " ");
  54. }
  55. if(middleNames == null)
  56. {
  57. middleNames = "";
  58. }
  59.  
  60. String[] firstMiddleLast = {nameSplit[0], middleNames, nameSplit[nameSplit.length -1]};
  61.  
  62. Person person;
  63.  
  64. if(lineSplit.length == 5){
  65. person = new Person(firstMiddleLast, lineSplit[1],lineSplit[2],lineSplit[3],lineSplit[4]);
  66.  
  67. //place
  68. addPlace(lineSplit,nextline);
  69.  
  70.  
  71. }else{
  72. person = new Person(firstMiddleLast, lineSplit[1],lineSplit[2],lineSplit[3],"Alive");
  73.  
  74. //place
  75. addPlace(lineSplit,nextline);
  76.  
  77.  
  78. }
  79. people.add(person);
  80.  
  81.  
  82. //Goes to next line
  83. nextline = br.readLine();
  84. }
  85. br.close();
  86. }
  87.  
  88. /**
  89. * Writes a given string to an output file. Asks the user first.
  90. * @param text - Text to be written to output file
  91. * @param inputReader - input reader to read from the keyboard
  92. * @throws IOException - Keeps eclipse happy
  93. */
  94. public static void writeFile(String text, BufferedReader inputReader) throws IOException{
  95. while(true){
  96. System.out.println("Save or skip?");
  97. String saveOrSkipAns = inputReader.readLine();
  98.  
  99. if(saveOrSkipAns.equalsIgnoreCase("save"))
  100. {
  101. FileWriter outfile = new FileWriter("output.txt");
  102. BufferedWriter bw = new BufferedWriter(outfile);
  103. bw.write(text);
  104. bw.close();
  105. break;
  106. }else if(saveOrSkipAns.equalsIgnoreCase("skip"))
  107. {break;}
  108.  
  109. }
  110.  
  111. }
  112.  
  113. /**
  114. * Adds a Place object to the place ArrayList
  115. * @param tokens - A line from the data file split into an array of strings
  116. * @param line - A line from the data file in a string
  117. */
  118. public static void addPlace(String[] tokens, String line)
  119. {
  120. if(tokens.length==4)
  121. {
  122. //START HERE
  123. if(!place.isEmpty())
  124. {
  125. //does this place exist?
  126.  
  127. int x = -1;
  128. //loop through all places and do manual check for place name
  129. for(int i=0; i<place.size(); i++)
  130. {
  131. Place lugar = place.get(i);
  132. if(lugar.getState().equals(tokens[3]))
  133. {
  134. x = i;
  135. break;
  136. }
  137. }
  138. if(x>=0)
  139. {
  140. //it does exist, add the city and state to that place
  141. Place lugar = place.get(x);
  142. lugar.addCity(tokens[2]);
  143. lugar.addPeople(line);
  144. }
  145. else
  146. {
  147. place.add(new Place(tokens[2],tokens[3],line));
  148. }
  149.  
  150.  
  151. }
  152. else
  153. {
  154.  
  155. place.add(new Place(tokens[2],tokens[3],line));
  156. }
  157. }
  158. }
  159.  
  160. /**
  161. * Main method of the program, everything runs from here.
  162. * @param args - Aarrgs
  163. * @throws IOException - throw to keep eclipse happy
  164. */
  165. public static void main(String [] args) throws IOException
  166. {
  167.  
  168. BufferedReader inputReader = new BufferedReader(
  169. new InputStreamReader( System.in ) );
  170.  
  171.  
  172. boolean boo = true;
  173. while(boo){
  174.  
  175. System.out.println("Please enter a valid filename");
  176. String file = inputReader.readLine();
  177.  
  178. if (new File(file).exists())
  179. {
  180.  
  181. Driver.fileName = file;
  182. boo = false;
  183. }
  184. }
  185.  
  186. //call readFile
  187. readFile();
  188.  
  189.  
  190. boolean cont = true;
  191. while(cont){
  192.  
  193. UserInterface.initialQuestions(inputReader,people,place);
  194.  
  195. boolean go = true;
  196. while(go){
  197. System.out.println("Continue or Exit?");
  198. String contOrExitAns = inputReader.readLine();
  199.  
  200. if(contOrExitAns.equalsIgnoreCase("exit"))
  201. {
  202. System.out.println("Thank you for using PeoplAce");
  203. go = false;
  204. cont = false;
  205. }else if(contOrExitAns.equalsIgnoreCase("continue")){
  206. go = false;
  207. cont = true;
  208. }
  209. }
  210. }
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement