Hi, I was asked to make a Phone Book thats saves entries and call them up. Then the teacher added a new task. The Program should delete entries. He gave us a methode and said we are not allowed to use ArrayList or Hashmaps, ONLY Arrays. I tried alot but just could not seem to get it right, Can someone please help me?
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
public class PhoneBook {
private static int maxSize = 100;
private static int actSize = 0;
private static String[] name = new String[maxSize];
private static String[] firstName = new String[maxSize];
private static String[] number = new String[maxSize];
public static String findEntry(String nameP, String firstNameP) {
for (int i = 0; i < actSize; i++) {
if (name[i].equals(nameP) && firstName[i].equals(firstNameP)) {
return number[i];
}
}
return null;
}
public static void addEntry(String nameP, String firstNameP, String numberP) {
if (actSize == maxSize) {
System.err.println("Memory full!");
return;
}
for (int i = 0; i < actSize; i++) {
if (name[i].equals(nameP) && firstName[i].equals(firstNameP)) {
System.err.println("It exists already!");
return;
}
}
name[actSize] = nameP;
firstName[actSize] = firstNameP;
number[actSize] = numberP;
actSize = actSize + 1;
}
public static String getActionDialog(String... actions) {
JRadioButton[] buttons = new JRadioButton[actions.length];
ButtonGroup group = new ButtonGroup();
for (int i = 0; i<actions.length; i++) {
buttons[i] = new JRadioButton(actions[i]);
group.add(buttons[i]);
}
buttons[0].setSelected(true);
Object[] message = buttons;
Object[] options = { "OK", "Cancel"};
int n= JOptionPane.showOptionDialog(null,
message,
"Choose an Action",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if(n==JOptionPane.OK_OPTION){
for (int i = 0; i<actions.length; i++) {
if(buttons[i].isSelected()){
return actions[i];
}
}
}
return null;
}
/**
* Deletes Entries.
* An entry matches the pattern if it fits in all components (name, first name, number).
* A component fits, when it is equal to the sample component, or the component pattern of the empty string.
*
* @param nameP Pattern for the name of the entries to be deleted.
* @param firstNameP Pattern for the first names of the entries to be deleted.
* @param numberP Pattern for the number of entries to be deleted.
* @post all matching entries are deleted. The capacity remains unchanged.
* The size is adjusted.
* @return Number of deleted entries.
*/
public static int deleteEntries(String nameP, String firstNameP, String numberP) {
return 0;
}
}
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class UI {
public static void main(String[] args) {
while (true) {
String action = PhoneBook.getActionDialog(
"Create an entry", "Call up an entry");
if (action == null) {
break;
}
if (action.equals("Create an entry")) {
String[] entryData = getEntryDataDialog();
if (entryData != null) {
PhoneBook.addEntry(entryData[0], entryData[1],
entryData[2]);
}
} else if (action.equals("Call up an entry")) {
String[] queryData = getQueryDataDialog();
if (queryData != null) {
String nr = PhoneBook.findEntry(queryData[0],
queryData[1]);
if (nr == null) {
JOptionPane.showMessageDialog(null, "Not found");
} else {
JOptionPane.showMessageDialog(null, "Last Name: "
+ queryData[0] + ", First Name: " + queryData[1]
+ ", Number:" + nr);
}
}
} else {
System.err.println("Error: unknow Action " + action);
break;
}
}
}
private static String[] getEntryDataDialog() {
JTextField nameTF = new JTextField();
JTextField firstNameTF = new JTextField();
JTextField numberTF = new JTextField();
Object[] message = { "Last Name", nameTF, "First Name", firstNameTF, "Number",
numberTF };
Object[] options = { "OK", "Cancel" };
int n = JOptionPane.showOptionDialog(null, message, "Create an entry",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);
if (n == JOptionPane.OK_OPTION) {
return new String[] { nameTF.getText(), firstNameTF.getText(),
numberTF.getText() };
} else if (n == JOptionPane.NO_OPTION
|| n == JOptionPane.CLOSED_OPTION) {
return null;
} else {
return null;
}
}
private static String[] getQueryDataDialog() {
JTextField nameTF = new JTextField();
JTextField firstNameTF = new JTextField();
Object[] message = { "Last Name", nameTF, "First Name", firstNameTF };
Object[] options = { "OK", "Cancel" };
int n = JOptionPane.showOptionDialog(null, message, "Call up an entry",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);
if (n == JOptionPane.OK_OPTION) {
return new String[] { nameTF.getText(), firstNameTF.getText() };
} else if (n == JOptionPane.NO_OPTION
|| n == JOptionPane.CLOSED_OPTION) {
return null;
} else {
return null;
}
}
}