Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi. I have trouble with my java code. I can't figure out how to proceed with the following task step. and there is something wrong

Hi. I have trouble with my java code. 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 it's wired in class GUI to the method save.

The code is simple I have one class Person, PhoneBook, GUI, and a txt file called PhoneList. You write in the textfield PhoneList.txt then press Load button, it hsould say Phone book loaded, then you press Save button, then it says provide a new for file, you can write anything you like for example oooga.txt and it should make the oooga.txt file and save from the PhoneList.txt to the ooog.txt file and state how many saved people it saved over to the new file. the rest of the information is in the task below. Below is 2 pictures, one for the task, one for the class Person, and two texts where you can copy paste the code directly for PhoneBook and GUI class, so you don't have to rewrite them. 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 Person and PhoneBook class.

[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 the "Save phonebook" button reads the text string in the search fleld 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 the GUI such that pressing the "Search" button reads the string in the search field and clears the search feld afterwards. The string ary that was read in is used to call the method search(arg) of PhoneBook. If the size of the dynamic array that is returned by the call is 0 , then the text "Provide a name" should be written to the name field and " " to the number feld. If the size of the dynamic array is 1 , then the full name of the returned person is written to the name field and the phone number to the number field. If the size of the returned array list exceeds 1 , then you enable the "Next name" button and set a person counter to 0 . The next figure shows the case where you searched for the surname "Walker", which exists twice. If you press the "Next name" button the value of the person counter should be increased by 1 and the GUI should display the next person in the dynamic array that was returned by the search. If you searched for Walker and preseed next once, your result should be that

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

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

Students also viewed these Databases questions

Question

=+5. Who could serve as your champion in the workplace?

Answered: 1 week ago

Question

=+7. What would freedom in the workplace look like for you?

Answered: 1 week ago