1. 程式人生 > >學以致用——Java原始碼——銷售員工資(固定工資+佣金)統計程式(Sales Commissions)

學以致用——Java原始碼——銷售員工資(固定工資+佣金)統計程式(Sales Commissions)

首先,此程式基於2年半前本人寫的程式改寫而成。這次,採用了新方法:使用陣列元素作為計數器(Using the Elements of an Array as Counters),而不是使用很多if...else 判斷。因此,程式碼更清晰、簡潔。而且,之前的方法有點hard-coding的感覺,當分檔數很多時,就有大麻煩了。而使用此方法,分檔標準及分檔數的修改非常靈活。

之前版本的程式碼參考:

https://blog.csdn.net/hpdlzu80100/article/details/51850208

程式碼如下:

package exercises.ch7Arrays;

//Java How To Program, Exercise 7.10: Sales Commissions
//by 
[email protected]
/**(Sales Commissions) Use a one-dimensional array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses $5,000 in sales in a week receives $200 plus 9% of $5,000, or a total of $650. Write an application (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount): a) $200–299 b) $300–399 c) $400–499 d) $500–599 e) $600–699 f) $700–799 g) $800–899 h) $900–999 i) $1,000 and over Summarize the results in tabular format.*/ import java.util.Scanner; public class SalesCommission2 { public static int calSalary(double grossSales){ int salary=0; salary=200+(int)(grossSales*0.09); return salary; } public static void main(String[] args) { double grossSales=0.0; //銷售員當月銷售總額(業績) double totalGrossSales=0.0; //所有銷售員當月銷售總額(業績)的合計 int salary=0; //銷售員當月的佣金 int totalSalary=0; //所有銷售員當月佣金的合計 int count=0; //錄入的銷售員的個數 //根據需求,共需要劃分200, 300至1000共9檔,使用陣列下標2-10方便統計頻率(下標0,1不用)故建立長度為11的陣列 int[] frequency=new int[11]; Scanner input=new Scanner(System.in); do { System.out.print("請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出):"); grossSales=input.nextDouble(); if(grossSales==-1) System.out.print("錄入已結束。\n"); else { salary=calSalary(grossSales); ++frequency[Math.min(salary/100,10)]; //佣金超過1000後,計入1000檔,即frequency[10] ++count; totalSalary+=salary; totalGrossSales+=grossSales; } } while (grossSales!=-1); System.out.printf("\n共錄入了%d位銷售員的業績:\n總銷售額為:%.2f美元,工資總額為:%d美元\n",count,totalGrossSales,totalSalary); System.out.printf("工資分佈如下表所示:\n"); System.out.printf("工資範圍\t\t人數\n"); for (int i=2;i<11;i++){ if (i<10) //2-9檔共8檔 System.out.print("$"+100*i+"-"+(100*(i+1)-1)+"\t"+frequency[i]+"\n"); if (i==10) //第9檔 System.out.print("$"+100*i+"以上"+"\t"+frequency[i]+"\n"); } input.close(); } }

執行結果:

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)200

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)500

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)1000

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)2000

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)3000

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)4000

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)3589

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)4568

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)5879

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)8762

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)8798

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)10000

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)200

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)50000

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)100000

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)100000000

請輸入下一位銷售員當月的銷售總額(整數,輸入-1退出)-1

錄入已結束。

 

共錄入了16位銷售員的業績:

總銷售額為:100204296.00美元,工資總額為:9021585美元

工資分佈如下表所示:

工資範圍                              人數

$200-299     3

$300-399     2

$400-499     1

$500-599     2

$600-699     1

$700-799     1

$800-899     0

$900-999     2

$1000以上   4