2017-10-04 94 views
-2

为什么工资里面的价值是不是被从工资法解析,以委员会的方法?从方法传递价值的方法

package salesrepresentativeapp; 

import java.util.Scanner; 

/** 
* 
* @author Kirin 
*/ 
public class SalesRepresentativeAPP { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     Scanner sc = new Scanner(System.in); 
     String ID; 
     String name; 
     int yearServiced; 
     double salesVolume; 

     //System.out.println("Please enter your ID : "); 
     // ID = sc.nextLine(); 
     // System.out.println("Please enter your Name : "); 
     // name = sc.nextLine(); 
     System.out.println("Please enter your Year of Service : "); 
     yearServiced = sc.nextInt(); 
     System.out.println("Please enter your sales volume : "); 
     salesVolume = sc.nextDouble(); 

     SalesRepresentative s1 = new SalesRepresentative("id", "name", yearServiced, salesVolume); 

     //System.out.println("ID : " + s1.getID() + "\nName : " + s1.getName()); 
     System.out.println("Your total salary is : " + s1.Commision() + "\nBasic Salary is : " + s1.Salary()); 

    } 

} 

package salesrepresentativeapp; 

/** 
* 
* @author Kiin 
* v1.0 
*/ 
public class SalesRepresentative { 

    private String ID; 
    private String name; 
    private int yearServiced,salary; 
    private double salesVolume,commision; 


    public SalesRepresentative(String ID, String name, int yearServiced, double salesVolume){ 
     this.ID = ID; 
     this.name = name; 
     this.yearServiced = yearServiced; 
     this.salesVolume = salesVolume; 
    } 

    public int Salary(){ 
     if (yearServiced >=1 && yearServiced <= 5) { 
      salary = 1200; 
     } 
     else if (yearServiced >=6 && yearServiced <= 10) { 
      salary = 1800; 
     } 
     else if (yearServiced > 10) { 
      salary = 2300; 
     } 
     return salary; 
    } 

    public double Commision(){ 
     if (salesVolume >= 1 && salesVolume <= 99.99) { 
      commision = salary + (salary * 0.05); 
     } 
     else if (salesVolume >= 100.00 && salesVolume <= 299.99) { 
      commision = salary + (salary * 0.10); 
     } 
     else if (salesVolume >= 300.00) { 
      commision = salary + (salary * 0.15); 
     } 
     return commision; 
    } 

    public String getID(){ 
     return ID; 
    } 

    public String getName(){ 
     return name; 
    } 
} 

当我没有把工资方程的方法效果很好英寸但是,当我把工资方程式里面的方法的返回值0.0

回答

0

你先调用佣金方法,值编辑打电话Salaray后。 改为改变你的System.out。

System.out.println("Basic Salary is : " + s1.Salary() + "\nYour total salary is : " + s1.Commision()); 

除了该方法名称应该以小写字母开头。

编辑:我希望我理解正确你的问题。在你的代码

0

寻找有相当您应该改善看编辑的副本一些改变,我会解释

public class SalesRepresentative { 

    private String ID; 
    private String name; 
    private int yearServiced,salary; 
    private double salesVolume,commision; 


    public SalesRepresentative(String ID, String name, int yearServiced, double salesVolume){ 
     this.ID = ID; 
     this.name = name; 
     this.yearServiced = yearServiced; 
     this.salesVolume = salesVolume; 

     salary(); 
     commision(); 
    } 

    private void salary(){ 
     if (yearServiced >=1 && yearServiced <= 5) { 
      salary = 1200; 
     } 
     else if (yearServiced >=6 && yearServiced <= 10) { 
      salary = 1800; 
     } 
     else if (yearServiced > 10) { 
      salary = 2300; 
     } 
    } 

    private void commision(){ 
     if (salesVolume >= 1 && salesVolume <= 99.99) { 
      commision = salary + (salary * 0.05); 
     } 
     else if (salesVolume >= 100.00 && salesVolume <= 299.99) { 
      commision = salary + (salary * 0.10); 
     } 
     else if (salesVolume >= 300.00) { 
      commision = salary + (salary * 0.15); 
     } 
    } 

    public double getCommision() 
    { 
     return commision; 
    } 

    public int getSalary() 
    { 
     return salary; 
    } 

    public String getID(){ 
     return ID; 
    } 

    public String getName(){ 
     return name; 
    } 
} 

你的方法名称应该按照惯例是骆驼。我已将它们的范围从public更改为private(意味着只能在此类中调用它),并在构造函数中调用它们。您调用这些方法的顺序非常重要,因为在计算工资之前无法计算佣金。

而且我已经改变了他们的返回类型void由于是在类中更新值的方法。我已经创建了两个getter方法getSalary() & getCommission() - 这些都将通过您的应用程序返回的值来调用。请参见下面的实现:

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

     Scanner sc = new Scanner(System.in); 
     String ID; 
     String name; 
     int yearServiced; 
     double salesVolume; 

     System.out.println("Please enter your Year of Service : "); 
     yearServiced = sc.nextInt(); 
     System.out.println("Please enter your sales volume : "); 
     salesVolume = sc.nextDouble(); 

     SalesRepresentative s1 = new SalesRepresentative("id", "name", yearServiced, salesVolume); 

     System.out.println("Your total salary is : " + s1.getCommision() + "\nBasic Salary is : " + s1.getSalary()); 

    } 

}