java - 如何将 ArrayList 中的对象实例与通过 Scanner 的用户输入进行比较
问题描述
我正在尝试自己学习arraylist的用法,但遇到了一个难题:
我有 3 个类,一个有 2 个子类的父抽象类,这些类的哪些实例存储在主程序的数组列表中,我的目标基本上是“提高”员工实例的薪水(送货员或商业) ,通过从控制台检索用户输入并将该输入与我的数组列表具有的所有实例的相应属性进行比较,例如:
我想增加 300 美元或给一个送货员加分
用户输入名称“george”>(我需要搜索名称为 george 的实例)并在该实例上使用 isPlus 和 setPlus 方法。(所有类对于所有属性和 tostrings 都有相应的 getter 和 setter)。
我不知道如何去做或实施,因为我是使用数组列表的新手,所以没有我已经尝试过的背景
班级员工
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;
}
}
商业类
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;
}
}
类送货员
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;
}
}
课程计划
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.");
}
}
}
}
解决方案
遍历数组列表。
通过在 equals 方法的帮助下比较名称来从 arraylist 中获取对象。
更新对象。在arraylist中插入更新的对象
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;
}
}
推荐阅读
- c++ - 使用 std::chrono 的两个日期之间还剩多少天
- json - 使用 jq(bash 脚本)更新 JSON 文件
- nestjs - Nestjs - 在 createParamDecorator 中使用 applyDecorators
- python - 从服务器到服务器的发布请求不正确
- python - Django 表单动作属性
- reactjs - 在 react 组件中导入 Antd css
- java - 从哪里进行 Google Play 控制台 ID 验证?
- bash - 在测试中出现错误成功退出
- aws-sam - 使用 AWS SAM 定义由容器映像支持的 Lambda 函数资源时,如何在我的 AWS 账户中使用现有 ECR 映像
- javascript - Heroku 应用程序不起作用(部署问题?)