我試圖創建一個應用程序,可以讓你創建使用變量構造函數名稱的小型數據庫
1 - 加人到一個小型的數據庫
2 - 追加他們的名字到一個數組
3 - 先前輸入的檢索信息時,所述陣列將被用來選擇人
4 - 檢索所選擇的人的唯一信息
我有兩個類Person(),它應該用給定的變量構造一個新的人,並存儲該信息以供稍後閱讀,而PeopleManager()。
Person類:
public class Person extends Object
{
private static int theNumPersons = 0; // initialize num
private String itsFirstName;
private String itsLastName;
private int itsBirthYear;
public Person (String first, String last, int year)
{
super();
theNumPersons++; // update num
itsFirstName = first;
itsLastName = last; // initialize last name
itsBirthYear = year;
}
/** Tell how many different Persons exist. */
public static int getNumPersons() // access num
{
return theNumPersons;
}
/** Return the birth year. */
public int getBirthYear()
{
return itsBirthYear;
}
/** Return the first name. */
public String getFirstName()
{
return itsFirstName;
}
/** Return the last name. */
public String getLastName() // access last name
{
return itsLastName;
}
/** Replace the last name by the specified value. */
public void setLastName (String name) // update last name
{
itsLastName = name;
}
}
PeopleManager類:
import java.util.*;
import javax.swing.*;
public class PeopleManager
{
static ArrayList names = new ArrayList();
static int selection;
public static void main()
{
askSelection();
}
public static void askSelection()
{
Object[] options = { "Add to Database", "Retrieve Info" };
selection = JOptionPane.showOptionDialog(null, "What would you like to do?", "People Database Application", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
executeSelection();
}
public static void executeSelection()
{
if (selection == 0)
{
addPerson();
askSelection();
}
if (selection == 1)
{
Object[] nameArray = names.toArray();
Object person = JOptionPane.showInputDialog(null, "Select person to grab info from.", "People Database Application", JOptionPane.DEFAULT_OPTION, null, nameArray, nameArray[0]);
getInfo(person);
askSelection();
}
}
public static void addPerson()
{
String newFirst = JOptionPane.showInputDialog (null, "Enter the first name.", "John");
String newLast = JOptionPane.showInputDialog (null, "Enter the last name.", "Doe");
String sNewYear = JOptionPane.showInputDialog (null, "Enter that person's birth year.", "1965");
String newFullName = (newFirst + " " + newLast);
int iNewYear = Integer.parseInt(sNewYear);
names.add(newFullName);
Person newFullName = new Person (newFirst, newLast, iNewYear);
JOptionPane.showMessageDialog (null, "Person successfully added.");
}
public static void getInfo(Object p)
{
String infoFirst = p.getFirstName;
String infoLast = p.getLastName;
String infoYear = p.getBirthYear;
String databaseSize = getNumPersons();
JOptionPane.showMessageDialog(null, "First Name: " + infoFirst + "\nLast Name: " + infoLast + "\nBirth Year: " + infoYear + "\n\nTotal people in database: " + databaseSize);
}
}
我知道我沒有做正確的事情,而且我敢肯定它與我想順便做通過使用變量來創建一個新的Person()。問題是,如果我不能使用一個變量來創建一個新的Person(),我該如何將這些統計信息提供給特定於他們所輸人的應用程序用戶?
爲什麼不在調用'addPerson'時返回您創建的'Person'實例? – MadProgrammer
對不起,我還是新手。你到底什麼意思?你能提供一個例子嗎? –
爲什麼在'PeopleManager'中一切都是靜態的?我也認爲你可能會在'PeopleManager'中算人,而不是靜態的'theNumPersons'。 –