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

Here is my java program: import java.io.*; import java.util.*; class Entry { pub

ID: 3716932 • Letter: H

Question

Here is my java program:

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); } }}

The file is a blank file named phonebook.txt, I bleieve you have to make the file as a new I'm not sure exactly how to attach it.

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.

Explanation / Answer

package phonebookfor1510;

import java.io.*;

import java.util.*;

class Entry {

public String firstname, lastname, 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 'a':

sortContactList();

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 sortContactList() {

for (int i = 0; i < contactList.length - 1; i++) {

for (int j = i + 1; i < contactList.length; j++) {

if ((contactList[j].firstname.toCharArray()[0]) < (contactList[i].firstname.toCharArray()[0])) {

Entry tmp = contactList[j];

contactList[j] = contactList[i];

contactList[i] = tmp;

}

}

}

}

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].firstname = S.next();

contactList[num_entries].lastname = 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].firstname = name;

name = stdin.next();

contactList[num_entries].lastname = 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++;

}

// Function to get the index of a key from an array // if not found, returns -1

public static int index(String Key) {

for (int i = 0; i < num_entries; i++) {

if (contactList[i].firstname.equalsIgnoreCase(Key)) {

return i; // Found the Key, return index.

}

}

return -1;

}

public static void displayContact(Entry contact) {

System.out.println("--" + contact.firstname + " " + contact.lastname + " " + 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].firstname + " " + contactList[i].lastname + " " + contactList[i].number + " " + contactList[i].note);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote