Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help using the ProgramState to pass all variables around. I have the person and

ID: 3571109 • Letter: H

Question

Help using the ProgramState to pass all variables around. I have the person and addressbook classes set but I'm stuck on the last part.

package finaltemplate;

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

class Person {

private int age;
private String firstName;
private String lastName;
private float height;
private float weight;
private String birthplace;

public Person() {
age = -1;
firstName = "N/A";
lastName = "N/A";
height = -1;
weight = -1;
birthplace = "N/A";
}

public Person(String firstName, String lastName, int age, float height, float weight, String birthplace) {
this.age = age;
this.firstName = firstName;
this.lastName = lastName;
this.height = height;
this.weight = weight;
this.birthplace = birthplace;
}

public int getAge() {
return age;
}

public void setAge(int x) {
age = x;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String f) {
firstName = f;
}

public String getLastName() {
return lastName;
}

public void setLastName(String l) {
lastName = l;
}

public String getFullName() {
return firstName + " " + lastName;
}

public float getHeight() {
return height;
}

public void setHeight(float h) {
height = h;
}

public float getWeight() {
return weight;
}

public void setWeight(float w) {
weight = w;
}

public String getBirthplace() {
return birthplace;
}

public void setBirthplace(String b) {
birthplace = b;
}

public int getBirthyear(Person aPerson) {
int birthyear = 2014 - getAge();
return birthyear;
}

public String toString(Person aPerson) {
String stringOutput = "I'm a person. My name is " + getFirstName()
+ ". I'm " + getAge() + " years old and was born in " + getBirthyear(aPerson) + " in " + getBirthplace();
return stringOutput;
}
}

class AddressBook {

// Ability to store a variable number of people (up to at least 100)
private Person[] contacts;
private int numberOfEntries;

// Default constructor
public AddressBook() {
contacts = new Person[100];
numberOfEntries = 0;
}

// Add person method
public void addPerson(Person aPerson) {
if (numberOfEntries < 100) {
contacts[0] = aPerson;
numberOfEntries++;
}
}

// Get length method
public int getLength(){
return numberOfEntries;
}
  
// A collection of filter methods for each Person property
public Person[] filterByFirstName(String fname){
Person[] filteredContacts = new Person[100];
int count = 0;
for (int i = 0; i < numberOfEntries; i++){
if(contacts[i].getFirstName().equalsIgnoreCase(fname)){
filteredContacts[count] = contacts[i];
count++;
} // end of
} // end for
return filteredContacts;
}
  
public Person[] filterByLastName(String lname){
Person[] filteredContacts = new Person[100];
int count = 0;
for (int i = 0; i < numberOfEntries; i++){
if(contacts[i].getLastName().equalsIgnoreCase(lname)){
filteredContacts[count] = contacts[i];
count++;
} // end of
} // end for
return filteredContacts;
}
  
public Person[] filterByAge(int age){
Person[] filteredContacts = new Person[100];
int count = 0;
for (int i = 0; i < numberOfEntries; i++){
if(contacts[i].getAge() == age){
filteredContacts[count] = contacts[i];
count++;
} // end of
} // end for
return filteredContacts;
}
  
public Person[] filterByHeight(int height){
Person[] filteredContacts = new Person[100];
int count = 0;
for (int i = 0; i < numberOfEntries; i++){
if(contacts[i].getHeight() == height){
filteredContacts[count] = contacts[i];
count++;
} // end of
} // end for
return filteredContacts;
}
  
public Person[] filterByWeight(int weight){
Person[] filteredContacts = new Person[100];
int count = 0;
for (int i = 0; i < numberOfEntries; i++){
if(contacts[i].getWeight() == weight){
filteredContacts[count] = contacts[i];
count++;
} // end of
} // end for
return filteredContacts;
}
  
public Person[] filterByBirthplace(String birthplace){
Person[] filteredContacts = new Person[100];
int count = 0;
for (int i = 0; i < numberOfEntries; i++){
if(contacts[i].getBirthplace().equalsIgnoreCase(birthplace)){
filteredContacts[count] = contacts[i];
count++;
} // end of
} // end for
return filteredContacts;
}
// Possibly:
// Load from file
// Save to file
}

// The following is a suggestion.
// You do not need to use it in your implementation
class ProgramState {

// A single scanner to use throughout the program
public Scanner userInput;

// An AddressBook variable to store the actual address book
private 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 ProgramState() {
userInput = new Scanner(System.in);
addresses = new AddressBook();
currentView = addresses;
}

// 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 {
// 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
// Extra credit:
// A) Assume that the args array in String *may* contain a single value
// that referes to a filename to load
// B) Remember the above name and allow the user to save back to that file
// without needing to specify the name again (i.e. Save)
// C) Anything else you decide to do
// You could also use ProgramState like this
//static ProgramState state;
public static void main(String[] args) {
ProgramState state = new ProgramState();

addPersonHint(state);
}

public static void addPersonHint(ProgramState state) {
// Do stuff with the program state here. Remember any changes will
// persist in your program after this method is done
}

}

Explanation / Answer

package finaltemplate;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

class Person
{
private int age;
private String firstName;
private String lastName;
private float height;
private float weight;
private String birthplace;

public Person()
{
  age = -1;
  firstName = "N/A";
  lastName = "N/A";
  height = -1;
  weight = -1;
  birthplace = "N/A";
}

public Person(String firstName, String lastName, int age, float height,
   float weight, String birthplace)
{
  this.age = age;
  this.firstName = firstName;
  this.lastName = lastName;
  this.height = height;
  this.weight = weight;
  this.birthplace = birthplace;
}

public int getAge()
{
  return age;
}

public void setAge(int x)
{
  age = x;
}

public String getFirstName()
{
  return firstName;
}

public void setFirstName(String f)
{
  firstName = f;
}

public String getLastName()
{
  return lastName;
}

public void setLastName(String l)
{
  lastName = l;
}

public String getFullName()
{
  return firstName + " " + lastName;
}

public float getHeight()
{
  return height;
}

public void setHeight(float h)
{
  height = h;
}

public float getWeight()
{
  return weight;
}

public void setWeight(float w)
{
  weight = w;
}

public String getBirthplace()
{
  return birthplace;
}

public void setBirthplace(String b)
{
  birthplace = b;
}

public int getBirthyear(Person aPerson)
{
  int birthyear = 2014 - getAge();
  return birthyear;
}

public String toString(Person aPerson)
{
  String stringOutput = "I'm a person. My name is " + getFirstName()
    + ". I'm " + getAge() + " years old and was born in "
    + getBirthyear(aPerson) + " in " + getBirthplace();
  return stringOutput;
}
}

class AddressBook
{
// Ability to store a variable number of people (up to at least 100)
private Person[] contacts;
private int numberOfEntries;

// Default constructor
public AddressBook()
{
  contacts = new Person[100];
  numberOfEntries = 0;
}

// Add person method
public void addPerson(Person aPerson)
{
  if(numberOfEntries < 100)
  {
   contacts[0] = aPerson;
   numberOfEntries++;
  }
}

// Get length method
public int getLength()
{
  return numberOfEntries;
}

// A collection of filter methods for each Person property
public Person[] filterByFirstName(String fname)
{
  Person[] filteredContacts = new Person[100];
  int count = 0;
  for(int i = 0; i < numberOfEntries; i++)
  {
   if(contacts[i].getFirstName().equalsIgnoreCase(fname))
   {
    filteredContacts[count] = contacts[i];
    count++;
   } // end of
  } // end for
  return filteredContacts;
}

public Person[] filterByLastName(String lname)
{
  Person[] filteredContacts = new Person[100];
  int count = 0;
  for(int i = 0; i < numberOfEntries; i++)
  {
   if(contacts[i].getLastName().equalsIgnoreCase(lname))
   {
    filteredContacts[count] = contacts[i];
    count++;
   } // end of
  } // end for
  return filteredContacts;
}

public Person[] filterByAge(int age)
{
  Person[] filteredContacts = new Person[100];
  int count = 0;
  for(int i = 0; i < numberOfEntries; i++)
  {
   if(contacts[i].getAge() == age)
   {
    filteredContacts[count] = contacts[i];
    count++;
   } // end of
  } // end for
  return filteredContacts;
}

public Person[] filterByHeight(float height)
{
  Person[] filteredContacts = new Person[100];
  int count = 0;
  for(int i = 0; i < numberOfEntries; i++)
  {
   if(contacts[i].getHeight() == height)
   {
    filteredContacts[count] = contacts[i];
    count++;
   } // end of
  } // end for
  return filteredContacts;
}

public Person[] filterByWeight(float weight)
{
  Person[] filteredContacts = new Person[100];
  int count = 0;
  for(int i = 0; i < numberOfEntries; i++)
  {
   if(contacts[i].getWeight() == weight)
   {
    filteredContacts[count] = contacts[i];
    count++;
   } // end of
  } // end for
  return filteredContacts;
}

public Person[] filterByBirthplace(String birthplace)
{
  Person[] filteredContacts = new Person[100];
  int count = 0;
  for(int i = 0; i < numberOfEntries; i++)
  {
   if(contacts[i].getBirthplace().equalsIgnoreCase(birthplace))
   {
    filteredContacts[count] = contacts[i];
    count++;
   } // end of
  } // end for
  return filteredContacts;
}

// Possibly:
// Load from file
// Save to file

public String toString()
{
   String result = "";
  
   for(int i = 0; i < numberOfEntries; i++)
    result += contacts[i] + " ";
  
   return result;
}
}

// The following is a suggestion.
// You do not need to use it in your implementation
class ProgramState
{

// A single scanner to use throughout the program
public Scanner userInput;

// An AddressBook variable to store the actual address book
private 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 ProgramState()
{
  userInput = new Scanner(System.in);
  addresses = new AddressBook();
  currentView = addresses;
}

// 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 String toString()
{
  return currentView.toString();
}
}

public class FinalTemplate
{
public static void main(String[] args) throws FileNotFoundException
{
  ProgramState state = new ProgramState();

  
  int choice;
  
  do
  {
   System.out.println("-------------MENU-------------");
   System.out.println("1) Add a Person");
   System.out.println("2) Display the number of people");
   System.out.println("3) Print the address book");
   System.out.println("4) Filter the address book");
   System.out.println("5) Remove a filter");
   System.out.println("6) Save the address book to a file");
   System.out.println("7) Exit");
   System.out.print("Enter your choice: ");
   choice = state.userInput.nextInt();
   
   switch(choice)
   {
    case 1:
     addPersonHint(state);
     break;
     
    case 2:
     System.out.println("Number of people in the address book: " + state.currentView.getLength());
     break;
     
    case 3:
     printPersons(state);
     break;
     
    case 4:
     filterPersons(state);
     break;
     
    case 5:
     state.resetView();
     break;
     
    case 6:
     saveToFile(state);
     break;
     
    case 7:
     System.out.println("Thank you");
     break;
     
    default:
     System.out.println("Invalid Choice!");
   }
   System.out.println();
  }while(choice != 8);
  addPersonHint(state);
}

public static void addPersonHint(ProgramState state)
{  
  System.out.println("Enter a person's details:");
  System.out.print("First name: ");
  String first = state.userInput.next();
  System.out.print("Last name: ");
  String last = state.userInput.next();
  System.out.print("Age: ");
  int ag = state.userInput.nextInt();
  System.out.print("Height: ");
  float ht = state.userInput.nextFloat();
  System.out.print("Weight: ");
  float wt = state.userInput.nextFloat();
  System.out.print("Birth place: ");
  String bp = state.userInput.next();
  
  Person p = new Person();
  p.setAge(ag);
  p.setFirstName(first);
  p.setLastName(last);
  p.setHeight(ht);
  p.setWeight(wt);
  p.setBirthplace(bp);
  
  state.currentView.addPerson(p);  
}

public static void printPersons(ProgramState state)
{
  System.out.println(state.currentView.toString());
}

public static void filterPersons(ProgramState state)
{
  System.out.print("A for age F for first name L for last name H for Height W for Weight B for Birth place Enter your type: ");
  String type = state.userInput.next();
  Person[] list;
  
  switch(type.charAt(0))
  {
   case 'A':
    System.out.print("Enter age: ");
    int ag = state.userInput.nextInt();
    
    list = state.currentView.filterByAge(ag);
    printList(list);
    break;
    
   case 'F':
    System.out.print("Enter first name: ");
    String first = state.userInput.next();
    
    list = state.currentView.filterByFirstName(first);
    printList(list);
    break;
    
   case 'L':
    System.out.print("Enter last name: ");
    String last = state.userInput.next();
    
    list = state.currentView.filterByLastName(last);
    printList(list);
    break;
    
   case 'H':
    System.out.print("Enter last name: ");
    float ht = state.userInput.nextFloat();
    
    list = state.currentView.filterByHeight(ht);
    printList(list);
    break;
    
   case 'W':
    System.out.print("Enter last name: ");
    float wt = state.userInput.nextFloat();
    
    list = state.currentView.filterByWeight(wt);
    printList(list);
    break;
    
   case 'B':
    System.out.print("Enter last name: ");
    String bp = state.userInput.next();
    
    list = state.currentView.filterByBirthplace(bp);
    printList(list);
    break;
    
   default:
    System.out.println("Invalid type!");
  }
}

public static void saveToFile(ProgramState state) throws FileNotFoundException
{
   PrintWriter pw = new PrintWriter(new File("addresses.txt"));
   pw.println(state.currentView);
   pw.close();
}

public static void printList(Person[] list)
{
  for(int i = 0; i < list.length; i++)
   System.out.println(list[i]);
  System.out.println();
}
}