Java getter 方法不显示更改的值

  • 本文关键字:显示 getter 方法 Java java
  • 更新时间 :
  • 英文 :


员工必须完成一组预定义的证书,这些证书在findRating函数的证书数组中给出。如果员工完成的证书与预定义的证书不匹配,则评级不会更改。每场比赛的评分都会增加一个。

这个问题要求根据员工所做的认证来更改员工的评级。我编写的代码正在更改值,但是当由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);
}
}

你的代码很好,只需调用 getRating(( 的方法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

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

你可以做的是将 Rating 设置为0in. in.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;
}

最新更新