//(1)汽车类
package com.MuTong.DadaUber;
public class AllCar {
private int num;
private String name;
private int money;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
//(2)轿车类
package com.MuTong.DadaUber;
public class PassengerCar extends AllCar {
private int passenger;
public PassengerCar(int num,String name,int money,int passenger){
setNum(num);
setName(name);
setMoney(money);
setPassenger(passenger);
}
public int getPassenger() {
return passenger;
}
public void setPassenger(int passenger) {
this.passenger = passenger;
}
}
//(3)皮卡类
package com.MuTong.DadaUber;
public class PickupCar extends AllCar {
private int passenger;
private int goods;
public PickupCar(int num,String name,int money,int passenger,int goods){
setNum(num);
setName(name);
setMoney(money);
setPassenger(passenger);
setGoods(goods);
}
public int getPassenger() {
return passenger;
}
public void setPassenger(int passenger) {
this.passenger = passenger;
}
public int getGoods() {
return goods;
}
public void setGoods(int goods) {
this.goods = goods;
}
}
(4)货车类
package com.MuTong.DadaUber;
public class carryCar extends AllCar{
private int goods;
public carryCar(int num,String name,int money,int goods){
setNum(num);
setName(name);
setMoney(money);
setGoods(goods);
}
public int getGoods() {
return goods;
}
public void setGoods(int goods) {
this.goods = goods;
}
}
(5)主干类
package com.MuTong.DadaUber;
import java.util.*;
public class initialTest {
public void circle() {
// TODO Auto-generated method stub
System.out.print("Welcome to Dada Uber!\n您是否需要租借车辆?(yes-1 no-2)");
Scanner sca=new Scanner(System.in);
int s=sca.nextInt();
if(s==1)
cal();
else{
if(s==2)
System.out.println("Thank you to come to Dada Uber!\n\n");
else
System.out.println("您输入有误。\n\n");
}
circle();
}
public void cal(){
AllCar cars[]={new PassengerCar(1,"玛莎拉蒂",3000,4),
new PassengerCar(2,"二手奥拓",200,4),
new PickupCar(3,"皮卡雪6",450,3,1),
new carryCar(4,"亚星载货",800,3)};
System.out.println("编号\t"+"汽车名称\t"+"租金\t"+"容量");
for(AllCar car:cars){
if(car instanceof PassengerCar){ //若它是客车类型的实例,则打印出客车应该有的属性值,这里注意强转,否则不会出现passengerCar类的passenger属性
System.out.println("编号:"+car.getNum()+"\t汽车名称:"+car.getName()+"\t租金:"+car.getMoney()+"\t载人数:"+((PassengerCar)car).getPassenger());
} //(PassengerCar)car 代表将AllCar类型的引用car强制转换成括号里的PassengerCar类型
if(car instanceof PickupCar){
System.out.println("编号:"+car.getNum()+"\t汽车名称:"+car.getName()+"\t租金:"+car.getMoney()+"\t载人数:"+((PickupCar)car).getPassenger()+"\t载货数:"+((PickupCar)car).getGoods());
}
if(car instanceof carryCar){
System.out.println("编号:"+car.getNum()+"\t汽车名称:"+car.getName()+"\t租金:"+car.getMoney()+"\t载货数:"+((carryCar)car).getGoods());
}
}
System.out.println("请输入您要租车的数量:");
Scanner c=new Scanner(System.in);
int n=c.nextInt();
//List <AllCar>carlist=new ArrayList<AllCar>();//List 集合框架!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//int k=0;
List <AllCar>passengercarlist=new ArrayList<AllCar>();//用来储存可载人的车辆
List <AllCar>carrycarlist=new ArrayList<AllCar>();//用来储存可载货的车辆
int totalpassenger=0;
int totalgoods=0;
int totalmoney=0;
for(int i=0;i<n;i++){
System.out.println("请输入选择的第"+(i+1)+"辆车的编号:");
int num=c.nextInt();//记录用户选择的车辆的编号
//carlist.add(cars[num-1]);
if(cars[num-1] instanceof PassengerCar){
totalpassenger += ((PassengerCar)cars[num-1]).getPassenger();
totalmoney += cars[num-1].getMoney();
passengercarlist.add(cars[num-1]);
}
if(cars[num-1] instanceof PickupCar){
totalpassenger += ((PickupCar)cars[num-1]).getPassenger();
totalgoods += ((PickupCar)cars[num-1]).getGoods();
totalmoney += cars[num-1].getMoney();
passengercarlist.add(cars[num-1]);
carrycarlist.add(cars[num-1]);
}
if(cars[num-1] instanceof carryCar){
totalgoods += ((carryCar)cars[num-1]).getGoods();
totalmoney += cars[num-1].getMoney();
carrycarlist.add(cars[num-1]);
}
}
System.out.println("请输入租用天数:");
int day=c.nextInt();
totalmoney*=day;
System.out.println("******************租用的可载人车有*********************");
for(AllCar car:passengercarlist)
System.out.println(car.getName());
System.out.println("******************租用的可载货车有*********************");
for(AllCar car:carrycarlist)
System.out.println(car.getName());
System .out.print("*********************总载人数***********************\n"+totalpassenger+"\n");
System .out.print("*********************总载货量***********************\n"+totalgoods+"\n");
System .out.print("*********************总租金***********************\n"+totalmoney+"\n\n\n");
}
}
(6)测试类
package com.MuTong.DadaUber;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
initialTest t=new initialTest();
t.circle();
}
}