手记

Java入门第二季课程——综合练习

父类:
package com.imooc;

public class Transport {
String name;
double rent;
double cargo;
int people;
public String getName(){
return name;
}
public double getRent(){
return rent;
}
public double getPeople(){
return people;
}
public double getCargo(){
return cargo;
}
}


子类1:
package com.imooc;

public class PassengerCar extends Transport {
private double people;
public PassengerCar(String name, double rent, int people){
this.name = name;
this.rent = rent;
this.people = people;
}
public double getPeople(){
return people;
}
}


子类2:
package com.imooc;

public class Truck extends Transport {
private double cargo;
public Truck(String name, double rent, double cargo){
this.name = name;
this.rent = rent;
this.cargo = cargo;
}
public double getCargo(){
return cargo;
}
}


子类3:
package com.imooc;

public class PickUp extends Transport {
private double cargo;
private double people;
public PickUp(String name, double rent, double cargo, int people){
this.name = name;
this.rent = rent;
this.cargo = cargo;
this.people = people;
}
public double getCargo(){
return cargo;
}
public double getPeople(){
return people;
}
}


主类:
package com.imooc;

import java.util.Scanner;
public class Initial {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Transport[] cars = {new PassengerCar("奥迪A4",500,4),new PassengerCar("马自达6",400,4),new Truck("东风",600,20),new Truck("奥拓",200,5),new PickUp("JEEP",500,2,4)};
    System.out.println("欢迎使用答答租车系统:");
    System.out.println("您是否要租车:1是  0否");
    Scanner s = new Scanner(System.in);
    String str = s.nextLine();
    if(Integer.parseInt(str)==1){
        //打印出数据表格
        System.out.println("您可租车的类型及其价目表:");
        System.out.println("序号\t汽车名称\t租金\t容量");
        int i = 1;
        for(Transport car:cars){
            if(car instanceof PassengerCar){
                System.out.println(""+i+"\t"+car.getName()+"\t"+car.getRent()+"元/天"+"\t"+"载人:"+car.getPeople()+"人");
            }
            if(car instanceof Truck){
                System.out.println(""+i+"\t"+car.getName()+"\t"+car.getRent()+"元/天"+"\t"+"载货:"+car.getCargo()+"吨");
            }
            if(car instanceof PickUp){
                System.out.println(""+i+"\t"+car.getName()+"\t"+car.getRent()+"元/天"+"\t"+"载人:"+car.getPeople()+"人"+"  "+"载货:"+car.getCargo()+"吨");
            }
            i++;
        }

        //租车数
        System.out.println("请输入您要租汽车的数量:");
        int num = s.nextInt();
        int[] n = new int[num];
        for(int j=0; j<num; j++){
            System.out.println("请输入第"+(j+1)+"辆车的序号:");
            int t = s.nextInt();
            if(t <= cars.length){
                n[j] = t;
            }else{
                j--;
            }
        }

        //租车天数
        System.out.println("请输入租车天数:");
        int day = s.nextInt();

        //输出结果
        System.out.println("您的账单:");
        //载人车名及总载人数
        System.out.println("***可载人的车有:");
        int sumPeople = 0;
        for(int j=0; j<num; j++){
            if(cars[n[j]-1] instanceof PassengerCar || cars[n[j]-1] instanceof PickUp){
                System.out.print(cars[n[j]-1].name+"\t");
                sumPeople += cars[n[j]-1].getPeople();
            }
        }
        System.out.println("共载人:"+sumPeople+"人");
        //载货车名及总载货量
        System.out.println("***可载货的车有:");
        double sumCargo = 0;
        for(int j=0; j<num; j++){
            if(cars[n[j]-1] instanceof Truck || cars[n[j]-1] instanceof PickUp){
                System.out.print(cars[n[j]-1].name+"\t");
                sumCargo += cars[n[j]-1].getCargo();
            }
        }
        System.out.println("共载货:"+sumCargo+"吨");
        //总费用
        double value = 0;
        for(int j=0; j<num; j++){
            value += cars[n[j]-1].rent;
        }
        double sumValue = day*value;
        System.out.println("***租车总价格:"+sumValue+"元");
    }
    s.close();
}

}

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