package com.jiay.dadacar;
public class Car {
public String name;//车名
public int rental;//租金
public String getName() {
return name;
}
public int getRental() {
return rental;
}
}
package com.jiay.dadacar;
public class PassengerCar extends Car {
public int zaiRen;
public PassengerCar(String name, int rental, int zaiRen) {
this.name = name;
this.rental = rental;
this.zaiRen = zaiRen;
}
public int getZaiRen() {
return zaiRen;
}
}
package com.jiay.dadacar;
public class ThingCar extends Car {
public int zaiHuo;
public ThingCar(String name, int rental, int zaiHuo) {
this.name = name;
this.rental = rental;
this.zaiHuo = zaiHuo;
}
public int getZaiHuo() {
return zaiHuo;
}
}
package com.jiay.dadacar;
public class PikCar extends Car {
public int zaiRen;
public int zaiHuo;
public PikCar(String name, int rental, int zaiRen, int zaiHuo) {
this.name = name;
this.rental = rental;
this.zaiRen = zaiRen;
this.zaiHuo = zaiHuo;
}
public int getZaiRen() {
return zaiRen;
}
public int getZaiHuo() {
return zaiHuo;
}
}
package com.jiay.dadacar;
import java.util.Scanner;
public class CarSystem {
//可供租的车的信息
public static Car[] carsForRent = { new PassengerCar("奥迪A4", 500, 4), new PassengerCar("马自达6", 400, 4),
new PikCar("皮卡雪6", 450, 4, 2), new PassengerCar("金龙", 800, 20), new ThingCar("松花江", 400, 4),
new ThingCar("依维柯", 1000, 20) };
//用于接收控制台输入
static Scanner scanner = new Scanner(System.in);
static StringBuffer psb=new StringBuffer(" ");//记录载人车名称
static StringBuffer tsb=new StringBuffer(" ");//记录载货车名称
static int allRen=0;//记录总人数
static int allHuo=0;//记录总吨数
static int allRental=0;//记录总租金
//判断是否需要租车
public static boolean isRent() {
String isRent = scanner.next();
if (isRent.equals("1")) {
return true;
}else {
return false;
}
}
//显示出租的车的具体信息
public static void displayCars() {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t\t租金\t\t容量");
//下面输出车辆信息
int i=1;
for (Car car : carsForRent) {
if (car instanceof PassengerCar) {
System.out.println(""+i+"\t"+car.getName()+"\t\t"
+car.getRental()+"元/天\t\t"+"载人:"+((PassengerCar)car).getZaiRen()+"人");
}
if (car instanceof ThingCar) {
System.out.println(""+i+"\t"+car.getName()+"\t\t"
+car.getRental()+"元/天\t\t"+"载货:"+((ThingCar)car).getZaiHuo()+"吨");
}
if (car instanceof PikCar) {
System.out.println(""+i+"\t"+car.getName()+"\t\t"
+car.getRental()+"元/天\t\t"+"载人:"+((PikCar)car).getZaiRen()+"人"+" 载货:"+((PikCar)car).getZaiHuo()+"吨");
}
i++;
}
}
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统!");
System.out.println("您是否要租车:1是 0否");
if (isRent()) {
displayCars();
System.out.println("请输入您要租汽车的数量:");
int rentNum = scanner.nextInt();
Car currentCar;//记录当前租用的汽车
for (int i = 0; i < rentNum; i++) {
System.out.println("请输入第"+(i+1)+"辆车的序号:");
currentCar =carsForRent[scanner.nextInt()-1];
allRental+=currentCar.getRental();
if (currentCar instanceof PassengerCar ) {
psb.append(currentCar.getName()).append(" ");
allRen+=((PassengerCar)currentCar).getZaiRen();
}
if (currentCar instanceof ThingCar) {
tsb.append(currentCar.getName()).append(" ");
allHuo+=((ThingCar)currentCar).getZaiHuo();
}
if (currentCar instanceof PikCar) {
psb.append(currentCar.getName()).append(" ");
tsb.append(currentCar.getName()).append(" ");
allRen+=((PikCar)currentCar).getZaiRen();
allHuo+=((PikCar)currentCar).getZaiHuo();
}
}
System.out.println("请输入租车天数:");
int rentDays = scanner.nextInt();
allRental*=rentDays;
scanner.close();
System.out.println("您的账单:");
if (allRen!=0) {
System.out.println("***可载人的车有:");
System.out.println(psb+"共载人:"+allRen+"人");
}else {
System.out.println("您没有租用载人的车");
}
if (allHuo!=0) {
System.out.println("***可载货的车有:");
System.out.println(tsb+"共载货:"+allHuo+"吨");
}else {
System.out.println("您没有租用载货的车");
}
System.out.println("***租车总价格:"+allRental);
}else {
System.out.println("Bye");
}
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦