为了账号安全,请及时绑定邮箱和手机立即绑定

Java入门第二季 6-2 答答租车系统

标签:
Java

-------------------------------------------------

public class Car {

public String Name = "";

public int Rent = 0;

public int FreightVolume = 0;//载物

public int CarryinCapacity = 0;//载人

}

--------------------------------------------

public class PassengerCar extends Car{  

public PassengerCar(String name,int rent,int ca) {

Name = name;

Rent = rent;

CarryinCapacity = ca;

}

}

-------------------------------------

public class Truck extends Car{

public Truck(String name,int rent,int fr) {

Name = name;

Rent = rent;

FreightVolume = fr;

}

}

-----------------------------------------------------

public class PickUp extends Car {

public PickUp(String name,int rent,int ca,int fr) {

Name = name;

Rent = rent;

CarryinCapacity = ca;

FreightVolume = fr;

}

}

--------------------------------------------------------

import java.util.Scanner;

public class Initail {


public static void main(String[] args) {


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("您是否要租车:1是 0否");

Scanner scan = new Scanner(System.in);

String input = scan.next();

if(input.equals("1")) {

System.out.println("您可以租车的类型及价目表:");

System.out.println("序号\t汽车\t租金\t容量");

int a = 1;//汽车的序号

for(Car car : cars) {

if(car instanceof PassengerCar) {

System.out.println("" + a + "\t" + car.Name + "\t" + car.Rent + "元/天" + "\t" + "载人:" + car.CarryinCapacity + "人");

}

if(car instanceof Truck) {

System.out.println("" + a + "\t" + car.Name + "\t" + car.Rent + "元/天" + "\t" + "载货:" + car.FreightVolume + "吨");

}

if(car instanceof PickUp) {

System.out.println("" + a + "\t" + car.Name + "\t" + car.Rent + "元/天" + "\t" + "载人:" + car.CarryinCapacity + "人" + " " + "载货:" + car.FreightVolume + "吨");

}

a++;

}

System.out.println("请输入您要租汽车的数量:");

int num = scan.nextInt();

int[] count = new int[10];

for(int i = 0;i < num;i++) {

int b = i + 1;

System.out.println("请输入第" + b + "辆车的序号");

count[i] = scan.nextInt() - 1;//序号是从1开始,而数组是从0开始

}

System.out.println("请输入租车天数:");

int day = scan.nextInt();

System.out.println("您的账单:");

System.out.println("***可载人的车有:");

int allcc = 0;//共载人

int allfv = 0;//共载货

int price = 0;//总价格

for(int i = 0;i < num;i++) {

if(cars[count[i]] instanceof PassengerCar || cars[count[i]] instanceof PickUp) {

//count[i]保存的是车的序号,cars[count[i]]就是第i+1辆车

System.out.print("\t" + cars[count[i]].Name);

allcc = cars[count[i]].CarryinCapacity + allcc;

price = cars[count[i]].Rent + price;

}

}

System.out.println("\t" + "共载人:" + allcc + "人");

System.out.println("***载货的车有:");

for(int i = 0;i < num;i++) {

if(cars[count[i]] instanceof Truck || cars[count[i]] instanceof PickUp) {

System.out.print("\t" + cars[count[i]].Name);

allfv = cars[count[i]].FreightVolume + allfv;

if(cars[count[i]] instanceof Truck) {//如果是皮卡,则价格已经在前面统计了

price = cars[count[i]].Rent + price;

}

}

}

System.out.println("\t" + "共载货:" + allfv + "吨");

price = price * day;

System.out.println("***租车总价格为:" + price + "元");

}

else {

System.exit(0);

}

scan.close();

}

}


点击查看更多内容
3人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消