基本java数据库组织
我正在为学校做这个数据库类型的程序。到目前为止,我已经能够使这部分代码完全正常工作:基本java数据库组织,java,database,Java,Database,我正在为学校做这个数据库类型的程序。到目前为止,我已经能够使这部分代码完全正常工作: import jpb.*; //jpb is a package that lets me use SimpleIO as you'll see below public class PhoneDirectory { public static void main(String[] args) { PhoneRecord[] records = new PhoneRecord[100];
import jpb.*;
//jpb is a package that lets me use SimpleIO as you'll see below
public class PhoneDirectory {
public static void main(String[] args) {
PhoneRecord[] records = new PhoneRecord[100];
int numRecords = 0;
// Display list of commands
System.out.println("Phone directory commands:\n" +
" a - Add a new phone number\n" +
" f - Find a phone number\n" +
" q - Quit\n");
// Read and execute commands
while (true) {
// Prompt user to enter a command
SimpleIO.prompt("Enter command (a, f, or q): ");
String command = SimpleIO.readLine().trim();
// Determine whether command is "a", "f", "q", or
// illegal; execute command if legal.
if (command.equalsIgnoreCase("a")) {
// Command is "a". Prompt user for name and number,
// then create a phone record and store it in the
// database.
if (numRecords < records.length) {
SimpleIO.prompt("Enter new name: ");
String name = SimpleIO.readLine().trim();
SimpleIO.prompt("Enter new phone number: ");
String number = SimpleIO.readLine().trim();
records[numRecords] =
new PhoneRecord(name, number);
numRecords++;
} else
System.out.println("Database is full");
} else if (command.equalsIgnoreCase("f")) {
// Command is "f". Prompt user for search key.
// Search the database for records whose names begin
// with the search key. Print these names and the
// corresponding phone numbers.
SimpleIO.prompt("Enter name to look up: ");
String key = SimpleIO.readLine().trim().toLowerCase();
for (int i = 0; i < numRecords; i++) {
String name = records[i].getName().toLowerCase();
if (name.startsWith(key))
System.out.println(records[i].getName() + " " +
records[i].getNumber());
}
} else if (command.equalsIgnoreCase("q")) {
// Command is "q". Terminate program.
return;
} else {
// Command is illegal. Display error message.
System.out.println("Command was not recognized; " +
"please enter only a, f, or q.");
}
System.out.println();
}
}
}
// Represents a record containing a name and a phone number
class PhoneRecord {
private String name;
private String number;
// Constructor
public PhoneRecord(String personName, String phoneNumber) {
name = personName;
number = phoneNumber;
}
// Returns the name stored in the record
public String getName() {
return name;
}
// Returns the phone number stored in the record
public String getNumber() {
return number;
}
}
我需要将记录设置为1,而不是100,并且每次记录满时都会有两倍的大小。我还没有搞错,但我不确定从哪里开始。因为要求能够删除记录,我建议使用动态增长的ArrayList,您可以轻松删除记录 它是这样的申报人:
Smith, John 555-5556
Shmoe, Joe 565-5656
ArrayList<PhoneRecord> records = new ArrayList<PhoneRecord>();
records.add(PhoneRecord(name, number)));
records.remove(i);
您可以删除这样的记录:
Smith, John 555-5556
Shmoe, Joe 565-5656
ArrayList<PhoneRecord> records = new ArrayList<PhoneRecord>();
records.add(PhoneRecord(name, number)));
records.remove(i);
删除列表的第i条记录
列表的当前大小由records.size()函数给出
至于第二个问题,您可以使用字符串格式告诉它将名称格式化为指定数量的字符。例如,您可以使用以下方法:
System.out.println(String.format("%s%15s", records[i].getName(), records[i].getNumber());
在本例中,将在电话前添加空格字符,以便字符总数为15
因此,如果您的号码是555-5556,则会在号码前添加7个空格字符