//基类
package car_rent;
public abstract class Car {
String name;
int person;
double price;
double weight;
//客车
public Car(String name,int person,double price){
this.name = name;
this.person = person;
this.price = price;
}
//货车
public Car(String name,double weight,double price){
this.name = name;
this.weight = weight;
this.price = price;
}
//皮卡
public Car(String name,int person, double weight,double price){
this.name = name;
this.weight = weight;
this.price = price;
this.person = person;
}
public void showInfo(int num,int days){
double totalAmount;
totalAmount = num * days * price;
System.out.println("您选择的车是:"+name+"价格是:"+price+"/天,数量为"+num+"辆,天数为"+days+"天,总计"+totalAmount+"元");
}
public abstract void show();
}
//客车类
public class PassengerCar extends Car {
public PassengerCar(String name,int person,double price){
super(name,person,price);
}
public void show(){
System.out.println(name+" "+price+"/天,可以载人数:"+person);
}
}
//卡车类
public class Truk extends Car {
Truk(String name,double weight,double price){
super(name,weight,price);
}
public void show(){
System.out.println(name+" "+price+"/天,载重量为:"+weight+"吨");
}
}
//皮卡类
public class Pickup extends Car {
Pickup(String name,int person, double weight,double price){
super(name,person,weight,price);
}
public void show(){
System.out.println(name+" "+price+"/天,可载人数"+person+" 载重量为:"+weight+"吨");
}
}
//入口
package car_rent;
import java.util.Scanner;
public class Index {
public static void main(String[] args){
Car[] cars = {
new PassengerCar("马自达",6,150),
new Pickup("皮卡雪",4,3,300),
new Truk("东风重卡",20,350)
};
System.out.println("欢迎使用XXX租车系统");
System.out.println("是否租车:1-是 0否");
Scanner input1 = new Scanner(System.in);
int xz = input1.nextInt();
if(xz == 1){
System.out.println("请选择租车类型:");
for (int i=1;i<= cars.length;i++){
System.out.print("编号:"+i+" ");
cars[i-1].show();
}
System.out.println("请选择编号:");
Scanner input2 = new Scanner(System.in);
int carIndex = input2.nextInt();
if(carIndex <= 0 || carIndex > cars.length){
System.out.println("输入错误,退出系统");
System.exit(0);
}
System.out.println("请选择数量:");
Scanner input4 = new Scanner(System.in);
int num = input4.nextInt();
if(num <= 0 ){
System.out.println("输入错误,退出系统");
System.exit(0);
}
System.out.println("请选择租车天数:");
Scanner input3 = new Scanner(System.in);
int days = input3.nextInt();
if(days <= 0){
System.out.println("输入错误,退出系统");
System.exit(0);
}
for (int i=1;i<= cars.length;i++){
if(carIndex == i){
cars[i-1].showInfo(num,days);
break;
}
}
}else {
System.out.println("退出系统");
System.exit(0);
}
}
}