为了账号安全,请及时绑定邮箱和手机立即绑定

嗒嗒坐车系统 一个父类car三个子类bus,truck,pickup和测试类test

标签:
Java

//父类car
package rentsystem;

public class car {
//首先定义父类的属性:车类的 名称 和 租金。
public String carname;
public double carrent;

}
//子类bus
package rentsystem;

public class Bus extends car {
//定义客车的私有载客量 ,所以要用private修饰
private int personload;
//有参的构造方法定义Bus的三个属性:名称,载客量,每天的租金;
public Bus(String carname, int personload, double carrent) {
this.carname = carname;
this.personload = personload;
this.carrent = carrent;
}
//
public int getPersonload() {
return personload;
}
//用toString方法,将输出的值转化成字符串
public String toString() {
return carname + "\t" + carrent + "元/天\t" + personload + "人\t";
}
}
//子类truck
package rentsystem;

public class Truck extends car {
//定义货车的私有载货量,用 private修饰符
private float goodload;
//有参的构造方法定义,truck的三个属性:名称,载货量,每天的租金。
public Truck(String carname,int goodload,double carrent){
this.carname=carname;
this.goodload=goodload;
this.carrent=carrent;
}
public float getgoodload(){
return goodload;
}
//用toString方法,将输出的值转换成字符串输出。
public String toString(){
return carname+"\t"+carrent+"元/天\t"+goodload+"頓\t";
}
}
//子类pickup
package rentsystem;

public class pickup extends car {
//定义皮卡的私有属性载客量和载货量
private int personload;
private float goodload;
//有参构造方法定义pickup的四个属性:名称,载客量,载货量,每天的租金;
public pickup(String carname, int personload, float goodload, double carrent) {
this.carname = carname;
this.personload = personload;
this.goodload = goodload;
this.carrent = carrent;
}

public int getpersonload() {
    return personload;
}
public float getgoodload() {
    return goodload;
}
//用toString方法将输出的值转换成字符串输出
public String toString(){
    return carname+"\t"+carrent+"元/天\t"+personload+"人\t"+goodload+"吨\t";
}

}
//测试类test
package rentsystem;

import java.util.Scanner;

public class test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    car[] cars = { new Bus("奧迪A4", 4, 500), new Bus("马自达6", 4, 400), new pickup("皮卡雪", 4, 2, 450),
            new Bus("金龍", 20, 800), new Truck("松花江", 4, 400), new Truck("依維柯", 20, 1000) };
    Scanner input = new Scanner(System.in);
    System.out.println("*****歡迎您使用嗒嗒租車系統******");
    System.out.println("請問您是否租車");
    System.out.println("是:輸入1 否:輸入任意鍵退出");
    if (input.nextInt() == 1) {
        System.out.println("您可租車的類型以及價目表");
        System.out.println("序号\t汽车名称\t租金\t容量\t");
        for (int i = 0; i < cars.length; i++) {
            System.out.println((i + 1) + ("\t") + cars[i]);
        }
        System.out.println("请输入您想要租汽车的数量");
        int carnum = input.nextInt();
        // 创建一新的数组carTotal,用来保存客户所选的车型,数组类型为Car
        car[] carTotal = new car[carnum];
        for (int j = 1; j <= carnum; j++) {
            System.out.println("请输入第" + j + "辆车的序号");
            int num = input.nextInt();
            // 将用户所选的车的序号对应的车型放入数组carTotal中
            carTotal[j - 1] = cars[num - 1];
        }
        System.out.println("请输入租车天数:");
        int carDay = input.nextInt();
        System.out.println("您的账单:");
        // 调用方法-总载客数
        totalperson(carTotal);
        // 调用方法-总载货量
        totalgood(carTotal);
        // 调用方法-总价格
        totalcarrent(carTotal, carDay);
    } else {
        System.out.println("输入错误,退出");
    }
    // 关闭输入函数
    input.close();
}

public static void totalperson(car[] carTotal) {// 总载客量方法
    int k = 0;// 定义总载客量 k
    System.out.println("*****可载人的车辆有:");
    for (int i = 0; i < carTotal.length; i++) {
        // 确认值的类型是否为Bus类型
        if (carTotal[i] instanceof Bus) {
            // 如果是Bus类型,则获取数组中对应的值
            k += ((Bus) carTotal[i]).getPersonload();
            System.out.println(((Bus) carTotal[i]).carname + "\t");
        } else if (carTotal[i] instanceof pickup) {
            k += ((pickup) carTotal[i]).getpersonload();
            System.out.println(((pickup) carTotal[i]).carname + "\t");
        }
    }
    System.out.println("共载客数:" + k + "人");
}

public static void totalgood(car[] carTotal) {// 总载货量方法
    float m = 0f;
    System.out.println("*****可载货的车有:");
    for (int i = 0; i < carTotal.length; i++) {
        if (carTotal[i] instanceof Truck) {
            m += ((Truck) carTotal[i]).getgoodload();
            System.out.println(((Truck) carTotal[i]).carname + "\t");
        } else if (carTotal[i] instanceof pickup) {
            m += ((pickup) carTotal[i]).getgoodload();
            System.out.println(((pickup) carTotal[i]).carname + "\t");
        }
    }
    System.out.println("共载货车有:" + m + "辆");
}

public static void totalcarrent(car[] carTotal, int carDay) {// 总费用
    double n = 0;// 定义总费用 n
    for (int i = 0; i < carTotal.length; i++) {
        n += carTotal[i].carrent;
    }
    n *= carDay;
    System.out.println("租车的总费用为:" + n);
}

}

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消