手记

【代码分享】答答租车系统练习

package com.imooc.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import com.imooc.bean.Aodi;
import com.imooc.bean.Car;
import com.imooc.bean.JinLong;
import com.imooc.bean.Mazida;
import com.imooc.bean.Pikaxue;
import com.imooc.bean.SongHuaJiang;
import com.imooc.bean.YiWeike;

public class MainTest {

    public static void main(String[] args) {

        Car car1 = new Aodi(1, "奥迪A4", 500, "载人:4人");
        Car car2 = new Mazida(2, "马自达6", 400, "载人:4人");
        Car car3 = new Pikaxue(3, "皮卡雪6", 450, "载人:4人 载货: 2吨");
        Car car4 = new JinLong(4, "金龙", 800, "载人:20人");
        Car car5 = new SongHuaJiang(5, "松花江", 400, "载货: 4吨");
        Car car6 = new YiWeike(6, "依维柯", 1000, "载货: 20吨");

        System.out.println("********欢迎使用答答租车系统********");
        System.out.println("您是否要租车:1是 0否");
        Scanner input = new Scanner(System.in);
        int bool =0 ;
         try {
               bool = input.nextInt();
               if (bool == 1) {
                    // 开始
                    System.out.println("您可租车的类型及其价目表");
                    System.out.println("序号 " + "汽车名称  " + "租金  " + "容量");
                    System.out.println("  " + car1.getNum() + "  " + car1.getName()
                            + "    " + car1.getPrice() + "  " + car1.getMuch());
                    System.out.println("  " + car2.getNum() + "  " + car2.getName()
                            + "   " + car2.getPrice() + "  " + car2.getMuch());
                    System.out.println("  " + car3.getNum() + "  " + car3.getName()
                            + "   " + car3.getPrice() + "  " + car3.getMuch());
                    System.out.println("  " + car4.getNum() + "  " + car4.getName()
                            + "      " + car4.getPrice() + "  " + car4.getMuch());
                    System.out.println("  " + car5.getNum() + "  " + car5.getName()
                            + "    " + car5.getPrice() + "  " + car5.getMuch());
                    System.out.println("  " + car6.getNum() + "  " + car6.getName()
                            + "    " + car6.getPrice() + " " + car6.getMuch());
                    System.out.println("请输入您要租车的数量");
                    int count = input.nextInt();
                    Map<Integer, Car> list = new HashMap<Integer,Car>();
                    for (int i = 1; i <= count; i++) {
                        System.out.println("请输入第" + i + "辆车的序号");
                        int inputNum = input.nextInt();
                        if (inputNum > 6) {
                            System.out.println("您的输入有误,请重新输入");
                            i--;
                        } else {
                            switch (inputNum) {
                            case 1:
                                list.put(i, car1);
                                break;
                            case 2:
                                list.put(i, car2);
                                break;
                            case 3:
                                list.put(i, car3);
                                break;
                            case 4:
                                list.put(i, car4);
                                break;
                            case 5:
                                list.put(i, car5);
                                break;
                            case 6:
                                list.put(i, car6);
                                break;

                            }
                        }

                    }
                    System.out.println("请输入租车天数:");
                    int data = input.nextInt();
                    System.out.println("您的租车账单:");
                    int price = 0;
                    for (int i = 1; i <= count; i++) {
                        System.out.println(list.get(i).getName() + "  "+list.get(i).getPrice()+"元/天");
                        price = price +list.get(i).getPrice();
                    }
                    System.out.println("\n"+"租车总价格"+price*data);
                    System.out.println("\n"+"************完毕*************");

                } else {
                    System.out.println("您已退出系统");
                }
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("请输入正确数值");
            System.out.println("您已退出系统");

        }

    }

}
24人推荐
随时随地看视频
慕课网APP

热门评论

讨厌看没有缩进的代码。

干货

不是java吗

查看全部评论