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

【Java入门第二季】最后的项目实践源代码参考

标签:
Java
父类:Vehicle
public class Vehicle {
    public String name;     //汽车名称  
    public double price;    //汽车租金(元/天)
    public int capacity1;   //汽车容量(人)
    public double capacity2;//汽车容量(货)

    public Vehicle(String name,double price,int capacity1){
        this.name = name;
        this.price = price;
        this.capacity1 = capacity1;
    }

    public Vehicle(String name,double price,double capacity2){
        this.name = name;
        this.price = price;
        this.capacity2 = capacity2;
    }

    public Vehicle(String name,double price,int capacity1,double capacity2){
        this.name = name;
        this.price = price;
        this.capacity1 = capacity1;
        this.capacity2 = capacity2;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getCapacity1() {
        return capacity1;
    }

    public void setCapacity1(int capacity1) {
        this.capacity1 = capacity1;
    }

    public double getCapacity2() {
        return capacity2;
    }

    public void setCapacity2(double capacity2) {
        this.capacity2 = capacity2;
    }   
}
三个子类:
public class Car extends Vehicle {

    public Car(String name, double price, int capacity1) {
        super(name, price, capacity1);
        // TODO Auto-generated constructor stub
    }
}

public class Truck extends Vehicle {

    public Truck(String name, double price, double capacity2) {
        super(name, price, capacity2);
        // TODO Auto-generated constructor stub
    }
}

public class Pickup extends Vehicle {

    public Pickup(String name, double price, int capacity1, double capacity2) {
        super(name, price, capacity1,capacity2);
        // TODO Auto-generated constructor stub
    }
}

测试类:
import java.util.Scanner;

public class Initial {

    public static void main(String[] args) {
        // TODO Auto-generated method stub      
        double totalPrice1 = 0;//定义变量用于计算载人车辆总价格
        double totalPrice2 = 0;//定义变量用于计算载货车辆总价格

        int totalPerson = 0;//定义变量用于计算总载人数

        double totalGoods = 0;//定义变量用于计算总载货量
        System.out.println("*****欢迎使用嗒嗒租车系统*****");
        System.out.println("您是否需要租车:1是  0否");
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        if(choice == 1){
            System.out.println("==您可租车的类型及价目表如下所示==");
            Vehicle[] vehicle={new Car("奥迪A4",500,4),new Car("马自达6",400,4),
                                new Pickup("皮卡雪6",450,4,2.0),new Car("金龙",800,20),
                                new Truck("松花江",400,4.0),new Truck("依维柯",1000,20.0)};
            System.out.println("序号\t汽车名称\t租金\t\t容量");
            for(int i = 0;i<vehicle.length;i++){    
                System.out.print(i+1 + "\t"+vehicle[i].getName()+"\t"+vehicle[i].getPrice()+"元/天\t\t");
                if(vehicle[i] instanceof Car){
                    System.out.println("载人:"+vehicle[i].getCapacity1()+"人");
                }else if(vehicle[i] instanceof Pickup){
                    System.out.println("载人:"+vehicle[i].getCapacity1()+"人"+"载货:"+vehicle[i].getCapacity2()+"吨");
                }else{
                    System.out.println("载货:"+vehicle[i].getCapacity2()+"吨");
                }
            }
            System.out.println("请输入您要租车的数量(最多可同时租5辆):");
            int rentNum = input.nextInt();
            if(rentNum>0 && rentNum<=5){
                int[] rentArray = new int[rentNum];
                for(int i=1;i<=rentNum;i++){
                    System.out.println("请输入第"+i+"辆车序号:");
                    rentArray[i-1] = input.nextInt()-1;
                }
                System.out.println("请输入租车天数:");
                int rentDay = input.nextInt();
                System.out.println("您的账单:");
                System.out.println("==可载人的车有==");
                for(int i=0;i<rentArray.length;i++){
                    if((vehicle[rentArray[i]] instanceof Car)||(vehicle[rentArray[i]] instanceof Pickup)){
                        System.out.print(vehicle[rentArray[i]].getName()+"  ");
                        totalPrice1 += vehicle[rentArray[i]].getPrice()*rentDay;
                        totalPerson += vehicle[rentArray[i]].getCapacity1();
                    }
                }
                System.out.println("共载人:"+totalPerson+"人");
                System.out.println("==可载货的车有==");
                for(int i=0;i<rentArray.length;i++){
                    if((vehicle[rentArray[i]] instanceof Pickup)||(vehicle[rentArray[i]] instanceof Truck)){
                        System.out.print(vehicle[rentArray[i]].getName()+"  ");
                        if(vehicle[rentArray[i]] instanceof Truck){
                            totalPrice2 += vehicle[rentArray[i]].getPrice()*rentDay;
                        }
                        totalGoods += vehicle[rentArray[i]].getCapacity2();
                    }
                }
                System.out.println("共载货:"+totalGoods+"吨");
                System.out.println("租车总价格:"+ (totalPrice1+totalPrice2)+"元");
                System.out.println("*****谢谢您的使用*****");
            }else{
                System.out.println("请修改租车数量");
            }

        }else{
            System.out.println("*****已经退出嗒嗒租车系统*****");
            input.close();
        }
    }
}

需要项目压缩包的请留下邮箱地址,欢迎大家一起讨论

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

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

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
21
获赞与收藏
100

关注作者,订阅最新文章

阅读免费教程

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消