我被要求制作一个保存条目并打电话给他们的电话簿。然后老师又添加了一项新任务。该计划应删除条目。他给了我们一个方法,并说我们不允许使用ArrayList或Hashmaps,只有Arrays。我尝试了很多,但似乎无法做到正确,有人可以帮我纠正我的错误吗?
共有2个类:PhoneBook
和UI
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 deleteMatchingEntries(String nameP, String firstNameP,
String numberP) {
for (int i = 0; i < actSize; i++) {
if (name[i].equals(nameP)) {
name[i] = name[i + 1];
actSize--;
} else if (firstName[i].equals(firstNameP)) {
firstName[i] = firstName[i + 1];
actSize--;
} else if (number[i].equals(numberP)) {
number[i] = number[i + 1];
actSize--;
}
}
return actSize;
}
}
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","Delete 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 if (action.equals("Delete Entry")) {
String[] delete = getEntryDataDialog();
if (delete != null) {
TelephoneDirectory.deleteMatchingEntries(delete[0],
delete[1], delete[2]);
JOptionPane.showMessageDialog(null, "Entry deleted");
}
} 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;
}
}
}
答案 0 :(得分:0)
您遇到的问题是,在删除条目时,您只移动了一条记录。
您可以执行以下一些更改:
1 - (如果老师允许的话)创建一个包含一个名称,姓氏和号码的PhoneEntry类
2 - (如果允许,再次)将3个数组更改为一个PhoneEntry对象数组(这样可以更容易处理)
3 - 删除时,您不需要移动下一个条目,只需将null放在删除PhoneEntry的位置即可。您还将actSize的含义更改为actQuantity,对于下一个条目添加,您将搜索数组中具有空值的第一个位置。
干杯
答案 1 :(得分:0)
你应该使用一个例如班级联系或其他什么。
如果你看一下:
public static int deleteMatchingEntries(String nameP, String firstNameP,
String numberP) {
for (int i = 0; i < actSize; i++) {
if (name[i].equals(nameP)) {
name[i] = name[i + 1];
actSize--;
} else if (firstName[i].equals(firstNameP)) {
firstName[i] = firstName[i + 1];
actSize--;
} else if (number[i].equals(numberP)) {
number[i] = number[i + 1];
actSize--;
}
}
return actSize;
}
假设您在阵列的[0]位置都有类似“Doe,John,555-5555”的内容。你的函数将删除Doe,然后保留John和555.突然你的索引不匹配。
此外,我将把deleteMatchingEntries和重构数组删除逻辑重构为一个单独的函数,你可以单独测试。