Public feed
#723863
import java.util.*; import java.io.*; public class Database { public static void main(String args[]) { ArrayList <Person> peopleDatabase = new ArrayList <Person>(); Scanner scan = new Scanner(System.in); // File will be in default directory under people.txt File newFile = new File("people.txt"); FileWriter wordsWriter = null; BufferedWriter bw = null; BufferedReader in = null; boolean incorrect = true; try { // Making Reader in = new BufferedReader(new FileReader(new File("people.txt") ) ); // Makes writer wordsWriter = new FileWriter(newFile, true); } catch(Exception e) { System.out.println(e.toString()); } try { boolean loop = true; while(loop == true) { System.out.printf("Do you want to create a person? (y or n): "); String input = scan.nextLine(); if(input.equalsIgnoreCase("n")) { loop = false; } else { // Create personObj object Person personObj = new Person(); System.out.print("Enter a first name: "); String fName = scan.nextLine(); while(incorrect) { try { personObj.setFirstName(fName); incorrect = false; } catch (BadValueException BadVE) { System.out.println("MAKE A FIRST NAME LESS THAN 12 CHARACTERS"); fName = scan.nextLine(); } } incorrect = true; System.out.print("Enter a last name: "); String lName = scan.nextLine(); while(incorrect) { try { personObj.setLastName(lName); incorrect = false; } catch (BadValueException BadVE) { System.out.println("MAKE A LAST NAME LESS THAN 16 CHARACTERS"); lName = scan.nextLine(); } } incorrect = true; System.out.print("Enter an address: "); String address = scan.nextLine(); while(incorrect) { try { personObj.setAddress(address); incorrect = false; } catch (BadValueException BadVE) { System.out.println("MAKE AN ADDRESS LESS THAN 51 CHARACTERS"); address = scan.nextLine(); } } incorrect = true; System.out.print("Enter a city: "); String city = scan.nextLine(); while(incorrect) { try { personObj.setCity(city); } catch (BadValueException BadVE) { System.out.println("MAKE A CITY LESS THAN 21 CHARACTERS"); city = scan.nextLine(); } } incorrect = true; System.out.print("Enter a state: "); String state = scan.nextLine(); while(incorrect) { try { System.out.println("MAKE A STATE CODE LESS THAN 3 CHARACTERS"); personObj.setState(state); } catch (BadValueException BadVE) { state = scan.nextLine(); } } incorrect = true; System.out.print("Enter a zipcode: "); String zipcode = scan.nextLine(); while(incorrect) { try { personObj.setZipcode(zipcode); incorrect = false; } catch (BadValueException BadVE) { System.out.println("MAKE A ZIPCODE LESS THAN 5 CHARACTERS"); zipcode = scan.nextLine(); } } incorrect = true; personObj = new Person(fName, lName, address, city, state, zipcode); peopleDatabase.add(personObj); bw = new BufferedWriter(new FileWriter(new File("people.txt"), true)); for (Person p : peopleDatabase) { bw.write(p.toString()); bw.newLine(); } bw.flush(); bw.close(); } // else }//while }//try catch(IOException ioe) { } // Making Reader try { in = new BufferedReader(new FileReader(new File("people.txt") ) ); } catch(Exception e) { System.out.println(e.toString()); } try { String pLine =""; while( (pLine = in.readLine()) != null) { System.out.println(pLine); } in.close(); } catch(IOException ioe) { } }//main }//class