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

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

堇延未七
关注TA
已关注
手记 4
粉丝 21
获赞 100
父类: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();
        }
    }
}

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

打开App,阅读手记
15人推荐
发表评论
随时随地看视频慕课网APP

热门评论

叹服!!!

巧妙之处:

  1. 父类中用重写构造函数的方式区别出“只载人”“只载货”“载人又载货”三类车;

  2. 父类中用俩俩对应的 获取和设置 值的方式,搞定“车名、租金、载人量、载货量”的获取和设置;

  3. 测试类中用instanceof关键字,对数组元素的类型进行检测,从而匹配相应的输出信息。

源码就不用了 支持下楼主,可以的。

感谢您!!!!!!!!!

查看全部评论