Java getter方法不显示更改的值

问题描述 投票:-1回答:2

员工必须完成在findRating函数的certificates数组中给出的一组预定义证书。如果员工提供的证书与预定义的证书不匹配,则评分不会更改。每场比赛将使评分提高1。

此问题要求根据雇员的工作证明更改雇员的等级。我编写的代码正在更改值,但是当由getter方法getRating()调用时,它显示为0。这是主类

import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);

    System.out.println("Enter the employee id:");
     String id = sc.next();
     System.out.println("Enter the salary:");
     double sal = sc.nextDouble();
     System.out.println("Enter the no of certification complete:");
    int numCer = sc.nextInt();
    System.out.println("Enter the certification names:");
    String [] certi = new String[numCer];

    for(int i = 0;i<numCer;i++){
        certi[i] = sc.next();
    }

    Employee emp = new Employee(id,sal,certi);
    System.out.println("Your rating is "+emp.getRating());
    //emp.findRating();
    System.out.println("Increment amount is "+emp.calculateIncrement());
    System.out.println("Current Salary "+emp.getSalary());
}
}

这是员工类别

import java.util.Arrays;
 public class Employee{
     private String employeeId;
     private double salary;
     private String [] certification;
     private int rating;

     public Employee(String empid, double sal, String[] certi){
         employeeId = empid;
        salary = sal;
        certification = Arrays.copyOf(certi,certi.length);
        this.rating = 0;

    }



    public void findRating(){
        String allCerti [] = {"JAVA","ORACLE","GCUX","CCNA","AWS"};
        int len = this.certification.length;
        // int count = 0;
        for(int i = 0;i<len;i++){
            String target = this.certification[i];
            for(String s: allCerti){
                if(s.equals(target)){
                    this.rating++;
                }
            }
        }

    }

    public double calculateIncrement(){
        switch(this.rating){
        case 1: return (this.salary * 2)/100;
        case 2: return (this.salary * 3.5)/100;
        case 3: return (this.salary * 5)/100;
        case 4: return (this.salary * 7.5)/100;
        case 5: return (this.salary * 10)/100;
        default: return 0; 
    }
    }

    public int getRating(){
       return rating;
    }

    public double getSalary(){
     double inc = calculateIncrement();
     return (salary + inc);
   }
   }
java
2个回答
0
投票

您需要调用findRating()方法来计算等级,尚未计算等级。

[您可以做的是在0的开头将setRating设置为findRating(),然后计算等级并封装代码,从getRating()内部而不是外部调用findRating(),如下所示。 :


    public void findRating(){
        this.rating = 0 ;
        String allCerti [] = {"JAVA","ORACLE","GCUX","CCNA","AWS"};
        int len = this.certification.length;
        // int count = 0;
        for(int i = 0;i<len;i++){
            String target = this.certification[i];
            for(String s: allCerti){
                if(s.equals(target)){
                    this.rating++;
                }
            }
        }

    }

public int getRating(){
        findRating();
        return rating;
    }

0
投票

您的代码很好,只需为getRating()以及[calculateIncrenment,:]调用findRating()方法>

 public int getRating(){
    findRating();
    return rating;
}



   public double calculateIncrement(){
    switch(getRating()){    // call method getRating instead directrly 
                                //accessing it 
    case 1: return (this.salary * 2)/100;
    case 2: return (this.salary * 3.5)/100;
    case 3: return (this.salary * 5)/100;
    case 4: return (this.salary * 7.5)/100;
    case 5: return (this.salary * 10)/100;
    default: return 0; 
}
}

   public void findRating(){
               this.rating =0 ; // set to zero before calculating rating each time 

样品运行

Enter the employee id:
12
Enter the salary:
20000
Enter the no of certification complete:
4
Enter the certification names:
JAVA
PYTHON
ORACLE
SQL
Your rating is 2
Increment amount is 1500.0
Current Salary 21500.0
    
© www.soinside.com 2019 - 2024. All rights reserved.