北京每月交通费用计算

         因为搬家,前两个月上班挺远的,每天坐完地铁又坐公交,折腾的够呛,花的交通费还多,搬完家不用坐公交了,就算下搬家前和搬家后每月支出交通费多少钱,写了这个小玩意,没有啥算法,没有啥规范,随便写的,记录一下。以后有时间用图形界面实现一下。

public class BJTransportation {
    private int subway; // 月初单次坐地铁花费的金额
    private int bus; // 每次坐公交花费的金额
    private int counts; // 乘坐次数
    private float cost = 0; // 花费的金额

    /**
     * 
     * Creates a new instance of fsd.
     * 
     * @param days
     *            多少个工作日
     * @param subway
     *            月初单次坐地铁花费的金额
     * @param bus
     *            每次坐公交花费的金额
     */
    public BJTransportation(int days, int subway, int bus) {
        this.counts = days * 2;
        this.subway = subway;
        this.bus = bus;
    }

    public float countSubway() {
        int c = counts;
        while (c > 0) {
            if (cost >= 150) {
                cost += subway * 0.5;
            } else if (cost >= 100) {
                cost += subway * 0.8;
            } else {
                cost += subway;
            }
            c--;
        }
        return cost;
    }

    public float countbus() {
        return cost += counts * bus;
    }

    public static void main(String[] args) {
        BJTransportation bj = new BJTransportation(23, 6, 0);
        bj.countSubway();
        bj.countbus();
        System.out.println(bj.cost);
    }

}

输出结果为:245.00003

posted @ 2018-07-02 13:44  墨梅一点清  阅读(329)  评论(0)    收藏  举报