2017-10-04 73 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()); 

    } 

}