Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi. I have trouble with my java code and this is the third time I ask the same question AS THE PREVIOUS EXPERTS haven't done

Hi. I have trouble with my java code and this is the third time I ask the same question AS THE PREVIOUS "EXPERTS" haven't done what I asked them to do correctly I need my classes Person, PhoneBook, and GUI to work so I when press save button it should state the amount of saved "people" like "Saved 50 people to the file", currently I get "Saved 0 people to the file". I can't figure out how to proceed with the following task step and there is something wrong with my savebutton. It's either the save method in class PhoneBook or how savebutton is wired in class GUI to the method save, could also be in class PhoneBook in method load, method addPerson and method save. but either way, I need help with updating my java code for class GUI and any other methods that might need to be updated from my other two classes, so it does what is described on the Task in the picture below. Don't give me any explanation or suggestion on what I should do. as I clearly tried it and hit a wall on what to do here. I just want a detailed explanation of what was wrong with my code and how to do the rest of the tasks below with an updated code for class GUI and any other that might need it, so I can continue with this code myself.

The code is simple I have one class Person, PhoneBook, GUI, and a txt file called PhoneList. You write in the text field PhoneList.txt and then press Load button, it should say "Phone book loaded" and enables all button except for next, then you press Save button, then it says "provide a new name for file", you can write anything you like for example oooga.txt and it then after you wrote oooga.txt and pressed save should make the oooga.txt file and save the data from loaded Phonebook (PhoneList.txt) to the new ooog.txt file and state how many saved people it saved over to the new file. And here's where the trouble for my code is so far. If you run my current code it says "Saved 0 people to the file", I need it to say something like in the picture below "Saved 50 people to the file", so the user knows it has copied over the full phone list to the new file.

Below are 2 pictures, one for the task, and one for the class Person, and at the very bottom of this question post is two texts where you can copy and paste the code directly for the class PhoneBook and class GUI, as well as a picture of PhoneList.txt, so you don't have to rewrite them and know what PhoneList look like. You are allowed to change anything you like except for the methods name in any class or add new methods and change their type and parameters within the class Person and class PhoneBook.

[This is the TASK]

image text in transcribed

[This is class Person]

image text in transcribed

[This is Class PhoneBook]

package lab6;

import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList;

public class PhoneBook {

private ArrayList listOfNumbers;

public PhoneBook() { listOfNumbers = new ArrayList(); }

public String load(String arg) { try { BufferedReader reader = new BufferedReader(new FileReader("F:\\Java Codes\\TND002Labs6\\src\\lab6\\" + arg)); String line = reader.readLine(); while (line != null) { String[] parts = line.split(","); if (parts.length == 3) { String givenName = parts[0].trim(); String surname = parts[1].trim(); int phoneNumber = Integer.parseInt(parts[2].trim()); listOfNumbers.add(new Person(givenName, surname, phoneNumber)); } line = reader.readLine(); } reader.close(); return "Phone book loaded"; } catch (IOException e) { return "Try again"; } }

public ArrayList search(String arg) { ArrayList result = new ArrayList(); for (Person person : listOfNumbers) { if (person.getSurname().equals(arg) || person.getPhoneNumber() == Integer.parseInt(arg)) { result.add(person); } } return result; }

public String deletePerson(String arg1, int arg2) { ArrayList searchResult = search(Integer.toString(arg2)); for (Person person : searchResult) { if (person.getFullName().equals(arg1)) { listOfNumbers.remove(person); return "Person deleted"; } } return "The personumber does not exist"; }

public boolean addPerson(String arg1, int arg2) { if (arg1.trim().split(" ").length != 2) { return false; } for (Person person : listOfNumbers) { if (person.getPhoneNumber() == arg2) { return false; } } String[] nameParts = arg1.trim().split(" "); String givenName = nameParts[0]; String surname = nameParts[1]; listOfNumbers.add(new Person(givenName, surname, arg2)); return true; }

public String save(String arg) { try { FileWriter writer = new FileWriter(arg); for (Person person : listOfNumbers) { String line = String.format("%-20s %-5d%n", person.getFullName(), person.getPhoneNumber()); writer.write(line); } writer.close(); return "Saved " + listOfNumbers.size() + " people to the file."; } catch (IOException e) { return "Could not save to the file"; } } } [This is class GUI]

package lab6;

import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField;

public class GUI extends JFrame {

/** * Serire number idk */ private static final long serialVersionUID = 1L; private JTextField searchField; private JTextField nameField; private JTextField numberField;

private JButton loadButton; private JButton saveButton; private JButton searchButton; private JButton nextButton; private JButton addButton; private JButton deleteButton;

private PhoneBook phoneBook;

public GUI() { setTitle("Interactive phone book"); Font font = new Font("SansSerif", Font.PLAIN, 20); setFont(font);

// Initialize buttons and text fields loadButton = new JButton("Load"); saveButton = new JButton("Save"); searchButton = new JButton("Search"); nextButton = new JButton("Next"); addButton = new JButton("Add"); deleteButton = new JButton("Delete");

searchField = new JTextField(); nameField = new JTextField(); numberField = new JTextField();

// Disable buttons except for "Load" saveButton.setEnabled(false); searchButton.setEnabled(false); nextButton.setEnabled(false); addButton.setEnabled(false); deleteButton.setEnabled(false);

// Disable text fields except for search field nameField.setEnabled(false); numberField.setEnabled(false);

// Initialize phone book phoneBook = new PhoneBook();

// Add action listeners to buttons and text fields loadButton.setActionCommand("load"); loadButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String fileName = searchField.getText(); String result = phoneBook.load(fileName);

if (result.equals("Phone book loaded")) { // Enable buttons except for "Next" saveButton.setEnabled(true); searchButton.setEnabled(true); addButton.setEnabled(true); deleteButton.setEnabled(true); // Display success message nameField.setText(result); } else { // Display error message nameField.setText("Try again"); } // Clear search field searchField.setText(""); } });

saveButton.setActionCommand("save"); saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String fileName = searchField.getText(); if (fileName.isEmpty()) { // Display error message nameField.setText("Provide a file name"); } else { String result = phoneBook.save(fileName); nameField.setText(result); } // Clear search field searchField.setText(""); } });

searchField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { loadButton.doClick(); } });

// Create panels for buttons and text fields JPanel buttonsPanel = new JPanel(new GridLayout(2, 3)); buttonsPanel.add(loadButton); buttonsPanel.add(saveButton); buttonsPanel.add(searchButton); buttonsPanel.add(nextButton); buttonsPanel.add(addButton); buttonsPanel.add(deleteButton);

JPanel textFieldsPanel = new JPanel(new GridLayout(3, 1)); textFieldsPanel.add(searchField); textFieldsPanel.add(nameField); textFieldsPanel.add(numberField);

JPanel mainPanel = new JPanel(new GridLayout(1, 2)); mainPanel.add(buttonsPanel); mainPanel.add(textFieldsPanel);

add(mainPanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); }

public static void main(String[] args) { GUI myGUI = new GUI(); }

}

[The PhoneList.txt]

image text in transcribed

Update GUI such that pressing either the active load button or 'return' in the search field extracts the string from the search field, clears the search field and calls with the extracted string the lood(arg) method of PhoneBook. If a file with the name arg can not be opened, then the the text "Try again" should be written to the name field. Type in the name of the existing file ("PhoneList.txt") into the search field and press 'return' 2 or the load button. The content of the file is loaded into the phone book and the string "Phone book loaded" should be written to the name field. All buttons except the next button should be unlocked and the GUI should book like Update GUI such that pressing the "Save phonebook" button reads the text string in the search field and clears the search field afterwards. If the text string is empty, then the text "Provide a file name" should be written to the name field. If the text string is not empty, call with it the save method of Phonebook and write its return value to the name field. Do not write the phone book to "PhoneList.txt", because you need this file and you may mess it up if your code is not working correctly. Update GUI such that pressing either the active load button or 'return' in the search field extracts the string from the search field, clears the search field and calls with the extracted string the lood(arg) method of PhoneBook. If a file with the name arg can not be opened, then the the text "Try again" should be written to the name field. Type in the name of the existing file ("PhoneList.txt") into the search field and press 'return' 2 or the load button. The content of the file is loaded into the phone book and the string "Phone book loaded" should be written to the name field. All buttons except the next button should be unlocked and the GUI should book like Update GUI such that pressing the "Save phonebook" button reads the text string in the search field and clears the search field afterwards. If the text string is empty, then the text "Provide a file name" should be written to the name field. If the text string is not empty, call with it the save method of Phonebook and write its return value to the name field. Do not write the phone book to "PhoneList.txt", because you need this file and you may mess it up if your code is not working correctly

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_2

Step: 3

blur-text-image_3

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions

Question

5. Have you stressed the topics relevance to your audience?

Answered: 1 week ago