用Java编写一个租车小程序
程序员文章站
2022-07-14 23:17:30
...
要求实现的功能有:
1.展示所有可租车辆
2.选择车型,租车量
3.显示租车清单,包括:总载货量,总载客量,总金额等;
4、车辆类型
客车:只能载客
货车:只能载货
皮卡:既能载人,有能载货
Java代码:
package car.rental.applet;
import java.util.Scanner;
class Vehicle{
String name;
int num;
int passenger_num;
int goods_num;
int rental_cost;
void vehicle_type(int a,int b) {
passenger_num = a;
goods_num = b;
if(passenger_num == 0) {
System.out.print("只能载货"+" ");
}
else if(goods_num == 0) {
System.out.print("只能载客"+" ");
}
else{
System.out.print("既能载人有能载货"+" ");
}
}
void vehicle_message(int a,String ch,int b) {
num = a;
name = ch;
rental_cost = b;
System.out.print(num+" "+name+" "+rental_cost+"元"+" ");
}
void capacity(int a,int b) {
passenger_num = a;
goods_num = b;
if(passenger_num == 0) {
System.out.println("载货量:"+goods_num+"吨");
}
else if(goods_num == 0) {
System.out.println("载客量:"+passenger_num+"人");
}
else{
System.out.println("载货量:"+goods_num+"吨"+" "+"载客量:"+passenger_num+"人");
}
}
}
class Bus extends Vehicle{
String name = "客车";
int num = 1;
int passenger_num = 33;
int goods_num = 0;
int rental_cost = 123;
}
class Truck extends Vehicle{
String name = "货车";
int num = 2;
int passenger_num = 0;
int goods_num = 210;
int rental_cost = 189;
}
class Pickup_truck extends Vehicle{
String name = "皮卡";
int num = 3;
int passenger_num = 22;
int goods_num = 120;
int rental_cost = 345;
}
public class Demo {
public Demo() {
// TODO 自动生成的构造函数存根
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println("欢迎来到在线租车系统:");
System.out.println("可供选择的车辆有如下几种:");
Bus bus = new Bus();
Truck truck = new Truck();
Pickup_truck pickup = new Pickup_truck();
System.out.println("型号"+" "+"车名"+" "+"日租金额"+" "+"类型"+" "+"容量");
bus.vehicle_message (bus.num,bus.name,bus.rental_cost);
bus.vehicle_type (bus.passenger_num, bus.goods_num);
bus.capacity (bus.passenger_num,bus.goods_num);
truck.vehicle_message (truck.num,truck.name,truck.rental_cost);
truck.vehicle_type (truck.passenger_num, truck.goods_num);
truck.capacity (truck.passenger_num,truck.goods_num);
pickup.vehicle_message (pickup.num,pickup.name,pickup.rental_cost);
pickup.vehicle_type (pickup.passenger_num, pickup.goods_num);
pickup.capacity (pickup.passenger_num,pickup.goods_num);
System.out.println ("请手动输入您要租用的车辆型号以及租车量:");
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
int num = in.nextInt();
int cost = 0,p_num = 0,g_num = 0;
if(choice == 1) {
System.out.println("您已选择:");
p_num = num*bus.passenger_num;
cost = cost+bus.rental_cost*num;
System.out.println("客车"+" "+"载客量为:"+p_num+" "+"金额为:"+cost+"元");
}
else if(choice == 2) {
System.out.println("您已选择:");
g_num = num*truck.goods_num;
cost = cost+truck.rental_cost*num;
System.out.println("货车"+" "+"载货量为:"+g_num+" "+"金额为:"+cost+"元");
}
else {
System.out.println("您已选择:");
p_num = num*pickup.passenger_num;
g_num = num*pickup.goods_num;
cost = cost+pickup.rental_cost*num;
System.out.println("皮卡"+" "+"载客量为:"+p_num+",载货量为:"+g_num+" "+"金额为:"+cost+"元");
}
System.out.println("是否继续选择租用车辆:输入0继续,输入1退出租车系统");
int flag = in.nextInt();
while(flag != 1) {
System.out.println ("请手动输入您要租用的车辆型号以及租车量:");
choice = in.nextInt();
num = in.nextInt();
if(choice == 1) {
System.out.println("您已选择:");
p_num = p_num+num*bus.passenger_num;
cost = cost+bus.rental_cost*num;
System.out.println("客车"+" "+"载客量为:"+p_num+" "+"金额为:"+cost+"元");
}
else if(choice == 2) {
System.out.println("您已选择:");
g_num = g_num+num*truck.goods_num;
cost = cost+truck.rental_cost*num;
System.out.println("货车"+" "+"载货量为:"+g_num+" "+"金额为:"+cost+"元");
}
else {
System.out.println("您已选择:");
p_num = p_num+num*pickup.passenger_num;
g_num = g_num+num*pickup.goods_num;
cost = cost+pickup.rental_cost*num;
System.out.println("皮卡"+" "+"载客量为:"+p_num+",载货量为:"+g_num+" "+"金额为:"+cost+"元");
}
System.out.println("是否继续选择租用车辆:输入0继续,输入1退出租车系统");
flag = in.nextInt();
}
System.out.println("以下是您的租车清单:");
System.out.println("总载客量为:"+p_num+" "+"总载货量为:"+g_num+" "+"总金额为:"+cost+"元");
System.out.println("感谢您的使用,在线租车系统很乐意为您服务,欢迎下次光临!");
}
}
上一篇: 试题 算法提高 输出二进制表示
下一篇: 蓝桥杯 输出二进制表示
推荐阅读
-
用Python编写一个国际象棋AI程序
-
微信小程序授权 获取用户的openid和session_key【后端使用java语言编写】,我写的是get方式,目的是测试能否获取到微信服务器中的数据,后期我会写上post请求方式。
-
java基础------环境变量的配置及编写第一个程序
-
Java入门(一)——编写一个简单的Java程序
-
什么是递归?用Java写一个简单的递归程序
-
用Python写一个模拟qq聊天小程序的代码实例
-
抖音上用记事本编写爱心小程序教程
-
Java经典编程习题100例:第18例:编写程序,将一个数组中的元素倒排过来。例如原数组为1,2,3,4,5;则倒排后数组中的值
-
编写第一个Java程序总结03
-
java学习之路-练习题:编写一个计算速度的程序,距离时间常量。