父类
package com.dadazuchu;
//父类
public abstract class Car {
protected String carName;
protected int rentPrice;
protected int passengerCapacity;
protected int goodsCapacity;
//构造四个方法
public String getcarName(){
return carName;
}
public int getrentPrice(){
return rentPrice;
}
public int getpassenger(){
return passengerCapacity;
}
public int getgoods(){
return goodsCapacity;
}
}
子类
package com.dadazuchu;
public class Pickup extends Car {
//子类对象初始化
Pickup(String carName,int rentPrice,int passengerCapacity,int goodsCapacity){
this.carName=carName;
this.rentPrice=rentPrice;
this.passengerCapacity=passengerCapacity;
this.goodsCapacity=goodsCapacity;
}
}
package com.dadazuchu;
public class Passenger extends Car {
//子类对象初始化
Passenger(String carName,int rentPrice,int passengerCapacity){
this.carName=carName;
this.rentPrice=rentPrice;
this.passengerCapacity=passengerCapacity;
}
}
package com.dadazuchu;
public class Goods extends Car {
//子类对象初始化
Goods(String carName,int rentPrice,int goodsCapacity){
this.carName=carName;
this.rentPrice=rentPrice;
this.goodsCapacity=goodsCapacity;
}
}
主函数
package com.dadazuchu;
import java.util.*;
public class Initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 租车类型及价格表,创建父类数组调用子类对象
Car[] carforRent={
new Passenger("奥迪A4", 500, 4),
new Passenger("马自达6", 400, 4),
new Pickup("皮卡雪",450,4,2),
new Passenger("金龙",800,20),
new Goods("松花江", 400, 4),
new Goods("依维柯", 1000, 20)};
System.out.println("欢迎使用嗒嗒租车系统!");
System.out.println("您是否要租车:1是,0否");
Scanner sn=new Scanner(System.in);
String input=sn.nextLine();
int num=1;
if(input.equals("1")){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t容量");
//新建父类 Car newcar来遍历父类数组carforRent
for(Car newcar:carforRent){
//如果数组内有Passenger对象,输出该对象的属性
if(newcar instanceof Passenger){
System.out.println(num+".\t"+newcar.carName+"\t"+newcar.rentPrice+"元/天"+"\t"+"载人:"+newcar.passengerCapacity+"人");
num++;}
//如果数组内有Pickup对象,输出该对象的属性
if(newcar instanceof Pickup){
System.out.println(num+".\t"+newcar.carName+"\t"+newcar.rentPrice+"元/天"+"\t"+"载人:"+newcar.passengerCapacity+"人"+newcar.goodsCapacity+"吨");
num++;}
//如果数组内与Goods对象,输出该对象的属性
if(newcar instanceof Goods){
System.out.println(num+".\t"+newcar.carName+"\t"+newcar.rentPrice+"元/天"+"\t"+"载人:"+newcar.goodsCapacity+"吨");
num++;}
}
System.out.println("请输入您要租汽车的数量:");
int Nums=sn.nextInt();//汽车使用量
//判断数量的可行性
if(Nums<=0){
System.out.println("您的输入有误,请重新输入!");
System.out.println("请输入您要租汽车的数量:");
Nums=sn.nextInt();
}
//这里用把输入值Nums数组化
int[] carNums=new int[Nums];
for(int i=0;i<carNums.length;i++){
System.out.println("请输入第"+(i+1)+"辆车的序号");
carNums[i]=sn.nextInt();
//判断输入序号的可行性
if(carNums[i]>6||carNums[i]<0){
System.out.println("您的输入有误,请重新输入第"+(i+1)+"辆车的序号");
if(i>=0){
i--;
}else{
i=0;
}
continue;
}
}
System.out.println("请输入租车天数:");
int days=sn.nextInt();
//判断输入的天数的可行性
if(days<=0){
System.out.println("您的输入有误,请重新输入!");
System.out.println("请输入租车天数:");
days=sn.nextInt();
}
int Sumcountpeople=0;
int Sumcoutgoods=0;
int rent=0;
System.out.println("您的账单:\n***可载人的车有:");
//利用for(){}来遍历父类数组对应的子类对象
for(int i=0;i<carNums.length;i++){
//通过if来判断父类数组是否跟子类对象相同
if((carforRent[carNums[i]-1]instanceof Passenger)||(carforRent[carNums[i]-1]instanceof Pickup)){
System.out.println(carforRent[carNums[i]-1].carName+"\t");
//通过if来判断父类数组是否跟子类对象相同
if((carforRent[carNums[i]-1])instanceof Passenger){
Sumcountpeople=Sumcountpeople+
((Passenger)carforRent[carNums[i]-1]).getpassenger();}//调用子类对象的方法
//通过if来判断父类数组是否跟子类对象相同
if((carforRent[carNums[i]-1])instanceof Pickup){
Sumcountpeople=Sumcountpeople+
((Pickup)carforRent[carNums[i]-1]).getpassenger();}//调用子类对象的方法
}
}System.out.println("共载人:"+Sumcountpeople+"人!");
System.out.println("您的账单:\n***可载货的车有:");
//利用for(){}来遍历父类数组对应的子类对象
for(int i=0;i<carNums.length;i++){
//通过if来判断父类数组是否跟子类对象相同
if((carforRent[carNums[i]-1]instanceof Goods)||(carforRent[carNums[i]-1]instanceof Pickup)){
System.out.println(carforRent[carNums[i]-1].carName+"\t");
//通过if来判断父类数组是否跟子类对象相同
if((carforRent[carNums[i]-1])instanceof Goods){
Sumcoutgoods=Sumcoutgoods+
((Goods)carforRent[carNums[i]-1]).getgoods();}
//通过if来判断父类数组是否跟子类对象相同
if((carforRent[carNums[i]-1])instanceof Pickup){
Sumcoutgoods=Sumcoutgoods+
((Pickup)carforRent[carNums[i]-1]).getgoods();}
}
}System.out.println("共可载货:"+Sumcoutgoods+"吨!");
for(int i=0;i<carNums.length;i++){
rent=rent+carforRent[carNums[i]-1].getrentPrice();
}
rent=rent*days;
System.out.println("***租车总价格:"+rent+"元!");
}else if(input.equals("0")){
System.out.println("谢谢光临");
return;
}else if(!input.equals("1")&&!input.equals("0")){
System.out.println("您的输入错误!");
Initial sys1=new Initial();
sys1.main(null);
}
}
}