Car.java
public class Car {
int num;
String name;
double rentPrice;//租车单价
int capacityForPeople;//载人数量
double capacityForCargo;//载客数量
public int capPeople(){
return capacityForPeople;
}
public double capCargo(){
return capacityForCargo;
}
public double totalPrice(int day){
return rentPrice*day;
}
}
Audi.java
package jimsty.test.carrental;
public class Audi extends Car {
{
num = 1;
name = "奥迪A4";
rentPrice = 500.0;
capacityForPeople = 4;
capacityForCargo = 0;
}
}
Chevrolet .java
package jimsty.test.carrental;
public class Chevrolet extends Car {
{
num = 3;
name = "皮卡雪6";
rentPrice = 450.0;
capacityForPeople = 4;
capacityForCargo = 2;
}
}
Mazda.java
package jimsty.test.carrental;
public class Mazda extends Car {
{
num = 2;
name = "马自达6";
rentPrice = 400.0;
capacityForPeople = 4;
capacityForCargo = 0;
}
}
KingLong.java
package jimsty.test.carrental;
public class KingLong extends Car {
{
num = 4;
name = "金龙";
rentPrice = 800.0;
capacityForPeople = 20;
capacityForCargo = 0;
}
}
SongHuaJiang.java
package jimsty.test.carrental;
public class SongHuaJiang extends Car {
{
num = 5;
name = "松花江";
rentPrice = 400.0;
capacityForPeople = 0;
capacityForCargo = 4;
}
}
Naveco.java
package jimsty.test.carrental;
public class Naveco extends Car {
{
num = 6;
name = "依维柯";
rentPrice = 1000.0;
capacityForPeople = 0;
capacityForCargo = 20;
}
}
主程序
InterfaceUI.java
package jimsty.test.carrental;
import java.util.Scanner;
public class InterfaceUI {
int rentNum = 0;// 租车数量
int day = 0;// 租车天数
int totalPeople = 0;
double totalCargo = 0;
double totalMoney = 0;
public void run() {
// 进入嗒嗒租车系统
System.out.println("欢迎使用嗒嗒租车系统:");
Scanner in = new Scanner(System.in);
System.out.println("您是否要租车:1是 0否");
int b = in.nextInt();
if (b == 0) {
in.close();
return;
}
// 可租车属性显示
System.out.println("你可租车的类型有:\n" + "序号 汽车名称 租金 容量\n"
+ "1. 奥迪A4 500元/天 载人:4人\n" + "2. 马自达6 400元/天 载人:4人 \n"
+ "3. 皮卡雪6 450元/天 载人:4人 载货:2吨\n" + "4. 金龙 800元/天 载人:20人\n"
+ "5. 松花江 400元/天 载货:4吨\n" + "6. 依维柯 1000元/天 载货:20吨\n");
// 租车数量类型判断
System.out.println("请输入您要租汽车的数量:");
rentNum = in.nextInt();
if (rentNum <= 0) {
System.out.println("输入有误,请输入您要租汽车的数量:");
}
int a[] = new int[rentNum];
for (int i = 0; i < rentNum; i++) {
System.out.println("请输入第" + (i + 1) + "辆车的序号:");
a[i] = in.nextInt();
}
// 租车天数
System.out.println("请输入租车天数:");
day = in.nextInt();
in.close();
// 输出用户账单
System.out.println("您的账单:");
String displayCarForPeople = "";// 显示账单中的载人车型
String displayCarForCargo = "";// 显示账单中的载货车型
for (int i = 0; i < rentNum; i++) {
switch (a[i]) {
case 1: {
Car car = new Audi();
totalPeople = totalPeople + car.capPeople();
displayCarForPeople = displayCarForPeople + car.name + " ";
totalMoney = totalMoney + car.totalPrice(day);
break;
}
case 2: {
Car car = new Mazda();
totalPeople = totalPeople + car.capPeople();
displayCarForPeople = displayCarForPeople + car.name + " ";
totalMoney = totalMoney + car.totalPrice(day);
break;
}
case 3: {
Car car = new Chevrolet();
totalPeople = totalPeople + car.capPeople();
totalCargo = totalCargo + car.capCargo();
displayCarForPeople = displayCarForPeople + car.name + " ";
displayCarForCargo = displayCarForCargo + car.name + " ";
totalMoney = totalMoney + car.totalPrice(day);
break;
}
case 4: {
Car car = new KingLong();
totalPeople = totalPeople + car.capPeople();
displayCarForPeople = displayCarForPeople + car.name + " ";
totalMoney = totalMoney + car.totalPrice(day);
break;
}
case 5: {
Car car = new SongHuaJiang();
totalCargo = totalCargo + car.capCargo();
displayCarForCargo = displayCarForCargo + car.name + " ";
totalMoney = totalMoney + car.totalPrice(day);
break;
}
case 6: {
Car car = new Naveco();
totalCargo = totalCargo + car.capCargo();
displayCarForCargo = displayCarForCargo + car.name + " ";
totalMoney = totalMoney + car.totalPrice(day);
break;
}
}
}
System.out.println("***可载人的车有:\n" + displayCarForPeople + "共载人:" + totalPeople + "人");
System.out.println("***载货的车有:\n" + displayCarForCargo + "" + totalCargo + "吨");
System.out.println("***租车总价格:" + totalMoney + "元");
}
public static void main(String[] args) {
InterfaceUI ui = new InterfaceUI();
ui.run();
}
}
点击查看更多内容
29人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦