[英]Creating a small database using variables for constructor names
我正在尝试创建一个应用程序,让您
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(),我如何才能将统计信息提供给特定于应用程序用户的输入用户?
您正在创建一个新的Person对象
names.add(newFullName);
Person newFullName = new Person (newFirst, newLast, iNewYear);
但是您并没有保留引用的内容(通过添加数组或其他东西),但是拥有可以跟踪名称的names数组。 另外,您应该将变量重命名为其他名称,因为您有2个名为相同的变量。
编辑:如您所问,这是一个简单的示例。
1类:
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();
}
}
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());
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.