创建Car父类,PassengerCar、Pickup、Truck子类
父类Car:
package com.imooc.exercise;
//Car汽车类,父类
public class Car {
//描述汽车的特征
private String name;//车的名称
private int space;//载客数量
private double weight;//车的载重量
private int dailyRent;//车的日租金
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSpace() {
return space;
}
public void setSpace(int space) {
this.space = space;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public int getDailyRent() {
return dailyRent;
}
public void setDailyRent(int dailyRent) {
this.dailyRent = dailyRent;
}
}
子类PassengerCar小汽车:
package com.imooc.exercise;
//Passenger载客车
public class PassengerCar extends Car{
public PassengerCar(String name,int space,int dailyRent){
super.setName(name);
super.setSpace(space);
super.setDailyRent(dailyRent);
}
}
子类Pickup皮卡:
package com.imooc.exercise;
//Pickup皮卡
public class Pickup extends Car{
public Pickup(String name,int space,double weight,int dailyRent){
super.setName(name);
super.setSpace(space);
super.setWeight(weight);
super.setDailyRent(dailyRent);
}
}
子类Truck卡车:
package com.imooc.exercise;
//Truck载货车
public class Truck extends Car{
public Truck(String name,double weight,int dailyRent){
super.setName(name);
super.setWeight(weight);
super.setDailyRent(dailyRent);
}
}
测试类Test:
package com.imooc.exercise;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Test {
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("#0.0");
Car[] cars = {new PassengerCar("奥迪A4",4,500),
new PassengerCar("马自达6",4,400),
new Pickup("皮卡雪6",4,2,450),
new PassengerCar("金龙",20,800),
new Truck("松花江",4,400),
new Truck("依维柯",20,1000)
};
System.out.println("欢迎使用答答租车系统!");
System.out.println("您是否要租车:1是 0否");
Scanner input = new Scanner(System.in);
int is = input.nextInt();
//输入1,列出可租车型及详细信息,挑选车型
if(is == 1){
int num = 1;//序号
double totalMoney = 0;//租金总额
int rentCarDays = 0;//租用天数
double totalDailyMoney = 0;//每日租金总额
int totalSpace = 0;//总载客量
int totalWeight = 0;//总载重量
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t"+"汽车名称\t"+"租金\t"+"容量");
for(Car viewCars:cars){
if(viewCars instanceof PassengerCar){
System.out.println(num+".\t"+viewCars.getName()+"\t"
+viewCars.getDailyRent()+"\t 载人:"
+viewCars.getSpace()
);
num++;
}else if(viewCars instanceof Pickup){
System.out.println(num+".\t"+viewCars.getName()
+"\t"+viewCars.getDailyRent()+"元/天"
+"\t 载人:"+viewCars.getSpace()+" 载货:"
+viewCars.getWeight()
);
num++;
}else if(viewCars instanceof Truck){
System.out.println(num+".\t"+viewCars.getName()+"\t"
+viewCars.getDailyRent()+"元/天"
+" 载货:"+viewCars.getWeight()
);
num++;
}
}
System.out.println("请输入您要租汽车的数量(最大租车数为"+cars.length+"):");
Scanner input2 = new Scanner(System.in);
int carNeeded = input2.nextInt();//需要租车的数量
if(carNeeded>0&&carNeeded<=num){
Car[] needCars = new Car[carNeeded];
Scanner input3 = new Scanner(System.in);
for(int i=1;i<=carNeeded;i++){
System.out.println("请输入第"+i+"辆车的序号:");
int currentCarNum = input3.nextInt();
needCars[i-1] = cars[currentCarNum-1];
totalDailyMoney += needCars[i-1].getDailyRent();//所有车辆每日资金总额
totalSpace += needCars[i-1].getSpace();//总载客量
totalWeight += needCars[i-1].getWeight();//总载重量
}
System.out.println("请输入租车天数:");
Scanner input4 = new Scanner(System.in);
rentCarDays = input4.nextInt();
totalMoney = rentCarDays*totalDailyMoney;//总金额
System.out.println("您的账单:\n***可载人的车有:");
//载客账单
for(int j=0;j<needCars.length;j++){
if((needCars[j] instanceof PassengerCar)||(needCars[j] instanceof Pickup)){
System.out.print(needCars[j].getName()+"\t");
}
}
System.out.println("共载人:"+totalSpace+"人");
System.out.println("\n***可载货的车有:");
//载货账单
for(int j=0;j<needCars.length;j++){
if((needCars[j] instanceof Pickup)||(needCars[j] instanceof Truck)){
System.out.print(needCars[j].getName()+"\t");
}
}
System.out.println("共载货:"+df.format(totalWeight)+"吨");
System.out.println("***租车总价格:"+df.format(totalMoney)+"元");
}else{
System.out.println("输入错误,请修改租车数量!");
}
}else{
System.out.println("感谢您访问答答租车系统!");
}
}
}
运行结果:
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦