繁体   English   中英

如何将 ArrayList 内的 object 实例与通过扫描仪的用户输入进行比较

[英]How to compare an instance of an object inside an ArrayList to user input via Scanner

我正在尝试自己学习 arraylist 的用法,但遇到了一个难题:

我有 3 个类,一个父亲抽象 class 有 2 个孩子,这些类的哪些实例存储在主程序上的 arraylist 中,我的目标是基本上“提高”员工实例的薪水(送货员或商业) ,通过从控制台检索用户输入并将该输入与我的 arraylist 具有的所有实例的相应属性进行比较,例如:

我想增加 300 美元或给一个送货员加分

用户输入名称“george”>(我需要搜索名称为 george 的实例)并在该实例上使用 isPlus 和 setPlus 方法。 (所有类对于所有属性和 tostrings 都有相应的 getter 和 setter)。

我不知道该怎么做或实施,因为我是 arraylist 用法的新手,所以没有我已经尝试过的背景

Class 员工

public abstract class Employee {
   private static final int PLUS = 300;
   private static final int EMPLOYEEQTY = 3;

   protected String name;
   protected int age;
   protected double salary;

   public Employee(String name, int age, double salary) {
      this.name = name;
      this.age = age;
      this.salary = salary;
   }
}

Class 商用

public class Commercial extends Employee{

   private double comission;

   public Commercial(String name, int age, double salary, double comission) {
      super(name, age, salary);
      this.comission = comission;
   }

   @Override
   public boolean isPlus() {
      if (this.comission > 200 && this.age > 30) {
         return true;
      }else {
         return false;
      }
   }

   @Override
   public double setPlus() {
      if (isPlus() == true) {
         return this.salary+getPlus();
      }
      return salary;
   }
}

Class 送货员

public class DeliveryMan extends Employee{
   private String zone;

   public DeliveryMan(String name, int age, double salary, String zone) {
      super(name, age, salary);
      this.zone = zone;
   }

   @Override
   public boolean isPlus() {
      if (this.zone.equalsIgnoreCase("zona 3") && this.age > 25) {
         return true;
      }
      return false;
   }

   @Override
   public double setPlus() {
      if (isPlus() ==  true) {
         return salary+getPlus();
      }
      return salary;
   }
}

Class 程序

public class Program {
   public static void main(String[] args) {

      String name,zone,opt;
      Integer age;
      Double salary,comission;
      ArrayList<Employee> al = new ArrayList<>();
      Scanner sc = new Scanner(System.in);

      for (int i = 0; i < Employee.getEmployeeqty(); i++) {

         System.out.println("Ingrese el nombre del empleado:  ");
         name = sc.next();
         System.out.println("Ingrese la edad del empleado: ");
         age = sc.nextInt();
         System.out.println("Ingrese el salario del empleado: ");
         salary = sc.nextDouble();
         System.out.println("Es el empleado un Repartidor o un Comercial?");
         opt = sc.nextLine();

         switch (opt.toLowerCase()) {
            case "repartidor":
            System.out.println("Ingrese la zona del repartidor: (ej: zona 3)");
            zone = sc.nextLine().toLowerCase();
            al.add(new DeliveryMan(name, age, salary, zone));
            break;
            case "comercial":
            System.out.println("Ingrese la cantidad de la comision del comercial: ");
            comission = sc.nextDouble();
            al.add(new Commercial(name, age, salary, comission));
            break;
            default: System.out.println("Opcion ingresada no es valida, por favor, indique si es un repartidor o un comercial.");
         }
      }
   }
}

遍历 arraylist。

通过使用 equals 方法比较名称,从 arraylist 中获取 object。

更新 object。 在 arraylist 中插入更新的 object

ArrayList<Employee> al = new ArrayList<>();

for(int i = 0 ; i < al.size() ; i++){
    if((al.get(i).name).equals(user_input_name)){
          al.salary(i) =  al.salary+increment_salary;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM