PLEASE...NEED HELP...Cannot get my program to work...all code and output needed
ID: 645748 • Letter: P
Question
PLEASE...NEED HELP...Cannot get my program to work...all code and output needed is included
AddressBook Class
public class AddressBook {
// Declare variables
private static final int ADD_PERSON=1;
private static final int REMOVE_PERSON=2;
private static final int SORT_FULL_NAME=3;
private static final int VIEW_PERSONS=4;
private static final int EXIT=5;
private List<Person> persons;
private Scanner input;
//
public AddressBook(){
persons = new ArrayList<Person>();
input = new Scanner(System.in);
}
// Core interaction logic, shows menu, reads in user option, calls methods
// to match users input option, and repeats this in a loop until the user
// chooses to exit the program.
public void runAddressBook() {
int userOption;
while(true) {
showMenu();
System.out.println("Enter your option ?");
userOption = input.nextInt();
switch(userOption) {
case ADD_PERSON:
addPerson();
break;
case REMOVE_PERSON:
removePerson(userOption);
break;
case SORT_FULL_NAME:
sortByFullName();
break;
case VIEW_PERSONS:
viewPersons();
break;
case EXIT:
break;
default:
System.out.println("Invalid Choice");
}
if(userOption==EXIT)
break;
}
}
// Displays the menu for the user using values
public void showMenu() {
System.out.println("1. Add person");
System.out.println("2. Delete person");
System.out.println("3. Sort by full name");
System.out.println("4. View persons");
System.out.println("5. Exit program");
}
// Attempts to add a person to Address Book after interacting with the user
// to get the needed information.
public void addPerson(){
System.out.println("Please enter full name");
String fullName = input.next();
System.out.println("Please enter phone number");
String phoneNumber = input.next();
System.out.println("Please enter email address");
String email = input.next();
persons.add(new Person(fullName, phoneNumber, email));
}
public void removePerson(int index){
System.out.println("Enter index of person to remove");
persons.remove(index);
}
public void sortByFullName(){
Collections.sort(persons, new PersonFullNameComparator());
}
public void viewPersons(){
for(int i = 0; i < persons.size(); ++i){
System.out.println("Person " + (i));
System.out.println("Name: " + persons.get(i).getFullName());
System.out.println("Phone Number: " + persons.get(i).getPhoneNumber());
System.out.println("Email: " + persons.get(i).getEmail());
}
}
}
PersonFullNameComparator Class
import java.util.Comparator;
public class PersonFullNameComparator implements Comparator {
@Override
public int compare(Person arg0, Person arg1) {
return arg0.getFullName().compareTo(arg1.getFullName());
}
}
AddressBookLauncher Class
public class AddressBookLauncher {
public static void main(String[] args) {
// AddressBook manager = new AddressBook();
// manager.runAddressBook();
(new AddressBook()).runAddressBook();
}
}
Person Class
public class Person {
// Declare variables
private String fullName;
private String phoneNumber;
private String email;
// Default constructor
public Person() {
fullName = "unknown";
phoneNumber = "unknown";
email = "unknown";
}
public Person(String fullName, String phoneNumber, String email){
this.fullName = fullName;
this.phoneNumber = phoneNumber;
this.email = email;
}
public String getFullName(){
return fullName;
}
public void setFullName(String fullName) throws ValidationException{
validateString(fullName, "fullName", 50);
this.fullName = fullName;
}
public String getPhoneNumber(){
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) throws ValidationException{
validateString(phoneNumber, "phoneNumber", 15);
this.phoneNumber = phoneNumber;
}
public String getEmail(){
return email;
}
public void setEmail(String email) throws ValidationException{
validateString(email, "email", 35);
this.email = email;
}
public String toString(){
return String.format("%s %s %s", fullName, phoneNumber, email);
}
public static void validateString(String value, String fieldName, int maxLength) throws ValidationException
{
if(fieldName.equals("fullName"))
{
if(value == null || value.equals("") || value.length() > maxLength)
throw new ValidationException("Full Name cannot be empty");
int spacesCount = value.length() - value.replace(" ", "").length();
int commaCount = value.length() - value.replace(",", "").length();
if(value.length() == spacesCount || commaCount > 0)
throw new ValidationException("Full Name cannot be empty");
}
if(fieldName.equals("phoneNumber"))
{
if(value == null || value.equals("") || value.length() > maxLength)
throw new ValidationException("Phone number cannot be empty");
int spacesCount = value.length() - value.replace(" ", "").length();
int commaCount = value.length() - value.replace(",", "").length();
if(value.length() == spacesCount || commaCount > 0)
throw new ValidationException("Phone number cannot be empty");
}
if(fieldName.equals("email"))
{
if(value == null || value.equals("") || value.length() > maxLength)
throw new ValidationException("Email cannot be empty");
int spacesCount = value.length() - value.replace(" ", "").length();
int commaCount = value.length() - value.replace(",", "").length();
if(value.length() == spacesCount || commaCount > 0)
throw new ValidationException("Email cannot be empty");
}
}
}
ValidationException Class
public class ValidationException extends Exception {
ValidationException()
{
super("Data not in correct format");
}
ValidationException(String detail) {
super(detail);
}
ValidationException(String detail,Throwable t) {
super(detail,t);
}
ValidationException(Throwable t) {
super(t);
}
}
OUTPUT NEEDED
1 Add person
2 Remove person
3 Sort by full name
4 View persons
5 Exit program
2
Nothing to remove, persons list is empty 1 Add person
2 Remove person
3 Sort by full name
4 View persons
5 Exit program
3
Nothing to sort
1
Add person
2
Remove person
3
Sort
by full name
4
View
persons
5
Exit
program
4
No persons to display
1
Add person
2
Remove person
3
Sort
by full name
4
View
persons
5
Exit
program
1
Please
enter
full name
Please
enter
phone number
Please
enter
email address
Full Name cannot be empty
1
Add person
2
Remove person
3
Sort
by full name
4
View
persons
5
Exit
program
1
Please
enter
full name
Xyz zzz
Please
enter
phone number
Please
enter
email address
Phone Number
cannot be empty
1
Add person
2
Remove person
3
Sort
by full name
4
View
persons
5
Exit
program
1
Please
enter
full name
xyz zzz
Please
enter
phone number
123
123 1234
Please
enter
email address
Email cannot
be empty
1
Add person
2
Remove person
3
Sort
by full name
4
View
persons
5
Exit
program
1
Please
enter
full name
xyz zzz
Please
enter
phone number
123
123 1234
No inputs, just "enter" "enter"
Please
enter email address
xyz@zzz.com
1
Add person
2
Remove person
3
Sort
by full
name
4
View
persons
5
Exit
program
4
Index:
0 Person: xyz zzz,123 123 1234,xyz@zzz.com
1
Add person
2
Remove person
3
Sort
by full
name
4
View
persons
5
Exit
program
1
Please
enter full name
abc def
Please
enter phone number
321
321 3210
Please
enter email address
abc@def.com
1
Add person
2
Remove person
3
Sort
by full
name
4
View
persons
5
Exit
program
4
Index:
0 Person: xyz zzz,123 123 1234,xyz@zzz.com
Index:
1 Person: abc def,321 321 3210,abc@def.com
1
Add person
2
Remove person
3
Sort
by full
name
4
View
persons
5
Exit
program
3
Add person
1
2
Remove person
3
Sort
by full
name
4
View
persons
5
Exit
program
4
Index:
0 Person: abc def,321 321 3210,abc@def.com
Index:
1 Person: xyz zzz,123 123 1234,xyz@zzz.com
1
Add person
2
Remove person
3
Sort
by full
name
4
View
persons
5
Exit
program
2
Enter index of
person to remove
0
Add person
1
2
Remove person
3
Sort
by full
name
4
View
persons
5
Exit
program
4
Index:
0 Person: xyz zzz,123 123 1234,xyz@zzz.com
1
Add person
2
Remove person
3
Sort
by full
name
4
View
persons
5
Exit
program
2
Enter index of
person to remove
1
Invalid index number 1 Add person
2 Remove person
3 Sort by full name
4 View persons
5 Exit program
2
Enter index of person to remove
0
1 Add person
2 Remove person
3 Sort by full name
4 View persons
5 Exit program
4
No persons to display 1 Add person
2 Remove person
3 Sort by full name
4 View persons
5 Exit program
5
Program will exit
Explanation / Answer
AddressBook Class
public class AddressBook {
// Declare variables
private static final int ADD_PERSON=1;
private static final int REMOVE_PERSON=2;
private static final int SORT_FULL_NAME=3;
private static final int VIEW_PERSONS=4;
private static final int EXIT=5;
private List<Person> persons;
private Scanner input;
//
public String FullName,phoneNumber,email;
public AddressBook(){
persons = new ArrayList<Person>();
input = new Scanner(System.in);
}
// Core interaction logic, shows menu, reads in user option, calls methods
// to match users input option, and repeats this in a loop until the user
// chooses to exit the program.
public void showMenu() {
System.out.println("1. Add person");
System.out.println("2. Delete person");
System.out.println("3. Sort by full name");
System.out.println("4. View persons");
System.out.println("5. Exit program");
}
public void runAddressBook() {
int userOption;
while(true) {
showMenu();
System.out.println("Enter your option ?");
userOption = input.nextInt();
switch(userOption) {
case ADD_PERSON:
addPerson();
break;
case REMOVE_PERSON:
removePerson();
break;
case SORT_FULL_NAME:
sortByFullName();
break;
case VIEW_PERSONS:
viewPersons();
break;
case EXIT:
break;
default:
System.out.println("Invalid Choice");
}
if(userOption==EXIT)
break;
}
}
// Displays the menu for the user using values // Attempts to add a person to Address Book after interacting with the user
// to get the needed information.
public void addPerson(){
System.out.println("Please enter full name");
FullName = input.next();
System.out.println("Please enter phone number");
phoneNumber = input.next();
System.out.println("Please enter email address");
email = input.next();
persons.add(new Person(FullName, phoneNumber, email));
}
public void removePerson(){
System.out.println("Enter index of person to remove");
index=input.nextint();
persons.remove(index);
}
public void remove(int index)
{
if(persons.size==0)
System.out.println(
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.