Vehicle为基类
public class Vehicle {
String name;//车名
double price;//价目
int manRide;//载人数
int load;//载重吨量
}
只载重车类
public class Truk extends Vehicle{
}
只载人车类
public class Coach extends Vehicle{
}
奥迪车类,继承载人车类
public class Audi extends Coach{
Audi(String name,double price,int manRide){
this.name=name;
this.price=price;
this.manRide=manRide;
}
}
金龙车类,继承载人车类
public class GoldLong extends Coach{
GoldLong(String name,double price,int manRide){
this.name=name;
this.price=price;
this.manRide=manRide;
}
}
依维柯车类,继承只载重车类
public class Iveco extends Truk{
Iveco(String name,double price,int load){
this.name=name;
this.price=price;
this.load=load;
}
}
马自达车类,继承只载人车类
public class Mazda extends Coach{
Mazda(String name,double price,int manRide){
this.name=name;
this.price=price;
this.manRide=manRide;
}
}
皮卡车类,继承基类,与载人,载重车类同级
public class PickUp extends Vehicle{
PickUp(String name,double price,int manRide,int load){
this.name=name;
this.price=price;
this.manRide=manRide;
this.load=load;
}
}
松花车类
public class SongHua extends Vehicle{
SongHua(String name,double price,int load){
this.name=name;
this.price=price;
this.load=load;
}
}
Test测试类,程序入口
package com.lyj;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Test {
Vehicle audi=new Audi("奥迪 A4",500,4);
Vehicle mazda=new Mazda("马自达6",400,4);
Vehicle pickUp=new PickUp("皮卡雪6",450,4,2);
Vehicle goldLong=new GoldLong("金 龙 ", 800,20);
Vehicle songHua=new SongHua("松 花 江 ",400,4);
Vehicle iveco=new Iveco("依 维 柯 ",1000,20);
Vehicle cars[]={audi,mazda,pickUp,goldLong,songHua,iveco};
int count;
Vehicle []rentCar;
static Scanner scan=new Scanner(System.in);
int days;
//double cost;
double sum=0;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.out.println(goldLong);
Test test=new Test();
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否需要租车:1是0否");
int input=scan.nextInt();
if(input==0)
return;
test.priceTable();
System.out.println("输入租车的数量");
int num=scan.nextInt();
test.setRentCar(num);
test.setDays();
test.bill();
}
/*
* 租车天数
*/
public void setDays(){
System.out.println("请输入租车天数:");
days=scan.nextInt();
}
/*
* 账单
*/
public void bill(){
int people=0;
int weight=0;
List<Vehicle>coachs=new LinkedList<Vehicle>();
List<Vehicle> truks=new LinkedList<Vehicle>();
for(int i=0;i<rentCar.length;i++){
boolean b0=rentCar[i] instanceof Coach ;
boolean b1=rentCar[i] instanceof Truk;
boolean b2=rentCar[i] instanceof PickUp;
if(b0||b2)
coachs.add((Vehicle)rentCar[i]);
if(b1||b2)
truks.add((Vehicle)rentCar[i]);
}
System.out.println("您的账单有:");
System.out.println("***可载人的车有:");
for(Vehicle coach:coachs){
System.out.print(coach.name+" ");
people+=coach.manRide;
}
System.out.println("共载人:"+people);
System.out.println("***可载物的车有:");
for(Vehicle truk:truks){
System.out.print(truk.name+" ");
weight+=truk.load;
}
System.out.println("共载货:"+weight);
System.out.println("***租车的总价格"+days*sum);
}
public void setCount(int num){
count=num;
}
/*
* 输入租车的类型
*/
public void setRentCar(int count){
rentCar=new Vehicle[count];
int []arr={1,2,3,4,5,6};
int flag;
for(int i=0;i<count;i++){
do{
System.out.println("输入第"+(i+1)+"辆车的序号");
int order=scan.nextInt();
flag=Arrays.binarySearch(arr, order);//返回值为数组的下标。
}while(flag<0||flag>5);
rentCar[i]=cars[flag];
//System.out.println(rentCar[i]);
sum+=rentCar[i].price;
}
}
/*
* 打印价目表
*/
public void priceTable(){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
for(int i=0;i<cars.length;i++){
switch(i){
case 0:
case 1:
case 3:
System.out.println(i+1+". "+cars[i].name+" "+cars[i].price+"元/天 " +
" 载人:"+cars[i].manRide+"人");
break;
case 2:
System.out.println(i+1+". "+cars[i].name+" "+cars[i].price+"元/天 " +
" 载人:"+cars[i].manRide+"人,载货:"+cars[i].load+"吨");
break;
case 4:
case 5:
System.out.println(i+1+". "+cars[i].name+" "+cars[i].price+"元/天 " +
" 载货:"+cars[i].load+"吨");
break;
}
}
}
}