首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用构造函数名称的变量创建一个小型数据库

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

Stack Overflow用户
提问于 2014-01-10 10:42:05
回答 2查看 618关注 0票数 0

我正在尝试创建一个应用程序,它可以让您

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

2-将它们的名称附加到数组中

3-检索之前输入的信息时,该数组将用于选择人员

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

我有两个类,Person()和PeopleManager(),person ()用给定的变量构造一个新的person,并存储该信息供以后读取。

Person类:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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(),我怎么能给应用程序用户提供特定于他们输入的人的统计数据呢?

EN

Stack Overflow用户

发布于 2014-01-10 10:48:03

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

代码语言:javascript
复制
    names.add(newFullName);
    Person newFullName = new Person (newFirst, newLast, iNewYear);

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

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

class1:

代码语言:javascript
复制
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;
    }
}

第二类:

代码语言:javascript
复制
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
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21035295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档