继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java入门第二季租车系统

志远3
关注TA
已关注
手记 1
粉丝 0
获赞 29
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);
    }
}
打开App,阅读手记
16人推荐
发表评论
随时随地看视频慕课网APP