Extend the program as follows: Extend the record format to allow both a first na
ID: 3718045 • Letter: E
Question
Extend the program as follows:
Extend the record format to allow both a first name and a last name
Keep the limit of 8 characters for the fist name and 8 characrers for the last name.
Include a command “a” (for alphabetize) to put the phonebook in alphabetical order.
Include a command “m” (for merge) to combine entries that may be duplicates or variants for a given person. For example, if John Smith has two entries, combine the notes into one entry, and if the phone numbers are different, put the second number in the notes.
Construct a Graphical User Interface (GUI) for the user using JavaFX. You must make the program object oriented. This means having a separate class for the internal representation of a phonebook, with methods like add, list, or delete. It also means having a separate class for the GUI aspects of the program.
Usability and Aesthetics: Make the GUI especially pleasing to see and use. One example would be to make the list command give a nice listing.
Human Engineering: Provide good human engineering for the user. This means being very tolerant of user errors and making it easy to use. For example, you might give the user an option to name the phonebook file, or you might check if the user tries to add another entry with the same name. Also, consider a simple Help command.
Reliability: Make the program especially reliable. Try to make it so that the program will not crash even under incorrect inputs. For example, handle a missing file or prevent an array out of bounds error.
Maintainability: Make the program especially well-structured and readable.
Functionality: Enhance the functionality of the program in various ways, even small ways. For functionality, one option is to add a command ‘d’ for delete to delete an entry corresponding to a name typed by the user. Another enhancement would be to check that a find or enter command actually has a non-null string for the name. A little more work would be to check the format of the phone number. For instance, check that a phone number has one of the following valid forms 419-460-1212 or (419)460-1212 or 460-1212 (Note that one can use regular expressions for this.)
Make a usable binary search to retrieve the phone number of a person.
Allow a partial match to find or delete an entry. For example, “F mi” would match any entry with “mi” in the name, for example “Smith” or “Hermit” or Mitchell. You may use the substring function in Java for this feature.
import java.io.*;
import java.util.*;
class Entry {
public String name, number, note;
}
public class PhonebookFor1510 {
public static Entry[] contactList;
public static int num_entries;
public static Scanner stdin = new Scanner(System.in);
public static void main(String args[]) throws Exception{
int i; char C;
String code, Command;
contactList = new Entry[200];
num_entries = 0;
readPhoneBook("phonebook.txt");
System.out.println("Codes are entered as 1 to 8 characters. Use" +
" "e" for enter," +
" "f" for find," +
" "l" for listing all the entries," +
" "q" to quit.");
Command = null;
C = ' ';
while(C != 'q'){
System.out.print("Command: ");
Command = stdin.next();
C = Command.charAt(0);
switch (C) {
case 'e': addContact(); break;
case 'f':
code = stdin.next();
stdin.nextLine();
i = index(code);
if (i >= 0) displayContact(contactList[i]);
else System.out.println("**No entry with code " + code); break;
case 'l':
listAllContacts(); break;
case 'q':
CopyPhoneBookToFile("PhoneBook1.txt");
System.out.println("Quitting the application. All the entries are "
+ "stored in the file PhoneBook1.txt"); break;
default:
System.out.println("Invalid command Please enter the command again!!!");
}
}
}
public static void readPhoneBook(String FileName) throws Exception {
File F;
F = new File(FileName);
Scanner S = new Scanner(F);
while (S.hasNextLine()) {
contactList[num_entries]= new Entry();
contactList[num_entries].name = S.next();
contactList[num_entries].number = S.next();
contactList[num_entries].note = S.nextLine(); num_entries++;
}
S.close();
}
public static void addContact() {
String name = stdin.next();
String number;
stdin.nextLine();
contactList[num_entries] = new Entry();
contactList[num_entries].name = name;
System.out.print("Enter Number: ");
number = stdin.nextLine();
contactList[num_entries].number = number;
System.out.print("Enter Notes: ");
contactList[num_entries].note = stdin.nextLine();
num_entries++;
}
public static int index(String Key) {
// Function to get the index of a key from an array
//if not found, returns -1
for (int i=0; i < num_entries; i++) {
if (contactList[i].name.equalsIgnoreCase(Key))
return i; // Found the Key, return index.
}
return -1;
}
public static void displayContact(Entry contact) {
System.out.println("--"+ contact.name+" "+
contact.number+" "+
contact.note);
}
public static void listAllContacts() {
int i = 0;
while (i < num_entries) {
displayContact(contactList[i]);
i++;
}
}
public static void CopyPhoneBookToFile(String FileName) throws Exception{
FileOutputStream out = new FileOutputStream(FileName);
PrintStream P = new PrintStream( out );
for (int i=0; i < num_entries; i++) {
P.println(contactList[i].name + " " + contactList[i].number +
" " + contactList[i].note);
}
}}
Explanation / Answer
Answer:-
The java fx conversion of the above project can be put as:
Main.java
Controller.java
sample.fxml
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.