/*-----------------Vehicle类----------------*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class vehicle{
public static int cNo; //车牌号
public String cStyle; //车类型
public float price; //购车价格
public static float dPrice; //租车单价
public static float mile; //租车里程数
public float zuJin; //租金
public static int count;
public vehicle()
{
}
public vehicle(int cNo1,String cStyle1,float price1, float dPrice1,float mile1){
cNo=cNo1;cStyle=cStyle1;price=price1;dPrice=dPrice1;mile=mile1;
count++;
}
public float getzuJin(){
zuJin=mile*dPrice;
return zuJin;
}
static String readStr() //输入数的方法
{
String str = "";
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
}
catch (IOException e){}
return str;
}
public static void cNo() //输入每个车的车牌号
{
System.out.println("请输入车牌号:");
String str=readStr();
cNo=Integer.parseInt(str);
}
public static void dprice() //输入每个车的单价
{
System.out.println("请输入单价:");
String str=readStr();
Float input=Float.parseFloat(str);
dPrice= input;
}
public static void mile() //输入每个车的里程数
{
System.out.println("请输入里程数:");
String str=readStr();
Float input=Float.parseFloat(str);
mile=input;
}
public void DisPlay()
{
System.out.println("车牌号为:"+cNo);
}
public static void menu()
{
System.out.println("请输入车类型:1 Truck 2 Bus 3 Car 4 退出");
}
public static void display(int i) //车型判断
{
if(i==1) //货车
{
cNo();
dprice();
mile();
Truck t=new Truck();
t.tonnage();
t.DisPlay();
}
else if(i==2) // 大客车
{
cNo();
dprice();
Bus b=new Bus();
b.time();
b.seat();
b.DisPlay();
}
else //小轿车
{
cNo();
dprice();
Car c=new Car();
c.Time();
c.DisPlay();
}
}
public static void main(String args[]){
boolean bexit=false;
while(!bexit) //实现重复输入要用循环来实现
{
menu();
String str=readStr();
int input = Integer.parseInt(str);
if(input>0&&input<=3)
{
display(input);
}
else
bexit=true;
}
}
}
/*------------------------Bus类---------------------------------------------------------------------*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Bus extends vehicle {
public static float Time;
public static int Seat;
public Bus()
{
}
public Bus(int c,String S,float p, float d,float m,float time,int seat)
{ super(c,S,p,d,m);
Time=time;
Seat=seat;
}
static String readStr() //输入数的方法
{
String str = "";
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
}
catch (IOException e){}
return str;
}
public static void time() //输入时间
{
System.out.println("请输入时间:");
String str=readStr();
Float input=Float.parseFloat(str);
Time=input;
}
public static void seat() // 输入座位数
{
System.out.println("请输入座位数:");
String str=readStr();
int input=Integer.parseInt(str);
Seat=input;
}
public float getzujin()
{
return zuJin=dPrice*Seat*Time;
}
public void DisPlay()
{
super.DisPlay();
System.out.println(" 大客车租金: "+ getzujin());
}
public static void main(String args[]){
Bus b=new Bus(2345,"大众 ",30000,3,20,34,8);
b.DisPlay();
}
}
/*--------------------------------------------Car类----------------------------------------*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Car extends vehicle {
public static float Time;
public Car()
{
}
public Car(int c,String S,float p, float d,float m,float time)
{
super(c,S,p,d,m);
Time=time;
}
public float getzujin()
{
return zuJin=dPrice*Time;
}
static String readStr() //输入数的方法
{
String str = "";
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
}
catch (IOException e){}
return str;
}
public static void Time() //输入时间
{
System.out.println("请输入时间:");
String str=readStr();
Float input=Float.parseFloat(str);
Time=input;
}
public void DisPlay()
{
super.DisPlay();
System.out.println("小轿车租金为: "+getzujin());
}
public static void main(String args[]){
Car c=new Car(3444,"宝马 ",40000,3,10,34);
c.DisPlay();
}
}
/*------------------------------------------------Truck类------------------------------*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Truck extends vehicle {
public static float Tonnage;
public Truck(){
}
public Truck(int c,String S,float p, float d,float m,float tonnage)
{
super(c,S,p,d,m);
Tonnage=tonnage;
}
static String readStr() //输入数的方法
{
String str = "";
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
}
catch (IOException e){}
return str;
}
public static void tonnage() // 输入吨位数
{
System.out.println("请输入吨位数:");
String str=readStr();
Float input=Float.parseFloat(str);
Tonnage=input;
}
public float getzujin()
{
zuJin=dPrice*mile*Tonnage;
return zuJin;
}
public void DisPlay()
{
super.DisPlay();
System.out.println(" 货车租金: "+getzujin());
}
public static void main(String args[]){
Truck t=new Truck(1234,"蓝猫",50000,3,100,789f);
vehicle veh[]={new Truck(1234,"蓝猫",50000,3,100,789f),
new Car(3444,"宝马 ",40000,3,10,34),
new Bus(2345,"大众 ",30000,3,20,34,8)
};
for(int i=0;i<veh.length;i++)
{
veh[i].DisPlay();
}
}
}
写于2009年11月10日18:44:07