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

Java入门第二季租车系统

标签:
Java
package com.carStore;

import java.util.Scanner;

public class car {
    int num;
    String name;
    int cargo;
    int people;
    int price;

    public void showcar() {
        car car[] = { new PickUp(1, "皮卡雪", 2, 4, 400), new PickUp(2, "皮卡丘", 3, 3, 600), new PeopleCar(3, "法拉利", 3, 900),
                new PeopleCar(4, "捷达", 3, 200), new CargoCar(5, "拖拉机", 4, 300), new CargoCar(6, "小推车", 1, 100) };
        int sumcargo = 0;
        int sumpeople = 0;
        int sumprice = 0;
        System.out.println("请输入您需要的总车种类:");
        Scanner r2 = new Scanner(System.in);
        int number = r2.nextInt();
        Scanner r3 = new Scanner(System.in);
        Scanner r4 = new Scanner(System.in);
        for (int i = 0; i < number; i++) {
            // 输入车序号
            System.out.println("请输入第" + (i + 1) + "种车的序号:");
            int carnum = r3.nextInt();
            // 输入该序号车数量
            System.out.println("请输入" + carnum + "号车的数量:");
            int carquantity = r4.nextInt();
            for (int j = 1; j < 7; j++) {
                if (carnum == j) {
                    sumcargo += car[j - 1].cargo * carquantity;
                    sumpeople += car[j - 1].people * carquantity;
                    sumprice += car[j - 1].price * carquantity;
                }
            }
        }
        System.out.println("您租的车共可运货:" + sumcargo + "吨");
        System.out.println("您租的车共可载人:" + sumpeople + "位");
        System.out.println("您租的车总价:" + sumprice + "元");
        r2.close();
        r3.close();
        r4.close();
    }
}

//货车
package com.carStore;

public class CargoCar extends car {
    public CargoCar(int newnum,String newname,int newcargo,int newprice){
        num=newnum;
        name=newname;
        cargo=newcargo;
        price=newprice;
        System.out.println(num+"\t"+name+"\t"+"载货"+cargo+"\t"+price);
    }
}

//主程序
package com.carStore;

import java.util.Scanner;

public class Initial {
    public static void main(String[] args) {
        Scanner r1 = new Scanner(System.in);
        System.out.println("欢迎使用租尼玛租车系统:请问是否租车?【Y/N】");
        while (true) {
            String command = r1.nextLine();
            if ("N".equals(command)) {
                System.out.println("请退出程序!");
                break;
            } else if ("Y".equals(command)) {
                System.out.println("车型如下所示:");
                car object = new car();
                object.showcar();
                break;
            } else {
                System.out.println("输入有误,请重试。");
            }
        }
        r1.close();
    }
}

//轿车
package com.carStore;

public class PeopleCar extends car {

    public PeopleCar(int newnum,String newname,int newpeople,int newprice){
        num=newnum;
        name=newname;
        people=newpeople;
        price=newprice;
        System.out.println(num+"\t"+name+"\t"+"载人"+people+"\t"+price);
    }
}

//皮卡
package com.carStore;

public class PickUp extends car {

    public PickUp(int newnum,String newname,int newcargo,int newpeople,int newprice){
        num=newnum;
        name=newname;
        cargo=newcargo;
        people=newpeople;
        price=newprice;
        System.out.println(num+"\t"+name+"\t"+"载货"+cargo+"载人"+people+"\t"+price);
    }
}
点击查看更多内容
16人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消