学以致用——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 pandenghuang@163.com
/**(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

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值