实现类:
import java.util.Scanner;
import com.car.Car;
import com.car.Huoche;
import com.car.Keche;
import com.car.Pika;
public class Go {
public static void main(String[] args) {
Car[] cars=new Car[]{new Keche(1,"奥迪A4",500,4),new Keche(2,"马自达6", 400, 4),new Pika(3,"皮卡雪6", 450, 4, 2),new Keche(4,"金龙", 800, 20),new Huoche(5,"松花江", 400, 4),new Huoche(6,"依维柯", 1000, 20)};
Scanner scanner=new Scanner(System.in);
System.out.println("请问你是否要租车:");
System.out.println("1:是 2:否");
if (scanner.nextInt()==2) {
System.out.println("再见");
}
System.out.println(" 序号\t车名\t租金\t容量");
for(Car car:cars){
if(car instanceof Keche){
System.out.println(" "+car.getNumber()+"\t"+car.getName()+"\t"+car.getMoney()+"\t载"+((Keche)car).getRen()+"人");
}
if(car instanceof Huoche){
System.out.println(" "+car.getNumber()+"\t"+car.getName()+"\t"+car.getMoney()+"\t载"+((Huoche)car).getDun()+"吨");
}
if(car instanceof Pika){
System.out.println(" "+car.getNumber()+"\t"+car.getName()+"\t"+car.getMoney()+"\t载"+((Pika)car).getDun()+"吨,载"+((Pika)car).getRen()+"人");
}
}
System.out.println("请问租车的数量:");
int count=scanner.nextInt();//租车数量
String [] rennames=new String[count];//载人名字
String [] huonames=new String[count];//载货名字
int sum=0;//总价
int rensum=0;//总载人数
int dunsum=0;//总载重数
for(int i=-0;i<count;i++){
System.out.println("请输入第"+(i+1)+"辆车的序号:");
int num=scanner.nextInt();
if(6<num||num<1){
System.out.println("没有该序号的车辆!请重新输入序号!");
i--;
continue;
}
if(cars[num-1] instanceof Keche){
rennames[i]=cars[num-1].getName();
sum+=cars[num-1].getMoney();
rensum+=((Keche)cars[num-1]).getRen();
}
if(cars[num-1] instanceof Huoche){
huonames[i]=cars[num-1].getName();
sum+=cars[num-1].getMoney();
dunsum+=((Huoche)cars[num-1]).getDun();
}
if(cars[num-1] instanceof Pika){
rennames[i]=cars[num-1].getName();
huonames[i]=cars[num-1].getName();
sum+=cars[num-1].getMoney();
rensum+=((Pika)cars[num-1]).getRen();
dunsum+=((Pika)cars[num-1]).getDun();
}
}
System.out.println("请问租车天数:");
sum*=scanner.nextInt();
System.out.println("你的账单:");
System.out.println("可载人的车有:");
for(String name:rennames){
if(name==null){
continue;
}
System.out.print(name+" ");
}
System.out.print(" 共载人:"+rensum+"人");
System.out.println("\n可载货的车有:");
for(String name:huonames){
if(name==null){
continue;
}
System.out.print(name+" ");
}
System.out.print(" 共载货:"+dunsum+"吨");
System.out.println("\n***租车总价格:"+sum+"元");
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章