I have been working on this project for about a week and a half now and I am at
ID: 3574179 • Letter: I
Question
I have been working on this project for about a week and a half now and I am at 350 lines of code. I didn't approach this correctly from the start. Currently, I have the listing, insert, search, write, and quit methods working. However, the listing method doesn't sort. Because of the approach I've taken I've been reaching countless dead-ends.
Description You are developing and maintaining a mini database that stores the contact information of your friends and family members. This mini database has three fields, last name, first name, and telephone number per record (one record per person). All records are originally on a file called MiniDB. You are asked to performing the following operations after input a records from MiniDB to your program and the commands as follow 1) L st all data records ast name, first name, and telephone number 2) l insert a new data records ast name, first name, and telephone number 3) S: search record using either last name, first name, or telephone number. Print the result on the screen, if there are multiple match print all. Example S John will find all persons with name fields match to "John". If no "John" exist, it outputs John not found on the screen. 4) D remove a data record from the MinnDB. Example: D John will delete the person who's name match "John". The D command need to confirm with the user if there is a match. multiple matches, the D command need to ask the user which one need to be removed from the MiniDB 5) M modify a record. Example: M John first search for "John", if found then ask the user for the updated information. For multiple matches, the D command need to ask the user which "John" need to be modified. 6) W write the changes to the MiniDB file. 7) Q: quit the program without change to the MiniDB fileExplanation / Answer
Scanner s=new Scanner(System.in);
int k=0;
public void modify(String key )throws Exception
{
BufferedReader br=new BufferedReader(new FileReader("F:\MiniDB.txt"));
String currLine="";
int count=0;
List<String> modify=new ArrayList<>();
List<String> all=new ArrayList<>();
while((currLine=br.readLine())!=null)
{
String []parts=currLine.split(" ");
all.add(currLine);
if(parts[0].equals(key)|| parts[1].equals(key)){
System.out.println(currLine);
modify.add(currLine);
count++;
}
}
if(count==0)
System.out.println("No matching records found");
else if(count>1)
{
System.out.println("Which record you want to modify?");
int num=s.nextInt();
String temp=modify.get(num-1);
System.out.println("enter new word :");
String newS=s.nextLine();
String modified=temp.replaceAll(key, newS);
for(int i=0;i<all.size();i++)
{
if(all.get(i).equals(key)|| all.get(i).equals(key))
{
k++;
if(k==num)
{
all.remove(i);
all.add(i, modified);
}
}
}
}
else
{
String temp=modify.get(0);
System.out.println("enter new word :");
String newS=s.nextLine();
String modified=temp.replaceAll(key, newS);
for(int i=0;i<all.size();i++)
{
if(all.get(i).equals(key)|| all.get(i).equals(key))
{
all.remove(i);
all.add(i, modified);
}
}
}
FileWriter fw = new FileWriter("F:\MiniDB.txt");
for (int i = 0; i <all.size(); i++) {
fw.write(all.get(i));
}
fw.close();
}
public void delete(String key)throws Exception
{
BufferedReader br=new BufferedReader(new FileReader("F:\MiniDB.txt"));
String currLine="";
int count=0;
List<String> delete=new ArrayList<>();
List<String> all=new ArrayList<>();
while((currLine=br.readLine())!=null)
{
String []parts=currLine.split(" ");
all.add(currLine);
if(parts[0].equals(key)|| parts[1].equals(key)){
System.out.println(currLine);
delete.add(currLine);
count++;
}
}
k=0;
if(count==0)
System.out.println("No matching records found");
else if(count>1)
{
System.out.println("Which record you want to delete?");
int num=s.nextInt();
String temp=delete.get(num-1);
for(int i=0;i<all.size();i++)
{
if(all.get(i).equals(key)|| all.get(i).equals(key))
{
k++;
if(k==num)
{
all.remove(i);
}
}
}
}
else
{
for(int i=0;i<all.size();i++)
{
if(all.get(i).equals(key)|| all.get(i).equals(key))
{
all.remove(i);
}
}
}
FileWriter fw = new FileWriter("F:\MiniDB.txt");
for (int i = 0; i <all.size(); i++) {
fw.write(all.get(i));
}
fw.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.