2014-01-10 31 views
0

我試圖創建一個應用程序,可以讓你創建使用變量構造函數名稱的小型數據庫

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(),我該如何將這些統計信息提供給特定於他們所輸人的應用程序用戶?

+0

爲什麼不在調用'addPerson'時返回您創建的'Person'實例? – MadProgrammer

+0

對不起,我還是新手。你到底什麼意思?你能提供一個例子嗎? –

+0

爲什麼在'PeopleManager'中一切都是靜態的?我也認爲你可能會在'PeopleManager'中算人,而不是靜態的'theNumPersons'。 –

回答

1

您正在創建一個新的Person對象

names.add(newFullName); 
    Person newFullName = new Person (newFirst, newLast, iNewYear); 

,但你們不守所引用(通過將其添加陣列或東西),但你有名字數組跟蹤的名稱。另外,您應該將該變量重命名爲其他名稱,因爲您有兩個名稱相同的變量。

編輯: 正如你所問,這裏是一個簡單的例子。

的Class1:

public class Person 
{ 
    public String name; 
    public String lastname; 

    public Person(String name, String lastname) 
    { 
     this.name = name; 
     this.lastname = lastname; 
    } 


    public String toString() 
    { 
     return this.name + " " + this.lastname; 
    } 
} 

類2:

import java.util.*; 

public class PersonManager{ 

    //array list to keep track of all the Person objects that will be created 
    public static ArrayList<Person>peoples = new ArrayList<Person>(); 

    //assume this function takes input from user and returns a new 
    //person object 
    public static Person getPerson(String name, String last) 
    { 
     Person p = new Person(name, last); 
     return p; 
    } 

    //this function removes a person with the first name 
    public static boolean removePerson(String name) 
    { 
     //we should loop through the array and find the object we want to delete 
     Person objectToRemove = null; 
     for (Person p : peoples) 
     { 
      if (p.name.equals(name))  
      { 
       //this is the object we want to remove 
       //because the name matched 
       objectToRemove = p; 
       break; 
      } 
     } 

     //we will actually remove the object outside of the loop 
     //so we don't run into errors... 
     if (objectToRemove != null) 
     { 
      //tell array list to remove the object we wanted to delete 
      peoples.remove(objectToRemove); 
      System.out.println("\nRemoving person = "+objectToRemove); 

     } 
     return objectToRemove != null; 
    } 

    public static void printInfo() 
    { 

     System.out.println("\n\nPrinting info"); 
     //loop through all the object in the peoples array and print out their names 
     for (Person p : peoples) 
     { 
      System.out.println(p); 
     } 

     System.out.println("In total, there are "+ peoples.size() +" objects saved in the array"); 

    } 
    public static void main(String []args) 
    { 
     //creating 3 different people and adding them to the array list 
     peoples.add(getPerson("John", "Doe"));  
     peoples.add(getPerson("Jane", "Doe"));  
     peoples.add(getPerson("Will", "Smith")); 

     //print all the users in the array and the size of the array 
     printInfo(); 

     //remove the person with first name = John. 
     removePerson("John"); 

     //print all the users in the array and the size of the array 
     printInfo();  
    } 
} 
+0

如果你有時間,你會介意分享一個你的意思嗎?非常感謝您的時間 –

0
String newFullName = (newFirst + " " + newLast); 
Person newFullName = new Person (newFirst, newLast, iNewYear); 

你說newFullName是字符串和人,這是不可能的

也,你必須改變過去的功能如下:

public static void getInfo(Person p) 
{ 
    String infoFirst = p.getFirstName(); 
    String infoLast = p.getLastName(); 
    String infoYear = Integer.toString(p.getBirthYear()); 
    String databaseSize = Integer.toString(Person.getNumPersons()); 

    ... 
} 
+0

請參閱,問題是需要創建幾個不同的實體(但實體的數量取決於用戶),以便可以存儲它們的所有單個數據。我認爲newFullName在構建一個新Person時會作爲變量工作。還有什麼其他的方式來做到這一點? –

+0

在這種情況下,我會將人員存儲在ArrayList中,而不是名稱和姓氏的字符串。在Person類中也實現toString()方法,以返回所需的字符串 – 2014-01-10 03:27:23

相關問題