ITPub博客

首页 > IT职业 > IT图书 > Java入门第二季课程——综合练习

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

IT图书 作者:freefly 时间:2021-09-09 09:04:46 0 删除 编辑

父类:
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

}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1064/viewspace-2807921/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    73
  • 访问量
    662