Your project must have the following: A Person class with: Default Constructor P
ID: 3570897 • Letter: Y
Question
Your project must have the following:
A Person class with:
Default Constructor
Parameterized constructor
4-5 properties including firstName and lastName
Other properties must be a variety of types
Accessors and mutators for each type
At least 1 computed property (derived from the others)
An AddressBook with:
Ability to store a variable number of people (up to at least 100)
Default constructor
Add person method
Get length method
A collection of filter methods for each Person property
And possibly: Load from file and Save to file
// Your program must provide the following features either all in the main
// method or, preferably as a collection of static methods in this class.
// Each of the methods can recieve the ProgramState object as a way of
// passing all the variables around
// Present the user a menu of different options for manipulating their
// address book:
1) Add a Person to the address book (remember to reset the view)
You will need to prompt the user for each property and the set that
value on the person, then add to the address book
2) Display how many people are in the address book and current view
(e.g. "Showing 5 of 37 entries")
3) Print the current address book view
This should nicely print all entries in the current view to System.out
4) Allow the user to filter the current address book view
using any of the filter methods defined above (you'll need to ask them
which they want to do and get input/handle accordingly)
5) Remove any filters on the address book
Just return the currentView to the full address book
6) Save the address book to a file (i.e. Save As)
Optionally: "Save current view" and/or "Save all addresses"
You'll need to prompt the user for a file name the write the contents
to the file in a way that you can read back in with operation (7)
below.
7) Load the address book from a file (i.e. Open)
Consider how you wrote the contents of the address bok out in (6).
Open this file and "scan" through it creating new Person objects, each
of which you add the to the address book
Explanation / Answer
import java.io.File;
import java.io.PrintWriter;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
class Person {
private String firstname, lastname, birthdate, address, city, state, phone;
private int zipcode, age;
private Date dob;
public Person(){
firstname = null;
lastname = null;
address = null;
city = null;
state = null;
phone = null;
zipcode = 0;
birthdate = null;
setDob();
setAge();
}
// Parameterized constructor
public Person(String firstname, String lastname, String phone ){
this();
this.firstname = firstname;
this.lastname = lastname;
this.phone = phone;
}
//Full constructor
public Person(String firstname, String lastname, String phone, String address, String city, String state, int zipcode, String birthdate ){
this(firstname, lastname, phone);
this.address = address;
this.city = city;
this.state = state;
this.phone = phone;
this.zipcode = zipcode;
this.birthdate = birthdate;
setDob();
setAge();
}
public void setFirstName(String firstname){
this.firstname = firstname;
}
public String getFirstName(){
return firstname;
}
public void setLastName(String lastname){
this.lastname = lastname;
}
public String getLastName(){
return lastname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getZipcode() {
return zipcode;
}
public void setZipcode(int zipcode) {
this.zipcode = zipcode;
}
public String getBirthdate() {
return birthdate;
}
public Date getBirthdate(boolean t){
if (t){
return dob;
}
return null;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
setDob();
setAge();
}
public int getAge(){
return age;
}
private void setDob(){
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("MMddyyyy");
if(birthdate != null){
try{
date = formatter.parse(birthdate);
dob = date;
}
catch (java.text.ParseException e) {
System.out.println("Error parsing dob.");
}
}
}
public String getBirthdateFormatted(){
return getBirthdateFormatted("MM/dd/yyyy");
}
public String getBirthdateFormatted(String format){
if(dob == null)
return "";
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.format(dob);
}
static public Date getDateFormatted(String format, String date){
if(date == null)
return null;
Date retdate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
try{
retdate = formatter.parse(date);
}
catch (java.text.ParseException e) {
System.out.println("Error parsing date.");
}
return retdate;
}
private void setAge(){
if(this.dob == null){
return;
}
Calendar now = Calendar.getInstance();
Calendar dob = Calendar.getInstance();
dob.setTime(this.dob);
int year1 = now.get(Calendar.YEAR);
int year2 = dob.get(Calendar.YEAR);
age = year1 - year2;
int month1 = now.get(Calendar.MONTH);
int month2 = dob.get(Calendar.MONTH);
if (month2 > month1) {
age--;
} else if (month1 == month2) {
int day1 = now.get(Calendar.DAY_OF_MONTH);
int day2 = dob.get(Calendar.DAY_OF_MONTH);
if (day2 > day1) {
age--;
}
}
}
@Override
public String toString(){
return this.lastname + " " +
this.firstname + " " +
this.address + ", " +
this.city + ", " +
this.state + " " +
this.zipcode + " " +
this.phone + " " +
getBirthdateFormatted() + " " +
this.age;
}
}
class AddressBook {
private AddressBookNode first;
private AddressBookNode last;
private int count;
class AddressBookNode {
public AddressBookNode next;
public AddressBookNode prev;
public Person person;
public AddressBookNode(Person person){
this.person = person;
next = null;
prev = null;
}
}
public AddressBook(){
first = null;
last = null;
count = 0;
}
public void add(Person person){
AddressBookNode add = new AddressBookNode(person);
if (first == null){
first = add;
last = first;
count++;
}
else{
last.next = add;
last.next.prev = last;
last = last.next;
count++;
}
}
public int size(){
return count;
}
public AddressBook filterName(String name){
AddressBook ret = new AddressBook();
AddressBookNode node = first;
while (node != null){
if (node.person.getLastName().equalsIgnoreCase(name) ||
node.person.getFirstName().equalsIgnoreCase(name)){
ret.add(node.person);
}
node = node.next;
}
return ret;
}
public AddressBook filterAddress(String addr){
AddressBook ret = new AddressBook();
AddressBookNode node = first;
while (node != null){
if (node.person.getAddress().equalsIgnoreCase(addr) ||
node.person.getState().equalsIgnoreCase(addr) ||
((Integer)node.person.getZipcode()).toString().equalsIgnoreCase(addr)){
ret.add(node.person);
}
node = node.next;
}
return ret;
}
public AddressBook filterPhone(String phone){
AddressBook ret = new AddressBook();
AddressBookNode node = first;
while (node != null){
if (node.person.getPhone().equals(phone)){
ret.add(node.person);
}
node = node.next;
}
return ret;
}
public AddressBook filterDOB(String birthday){
AddressBook ret = new AddressBook();
AddressBookNode node = first;
Date date = Person.getDateFormatted("MM/dd/yyyy", birthday);
while (node != null){
if (node.person.getBirthdate(true).equals(date)){
ret.add(node.person);
}
node = node.next;
}
return ret;
}
public AddressBook filterAge(String a){
int age = Integer.parseInt(a);
AddressBook ret = new AddressBook();
AddressBookNode node = first;
while (node != null){
if (node.person.getAge() == age){
ret.add(node.person);
}
node = node.next;
}
return ret;
}
public boolean save(String filename){
File file = new File (filename);
try{
AddressBookNode node = first;
PrintWriter pw = new PrintWriter(file);
while(node != null){
String person = node.person.getFirstName() + "," +
node.person.getLastName() + "," +
node.person.getAddress() + "," +
node.person.getCity() + "," +
node.person.getState() + "," +
node.person.getZipcode() + "," +
node.person.getPhone() + "," +
node.person.getBirthdate();
pw.println(person);
node = node.next;
}
pw.close();
System.out.println(file + " saved!");
return true;
}
catch(Exception e){
System.out.println("Error saving file!");
return false;
}
}
public boolean open(String filename){
File file = new File (filename);
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] sline = line.split(",");
Person addp = new Person();
addp.setFirstName(sline[0]);
addp.setLastName(sline[1]);
addp.setAddress(sline[2]);
addp.setCity(sline[3]);
addp.setState(sline[4]);
addp.setZipcode(Integer.parseInt(sline[5]));
addp.setPhone(sline[6]);
addp.setBirthdate(sline[7]);
this.add(addp);
}
scanner.close();
System.out.println(file + " loaded!");
return true;
}
catch (Exception e) {
System.out.println("Error opening file!");
return false;
}
}
@Override
public String toString(){
AddressBookNode node = first;
String ret = "";
while(node != null){
ret += node.person + " ";
node = node.next;
}
return ret;
}
}
class ProgramState {
// A single scanner to use throughout the program
public Scanner userInput;
// An AddressBook variable to store the actual address book
public AddressBook addresses;
// An AddressBook variable to store the current filtered view
// Note that this should be reset (using the method below) after any
// change to the "addresses variable"
public AddressBook currentView;
public String filename;
public ProgramState() {
userInput = new Scanner(System.in);
addresses = new AddressBook();
currentView = addresses;
filename = null;
}
// Remember to call this after making any changes to the address book
// Or when the user requests a reset
public void resetView() {
currentView = addresses;
}
}
public class FinalTemplate {
static ProgramState state;
public static void main(String [] args) {
state = new ProgramState();
boolean run = true;
int choice;
if(args.length != 0)
state.addresses.open(args[0]);
while(run){
choice = menu();
doCommand(choice);
}
}
public static void addPerson() {
Person addperson = new Person();
printHeader("ADD PERSON");
print("Enter first name=>");
addperson.setFirstName(state.userInput.nextLine());
print("Enter last name=>");
addperson.setLastName(state.userInput.nextLine());
print("Enter Street Address=>");
addperson.setAddress(state.userInput.nextLine());
print("Enter City=>");
addperson.setCity(state.userInput.nextLine());
print("Enter State=>");
addperson.setState(state.userInput.nextLine());
print("Enter Zip=>");
addperson.setZipcode(state.userInput.nextInt());
state.userInput.nextLine();
print("Enter Phone Number=>");
addperson.setPhone(state.userInput.nextLine());
print("Enter DOB=>");
addperson.setBirthdate(state.userInput.nextLine());
state.addresses.add(addperson);
}
private static void applyFilter(){
boolean valid = false;
int choice = 0;
while (!valid){
printHeader("CHOOSE FILTER");
println("1)Name");
println("2)Address(Street, City, State, Zip)");
println("3)Phone Number");
println("4)Birthday");
println("5)Age");
println("0)CANCEL");
println("*******************************");
println("");
print("Choice=>");
choice = state.userInput.nextInt();
state.userInput.nextLine();//clear buffer
if (choice >= 0 && choice < 6){
valid = true;
}
else
println("Invalid Choice!");
}
if(choice == 0)
return;
print("Enter argument to filter=>");
String arg = state.userInput.nextLine();
switch(choice){
case 1: state.currentView = state.addresses.filterName(arg);
break;
case 2: state.currentView = state.addresses.filterAddress(arg);
break;
case 3: state.currentView = state.addresses.filterPhone(arg);
break;
case 4: state.currentView = state.addresses.filterDOB(arg);
break;
case 5: state.currentView = state.addresses.filterAge(arg);
break;
}
}
private static void openAddressBook(boolean overwrite){
if(overwrite == true){
state.addresses = new AddressBook();
state.currentView = state.addresses;
}
print("Enter filename to open (x to cancel)=>");
String file = state.userInput.nextLine();
if(file.toLowerCase().charAt(0) != 'x'){
state.addresses.open(file);
state.filename = file;
}
}
private static void saveAddressBook(AddressBook addr){
String file = "";
char yn = 'n';
if (state.filename != null){
print("Save to " + state.filename + "?(y/n)=>");
yn = state.userInput.nextLine().toLowerCase().charAt(0);
}
if(yn != 'n'){
file = state.filename;
}
else if(yn == 'n'){
print("Enter filename to save (x to cancel)=>");
file = state.userInput.nextLine();
}
if(file.toLowerCase().charAt(0) != 'x'){
addr.save(file);
}
}
private static void viewAddressBook(){
println("LAST" + " " +
"FIRST" + " " +
"ADDRESS" + ", " +
"CITY" + ", " +
"STATE" + " " +
"ZIP" + " " +
"PHONE" + " " +
"DOB" + " " +
"AGE");
println(state.currentView);
println("Showing " + state.currentView.size() + " of " + state.addresses.size() + " entries.");
}
private static int menu(){
boolean valid = false;
int choice = 0;
while (!valid){
println("*******************************");
println("1)Add Person");
println("2)View Address Book");
println("3)Filter View");
println("4)Remove Filter");
println("5)Open Address Book");
println("6)Save Address Book - Current View");
println("7)Save Address Book - Full View");
println("8)Import Address Book");
println("0)EXIT");
println("*******************************");
println("");
print("Command=>");
choice = state.userInput.nextInt();
state.userInput.nextLine();
if (choice >= 0 && choice < 9){
valid = true;
}
else
println("Invalid Choice!");
}
return choice;
}
static private void doCommand(int choice){
switch(choice){
case 0: exit();
break;
case 1: addPerson();
break;
case 2: viewAddressBook();
break;
case 3: applyFilter();
break;
case 4: state.resetView();;
break;
case 5: openAddressBook(true);
break;
case 6: saveAddressBook(state.currentView);
break;
case 7: saveAddressBook(state.addresses);
break;
case 8: openAddressBook(false);
break;
default: break;
}
}
static private void exit(){
System.exit(0);
}
static private void printHeader(String header){
println("***************" + header + "****************");
}
static private void print(Object msg){
System.out.print(msg);
}
static private void println(Object msg){
System.out.println(msg);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.