首页 > 代码库 > 一个控制台输出的租车程序(---优化版---)

一个控制台输出的租车程序(---优化版---)

package cn.String.Day.Cars;

import java.util.Scanner;

/**
 * Created by Void on 2017/6/29.
 */
public class CarsDemo {
    public static void main(String[] args) {
        Cars[] cars = new Cars[]{
                new BusCar("奥迪A3", 500, 4),
                new BusCar("金龙", 1500, 20),
                new TruckCar("解放130", 1000, 20),
                new TruckCar("金杯", 500, 8),
                new PickCar("福田130", 800, 4, 5),
                new PickCar("猛禽150", 1500, 4, 10)
        };
        System.out.println("*------------欢迎来到我要租车-----------*");
        System.out.println("*------------请问是否需要租车?-----------*");
        System.out.println("*------------是选择1,否选择2?-----------*");
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        if (a == 1) {
            System.out.println("*------------请选择车型-----------*");
            System.out.println("序号\t车型\t\t租金(元/天)\t容量");
            for (int i = 0; i < cars.length; i++) {
                System.out.println((i + 1) + "\t" + cars[i]);
            }
            System.out.println("*------------想租多少辆车?-----------*");

            int b = scanner.nextInt();
            Cars[] zairen = new Cars[cars.length];
            Cars[] zaihuo = new Cars[cars.length];
            int ren = 0;
            int dun = 0;
            float zongjia = 0;
            float zongzhong = 0.0f ;
            int zongren = 0;
            for (int i = 1; i<= b ; i++){
                if (b==1){
                    System.out.println("*------------请输入第这辆车序号-----------*");}
                else
                    System.out.println("*------------请输入第"+i+"辆车序号-----------*");
                int c = scanner.nextInt();
                System.out.println("您想租的车型是 :"+cars[c-1].carName);
                System.out.println("----确认选1,重新选2----");
                /*
                * 错误的地方:
                * int b = getScanner(scanner,i)
                * if(b!=temp){continue;}-------->  这里是变量b记录中递归返回的值i,没有修改循环中i的变量值,所以循环还是继续*/
                int temp = i;
                i =getScanner(scanner,i);
                if (i<temp){continue;}
                System.out.println("----需要租几天?----");
                int d = scanner.nextInt();
                zongjia = cars[c-1].carMoney*d+zongjia;
                zongzhong = cars[c-1].carGo+zongzhong;
                zongren = cars[c-1].carManned + zongren;
                if (cars[c-1].carManned == 0){
                    zaihuo[dun] = cars[c-1];
                    dun++;
                }
                if (cars[c-1].carManned != 0){
                    zairen[ren] = cars[c-1];
                    ren++;
                }
                if (cars[c-1].carManned !=0 && cars[c-1].carGo!=0){
                    zaihuo[dun] = cars[c-1];
                    dun++;
                }
            }
            System.out.println("您的账单如下:");
            System.out.println("可载人的车有:");
            for (int i = 0; i <=ren-1 ; i++) {
                if (i ==ren-1){
                    System.out.println(zairen[i].carName +"    共载人:" + zongren +"");
                }else {
                System.out.print(zairen[i].carName +"  ");}
            }
            System.out.println("可载货的车有:");
            for (int t = 0; t <=dun-1 ; t++) {
                if (t ==dun-1){
                    System.out.println(zaihuo[t].carName +"    共载货:" + zongzhong +"");
                }else {
                System.out.print(zaihuo[t].carName +"  ");}
            }
            System.out.println("租车总价格为:" + zongjia +"");
            System.out.println("确认支付请按1,取消支付请按2");
            int e = scanner.nextInt();
            if (e==1){
                System.out.println("支付成功,欢迎再次光临!");
            }
        }

    }
    private static int getScanner(Scanner scanner, int i) {
        int count = scanner.nextInt();
        switch (count){
            case 1:
                return i;
            case 2:
                i--;
                return i;
            default:
                System.out.println("输入错误请重新输入");
                return getScanner(scanner,i);

        }

    }
}

改进了上个版本的一些bug,增加了一点场景判断

一个控制台输出的租车程序(---优化版---)