裥簞點
2016-05-03 23:26:55浏览 5519
package com.imooc2;
public class Car {
double rent;
String name;
double cargoCapacity;
double peopleCapacity;
public String getName(){
return name;
}
public double getRent(){
return rent;
}
}
package com.imooc2;
public class passengerCar extends Car {
private double peopleCapacity;
public passengerCar(String name, double rent, double peopleCapacity){
this.name=name;
this.rent=rent;
this.peopleCapacity=peopleCapacity;
}
public double getPeopleCapacity(){
return peopleCapacity;
}
public void setPeopleCapacity(double peopleCapacity){
this.peopleCapacity=peopleCapacity;
}
}
package com.imooc2;
public class Pickup extends Car {
private double cargoCapacity;
private double peopleCapacity;
public Pickup(String name, double rent, double cargoCapacity,double peopleCapacity){
this.name=name;
this.rent=rent;
this.cargoCapacity=cargoCapacity;
this.peopleCapacity=peopleCapacity;
}
public double getCargoCapacity(){
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity){
this.cargoCapacity=cargoCapacity;
}
public double getPeopleCapacity(){
return peopleCapacity;
}
public void setPeopleCapacity(double peopleCapacity){
this.peopleCapacity=peopleCapacity;
}
}
package com.imooc2;
public class Trunk extends Car {
private double cargoCapacity;
public Trunk(String name, double rent, double cargoCapacity){
this.name=name;
this.rent=rent;
this.cargoCapacity=cargoCapacity;
}
public double getCargoCapacity(){
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity){
this.cargoCapacity=cargoCapacity;
}
}
package com.imooc2;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car[] carsForRent={new passengerCar("奥迪A4",500,4),new passengerCar("马自达6",400,4),new Pickup("皮卡雪6",450,4,2),new passengerCar("金龙",800,20),new Trunk("松花江",400,4),new Trunk("依维柯",1000,20)};
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否需要租车:1是 0否");
Scanner scan=new Scanner(System.in);
String input=scan.next();
if(input.equals("1")){
System.out.println("你可租车的类型机器价目表:");
System.out.println("序号\t汽车名称\t租金\t\t容量");
int i=0;
for(Car currentCar:carsForRent){
if(currentCar instanceof passengerCar){
System.out.println(""+i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()+"元/天\t"+((passengerCar)currentCar).getPeopleCapacity()+"人");
}
if(currentCar instanceof Trunk){
System.out.println(""+i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()+"元/天\t"+((Trunk)currentCar).getCargoCapacity()+ "吨");
}
if(currentCar instanceof Pickup){
System.out.println(""+i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()+"元/天\t"+((Pickup)currentCar).getPeopleCapacity()+"人"+((Pickup)currentCar).getCargoCapacity()+"吨");
}
i++;
}
System.out.println("请选择所租车序号:");
Scanner num=new Scanner(System.in);
int nums=num.nextInt();
System.out.println("请选择租车天数:");
Scanner day=new Scanner(System.in);
int days=num.nextInt();
double count;
count=(carsForRent[nums].getRent())*days;
System.out.println("应支付租金:"+count);
}
}
}