[英]Java search method between objects using scanner
I am trying to make a program in java oop in which I create some persons in the main (every person has a name, an age, a status: employed or not). 我试图在Java oop中创建一个程序,在其中创建一些主要人员(每个人的名字,年龄,状态:受雇与否)。
I want to search these persons by name and display all the details. 我想按名称搜索这些人并显示所有详细信息。
For example if a have a person named John and I find it by name, I want to list all the details (status, age and so on). 例如,如果某人有一个名叫John的人,而我按名字找到了它,我想列出所有详细信息(状态,年龄等)。
I tried to implement this method in Person class. 我试图在Person类中实现此方法。
I don t know if is better to create a map which contains all the persons and the name and then to search in it. 我不知道是否最好创建一个包含所有人员和姓名的地图,然后在其中进行搜索。
Below is my code: 下面是我的代码:
Person CLASS: 人类:
package app;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Person extends Employed {
Scanner scan = new Scanner(System.in);
private String name;
private int age;
private int kids;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getKids() {
return kids;
}
public void setKids(int kids) {
this.kids = kids;
}
public Person(){
System.out.println("************************************************************");
}
public void displayPerson(){
if(super.getPeriod()==0 && super.getUnemploymentBenefit()==0){
System.out.println(name + " is " + age + " years old, has " + kids + " kids and is employed" + "\nWorking Place: " + super.getWorkingPlace()
+ "\nSallary: " + df.format(super.getSallary()) + " EUR per year" + "\nWorking Time: " + super.getHours() + " hours per day");
}else
System.out.println(name + " is " + age + " years old, has " + kids + " kids and is unemployed" +"\nUnemployment Time: " + Math.round(super.getPeriod())
+ "\nUnemployment Benefit: " + df.format(super.getUnemploymentBenefit()) + " EUR per year");
}
public void searchMethod(){
System.out.println("Are you looking for someone?");
String s = scan.nextLine();
if(s==name) {
System.out.println("Here are all the details about the person you are looking for: ");
}
}
}
Employed CLASS: 就业班:
package app;
import java.text.DecimalFormat;
public class Employed extends Unemployed {
DecimalFormat df = new DecimalFormat("0.000");
private String WorkingPlace;
private double sallary;
private double hours;
public String getWorkingPlace() {
return WorkingPlace;
}
public void setWorkingPlace(String WorkingPlace) {
this.WorkingPlace = WorkingPlace;
}
public double getSallary() {
return sallary;
}
public void setSallary(double sallary) {
this.sallary = sallary;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
}
Unemployeed CLASS: 失业班级:
package app;
public class Unemployed{
private double period;
private double UnemploymentBenefit;
public double getPeriod() {
return period;
}
public void setPeriod(double period) {
this.period = period;
}
public double getUnemploymentBenefit() {
return UnemploymentBenefit;
}
public void setUnemploymentBenefit(double UnemploymentBenefit) {
this.UnemploymentBenefit = UnemploymentBenefit;
}
}
Program CLASS: 程式类别:
package app;
public class Program extends Person{
public static void main(String[] args) {
Person p1 = new Person();
p1.setName("John Doe");
p1.setAge(47);
p1.setKids(3);
p1.setWorkingPlace("IKEA");
p1.setSallary(12.500);
p1.setHours(12.5);
p1.displayPerson();
p1.searchMethod();
Person p2 = new Person();
p2.setName("Snow Tiffany");
p2.setAge(27);
p2.setKids(0);
p2.setPeriod(15.9);
p2.setUnemploymentBenefit(7.000);
p2.displayPerson();
}
}
First you should add a construtor. 首先,您应该添加一个构造器。
Then you can create an object from class Person like this: 然后,您可以从Person类创建一个对象,如下所示:
Person p1 = new Person("John Doe", 47,3, "IKEA", 12.500, 12.5);
String personInfo = p1.get(..) + p1.get(..);
System.out.println(personInfo);
Your code is a bit desorganized. 您的代码有点混乱。 Java classes should have constructors. Java类应具有构造函数。 Variables, in general, starts with lowerCase character. 通常,变量以lowerCase字符开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.