package com.dada;
public class Cars {
//定义子类应该有的属性
public String carName; //车名
public int id, carryMan; //编号, 载客量
public float unitPrice, carryGoods; //每日价格, 载货量
}
package com.dada;
public class Bus extends Cars{
//创建有参构造方法
public Bus(int id, String carName, float unitPrice, int carryMan ) {
this.carName = carName;
this.id = id;
this.carryMan = carryMan;
this.unitPrice = unitPrice;
}
}
package com.dada;
public class Truck extends Cars {
public Truck( int id, String carName, float unitPrice, float carryGoods ){
this.carName = carName;
this.id = id;
this.unitPrice = unitPrice;
this.carryGoods = carryGoods;
}
}
package com.dada;
public class PickUp extends Cars {
public PickUp( int id, String carName, float unitPrice, int carryMan, float carryGoods ){
this.carName = carName;
this.id = id;
this.carryMan = carryMan;
this.unitPrice = unitPrice;
this.carryGoods = carryGoods;
}
}
package com.dada;
import java.util.Scanner;
public class Users {
static Cars[] cars;
static int[] carsId;
static int days;
static float allMoney;
static int allPersons;
static float allGoods;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎进入哒哒租车系统:\n 是否要租车:1 是 0 否");
Scanner input = new Scanner(System.in);
int entry = input.nextInt();
if(entry == 1) {
//罗列车目类型
Users use = new Users();
System.out.println("您可租车的类型及价目表:\n 编号\t 车名\t 单价\t\t 容量");
use.getCars();
System.out.println("请选择要租车的数量:");
int numbers = input.nextInt();
//调用租车数量
use.carsNum(numbers);
System.out.println("输入要租的天数: ");
days = input.nextInt();
//调用打印结果
use.display();
}
}
//车目类型
public void getCars(){
Bus audi = new Bus(1, "奥迪a4", 500f, 5);
Bus mzd = new Bus(2, "马自达6", 400f, 5);
Truck shj = new Truck(3, "松花江", 400f, 4);
Truck ywk = new Truck(4, "依维柯", 1000f, 20);
PickUp mq = new PickUp(5, "猛禽", 1200f, 5, 5);
PickUp cc = new PickUp(6, "长城", 600f, 5, 3);
Cars cars[] = {audi, mzd, shj, ywk, mq, cc};
for(Cars car:cars){
if(car.getClass() == Bus.class) {
System.out.println(car.id + "\t" + car.carName + "\t" + car.unitPrice + "/天\t\t" + car.carryMan + "人");
} else if(car.getClass() == Truck.class) {
System.out.println(car.id + "\t" + car.carName + "\t" + car.unitPrice + "/天\t\t" + car.carryGoods + "吨");
} else if(car.getClass() == PickUp.class) {
System.out.println(car.id + "\t" + car.carName + "\t" + car.unitPrice + "/天\t\t" + car.carryMan + "人," + car.carryGoods + "吨");
}
}
}
//租车数量
void carsNum(int numbers) {
//选择每辆车的编号组成的数组
Scanner input = new Scanner(System.in);
//定义数组长度
carsId = new int[numbers];
for(int i = 1; i <= numbers; i++) {
System.out.println("请选择第" + i + "辆车的编号");
int carId = input.nextInt();
carsId[i - 1] = carId;
}
}
//计算并打印
void display(){
Bus audi = new Bus(1, "奥迪a4", 500f, 5);
Bus mzd = new Bus(2, "马自达6", 400f, 5);
Truck shj = new Truck(3, "松花江", 400f, 4);
Truck ywk = new Truck(4, "依维柯", 1000f, 20);
PickUp mq = new PickUp(5, "猛禽", 1200f, 5, 5);
PickUp cc = new PickUp(6, "长城", 600f, 5, 3);
Cars cars[] = {audi, mzd, shj, ywk, mq, cc};
StringBuilder person=new StringBuilder();
StringBuilder goods=new StringBuilder();
for(Cars car:cars) {
for(int id:carsId) {
if(id == car.id) {
allMoney += car.unitPrice;
if(car.getClass() == Bus.class) {
person.append(car.carName + " ");
allPersons += car.carryMan;
}
if(car.getClass() == Truck.class) {
goods.append(car.carName + " ");
allGoods += car.carryGoods;
}
if(car.getClass() == PickUp.class) {
person.append(car.carName + " ");
allPersons += car.carryMan;
goods.append(car.carName + " ");
allGoods += car.carryGoods;
}
}
}
}
allMoney = allMoney * days;
System.out.println("可载人的车有: \n" + person +"\n 可载货的车有: \n" + goods);
System.out.println("载人总数: \n" + allPersons + "\n 载货总量: \n" + allGoods);
System.out.println("总金额: \n" + allMoney);
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章