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

Java入门第二季-答答租车系统源代码-原创-(含截图)

标签:
Java
图片描述
整体思路
  1. 创建名为Car的抽象类,声明共有属性(名字,载客量busLoad,载货量trunkLoad,租金);
  2. 分别创建卡车类Trunk,小轿车类Bus,皮卡类PickUp,均继承于Car类;
  3. 在主方法中分别创建各种车的对象,并用一个数组存放这些对象,最后对用户输入的序号对应的租车价格计算;

/**
 * 抽象类
 * Created by haivo on 2016/3/24.
 */
public abstract class Car {
    public String name = "";
    public  int trunkLoad = 0;
    public int busLoad = 0;
    public  int price = 0;
}

/**
 * 卡车(东风,解放)
 * Created by haivo on 2016/3/24.
 */
public class Trunk extends Car{
    public Trunk(String name,int trunkLoad,int price){
        this.name = name;
        this.trunkLoad = trunkLoad;
        this.price = price;
    }

    @Override
    public String toString() {
        return String.format("%s\t\t%s\t\t%d%s\t\t%d%s", name,"-",trunkLoad,"吨",price,"元/天");
    }
}

/**
 * 小轿车(奔驰,雪佛兰)
 * Created by haivo on 2016/3/24.
 */
public class Bus extends Car {
    public Bus(String name,int busLoad,int price){
        this.name = name;
        this.busLoad = busLoad;
        this.price = price;
    }

    @Override
    public String toString() {
        return String.format("%3s\t%d%s\t\t%s\t\t%d%s", name,busLoad,"人","-",price,"元/天");
    }
}

/**
 * 皮卡(风骏,拓路者)
 * Created by haivo on 2016/3/24.
 */
public class PickUp extends Car {
    public PickUp(String name,int busLoad,int trunkLoad,int price){
        this.name = name;
        this.busLoad = busLoad;
        this.trunkLoad = trunkLoad;
        this.price = price;
    }
    @Override
    public String toString() {
        return String.format("%3s\t%d%s\t\t%d%s\t\t%d%s", name,busLoad,"人",trunkLoad,"吨",price,"元/天");
    }
}

/**
 * 程序入口
 * Created by haivo on 2016/3/24.
 */
public class SystemSurface {
    public static void main(String[] args) {

        Trunk donfFeng = new Trunk("东风", 10, 400);
        Trunk jieFang = new Trunk("解放", 15, 500);
        Bus xueFuLan = new Bus("雪佛兰", 6, 200);
        Bus benz = new Bus("奔驰", 4, 300);
        PickUp fengJun = new PickUp("风骏", 4, 3, 200);
        PickUp tuoLuZhe = new PickUp("拓路者", 4, 5, 260);
        Car car[] = new Car[]{donfFeng,jieFang,xueFuLan,benz,fengJun,tuoLuZhe};
        String[] carInfo = new String[]{
                donfFeng.toString(),
                jieFang.toString(),
                xueFuLan.toString(),
                benz.toString(),
                fengJun.toString(),
                tuoLuZhe.toString()
        };

        System.out.println("欢迎使用达达租车系统.");
        System.out.println("以下是价目表:\n");
        System.out.println("序号\t\t汽车名称\t载客量\t载货量\t租金\t");
        for(int num =0;num<=carInfo.length-1;num++) {
            System.out.println(" "+(num+1)+"\t\t"+carInfo[num]);
        }
        System.out.println("您要租几辆车?");
        Scanner scanner = new Scanner(System.in);
        int carNumber = scanner.nextInt();//租车数量carNum
        int chooseNo[] = new int[20]; //要租的车的序号chooseNo数组保存
        for(int i=1;i<=carNumber;i++){
            System.out.print("请输入第"+i+"辆车的序号:");
            chooseNo[i] = scanner.nextInt();
        }
        int subPrice = 0;
        for(int i=1;i<=carNumber;i++){
            subPrice = subPrice + car[(chooseNo[i])-1].price;
        }
        System.out.println("您需要支付的金额是:"+subPrice);
    }
}
点击查看更多内容
9人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消