java,编写一个汽车类

package org.hanqi.pn0120;

//汽车
public class Car {
    // 颜色
    private String yanse;
    // 品牌
    private String pinpai;
    // 油箱容量
    private double youxiangrl = 40;
    // 当前油量
    private double dangqianyl = 0;

    public double getdangqianyl() {
        return this.dangqianyl;
    }

    // 加油
    public void jiaYou(double youliang) {
        if (youliang < 0) {
            System.out.println("不要偷我的油!");
        } else if (this.dangqianyl + youliang > this.youxiangrl) {
            // 本次加油量
            double bcyl = this.youxiangrl - this.dangqianyl;
            this.dangqianyl = this.youxiangrl;
            System.out.println("油太多了,已经加满了,这次加了" + (bcyl) + "");
        } else {
            this.dangqianyl += youliang;
            System.out.println("这次加了" + youliang + "升油");
        }

    }

    // private double youhao=
    private double xingshizlc = 0;

    public double getXingshizlc() {
        return this.xingshizlc;
    }

    // 行驶
    public void xingshi(double xingshilc, double youhao) {
        if (this.dangqianyl <= 0) {
            System.out.println("车没有油,怎么开车");
            return;
        }
        if (xingshilc < 0 || youhao < 0) {
            System.out.println("不可能这么开车");
        } else {

        }
        // 本次油耗
        double dangqyh = xingshilc * youhao / 100;
        this.dangqianyl -= dangqyh;
        // this.dangqianyl = this.dangqianyl > 0 ? this.dangqianyl : 0;
        if (this.dangqianyl < 0) {
            this.dangqianyl = 0;
            System.out.println("你是推着车走么");
        }
        this.xingshizlc += xingshilc;
        System.out.println("本次行驶了" + xingshilc + "公里");
    }

    public String getYanse() {
        return yanse;
    }

    public void setYanse(String yanse) {
        this.yanse = yanse;
    }

    public String getPinpai() {
        return pinpai;
    }

    public void setPinpai(String pinpai) {
        this.pinpai = pinpai;
    }

    public double getYouxiangrl() {
        return youxiangrl;
    }

    public void setYouxiangrl(double youxiangrl) {
        this.youxiangrl = youxiangrl;
    }

    public Car(String pinpai, double youxiangrl) {
        super();
        this.pinpai = pinpai;
        this.youxiangrl = youxiangrl;
    }

    // main方法
    public static void main(String[] args) {
        Car myBenchi = new Car("奔驰", 50);
        System.out.println("品牌=" + myBenchi.getPinpai());
        System.out.println("油箱容量=" + myBenchi.getYouxiangrl());
        myBenchi.xingshi(200, 13);
        myBenchi.jiaYou(30);
        myBenchi.jiaYou(30);
        System.out.println("当前容量=" + myBenchi.getdangqianyl());
        myBenchi.xingshi(300, 15);
        System.out.println("当前容量=" + myBenchi.getdangqianyl());
        myBenchi.xingshi(200, 13);
        System.out.println("行驶总里程=" + myBenchi.getXingshizlc());
        System.out.println("当前容量=" + myBenchi.getdangqianyl());

    }

}

posted @ 2016-03-05 16:24  什么玩楞啊,我叫旺仔  阅读(2298)  评论(0编辑  收藏  举报