早起的鸟儿有虫吃,瞎写的代码有Bug!
出租车计费训练任务
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class XXX(自己取类名) {
public static void main(String[] args) throws Exception {
System.out.print("请输入打车总里程(km):");
Scanner inputKm = new Scanner(System.in);
double km = inputKm.nextDouble();
double kmMoney = 0.0;
kmMoney = kmDriving(km);
double nightMoney = 0.0;
nightMoney = nightDriving(km);
double appointmentMoney = 0.0;
appointmentMoney = appointmentDriving();
Map<String, Integer> mapSum = new HashMap<String, Integer>();
mapSum = slowDriving();
double noHeightDrivingMoney = (double)mapSum.get("noHeightDrivingMoney");
double heightDrivingMoney = (double)mapSum.get("heightDrivingMoney");
double slowDrivingMoney = (double)mapSum.get("slowDrivingMoney");
double returnDrivingMoney = 0.0;
returnDrivingMoney = returnDriving(km);
double olimoney = 1.0;
double moneySum = 0.0;
moneySum = kmMoney + slowDrivingMoney + appointmentMoney +
returnDrivingMoney + nightMoney + olimoney;
System.out.println("总车费:"+moneySum+"元");
System.out.println("---产生费用项目详细情况---");
if (kmMoney>13.0) {
System.out.println("里程超过3公里,里程价格:"+kmMoney);
}else {
System.out.println("里程小于3公里,里程价格:"+kmMoney);
}
if (slowDrivingMoney>0.0) {
System.out.println("低速行驶费一共:"+slowDrivingMoney+"元,其中:");
if (noHeightDrivingMoney>0.0) {
System.out.println("其他时间段低速行驶费:"+noHeightDrivingMoney);
}
if (heightDrivingMoney>0.0) {
System.out.println("早晚高峰时间段低速行驶费:"+heightDrivingMoney);
}
}else {
System.out.println("无低速行驶费");
}
if (appointmentMoney>0.0) {
System.out.println("预约叫车服务费:"+appointmentMoney);
}else {
System.out.println("无预约费用");
}
if (returnDrivingMoney>0) {
System.out.println("空驶费"+returnDrivingMoney);
}else {
System.out.println("无空驶费");
}
if (nightMoney>0.0) {
System.out.println("夜间收费"+nightMoney);
}else {
System.out.println("无夜间费用");
}
System.out.println("燃油附加费:"+olimoney+"元");
}
private static Double kmDriving(double km){
if (km>3.0) {
double kmMoney = (km-3.0)*2.3+13.0;
kmMoney = (double) Math.round(kmMoney);
return kmMoney;
}else {
double kmMoney = 13.0;
return kmMoney;
}
}
public static double appointmentDriving(){
System.out.print("请输入是否预约叫车(是|否)?");
Scanner inputChoose = new Scanner(System.in);
String choose = inputChoose.next();
if (choose.equals("是")) {
System.out.print("预约是否在4个小时以内(是|否)?");
Scanner inputAppointment = new Scanner(System.in);
String appointment = inputAppointment.next();
if (appointment.equals("是")) {
double appointmentMoney = 5.0;
return appointmentMoney;
}else if (appointment.equals("否")) {
double appointmentMoney = 6.0;
return appointmentMoney;
}else {
System.out.println("“是否在4个小时以内”请输入“是”或者“否”!!!");
System.exit(0);
}
}else if (choose.equals("否")) {
double appointmentMoney = 0.0;
return appointmentMoney;
}else {
System.out.println("“预约叫车”请输入“是”或者“否”!!!");
System.exit(0);
}
return 0;
}
public static Map<String, Integer> slowDriving(){
System.out.print("请输入是否有低速行驶(是|否)?");
Scanner inputChoose = new Scanner(System.in);
String choose = inputChoose.next();
if (choose.equals("是")) {
System.out.print("请输入不含早晚高峰期间低速行驶时长(分钟):");
Scanner noHeight = new Scanner(System.in);
int noHeightDriving = noHeight.nextInt();
System.out.print("请输入早晚高峰期间低速行驶时长(分钟):");
Scanner height = new Scanner(System.in);
int heightDriving = height.nextInt();
heightDriving = heightDriving/5;
noHeightDriving = noHeightDriving/5;
double heightDrivingMoney = heightDriving*4.6;
double noHeightDrivingMoney = noHeightDriving*2.3;
heightDrivingMoney = Math.round(heightDrivingMoney);
noHeightDrivingMoney = Math.round(noHeightDrivingMoney);
double slowDrivingMoney = heightDrivingMoney+noHeightDrivingMoney;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("noHeightDrivingMoney", (int) noHeightDrivingMoney);
map.put("heightDrivingMoney", (int) heightDrivingMoney);
map.put("slowDrivingMoney", (int) slowDrivingMoney);
return map;
}else if (choose.equals("否")) {
int heightDrivingMoney = 0;
int noHeightDrivingMoney = 0;
int slowDrivingMoney = 0;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("noHeightDrivingMoney", noHeightDrivingMoney);
map.put("heightDrivingMoney", heightDrivingMoney);
map.put("slowDrivingMoney", slowDrivingMoney);
return map;
}else {
System.out.println("“是否低速行驶”请输入“是”或者“否”!!!");
System.exit(0);
}
return null;
}
public static double returnDriving(double km){
System.out.print("请输入是否往返载客(是|否)?");
Scanner inputChoose = new Scanner(System.in);
String choose = inputChoose.next();
if (choose.equals("是")) {
double returnDrivingMoney = 0.0;
return returnDrivingMoney;
}else if (choose.equals("否")) {
if (km<=15) {
double returnDrivingMoney = 0.0;
return returnDrivingMoney;
}else {
double returnDrivingMoney = (km-15)*1.15;
returnDrivingMoney = (double) Math.round(returnDrivingMoney);
return returnDrivingMoney;
}
}else {
System.out.println("“是否往返”请输入“是”或者“否”!!!");
System.exit(0);
}
return 0;
}
public static double nightDriving(double km) throws Exception{
System.out.print("请输入开始乘车时间,24小时制(hh:mm:ss):");
Scanner first = new Scanner(System.in);
String firstTime = first.next();
System.out.print("请输入结束乘车时间,24小时制(hh:mm:ss):");
Scanner last = new Scanner(System.in);
String lastTime = last.next();
DateFormat df = new SimpleDateFormat("HH:mm:ss");
Date timeFirst = df.parse(firstTime);
Date timeLast = df.parse(lastTime);
Date nightTime = df.parse("23:00:00");
Date moningTime = df.parse("05:00:00");
if (nightTime.getTime() >= timeFirst.getTime() && timeFirst.getTime() > moningTime.getTime() &&
nightTime.getTime() >= timeLast.getTime() && timeLast.getTime() > moningTime.getTime()) {
double nightMoney = 0.0;
return nightMoney;
}else {
double nightMoney = 0.0;
nightMoney = km*2.3*0.2;
nightMoney = (double) Math.round(nightMoney);
return nightMoney;
}
}
}