【问题标题】:Java search method between objects using scanner使用扫描仪的对象之间的Java搜索方法
【发布时间】:2015-11-27 00:14:28
【问题描述】:

我正在尝试用 java oop 编写一个程序,在该程序中我主要创建一些人(每个人都有一个名字、一个年龄、一个状态:是否受雇)。

我想按姓名搜索这些人并显示所有详细信息。

例如,如果一个人叫 John,我通过名字找到它,我想列出所有详细信息(状态、年龄等)。

我尝试在 Person 类中实现此方法。
我不知道是否更好地创建一个包含所有人员和姓名的地图,然后在其中进行搜索。

下面是我的代码:

人物类:

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: ");
   }
}

}

就业班级:

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;
}

}

失业阶级:

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;
}

}

程序类:

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







}
}

【问题讨论】:

    标签: java java.util.scanner


    【解决方案1】:

    首先你应该添加一个构造器。

    然后你可以像这样从 Person 类创建一个对象:

     Person p1 = new Person("John Doe", 47,3, "IKEA", 12.500, 12.5);
    
     String personInfo = p1.get(..) + p1.get(..);
    
     System.out.println(personInfo);
    

    您的代码有点杂乱无章。 Java 类应该有构造函数。通常,变量以小写字符开头。

    【讨论】:

    • 感谢您的建议。我会尝试用构造函数来实现。
    猜你喜欢
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    相关资源
    最近更新 更多