Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- /**
- * Project #2
- * CS 2334, Section 010
- * 2/27/2015
- * <P>
- * This is the driver class, it contains methods that interact with the Date and Person class
- * as well as reads and writes from text files and runs the UserInterface class.
- * </P>
- * @version 1.0
- */
- public class Driver {
- private static ArrayList<Place> place = new ArrayList<Place>();
- private static ArrayList<Person> people = new ArrayList<Person>();
- public static String fileName;
- /**
- * Reads lines from file and copies them into an ArrayList
- * @throws IOException To keep the compiler from complaining
- */
- public static void readFile() throws IOException
- {
- FileReader fr = new FileReader(fileName);
- BufferedReader br = new BufferedReader(fr);
- String nextline = br.readLine();
- //Loop fills ArrayList with values from file
- while (nextline != null) {
- String[] lineSplit = nextline.split(",");//splits each line by the commas
- String[] nameSplit = lineSplit[0].split(" ");
- if(!Character.isDigit(lineSplit[1].charAt(0)))
- {
- lineSplit[3] = lineSplit[2];
- lineSplit[2] = lineSplit[1];
- lineSplit[1] = ("00/00/0000");
- }
- String middleNames = null;
- for(int i = 1; i < nameSplit.length -1; i++)
- {
- middleNames = (nameSplit[i] + " ");
- }
- if(middleNames == null)
- {
- middleNames = "";
- }
- String[] firstMiddleLast = {nameSplit[0], middleNames, nameSplit[nameSplit.length -1]};
- Person person;
- if(lineSplit.length == 5){
- person = new Person(firstMiddleLast, lineSplit[1],lineSplit[2],lineSplit[3],lineSplit[4]);
- //place
- addPlace(lineSplit,nextline);
- }else{
- person = new Person(firstMiddleLast, lineSplit[1],lineSplit[2],lineSplit[3],"Alive");
- //place
- addPlace(lineSplit,nextline);
- }
- people.add(person);
- //Goes to next line
- nextline = br.readLine();
- }
- br.close();
- }
- /**
- * Writes a given string to an output file. Asks the user first.
- * @param text - Text to be written to output file
- * @param inputReader - input reader to read from the keyboard
- * @throws IOException - Keeps eclipse happy
- */
- public static void writeFile(String text, BufferedReader inputReader) throws IOException{
- while(true){
- System.out.println("Save or skip?");
- String saveOrSkipAns = inputReader.readLine();
- if(saveOrSkipAns.equalsIgnoreCase("save"))
- {
- FileWriter outfile = new FileWriter("output.txt");
- BufferedWriter bw = new BufferedWriter(outfile);
- bw.write(text);
- bw.close();
- break;
- }else if(saveOrSkipAns.equalsIgnoreCase("skip"))
- {break;}
- }
- }
- /**
- * Adds a Place object to the place ArrayList
- * @param tokens - A line from the data file split into an array of strings
- * @param line - A line from the data file in a string
- */
- public static void addPlace(String[] tokens, String line)
- {
- if(tokens.length==4)
- {
- //START HERE
- if(!place.isEmpty())
- {
- //does this place exist?
- int x = -1;
- //loop through all places and do manual check for place name
- for(int i=0; i<place.size(); i++)
- {
- Place lugar = place.get(i);
- if(lugar.getState().equals(tokens[3]))
- {
- x = i;
- break;
- }
- }
- if(x>=0)
- {
- //it does exist, add the city and state to that place
- Place lugar = place.get(x);
- lugar.addCity(tokens[2]);
- lugar.addPeople(line);
- }
- else
- {
- place.add(new Place(tokens[2],tokens[3],line));
- }
- }
- else
- {
- place.add(new Place(tokens[2],tokens[3],line));
- }
- }
- }
- /**
- * Main method of the program, everything runs from here.
- * @param args - Aarrgs
- * @throws IOException - throw to keep eclipse happy
- */
- public static void main(String [] args) throws IOException
- {
- BufferedReader inputReader = new BufferedReader(
- new InputStreamReader( System.in ) );
- boolean boo = true;
- while(boo){
- System.out.println("Please enter a valid filename");
- String file = inputReader.readLine();
- if (new File(file).exists())
- {
- Driver.fileName = file;
- boo = false;
- }
- }
- //call readFile
- readFile();
- boolean cont = true;
- while(cont){
- UserInterface.initialQuestions(inputReader,people,place);
- boolean go = true;
- while(go){
- System.out.println("Continue or Exit?");
- String contOrExitAns = inputReader.readLine();
- if(contOrExitAns.equalsIgnoreCase("exit"))
- {
- System.out.println("Thank you for using PeoplAce");
- go = false;
- cont = false;
- }else if(contOrExitAns.equalsIgnoreCase("continue")){
- go = false;
- cont = true;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement