Public feed
#723786
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;
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();
personObj.setFirstName(fName);
System.out.print("Enter a last name: ");
String lName = scan.nextLine();
personObj.setLastName(lName);
System.out.print("Enter an address: ");
String address = scan.nextLine();
personObj.setAddress(address);
System.out.print("Enter a city: ");
String city = scan.nextLine();
personObj.setCity(city);
System.out.print("Enter a state: ");
String state = scan.nextLine();
personObj.setState(state);
System.out.print("Enter a zipcode: ");
String zipcode = scan.nextLine();
personObj.setZipcode(zipcode);
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(BadValueException bve)
{
}
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