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

java入门第二季实战

标签:
C

```//父类
package com.buaa.car;

public class Car {
protected int id;
protected String name;
protected int price;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getPrice() {
    return price;
}
public void setPrice(int price) {
    this.price = price;
}   

@Override
public String toString() {
    // TODO Auto-generated method stub
    return "车名:"+name+",租金:"+price+"元";
}

}
子类
``package com.buaa.car;

import com.buaa.car.impl.Zaihuo;
import com.buaa.car.impl.Zaike;

public class PickUp extends Car implements Zaike,Zaihuo{

public PickUp() {
    name = "F150";
    price = 5000;
}

@Override
public double cargo() {
    int weight = 3;
    return weight;
}

@Override
public int passengers() {
    int num = 5;
    return num;
}
@Override
public String toString() {
    // TODO Auto-generated method stub
    return super.toString()+"载重:"+cargo()+"吨,载客:"+passengers()+"人";
}

}


package com.buaa.car;

import com.buaa.car.impl.Zaike;

public class Bus extends Car implements Zaike{
    public Bus(){

        name = "宇通";
        price = 6000;
    }
    @Override
    public int passengers() {
        int num = 45 ;
        return num;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString()+"载客量:"+passengers()+"人";
    }
}

```输入代码
package com.buaa.car;

import com.buaa.car.impl.Zaihuo;

public class Truck extends Car implements Zaihuo{

    public Truck(){

        name = "擎天柱";
        price = 8000;
    }

    @Override
    public double cargo() {
        int weight = 25;
        return weight;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString()+"载重:"+cargo()+"吨";
    }
}
业务逻辑
``package com.buaa.main;

import java.util.Scanner;

import com.buaa.car.Bus;
import com.buaa.car.Car;
import com.buaa.car.PickUp;
import com.buaa.car.Truck;

public class Main {
    static int totalPrice = 0;
    static int totalCargo = 0;
    static int totalPass = 0;
    static Scanner input1;
    static Scanner input2;
    static Car[] c;

    public static void main(String[] args) {

        // 欢迎页
        System.out.println("选择租车类型");
        c = new Car[] { new Bus(), new Truck(), new PickUp() };
        int count = 0;
        // 遍历所有的车
        for (Car List : c) {
            count++;
            System.out.println(count + ":" + List.toString());
        }
        System.out.print("请输入选择车型:    ");
        input1 = new Scanner(System.in);
        int type = input1.nextInt();
        if (type > 0 && type <= c.length) {
            choose(type);
        } else {
            System.out.println("请输入正确的编号!");
            main(args);
        }
    }

    public static void choose(int type) {

        System.out.print("请输入租赁数量:    ");
        input2 = new Scanner(System.in);
        int num = input2.nextInt();
        if (type == 1) {
            Bus b = new Bus();
            totalPrice = (int) b.getPrice() * num;
            totalPass = b.passengers() * num;
            System.out.println("租用大巴" + num + "辆,共" + totalPrice + "元,可载客" + totalPass + "人");
        }
        if (type == 2) {
            Truck t = new Truck();
            totalPrice = (int) t.getPrice() * num;
            totalCargo = (int) t.cargo() * num;
            System.out.println("租用大巴" + num + "辆,共" + totalPrice + "元,可载重" + totalCargo + "吨");
        }
        if (type == 3) {
            PickUp p = new PickUp();
            totalPrice = (int) p.getPrice() * num;
            totalCargo = (int) p.cargo() * num;
            totalPass = p.passengers() * num;
            System.out.println("租用大巴" + num + "辆,共" + totalPrice + "元,可载重" + totalCargo + "吨,可载客" + totalPass + "人");
        }

    }
}
点击查看更多内容
7人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消