UT源码 141
设计佣金问题的程序
commission方法是用来计算销售佣金的需求,手机配件的销售商,手机配件有耳机(headphone)、手机壳(Mobile phone shell)、手机贴膜(Cellphone screen protector)三个部件,每个部件单价为:耳机80元,手机壳10元,手机贴膜8元,每月月末向制造商报告销量,制造商根据销量给销售商佣金。如果销售额不足1000元按10%提取佣金,1000-1800元部分按15%提取佣金,超过1800元部分按20%提取佣金。
程序要求:
1)先显示“请分别输入三种手机配件的销售情况:”
2)不满足条件,返回:“输入数量不满足要求”,返回重新输入;
3)条件均满足, 则返回佣金额。返回等待输入。
package demo; import java.util.Scanner; public class Commission { private static final double xv = 80.0; private static final double yv = 10.0; private static final double zv = 8.0; // 计算佣金 public static double commission(int xn, int yn, int zn){ double res = 0.0; double cnt = xn * xv + yn * yv + zn * zv; if(cnt < 1000){ res = cnt * 0.1; // 小于 1000 }else{ res = 100; if(cnt < 1800){ res = res + (cnt - 1000) * 0.15; // 大于 1000 小于 1800 }else{ res = res + 120 + (cnt - 1800) * 0.2; // 大于 1800 } } return res; } // 字符串转整数 private static int String2Integer(String s){ int res = 0; s = s.trim(); String [] sArray = s.split("\\s+"); if(1 != sArray.length){ // 输入不止一个字符串 res = -1; }else{ try{ res = Integer.parseInt(sArray[0]); if(res < 0){ res = -1; } }catch(Exception e){ res = -1; // 输入的字符串含有非法字符 } } return res; } public static void main(String args[]){ String flag = "是"; Scanner scanner = new Scanner(System.in); java.text.DecimalFormat format = new java.text.DecimalFormat("#.00"); // 格式化输出 while("是".equals(flag)){ int xn = -1, yn = -1, zn = -1; System.out.println("请分别输入三种手机配件的销售情况:"); System.out.print("请输入耳机销售情况:"); do{ xn = String2Integer(scanner.next()); if(-1 == xn){ System.out.print("输入数量不满足要求,请重新输入:"); } }while(-1 == xn); System.out.print("请输入手机壳销售情况:"); do{ yn = String2Integer(scanner.next()); if(-1 == yn){ System.out.print("输入数量不满足要求,请重新输入:"); } }while(-1 == yn); System.out.print("请输入手机贴膜销售情况:"); do{ zn = String2Integer(scanner.next()); if(-1 == zn){ System.out.print("输入数量不满足要求,请重新输入:"); } }while(-1 == zn); double res = commission(xn, yn, zn); System.out.println("获得的销售佣金为:" + format.format(res) + " 元."); System.out.print("\n是否继续使用《是 或 否》: "); boolean error = false; do{ error = false; flag = scanner.next(); if(!("是".equals(flag))&&!("否".equals(flag))){ error = true; System.out.print("输入有误,请输入《是 或 否》:"); } }while(error); System.out.println(""); } System.out.println("感谢您的使用!"); scanner.close(); } }