手记

Java第二季达达租车系统

//定义一个抽象类
public abstract class Vehicle {
int id;
String name;
int price;
int getPrice(){
return price;
}

}

//定义一个Car类,实现载人
public class Car extends Vehicle {
int num;
Car(int i,String n,int p,int m){
id=i;
name=n;
price=p;
num=m;
}
public String toString(){
return id+"号车 "+name+" "+price+"元/天 "+num+"人座";
}

}
//定义一个Truck类,实现载货
public class Truck extends Vehicle {
int weight;
Truck(int i,String n,int p,int w){
id=i;
name=n;
price=p;
weight=w;
}
public String toString(){
return id+"号车 "+name+" "+price+"元/天 "+weight+"吨";

}

}
//定义一个Pickup,实现载人载货
public class Pickup extends Vehicle {
int num;
int weight;
Pickup(int i, String n, int p, int m, int w) {
// TODO Auto-generated constructor stub
id=i;
name=n;
price=p;
num=m;
weight=w;
}
public String toString(){
return id+"号车 "+name+" "+price+"元/天 "+num+"人座 "+weight+"吨";

}

}

//创建一个Tset类

private static void Test() {
Vehicle[] vehicles={
new Car(1,"奥迪",500,4),
new Car(2,"马自达",400,4),
new Pickup(3,"皮卡雪",450,4,2),
new Car(4,"金龙",800,20),
new Truck(5,"松花江",400,4),
new Truck(6,"依维柯",1000,20)
};
System.out.println("车辆需求:载人请按1,载货请按2,载人载货请按3,退出请按0");
Scanner input=new Scanner(System.in);
switch(input.nextInt()){
case 0:
System.out.println("欢迎下次使用!");
System.exit(0);break;
case 1:
for(int i=0;i<=vehicles.length-1;i++){
if(vehicles[i].getClass()==Car.class){
System.out.println(vehicles[i]);
}}break;
case 2:
for(int i=0;i<=vehicles.length-1;i++){
if(vehicles[i].getClass()==Truck.class){
System.out.println(vehicles[i]);
}}break;
case 3:
for(int i=0;i<=vehicles.length-1;i++){
if(vehicles[i].getClass()==Pickup.class){
System.out.println(vehicles[i]);
}}break;
default:
System.out.println("输入错误!");
System.exit(0);break;
}
System.out.println("请输入车辆编号");
int read2=input.nextInt();
System.out.println("车辆信息:"+vehicles[read2-1].toString());
System.out.println("请输入租车天数:");
int read3=input.nextInt();
System.out.println("此次租车消费"+read3*vehicles[read2-1].getPrice());
}
}
//main函数

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("欢迎使用达达租车系统:");
    System.out.println("你是否要租车:1是 0否");
    Scanner input=new Scanner(System.in);
    int read=input.nextInt();
    if(read==0){
        System.out.println("欢迎下次使用!");

    }
    else if(read==1){
        int read4;
        do{
        Test();
        System.out.println("继续租车请按1,退出请按0");

        read4=input.nextInt();
        }while(read4==1);
        System.out.println("欢迎下次使用!");
    }
    else{
        System.out.println("你的输入有误!");

    }

}

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