使用构造函数名称的变量创建小型数据库

时间:2014-01-10 02:42:05

标签: java

我正在尝试创建一个可以让你

的应用程序

1 - 将人员添加到小型数据库

2 - 将他们的名字附加到数组

3 - 当检索先前输入的信息时,该阵列将用于选择人

4 - 检索所选人员的唯一信息

我有两个类,Person(),它应该构造一个具有给定变量的新人并存储该信息以供稍后阅读,以及PeopleManager()。

人员类:

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(),我如何将数据提供给他们输入的人特定的应用程序用户?

2 个答案:

答案 0 :(得分:1)

您正在创建一个新的Person对象

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

但你没有保留引用(通过添加数组或其他东西),但你有名称数组跟踪名称。此外,您应该将变量重命名为其他变量,因为您有2个名为相同的变量。

编辑: 正如你所问,这是一个简单的例子。

的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();     
     }
}

答案 1 :(得分:0)

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

你说newFullName是String和Person,这是不可能的

另外,你必须将最后一个功能更改为:

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());

    ...
}