1. 程式人生 > >學以致用——Java原始碼——銷售員業績統計小程式(Total Sales)

學以致用——Java原始碼——銷售員業績統計小程式(Total Sales)

程式功能:

依次輸入銷售單資料(銷售員編號、銷售產品編號、銷售金額),程式進行相應的統計。

執行結果示例:

銷售額月度彙總小程式:

 

請輸入0開始輸入銷售單資料(輸入-1退出):0

請輸入銷售員編號(銷售員編號範圍:1 - 4):1

請輸入產品編號(產品編號範圍:1 - 5):1

請輸入銷售額:200

該銷售單錄入成功!

 

請輸入0開始輸入銷售單資料(輸入-1退出):0

請輸入銷售員編號(銷售員編號範圍:1 - 4):1

請輸入產品編號(產品編號範圍:1 - 5):2

請輸入銷售額:300

該銷售單錄入成功!

 

請輸入0開始輸入銷售單資料(輸入

-1退出):0

請輸入銷售員編號(銷售員編號範圍:1 - 4):1

請輸入產品編號(產品編號範圍:1 - 5):1

請輸入銷售額:400.19

該銷售單錄入成功!

 

請輸入0開始輸入銷售單資料(輸入-1退出):0

請輸入銷售員編號(銷售員編號範圍:1 - 4):2

請輸入產品編號(產品編號範圍:1 - 5):1

請輸入銷售額:18.18

該銷售單錄入成功!

 

請輸入0開始輸入銷售單資料(輸入-1退出):0

請輸入銷售員編號(銷售員編號範圍:1 - 4):2

請輸入產品編號(產品編號範圍:1 - 5):3

請輸入銷售額:19.18

該銷售單錄入成功!

 

請輸入0開始輸入銷售單資料(輸入

-1退出):0

請輸入銷售員編號(銷售員編號範圍:1 - 4):3

請輸入產品編號(產品編號範圍:1 - 5):4

請輸入銷售額:700

該銷售單錄入成功!

 

請輸入0開始輸入銷售單資料(輸入-1退出):0

請輸入銷售員編號(銷售員編號範圍:1 - 4):4

請輸入產品編號(產品編號範圍:1 - 5):5

請輸入銷售額:900

該銷售單錄入成功!

 

請輸入0開始輸入銷售單資料(輸入-1退出):0

請輸入銷售員編號(銷售員編號範圍:1 - 4):4

請輸入產品編號(產品編號範圍:1 - 5):5

請輸入銷售額:1000

該銷售單錄入成功!

 

請輸入0開始輸入銷售單資料(輸入-1退出):

0

請輸入銷售員編號(銷售員編號範圍:1 - 4):4

請輸入產品編號(產品編號範圍:1 - 5):3

請輸入銷售額:1000

該銷售單錄入成功!

 

請輸入0開始輸入銷售單資料(輸入-1退出):-1

已退出程式銷售單彙總資訊:

產品編號            銷售員1             銷售員2             銷售員3             銷售員4             產品合計

產品1               600.19             18.18              0.00               0.00               618.37            

產品2               300.00             0.00               0.00               0.00               300.00            

產品3               0.00               19.18              0.00               1000.00            1019.18           

產品4               0.00               0.00               700.00             0.00               700.00            

產品5               0.00               0.00               0.00               1900.00            1900.00           

銷售員合記 900.19             37.36              700.00             2900.00           

程式碼如下:

import java.util.Scanner;

//JHTP Exercise 7.20: Total Sales
//by [email protected]
/**
 * 7.20 (Total Sales) Use a two-dimensional array to solve the following
 * problem: A company has four salespeople (1 to 4) who sell five different
 * products (1 to 5). Once a day, each salesperson passes in a slip for each
 * type of product sold. Each slip contains the following: 
 * a) The salesperson number 
 * b) The product number 
 * c) The total dollar value of that product sold that day 
 * Thus, each salesperson passes in between 0 and 5 sales slips per
 * day. Assume that the information from all the slips for last month is
 * available. Write an application that will read all this information for last
 * month’s sales and summarize the total sales by salesperson and by product.
 * All totals should be stored in the two-dimensional array sales. After
 * processing all the information for last month, display the results in tabular
 * format, with each column representing a salesperson and each row representing
 * a particular product. Cross-total each row to get the total sales of each
 * product for last month. Cross-total each column to get the total sales by
 * salesperson for last month. Your output should include these cross-totals to
 * the right of the totaled rows and to the bottom of the totaled columns.
 */


public class TotalSales {

	 static final int PRODUCTS = 5; //產品數
	 static final int SALESPEOPLE = 4; //銷售員數
	 private static double salesAmountSlip= 0.0;  //單張銷售單金額
	 private static int salesPersonNum= 1;  //銷售員編號
	 private static int productNum= 1;  //銷售員編號
	 private static double[][] salesAmount=  new double[PRODUCTS][SALESPEOPLE];  //彙總銷售額(按產品和銷售員分類)
	 private static Scanner input=new Scanner(System.in);
	 private static int exitFlag = 0;
	 private static double[] salesAmountByProduct = new double [PRODUCTS]; //彙總銷售額(按產品分類)
	 private static double[] salesAmountBySalesPerson = new double [SALESPEOPLE]; //彙總銷售額(按銷售員分類)


public static void main(String[] args)
{

   System.out.println("銷售額月度彙總小程式:");

		
		do{
			System.out.printf("%n請輸入0開始輸入銷售單資料(輸入-1退出):");
			exitFlag =input.nextInt();
			if(exitFlag ==-1){
				System.out.printf("已退出程式");
				break;
			}
			
			if (exitFlag == 0)
			inputSlip();			
			} while (exitFlag != -1);
		
		input.close(); //結束銷售單錄入
		
		//彙總銷售額(按產品分類)
		for (int i=0; i<PRODUCTS;i++) {
			for (int j=0; j<SALESPEOPLE; j++)
			salesAmountByProduct[i] += salesAmount[i][j];
		}
		
		//彙總銷售額(按銷售員分類)
		for (int i=0; i<SALESPEOPLE;i++) {
			for (int j=0; j<PRODUCTS; j++)
			salesAmountBySalesPerson[i] += salesAmount[j][i];
		}
		
		System.out.println("銷售單彙總資訊:");
		System.out.printf("產品編號\t\t");
		for (int i=0; i<SALESPEOPLE; i++)
			System.out.printf("銷售員%d\t\t",i+1);
		System.out.printf("產品合計%n");
		
		for (int i=0; i<PRODUCTS;i++) {
			System.out.printf("產品%d\t\t", i+1);
			for (int j=0; j<SALESPEOPLE;j++)
				System.out.printf("%.2f\t\t", salesAmount[i][j]);
			System.out.printf("%.2f\t\t%n", salesAmountByProduct[i]);
		}
			
		System.out.printf("銷售員合記\t");
		for (int i=0; i<SALESPEOPLE;i++) {
			System.out.printf("%.2f\t\t", salesAmountBySalesPerson[i]);
		}
}
  
public static void inputSlip() {
	//輸入銷售員編號
	System.out.printf("請輸入銷售員編號(銷售員編號範圍:1 - %d):", SALESPEOPLE);
	salesPersonNum =input.nextInt();
	while(salesPersonNum <1 || salesPersonNum > SALESPEOPLE) {
		System.out.printf("請輸入銷售員編號(銷售員編號範圍:1 - %d):", SALESPEOPLE);
	salesPersonNum =input.nextInt();
	}
	
	//輸入產品編號
	System.out.printf("請輸入產品編號(產品編號範圍:1 - %d):", PRODUCTS);
	productNum =input.nextInt();
	while(productNum <1 || productNum > PRODUCTS) {
		System.out.printf("請輸入有效的產品編號(產品編號範圍:1 - %d):", PRODUCTS);
		productNum =input.nextInt();
	}
	
	//輸入銷售額
	System.out.printf("請輸入銷售額:");
	salesAmountSlip =input.nextDouble();
	while(salesAmountSlip <0.0) {
		System.out.printf("請輸入有效的金額:");
		salesAmountSlip =input.nextInt();
	}
	
	System.out.println("該銷售單錄入成功!");
	salesAmount[productNum-1][salesPersonNum-1]+=salesAmountSlip;
	

}