手记

答答租车系统练习代码

2017年6月21日修改:私有化各个属性并添加get和set方法。修改了输入字母报错的bug,添加了一些注释。欢迎互相交流。

package ddzc_crc;
/**
 * 汽车主类
 * @author crc
 *
 */
public abstract class Car {
    private String name;
    private int zhujin;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getZhujin() {
        return zhujin;
    }

    public void setZhujin(int zhujin) {
        this.zhujin = zhujin;
    }

    public Car(String name, int zhujin) {
        super();
        this.name = name;
        this.zhujin = zhujin;
    }

    public String toString() {
        return name + zhujin;

    }
}
package ddzc_crc;
/**
 * Car类子类载货车型
 * @author crc
 *
 */
public class Zaihuo extends Car {
    private int huowu;

    public Zaihuo(String name, int zhujin, int huowu) {
        super(name, zhujin);
        this.huowu = huowu;
    }

    public int getHuowu() {
        return huowu;
    }

    public void setHuowu(int huowu) {
        this.huowu = huowu;
    }

    public String toString() {
        return this.getName() + " " + this.getZhujin() + "/天" + " 载货:" + huowu + "吨";
    }

}
package ddzc_crc;
/**
 * 载人车型
 * @author crc
 *
 */
public class Zairen extends Car {
    private int renshu;

    public int getRenshu() {
        return renshu;
    }

    public void setRenshu(int renshu) {
        this.renshu = renshu;
    }

    public Zairen(String name, int zhujin, int renshu) {
        super(name, zhujin);
        this.renshu = renshu;
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
        return this.getName() + " " + this.getZhujin() + "/天" + " 载人:" + renshu + "人";
    }
}
package ddzc_crc;
/**
 * 子类皮卡,可以载人和载货
 * @author crc
 *
 */
public class Zairenhuo extends Car {
    private int huowu;
    private int renshu;
    public int getHuowu() {
        return huowu;
    }
    public void setHuowu(int huowu) {
        this.huowu = huowu;
    }
    public int getRenshu() {
        return renshu;
    }
    public void setRenshu(int renshu) {
        this.renshu = renshu;
    }
    public Zairenhuo(String name, int zhujin,int renshu,int huowu) {
        super(name, zhujin);
        this.huowu=huowu;
        this.renshu=renshu;
        // TODO Auto-generated constructor stub
    }
    public String toString() {
        return this.getName() + " "+this.getZhujin()+"/天" + " 载人:"+ renshu + "人,载货:"+ huowu + "吨";
    }

}
package ddzc_crc;

import java.util.Scanner;

/**
 * 答答租车系统主函数
 * 
 * @author crc
 *
 */
public class DDxt {
    public static void main(String[] args) {
        // 几种可选车型
        Car c[] = { new Zairen("奥迪A4", 500, 4), new Zairen("马自达6", 400, 4), new Zairenhuo("皮卡雪6", 450, 4, 2),
                new Zairen("金龙", 800, 20), new Zaihuo("松花江", 400, 4), new Zaihuo("依维柯", 1000, 20) };
        int sum = 0;// 总价格
        int i = -1;// 是否租车
        int j = -1;// 租车数量
        System.out.println("欢迎使用答答租车系统");
        System.out.println("您是否要租车?1是0否");
        while (true) {// 是否租车选项
            try {
                Scanner sr = new Scanner(System.in);
                i = sr.nextInt();
                if (i == 0 || i == 1) {
                    break;
                } else {
                    System.out.println("输入错误,请重新输入!");
                    continue;
                }
            } catch (Exception e) {
                System.out.println("输入错误,请重新输入!");
                continue;
            }
        }
        if (i == 0) {
            System.out.println("欢迎下次再来!");
            System.exit(0);
        }
        System.out.println("您可租的车型及价目表如下");
        for (int i1 = 1; i1 <= c.length; i1++) {// 显示可租车型
            System.out.print(i1 + ". " + c[i1 - 1]);
            System.out.println();
        }
        System.out.println("请输入您要租车的数量:");
        while (true) {
            try {
                Scanner sr = new Scanner(System.in);
                j = sr.nextInt();
                if (j < 0) {
                    System.out.println("输入有误,请重新输入!");
                    continue;
                } else if (j > 10) {
                    System.out.println("一次最多只能选择10辆,请重新输入。");
                    continue;
                } else
                    break;
            } catch (Exception e) {
                System.out.println("输入有误,请重新输入!");
                continue;
            }
        }

        Car c2[] = new Car[j];// 租车车型数据
        int ct[] = new int[j];// 租车天数数据
        for (int j1 = 0; j1 < j; j1++) {
            System.out.println("请选择第" + (j1 + 1) + "辆车的车型:");
            int jx = -1;// 输入的车号
            int jt = -1;// 输入的天数
            while (true) {
                try {
                    Scanner sr = new Scanner(System.in);
                    jx = sr.nextInt();
                    if (jx < 1 || jx > 6) {
                        System.out.println("输入错误,请重新输入。");
                        continue;
                    } else {
                        c2[j1] = c[jx - 1];
                        break;
                    } // 将选择的车计入车数量数组对应位置
                } catch (Exception e) {
                    System.out.println("输入错误,请重新输入。");
                    continue;
                }
            }
            System.out.println("请输入该车租车天数");
            while (true) {
                try {
                    Scanner sr = new Scanner(System.in);
                    jt = sr.nextInt();
                    if (jt < 1 || jt > 10) {
                        System.out.println("天数1到10天,输入错误,请重新输入。");
                        continue;
                    } else {
                        ct[j1] = jt;
                        break;
                    }
                } catch (Exception e) {
                    System.out.println("最大一次10天,输入错误,请重新输入。");
                    continue;
                }
            }
        }
        System.out.println("租车完成,情况如下:");
        for (int i2 = 0; i2 < c2.length; i2++) {// 输出租车类型、数量、天数和总价
            sum += c2[i2].getZhujin() * ct[i2];
            System.out.println("第" + (i2 + 1) + "辆:" + c2[i2].getName() + "  租" + ct[i2] + "天  " + "  费用:"
                    + (c2[i2].getZhujin() * ct[i2]) + "元。");
        }
        System.out.println("总共租金为:" + sum + "元");
    }
}
7人推荐
随时随地看视频
慕课网APP