簡體   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