把分的类别减少,并且让没有封装之后,代码简单多了。用了一会又写了一个接口版的。
package lu.zhang;
public abstract class allCar implements ICarryCargo,ICarryPassenger{
public String name;
public float price;
public float cargo;
public int passenger;
//getter and setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
//车辆能否载人/载货的标志
public abstract boolean ifCarryCargo();
public abstract boolean ifCarryPassenger();
//车的信息
public void info(){
System.out.println("老司机来告诉你这是一辆车[che,一声]:)");
}
//constructor
public allCar(String name, float price){
this.name=name;
this.price=price;
}
//接口函数重写
@Override
public void setPassengerNum(int passengerNum) {
// TODO Auto-generated method stub
this.passenger=passengerNum;
}
@Override
public int getPassengerNum() {
// TODO Auto-generated method stub
return this.passenger;
}
@Override
public void setCargoNum(float cargoNum) {
// TODO Auto-generated method stub
this.cargo=cargoNum;
}
@Override
public float getCargoNum() {
// TODO Auto-generated method stub
return this.cargo;
}
}
package lu.zhang;
public class Coach extends allCar{
//constructor
public Coach(String name, float price) {
super(name, price);
// TODO Auto-generated constructor stub
}
@Override
public boolean ifCarryCargo() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean ifCarryPassenger() {
// TODO Auto-generated method stub
return true;
}
public void info(){
System.out.println("名称:"+name+" 价格:"+price+"元/天 载客量:"+passenger+"人");
}
}
package lu.zhang;
public class Pickup extends allCar{
//constructor
public Pickup(String name, float price) {
super(name, price);
// TODO Auto-generated constructor stub
}
@Override
public boolean ifCarryCargo() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean ifCarryPassenger() {
// TODO Auto-generated method stub
return true;
}
public void info(){
System.out.println("名称:"+name+" 价格:"+price+"元/天 载客量"+passenger+"人 载货量:"+cargo+"吨");
}
}
package lu.zhang;
public class Truck extends allCar{
//constructor
public Truck(String name, float price) {
super(name, price);
// TODO Auto-generated constructor stub
}
@Override
public boolean ifCarryCargo() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean ifCarryPassenger() {
// TODO Auto-generated method stub
return false;
}
public void info(){
System.out.println("名称:"+name+" 价格:"+price+"元/天 载货量"+cargo+"吨");
}
}
package lu.zhang;
public interface ICarryCargo {
public void setCargoNum(float cargoNum);
public float getCargoNum();
}
package lu.zhang;
public interface ICarryPassenger {
public void setPassengerNum(int passengerNum);
public int getPassengerNum();
}
package lu.zhang;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//首先初始化现有的车辆数据
//使用多态
allCar car1=new Coach("奥迪A4 ",500);
car1.setPassengerNum(4);
allCar car2=new Coach("马自达6",400);
car2.setPassengerNum(4);
allCar car3=new Pickup("皮卡雪6",450);
car3.setPassengerNum(4);
car3.setCargoNum(2);
allCar car4=new Coach("金龙",800);
car4.setPassengerNum(4);
allCar car5=new Truck("松花江",400);
car5.setCargoNum(2);
allCar car6=new Truck("依维柯",1000);
car6.setCargoNum(2);
allCar carArray[]={car1,car2,car3,car4,car5,car6};
//欢迎、提示信息
System.out.println("欢迎来到答答租车系统!");
System.out.println("租车请按“1”,退出请按任意键并回车");
//等待用户键盘输入
Scanner in=new Scanner(System.in);
int start=in.nextInt();
if(start==1){
//展示所有车辆信息
int cnt=0;
for(allCar carx:carArray){
cnt++;
System.out.print(cnt+": ");
carx.info();
}
//输入要租的车数
allCar[] clientArray=new allCar[6];
int[] numArray=new int[]{0,0,0,0,0,0};
for(cnt=0;cnt<6;){
System.out.println("请您输入要租的车的序号:");
in=new Scanner(System.in);
int num=in.nextInt();
if(num<=0num>6) break;
clientArray[cnt]=carArray[num-1];
System.out.println("请您输入要租的车的数量:");
int num1=in.nextInt();
if(num1==0) break;
numArray[cnt]=num1;
System.out.println("请您输入要租的车的天数:");
int num2=in.nextInt();
if(num2==0) break;
numArray[cnt]*=num2;
cnt++;
System.out.println("结束请按0,继续请按任意键");
int num3=in.nextInt();
if(num3==0) break;
}
System.out.println(numArray[0]+numArray[1]);
//输出总金额,总载客/载货量
int sumPassenger=0;
int sumFreight=0;
float Amount=0;
for(int i=0;i<cnt;i++){
if (clientArray[i].ifCarryCargo())
sumFreight+=clientArray[i].cargo;
if(clientArray[i].ifCarryPassenger())
sumPassenger+=clientArray[i].passenger;
Amount+=numArray[i]*clientArray[i].getPrice();
}
System.out.println("总金额:"+Amount+" 总载客:"+sumPassenger+" 总载货:"+sumFreight);
}
else{
System.out.println("退出租车系统");
}
}
}