一个控制台输出的租车程序,不好的地方还望指出

 1 2 import java.util.Scanner;
 3 
 4 /**
 5  * Created by Void on 2017/6/26.
 6  */
 7 public class CarDemo {
 8     public static void main(String[] args){
 9         Car cars[] = new Car[]{
10                 new CarModelsKeche("奥迪A4", 500, 4),
11                 new CarModelsKeche("马自达6", 400, 4),
12                 new CarModelsKeche("金龙", 1500, 20),
13                 new CarModelsHuoche("大力神", 6500, 200),
14                 new CarModelsHuoche("解放130", 1000, 30),
15                 new CarModelsPika("猛禽", 1000, 4, 5),
16                 new CarModelsPika("坦途", 1000, 4, 6)};
17         System.out.println("********欢迎来到“我要租车”系统********");
18         System.out.println("********请问是否需要租车?********");
19         System.out.println("********是请选择(1),否请选择(2)********");
20         Scanner sc = new Scanner(System.in);
21         if (sc.nextInt() == 1) {
22             System.out.println("********您可以租用的车型以及价目表如下********");
23             System.out.println("序号	车型		价格(元/天)		容量");
24             for (int i = 0; i < cars.length; i++) {
25                 System.out.println((i + 1) + "		" + cars[i]);
26             }
27             System.out.println("********您要租几辆车********");
28             float zongJia = 0;
29             int zongRen = 0;
30             float zongZhong = 0;
31             String laRen = "";
32             String laHuo = "";
33             int temp = 0;
34             int a = sc.nextInt();
35             for (int i = 1; i <= a; i++) {
36                 System.out.println("********请输入第" + i + "辆车的序号********");
37                 int b = sc.nextInt();
38                 System.out.println("********您要租的车型为" + cars[b - 1].carName + "********");
39                 System.out.println("********确认选(1),重新选(2)********");
40                 temp = i;
41                 i = getSelect(sc, i);
42                 if (i < temp)
43                     continue;
44 
45                 System.out.println("********您要租用几天?********");
46                 int c = sc.nextInt();
47                 zongJia = cars[b - 1].money * c+zongJia;
48                 zongRen = cars[b - 1].zairen+zongRen;
49                 zongZhong = cars[b - 1].zaihuo+zongZhong;
50                 if ((cars[b - 1].zairen) != 0) {
51                     laRen = cars[b - 1].carName;
52                 }
53                 if ((cars[b - 1].zairen) >= 0) {
54                     laHuo = cars[b - 1].carName;
55                 }
56 
57             }
58             System.out.println("******您的账单如下******");
59             System.out.println("载客的车有:");
60             System.out.println(laRen + "	总人数为:" + zongRen);
61             System.out.println("载货的车有:");
62             System.out.println(laHuo + "	总重量为:" + zongZhong);
63             System.out.println("总价格为:" + zongJia);
64             System.out.println("******是否确认支付" + zongJia + "元进行租车?******");
65             System.out.println("******是(1)	否(2)******");
66             if (sc.nextInt() == 1) {
67                 System.out.println("******您已成功支付" + zongJia + "元进行租车,祝您生活愉快!******");
68             } else
69                 System.out.println("*****您已成功取消本次租车。欢迎下次光临******");
70             System.out.println("*****欢迎下次光临******");
71             return;
72         }
73     }
74     private static int getSelect(Scanner sc, int i) {
75         int d = sc.nextInt();
76         switch (d){
77             case 1:
78                 return i;
79             case 2:
80                 i--;
81                 return i;
82             default: System.out.println("********输入错误,请重新输入********");
83                return getSelect(sc,i);
84         }
85     }
86 }
public abstract class Car {
    public String carName;
    public float  money;  
    public int zairen;    
    public float  zaihuo;  
}
public class CarModelsHuoche extends Car{
    public CarModelsHuoche(String carName ,float  money , float  zaihuo){
        this.carName = carName;
        this.money = money;
        this.zaihuo = zaihuo;
    }
    public String toString(){
        return (carName+"		"+money+"			"+"吨: "+zaihuo);
    }
}
public class CarModelsKeche extends Car{
    public CarModelsKeche(String carName,float money,int zairen){
        this.carName = carName;
        this.money = money;
        this.zairen = zairen;
    }
    @Override
    public String toString(){
        return (carName+"		"+money+"			"+"载客量: "+zairen);
    }
}
public class CarModelsPika extends Car{
    public CarModelsPika(String carName ,float  money ,int zairen, float  zaihuo ){
        this.carName = carName;
        this.money = money;
        this.zaihuo = zaihuo;
        this.zairen = zairen;
    }
    public String toString(){
        return (carName+"		"+money+"			"+"载人数: "+zairen+"	"+"吨位:"+zaihuo);
    }
}

感觉还可以优化,等学到更多的东西后,再回来优化一下代码

原文地址:https://www.cnblogs.com/SubFirst-D/p/7088588.html