qq_狼狈_0
2020-02-12 18:02
package com.imooc;
import java.util.Scanner;
public class Initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用租车系统\n您是否要租车?1-是,0-否");
Scanner input=new Scanner(System.in);
int isRent=input.nextInt();
double allrent=0;
double allloadage=0;
int allseats=0;
if(isRent==1) {
System.out.println("您可租车的类型和价目表:");
System.out.println("序号 汽车名称 租金 容量");
Car[] cars= {
new Sedan("哈弗",200,5),
new Sedan("雪佛兰",240,5),
new PickUp("本田",400,3,800.5),
new PickUp("丰田",450,3,1000.5),
new Truck("东风",600,3000),
new Truck("五菱",650,3500)
};
for (int i=0;i<cars.length;i++)
{
int index=i+1;
if(cars[i] instanceof Sedan)
{
Sedan car=(Sedan)cars[i];//强制类型转换
System.out.println(index+" "+car.CarName+" "+
car.Rent+"元/天"+" "+"载人: "+car.getSeats()+"人 ");
}
else if(cars[i] instanceof Truck)
{
Truck car=(Truck)cars[i];
System.out.println(index+" "+car.CarName+" "+
car.Rent+"元/天"+" "+"载货:"+car.getLoadage()+"kg");
}
else
{
PickUp car=(PickUp)cars[i];
System.out.println(index+" "+car.CarName+" "+
car.Rent+"元/天"+" "+"载人: "+car.getSeats()+"人 "+" 载货:"+car.getLoadage()+"kg");
}
}
System.out.println("请输入您想要的租赁的车辆数目:");
int carsnum=input.nextInt();
Car[] chooseCar=new Car[carsnum];
for(int i=0;i<carsnum;i++) {
int num=i+1;
System.out.println("请选择第"+num+"辆车:");
int whichcar=input.nextInt();
if (whichcar<=cars.length&& whichcar>0)
{
chooseCar[i]=cars[whichcar-1];
}
else {
System.out.println("请输入正确的车辆序号.\n");
i--;
continue;
}
}
System.out.println("您一共租了"+carsnum+"辆车");
System.out.println("分别是:");
for(int i=0;i<carsnum;i++)
{
System.out.println((i+1)+":"+chooseCar[i].getCarName());
}
for(int i=0;i<carsnum;i++)
{
if(chooseCar[i] instanceof Sedan)
{
Sedan car=(Sedan)chooseCar[i];
allrent+=car.getRent();
allseats+=car.getSeats();
}
else if(chooseCar[i] instanceof Truck)
{
Truck car=(Truck)chooseCar[i];
allrent+=car.getRent();
allloadage+=car.getLoadage();
}
else
{
PickUp car =(PickUp)chooseCar[i];
allrent+=car.getRent();
allseats+=car.getSeats();
allloadage+=car.getLoadage();
}
}
System.out.println("一共能载:"+allloadage+"kg货物,能载"+allseats+"人.");
System.out.println("一共需要:"+allrent+"元/天.\n");
System.out.println("请输入租赁天数:");
int days=input.nextInt();
System.out.println("您一共租赁"+days+"天,租金是:"+allrent*days+"元.");
}
}
}
我也懵逼了
Car[] cars= {
new Sedan("哈弗",200,5),
new Sedan("雪佛兰",240,5),
new PickUp("本田",400,3,800.5),
new PickUp("丰田",450,3,1000.5),
new Truck("东风",600,3000),
new Truck("五菱",650,3500)
};
朋友 能不能解释下,这段是什么意思,前面有那一课有教过吗
package com.imooc;
/*
* 皮卡,既能载货又能载人。
*/
public class PickUp extends Car {
protected int seats;
protected double Loadage;
public PickUp(String CarName,double Rent,int seats,double Loadage) {
super(CarName,Rent);
this.seats=seats;
this.Loadage=Loadage;
}
public int getSeats() {
return this.seats;
}
public double getLoadage() {
return this.Loadage;
}
}
package com.imooc;
/*
* 货车
*/
public class Truck extends Car {
protected double Loadage;
public Truck(String CarName,double Rent,double Loadage) {
super(CarName,Rent);
this.Loadage=Loadage;
}
public double getLoadage() {
return this.Loadage;
}
}
package com.imooc;
/*
* 载人汽车
*/
public class Sedan extends Car {
protected int seats;
public Sedan(String CarName,double Rent,int seats) {
super(CarName,Rent);
this.seats=seats;
}
public int getSeats() {
return this.seats;
}
}
package com.imooc;
public class Car {
protected String CarName;
protected double Rent;
public Car(String CarName,double Rent) {
this.CarName=CarName;
this.Rent=Rent;
}
public String getCarName() {
return this.CarName;
}
public Double getRent() {
return this.Rent;
}
}
Java入门第二季 升级版
530562 学习 · 6091 问题
相似问题