java租车系统_Java 哒哒租车系统(控制台)

这是一个使用Java编写的控制台租车系统,包括了Cars父类和Truck、Coach、Pickup三个子类,实现了不同车型的租车功能。用户可以选择租车并输入车辆序号、数量和天数,系统会计算总费用并显示租车详情。
摘要由CSDN通过智能技术生成

哒哒租车系统

Cars类为父类

重写Cars类toString方法

三种车型3个子类继承父类

Cars.java

package com.xk;

public class Cars {

public int id;

public String name;

public float price;

public float carryingCapacity;

public int seatingCapacity;

@Override

public String toString() {

if(seatingCapacity==0) {

return id+".\t"+name+"\t"+price+"元/天\t载货"+carryingCapacity+"吨";

}else if(carryingCapacity==0){

return id+".\t"+name+"\t"+price+"元/天\t载客"+seatingCapacity+"人";

}else{

return id+".\t"+name+"\t"+price+"元/天\t载货"+carryingCapacity+"吨\t载客"+seatingCapacity+"人";

}

}

}

Truck.java

package com.xk;

public class Truck extends Cars{

public Truck(int id,String name,float price,float carryingCapacity){

this.id=id;

this.name=name;

this.price=price;

this.carryingCapacity=carryingCapacity;

}

}

Coach.java

package com.xk;

public class Coach extends Cars {

public Coach(int id,String name,float price,int seatingCapacity){

this.id=id;

this.name=name;

this.price=price;

this.seatingCapacity=seatingCapacity;

}

}

Pickup.java

package com.xk;

public class Pickup extends Cars {

public Pickup(int id,String name,float price,float carryingCapacity,int seatingCapacity){

this.id=id;

this.name=name;

this.price=price;

this.carryingCapacity=carryingCapacity;

this.seatingCapacity=seatingCapacity;

}

}

Test.java

package com.xk;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

Cars Cars[] = {new Truck(1,"小货车",500f,3.5f),new Truck(2,"大货车",800f,7.8f),new Coach(3,"小客车",400f,12),new Coach(4,"大客车",900f,25),new Pickup(5,"小皮卡",600f,1.1f,4),new Pickup(6,"大皮卡",1100f,2.5f,6)};

System.out.println("----------哒哒租车系统----------\n是否进行租车:1.是\t2.否");

Scanner input = new Scanner(System.in);

int in = input.nextInt();

if(in==1) {

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

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

System.out.println(Cars[i]);

}

float sum = 0.0f;

float capacity1=0.0f;

int capacity2=0;

String[] car1=new String[6];

String[] car2=new String[6];

for(int i=1;;i++){

System.out.print("请输入你要购买第"+ i +"辆车的序号:");

int id=input.nextInt();

System.out.print("请输入你要租用的数量:");

int qty=input.nextInt();

System.out.print("请输入你要租用的天数:");

int day=input.nextInt();

switch(id){

case 1: sum+=qty*day*Cars[0].price;car1[i-1]=Cars[0].name;capacity1+=qty*Cars[0].carryingCapacity;break;

case 2: sum+=qty*day*Cars[1].price;car1[i-1]=Cars[1].name;capacity1+=qty*Cars[1].carryingCapacity;break;

case 3: sum+=qty*day*Cars[2].price;car2[i-1]=Cars[2].name;capacity2+=qty*Cars[2].seatingCapacity;break;

case 4: sum+=qty*day*Cars[3].price;car2[i-1]=Cars[3].name;capacity2+=qty*Cars[3].seatingCapacity;break;

case 5: sum+=qty*day*Cars[4].price;car1[i-1]=Cars[4].name;car2[i-1]=Cars[4].name;capacity1+=qty*Cars[4].carryingCapacity;capacity2+=qty*Cars[4].seatingCapacity;break;

case 6: sum+=qty*day*Cars[5].price;car1[i-1]=Cars[5].name;car2[i-1]=Cars[5].name;capacity1+=qty*Cars[5].carryingCapacity;capacity2+=qty*Cars[5].seatingCapacity;break;

default : System.out.print("输入错误,");i--;break;

}

System.out.println("是否继续租车:1.继续\t2.退出");

int choose=input.nextInt();

if(choose==2){

break;

}

}

System.out.println("--------------------------\n租车成功,您的账单:\n可载货的车:");

for(int i=0;i

if(car1[i]==null){

continue;

}

System.out.print(car1[i]+'\t');

}

System.out.println("共载货:"+capacity1+"吨\n可载人的车:");

for(int i=0;i

if(car2[i]==null){

continue;

}

System.out.print(car2[i]+'\t');

}

System.out.println("共载人:"+capacity2+"人\n租车共需付款:"+sum+"元");

}else{

System.out.println("已退出哒哒租车系统");

}

input.close();

}

}

运行效果

AAffA0nNPuCLAAAAAElFTkSuQmCC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值