Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me complete the following java programming tasks. I need a correct answer as a reference for me to correct my own tasks. Thank

Please help me complete the following java programming tasks. I need a correct answer as a reference for me to correct my own tasks. Thank you very much.
Here are the tasks in the exercise:
Checking for duplicated entry when a new phone number is added(no need to finish)
Finding a phone number entry by name(no need to finish)
Delete a phone number entry by name(please finish this part)
Here is a part of the uncompleted java code given in the task. Please finish this part:
// Task 3- Deleting a phone book entry by name
//
// Get the path of the text file
// Use a BufferedReader to read the data one by one
// For each data line create a PhoneBookEntry
// If the name in the PhoneBookEntry is NOT equal to the input name
// add the entry to the entries array
// Use a BufferedWriter to write the stored data from the array into
// the text file one by one
// Close the BufferedWriter
}
// The main method to start the phone book program
public static void main(String[] args)
{
// Start the phone book program
PhoneBook phonebook = new PhoneBook();
phonebook.mainmenu();
}
}
In this exercise, each phone number entry is still stored as a separate line. However, the name and the phone number of one entry are separated by a colon, without a space. You can see the format of the phone book from the given phonebook.txt, as shown below:
CSO:2358-6333
FMO:2358-6421
FO:2358-6418
HRO:2358-6580
ITSC:2358-6188
SAO:2358-0842
The third task is deleting a phone number entry by name.
Since we only have one single file, you will need to read the phone book into a temporary storage, delete the appropriate entry and then save the data back to the same file. The temporary place that we are going to use is an array. You can find this code in the given deleteNumberByName method:
// Prepare an array to store the phone number entries
PhoneBookEntry[] entries = new PhoneBookEntry[100];
// Store the number of entries in the phone book
int numOfEntries =0;
The above code creates an array of PhoneBookEntry. The size of the array has been fixed at 100 so we need something to remember the actual number of entries in the phone book. The numOfEntries variable is created for this purpose. It stores the actual number of entries in the phone book.
You will read the data file line by line. For each line in the file you will create a new PhoneBookEntry object and put the object in the entries array. You will need to store the phone book entry into the array using this code:
entries[numOfEntries]= entry;
numOfEntries++;
The first line of above code stores the PhoneBookEntry object into the array, using the position indicated by the value of numOfEntries. Initially the numOfEntries variable is 0. That means the first time the above code is run, the PhoneBookEntry object becomes the first item of the array. When you run it the second time, you should store the PhoneBookEntry object in the second position of the array. Therefore, each time after you store the object in the array you need to increase the numOfEntries variable by one, as shown in the second line of the above code. If you do this, the numOfEntries variable will keep track of the current item you want to store the object as well as the actual number of items you have stored so far.
You don't actually need to store all phone number entries into the entries array. Remember that the objective of this task is to delete one entry from the phone book, you can store all entries in the array, except the one which matches the name entered by the user. You can use an if statement to do this, as shown below:
if (!entry.getName().equals(name)){
...Store the object in the array...
}
After this stage, the entries array should contain all phone number entries, except the one you want to delete.
You can save the entries back to the data file. You can use a BufferedWriter to do that.
To get a BufferedWriter, you can use the following line of code (assuming you have a correct Path object):
BufferedWriter writer =
Files.newBufferedWriter(path, Charset.defaultCharset(), StandardOpenOption.APPEND);
However, you need to use a slightly different BufferedWriter this time. That means you need to remove the StandardOpenOption.APPEND parameter so that the BufferedWriter will erase the current content of the file before writing the new one.
If the BufferedWriter is ready, you can use a for loop to write the entries into the file. For each entry you need to write the content of the entry into the file using the following code, assuming entries[i] points to the current entry in the array inside the loop:
// Add a phone number entry in a line
writer.write(entries[i].toLine());
writer.newLine();

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Repairing And Querying Databases Under Aggregate Constraints

Authors: Sergio Flesca ,Filippo Furfaro ,Francesco Parisi

2011th Edition

146141640X, 978-1461416401

More Books

Students also viewed these Databases questions

Question

CL I P COL Astro- L(1-cas0) Lsing *A=2 L sin(0/2)

Answered: 1 week ago