java 计程车计费
Posted javahanlong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 计程车计费相关的知识,希望对你有一定的参考价值。
/**
- 1.声明好程序所需的变量,用于存储数据,请注意数据类型。
- 2.提示用户输入总里程数、总乘车时间、是否预约叫车
- (如果是预约叫车 还需要确认是否在4小时以内)、
- 是否有低速行驶(如果有,提示输入低速行驶时间,
-
需要考虑早晚高峰),
-
-
分别将这些用户输入的值,通过赋值存入对应的变 量中。
- @since 2020-7-1
- @author Administrator
*/
import java.util.Scanner;
public class CostOfTaxi {
//起步价
float startingPrice = 13.0f ;
//行驶里程
float inputMileage = 0.0f;
float mileage = 0.0f;
//基本单价
float basicUnitPrice = 2.3f;
//油费
float oilCost = 1.0f;
//总消费
float sumOfCost = 0.0f;
public static void main (String[] arg) {
// hanlong乘车
CostOfTaxi hanlong = new CostOfTaxi ();
hanlong.CostOfTaxi();
}
void CostOfTaxi () {
System.out.println("*********计程车收费**********");
System.out.println();
System.out.print("请输入行驶里程(千米): ");
Scanner inputif = new Scanner(System.in);
inputMileage = inputif.nextInt();
System.out.print("输入上车时间:hh:mm ");
String upCar = inputif.next();
int upCarHour = Integer.parseInt(upCar.substring(0, 2));
int upCarMinute = Integer.parseInt(upCar.substring(3, 5));
System.out.print("输入下车时间: ");
String downCar = inputif.next();
int downCarHour = Integer.parseInt(downCar.substring(0, 2));
int downCarMinute = Integer.parseInt(downCar.substring(3, 5));
System.out.print("输入是否预约叫车(是|否): ");
String ifAppointment = inputif.next();
System.out.print("预约时间是否在4小时以内(是|否):");
String ifShortAppointmentTime = inputif.next();
System.out.print("是否让司机等候或低速行驶(是|否): ");
String ifLowSpeed = inputif.next();
System.out.print("请输入不含早晚高峰期间低速行驶时长(分钟):");
int withoutPeakTime = inputif.nextInt();
System.out.print("请输入早晚高峰期间低速行驶时长(分钟):");
int peakTime = inputif.nextInt();
System.out.print("是否往返(起点终点2公里)(是|否):");
String ifNoPassengers = inputif.next();
System.out.println();
String ifNightCar = "否";
if (upCarHour>=23 || downCarHour <=5 || upCarMinute>=23 || downCarMinute<=5) {
ifNightCar = "是";
}
inputif.close();
System.out.println();
System.out.println("*****费用明细*****");
float sumOfCost = startingPrice + oilCost;
sumOfCost = sumOfCost
//以下方法会额外收费
//超过三公里
+ over3Km()
//慢速行驶(12公里时以下), +2*basicUnitPrice/5 min;非高峰+basicUnitPrice/5 min
+ lowSpeed(ifLowSpeed,withoutPeakTime,peakTime)
//预约 <= 4: 5, >4:6
+ appointment(ifAppointment,ifShortAppointmentTime)
//空驶 往返(起点终点2公里):0;不往返>15 +0.5*basicUnitPrice
+ noPassengers(ifNoPassengers)
//夜间行驶 题目理解为23-5上车就算钱,而不是23-5内行驶的公里数算钱
+ nightCar(ifNightCar);
System.out.println("油费: 1.0元");
System.out.println("总车费:" + Math.round(sumOfCost) + "元");
}
public float over3Km() {
// TODO Auto-generated method stub
float over3kmCost = 0;
if (inputMileage > 3) {
over3kmCost = basicUnitPrice * (inputMileage - 3);
System.out.println("里程超过3公里,里程价格:" + over3kmCost+ "元");
} else {
System.out.println("3公里内,里程价格:" + over3kmCost+ "元");
}
return over3kmCost;
}
public float nightCar(String ifNightCar) {
// TODO Auto-generated method stub
float nightCarCost;
if (ifNightCar.equals("是")) {
nightCarCost = (float) ((inputMileage - 3)*0.2*basicUnitPrice);
}else {
nightCarCost = 0 ;
}
System.out.println("夜间收费:" + nightCarCost + "元");
return nightCarCost ;
}
public float noPassengers(String ifNoPassengers) {
// TODO Auto-generated method stub
float noPassengersCost = 0.0f;
if ((inputMileage > 15) && ifNoPassengers.equals("否")) {
noPassengersCost = (float) ((inputMileage - 15)*0.5*basicUnitPrice);
} else {
noPassengersCost = 0;
}
System.out.println("空驶费:" + noPassengersCost + "元");
return noPassengersCost;
}
public float lowSpeed(String ifLowSpeed, int withoutPeakTime, int peakTime) {
// TODO Auto-generated method stub
float lowSpeedCost = 0.0f;
if (ifLowSpeed.equals("是")) {
lowSpeedCost = (withoutPeakTime / 5) * 1 * basicUnitPrice+(peakTime / 5) * 2 * basicUnitPrice;;
}
else {
lowSpeedCost = 0.0f;
}
System.out.println("低速行驶或司机等候服务费:" + lowSpeedCost + "元");
return lowSpeedCost;
}
public float appointment(String ifAppointment, String ifShortAppointmentTime) {
float appointmentCost = 0.0f;
if (ifAppointment.equals("是")) {
if (ifShortAppointmentTime.equals("是")) {
appointmentCost = 4;
} else {
appointmentCost = 6;
}
}
System.out.println("预约叫车服务费:" + appointmentCost + "元");
return appointmentCost;
}
}
以上是关于java 计程车计费的主要内容,如果未能解决你的问题,请参考以下文章