父类:Car.java
package www.dadacar;
public class Car {
protected String carName;
protected double carRent;
//有参构造方法,赋值车名称和租金
public Car(String carname,double carrent){
this.carName = carname;
this.carRent = carrent;
}
//利用右击-Generate,自动生成getter和setter函数
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public double getCarRent() {
return carRent;
}
public void setCarRent(double carRent) {
this.carRent = carRent;
}
}子类:Passenger.java
package www.dadacar;
class PassengerCar extends Car {
private int peopleCapacity;
//构造方法,赋值载客量
public PassengerCar(String carname,double carrent,int peoplecapacity){
super(carname,carrent); //因为父类是有参构造方法,要用super调用
this.peopleCapacity=peoplecapacity;
}
public int getPeopleCapacity() {
return peopleCapacity;
}
public void setPeopleCapacity(int peopleCapacity) {
this.peopleCapacity = peopleCapacity;
}
}子类:Truck.java
package www.dadacar;
public class Truck extends Car {
private double cargoCapacity;
//构造方法,赋值载货量
public Truck(String carname,double carrent,double cargocapacity){
super(carname,carrent);
this.cargoCapacity = cargocapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
}子类:PickUp.java
package www.dadacar;
public class PickUp extends Car {
private int peopleCapacity;
private double cargoCapacity;
//构造方法,赋值载客量和载货量
public PickUp(String carname,double carrent,int peoplecapacity,double cargocapacity){
super(carname,carrent);
this.cargoCapacity=cargocapacity;
this.peopleCapacity=peoplecapacity;
}
public int getPeopleCapacity() {
return peopleCapacity;
}
public void setPeopleCapacity(int peopleCapacity) {
this.peopleCapacity = peopleCapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
}主方法:TestSoftware.java
package www.dadacar;
import java.util.Scanner;
import java.util.Arrays;
public class TestSoftware {
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车:1是,0否");
//利用Scanner获取输入信息
Scanner input = new Scanner(System.in); //定义一次即可,后面均可引用input
int isOrno = input.nextInt();
if(isOrno == 1){
//创建一个包含六个车对象的数组
Car[] cars = {new PassengerCar("奥迪A4",500,4),
new PassengerCar("马自达6",400,4),
new PickUp("皮卡雪6",450,4,2),
new PassengerCar("金龙",800,20),
new Truck("松花江",400,4),
new Truck("依维柯",1000,20)
};
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号"+" "+"汽车名称"+" "+"租金"+"\t"+"\t"+"容量");
int i = 1;
//利用foreach遍历数组cars,依次进行显示
for(Car carCurrent:cars){
//判断当前数组对象是否为载人汽车
if(carCurrent instanceof PassengerCar){
System.out.println(""+i +".\t"+
carCurrent.carName+"\t"+
carCurrent.carRent+"元/天"+"\t"+
"载人:"+((PassengerCar) carCurrent).getPeopleCapacity()+"人");
}
//判断当前数组对象是否为货车
if(carCurrent instanceof Truck){
System.out.println(i + ".\t"+
carCurrent.carName+"\t"+
carCurrent.carRent+"元/天"+"\t"+
"载货:"+((Truck) carCurrent).getCargoCapacity()+"吨");
}
//判断当前数组对象是否为皮卡车
if(carCurrent instanceof PickUp){
System.out.println(i + ".\t"+
carCurrent.carName+"\t"+
carCurrent.carRent+"元/天"+"\t"+
"载人:"+((PickUp) carCurrent).getPeopleCapacity()+"人 "+
"载货:"+((PickUp) carCurrent).getCargoCapacity()+"吨");
}
i ++;
}
System.out.println("请输入您要租汽车的数量:");
//获取租车数量
int carNums = input.nextInt();
//创建一个数组,长度为租车数量,用于放置租车序号
int[] carNum = new int[carNums];
//遍历租车数组,并存储汽车序号
for(int j = 1;j <= carNums; j ++ ){
System.out.println("请输入第"+j+"辆车的序号:");
carNum[j-1]= input.nextInt();
}
//System.out.println(Arrays.toString(carNum)); //输出租车序号数组,检验代码是否正确
System.out.println("请输入租车天数:");
//获取租车天数
int carDays = input.nextInt();
System.out.println("您的账单为:");
System.out.println("***可载人的车有:");
//定义一个变量存储总租金,注意变量的类型
double sumRent = 0;
//定义一个变量存储总载人数
int numPeople = 0;
//定义一个变量存储总载货量
double numCargo = 0;
//遍历所有选择的租车序号数组,输出租车汽车名称,计算总载人数、总租金和总载货量
for(i = 0; i<=carNums-1; i++) {
//判断当前车是否为载人汽车
if(cars[carNum[i]-1] instanceof PassengerCar){
//将当前cars对象的类向下转型,便于获取子类中定义的函数getPeopleCapacity()
PassengerCar carCurrent = (PassengerCar) cars[carNum[i]-1];
System.out.print(carCurrent.carName + "\t");
numPeople = numPeople + carCurrent.getPeopleCapacity();
sumRent += carCurrent.carRent;
}
//判断当前车是否为皮卡车
if(cars[carNum[i]-1] instanceof PickUp){
//将当前cars对象的类向下转型,便于获取子类中定义的函数getPeopleCapacity()
PickUp carCurrent = (PickUp) cars[carNum[i]-1];
System.out.print(carCurrent.carName + "\t");
numPeople = numPeople + carCurrent.getPeopleCapacity();
sumRent += carCurrent.carRent;
}
}
System.out.println("共载人:"+numPeople+"人");
System.out.println("***可载货的车有:");
for(i = 0; i<=carNums-1; i++) {
if(cars[carNum[i]-1] instanceof Truck){
Truck carCurrent = (Truck) cars[carNum[i]-1];
System.out.print(carCurrent.carName + "\t");
numCargo = numCargo + carCurrent.getCargoCapacity();
sumRent += carCurrent.carRent;
}
if(cars[carNum[i]-1] instanceof PickUp){
PickUp carCurrent = (PickUp) cars[carNum[i]-1];
System.out.print(carCurrent.carName + "\t");
numCargo = numCargo + carCurrent.getCargoCapacity();
sumRent += carCurrent.carRent;
}
}
System.out.println("共载货:"+numCargo+"吨");
System.out.println("***租车总价格为"+(sumRent*carDays));
}else{
System.out.println("期待您选择答答租车系统");
}
}
}点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦