需要销售和佣金的Java程序

时间:2012-04-07 13:23:10

标签: sales

下表显示了汽车的类别代码以及销售人员可以从销售价值中获得的佣金百分比:

  
    

汽车产品代码:     佣金率(%)
    ABC-A%5
    ABC-B%10
    ABC-C%12
    ABC-D%15
    ABC-E%17

  

需要一个程序,允许用户输入汽车产品代码和销售价值。然后,该计划计算并输出该销售所赚取的佣金。

1 个答案:

答案 0 :(得分:0)

import java.util.Scanner;

public class demo {
    public static double getCommissionRate(String productCode, double sale) throws Exception {
    if(productCode.equals("ABC-A")) {
        return sale * 0.05;
    }else if(productCode.equals("ABC-B")) {
        return sale * 0.1;
    }else if(productCode.equals("ABC-C")) {
        return sale * 0.12;
    }else if(productCode.equals("ABC-D")) {
        return sale * 0.15;
    }else if(productCode.equals("ABC-E")) {
        return sale * 0.17;
    }else {
        // You shouldn't be here
        throw new Exception("Invalid Product Code");
    }
    }

    public static void main(String[] args) throws Exception {
    Scanner z = new Scanner(System.in);
    System.out.print("Enter Product Code: ");
    String productCode = z.next();
    System.out.print("Enter value of sale: ");
    // In reality you should never do currency calculations with doubles or floats
    double sale = z.nextDouble();
    System.out.println("Commission: " + getCommissionRate(productCode, sale));
    }
}