设计佣金

commission方法是用来计算销售佣金的需求,手机配件的销售商,手机配件有耳机(headphone)、手机壳(Mobile phone shell)、手机贴膜(Cellphone screen protector)三个部件,每个部件单价为:耳机80元,手机壳10元,手机贴膜8元,每月月末向制造商报告销量,制造商根据销量给销售商佣金。如果销售额不足1000元按10%提取佣金,1000-1800元部分按15%提取佣金,超过1800元部分按20%提取佣金。

 程序要求:

1)先显示“请分别输入三种手机配件的销售情况:”

2)不满足条件,返回:“输入数量不满足要求”,返回重新输入;

3)条件均满足, 则返回佣金额。返回等待输入。

    float  commission (int headphone, int shell, int protector)

 

// ETestOne by:mtLin 2017.3.10
import java.util.Scanner;

public class One {
    /*
     * hp:耳机 80元 mpc:手机壳 10元 cpsp:手机贴膜 8元
     */

    public static void main(String[] args) {
        String line;
        int hp = 0, mpc = 0, cpsp = 0;
        String[] input = null;
        float money = 0;
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请分别输入三种手机配件的销售情况:");
            line = scanner.nextLine();
            input = line.split(" ");
            if (Judge(input)) {
                // 判断是否不小于0
                if ((hp = Integer.parseInt(input[0])) < 0) {
                    System.out.println("输入数量不满足要求");
                    continue;
                }
                if ((mpc = Integer.parseInt(input[1])) < 0) {
                    System.out.println("输入数量不满足要求");
                    continue;
                }
                if ((cpsp = Integer.parseInt(input[2])) < 0) {
                    System.out.println("输入数量不满足要求");
                    continue;
                }
            } else {
                System.out.println("输入数量不满足要求");
                continue;
            }
            money = commission(hp, mpc, cpsp);
            System.out.println("佣金金额:" + money);
        }

    }

    // 计算佣金
    private static float commission(int hp, int mpc, int cpsp) {
        float commission = 0;
        int total = hp * 80 + mpc * 10 + cpsp * 8;
        if (total < 1000) {
            commission = (float) (total * 0.1);
        } else if (total <= 1800) {
            commission = (float) (1000 * 0.1 + (total - 1000) * 0.15);
        } else {
            commission = (float) (1000 * 0.1 + 800 * 0.15 + (total - 1800) * 0.2);
        }
        return commission;
    }

    // 判断用户输入的是不是三个整数
    private static boolean Judge(String[] input) {
        String number = "0123456789";
        // 判断输入的是不是三个字符串
        if (input.length != 3) {
            return false;
        }
        // 判断三个字符串是不是纯数字且不含小数点
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < input[i].length(); j++) {
                if ("+-".indexOf(input[i].charAt(0)) == 1) {
                    continue;
                }
                if (number.indexOf(input[i].charAt(j)) == -1) {
                    return false;
                }
            }
        }
        return true;
    }
}

 

posted on 2017-03-10 16:16  VioletGlass  阅读(185)  评论(1编辑  收藏  举报