峰谷电电费计算程序

2019-04-14 12:37发布

package nathan.epower; public class PowerCalc { private double top; private double down; private double topCost; private double downCost; private boolean status; public void calc(String[] args) { if (!validateArgs(args)) { status = false; return; } PowerMeter curMeter = getCurMeter(args); PowerMeter lstMeter = getLastMeter(args); down = curMeter.down - lstMeter.down; top = curMeter.total - lstMeter.total - down; topCost = top * 0.56; downCost = down * 0.28; status = true; } private boolean validateArgs(String[] args) { if (args == null || args.length != 4) { System.out .println("Useage: java PowerCalc 当前总电量度数 当前谷电量度数 上次总电量度数 上次谷电量度数"); return false; } return true; } private PowerMeter getCurMeter(String[] args) { PowerMeter m = new PowerMeter(); m.total = Double.valueOf(args[0]); m.down = Double.valueOf(args[1]); return m; } private PowerMeter getLastMeter(String[] args) { PowerMeter m = new PowerMeter(); m.total = Double.valueOf(args[2]); m.down = Double.valueOf(args[3]); return m; } public void report() { if (status) { custmorReport(); } } protected void custmorReport() { System.out.println("--------------------------------------"); System.out.println("|峰电度数:" + top + " " + "谷电度数:" + down); System.out.println("|峰电电费:" + topCost); System.out.println("|谷电电费:" + downCost); System.out.println("|总电费: " + top + "*0.56+" + down + "*0.28=" + getCost()); System.out.println("--------------------------------------"); } public double getTop() { return top; } public double getDown() { return down; } public double getCost() { return topCost + downCost; } class PowerMeter { double total; double down; } public double getTopCost() { return topCost; } public double getDownCost() { return downCost; } public static void main(String[] args) { PowerCalc pc = new PowerCalc(); pc.calc(args); pc.report(); } }
java nathan.epower.PowerCalc 12580 6284 12312 6149
-------------------------------------- |峰电度数:133.0 谷电度数:135.0 |峰电电费:74.48 |谷电电费:37.800000000000004 |总电费: 133.0*0.56+135.0*0.28=112.28 --------------------------------------