手记

java入门第二季-答答租车系统

新手入门,请各位同学和大神们多多指导。
目前只使用到了继承,多态,对于main中的数组这块研究了一晚上,还有点模糊。期间参考了几位同学的写的代码,自己总结了下,代码如下;
父类car


public class Car {//客车涉及的参数 名称 价格 载人数  载货数
     public String name;
     public double price;
     public int manned;
     public double cargo;
     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 getManned() {
        return manned;
    }
    public void setManned(int manned) {
        this.manned = manned;
    }
    public double getCargo() {
        return cargo;
    }
    public void setCargo(int cargo) {
        this.cargo = cargo;
    }

}

客车


public class PassengerCar extends Car {//客车的 属性和构造函数
    private String name;
    private int manned;
    private double price;
    public  PassengerCar(String name,int manned,int price){
        this.name=name;
        this.manned=manned;
        this.price=price;       
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getManned() {
        return manned;
    }
    public void setManned(int manned) {
        this.manned = manned;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "小汽车 [名称=" + name + ", 载人数=" + manned + ", 价格="
                + price + "]";
    }

}

皮卡

public class Pickup extends Car {//皮卡属性
    private String name;
    private int manned;
    private double price;
    private double cargo;
    public  Pickup(String name,double cargo,double price,int manned){
        this.name=name;
        this.manned=manned;
        this.price=price;
        this.cargo=cargo;
    }

    public String getName() {
        return name;
    }

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

    public int getManned() {
        return manned;
    }

    public void setManned(int manned) {
        this.manned = manned;
    }

    public double getPrice() {
        return price;
    }

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

    public double getCargo() {
        return cargo;
    }

    public void setCargo(double cargo) {
        this.cargo = cargo;
    }

    @Override
    public String toString() {
        return "皮卡 [名称=" + name + ", 载人=" + manned + ", 价格="
                + price + ", 载重=" + cargo + "]";
    }   
}

货车


public class Truck extends Car {//货车的属性和构造函数
    private String name;
    private double cargo;
    private double price;
    public  Truck(String name,double cargo,double price){
        this.name=name;
        this.cargo=cargo;
        this.price=price;
    }

    public String getName() {
        return name;
    }

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

    public double getCargo() {
        return cargo;
    }

    public void setCargo(double cargo) {
        this.cargo = cargo;
    }

    public double getPrice() {
        return price;
    }

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

    @Override
    public String toString() {
        return "货车 [名称=" + name + ", 载重=" + cargo + ", 价格="
                + price + "]";
    }
}

主函数

import java.util.*;
//import java.
public class Initail {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("欢迎使用答答租车系统");
        System.out.println("您是否需要租车:1 是,0 否");
        Scanner sc=new Scanner(System.in);
        int input1=sc.nextInt();//获取输入的值
        if (input1==0) {//判断值如果为1 操作 遍历显示所有的车型 ,判断需求的车辆数量 天数 和载重 ,统计total
            System.out.println("谢谢");
        }else if (input1==1) {
            Car[] cars={new PassengerCar("奥迪A4",5,500), 
                    new Truck("小货车",5,150),
                    new PassengerCar("奔驰E400",5,700),
                    new Pickup("皮卡",1.5,200,4), 
                    new Truck("大型货车",600,10), 
                    new PassengerCar("大客车",25,650)}; 
            for(int i=0;i<cars.length;i++){//遍历所有车型
                System.out.println("车型为 "+cars[i]+"序号为"+(i+1));
            }
                System.out.println("请输入需要租车的数量");
                int input2=sc.nextInt();//获取租车数量
                double totalprice=0;
                int totalper=0;
                double totalicargo=0;
                if (input2>0&&input2<=6) {              
                    for(int j=0;j<input2;j++){//获取租车数量 判断是哪种类型的租车 然后输出名称 钱和载人或载重
                        System.out.println("请输入需要租车的序号");
                        int input3=sc.nextInt();
                        int a =input3-1;
                        if (cars[a] instanceof PassengerCar) {//如果是客车 输出载人 名称和价格
                        System.out.println(cars[a]);
                        }else if(cars[a] instanceof Truck) {
                            System.out.println(cars[a]);
                        }else if (cars[a] instanceof Pickup){
                            System.out.println(cars[a]);
                        }else {
                            System.out.println(cars[a]);
                        }
            //获取选择中的钱 名称 载重 载人
                        totalper=totalper+((Car) cars[a]).getManned();
                        totalicargo+=((Car) cars[a]).getCargo();
                        totalprice+=((Car) cars[a]).getPrice();
                        }
                    }else {
                        System.out.println("输入有误 请重新输入");
                    }
                System.out.println("请输入需要租用的天数");
                int input4=sc.nextInt();
                if (input4>0) {
                    double sumpri=0;
                    double sumico=0;
                    int sumper=0;
                    sumpri=input4*totalprice;
                    sumico=input4*totalicargo;
                    sumper=input4*totalper;
                    System.out.println("您的账单如下");
                    System.out.println("总价格"+sumpri+"总载重"+sumico+"总载人"+sumper);
                }else {
                    System.out.println("输入有误");
                    sc.close();
                }

        }
    }
}

以上就是,这是运行结果

现在是主要是去完善这个代码,如何利用上接口和优化下主函数 ,父类如果用抽象类的话这么写,我的再想想,请各位大神指点。

1人推荐
随时随地看视频
慕课网APP