请各位多指点

来源:12-1 综合练习

微笑即使悲伤_03178099

2017-05-26 16:29

http://img.mukewang.com/5927e6e40001dcc209440375.jpg

http://img.mukewang.com/5927e6e50001eab908890453.jpg

http://img.mukewang.com/5927e6e60001e53b08300352.jpg

  • http://img.mukewang.com/5927e6e600013fd606890298.jpg

  • public class Encapsulation {
        //获取List<Car<String,Integer>>类型arrayList对象的方法
        public static List<Car<String,Integer>> getarrayList(){
            return new ArrayList<Car<String,Integer>>();
        }
        //获取List<String>类型 对象的方法
        public static List<String> getArrayList(){
            return new ArrayList<String>();
        }
        //获得 账单列表的方法
        public static void  carBill(List<String> car){
            Iterator<String> it = car.iterator();
            while(it.hasNext()){
                System.out.printf("     "+it.next());
            }
        }
    
        // 获得菜单的方法
       public static void getMenu(List<Car<String,Integer>> car){
        Iterator<Car <String,Integer>> it = car.iterator();
        int serialNumber =1; // 序号
            while(it.hasNext()) {
                System.out.println(serialNumber + "   " + it.next().toSring()+"\n");
                serialNumber++;
            }
        }
    }
  • public class Test {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            //************************************将汽车类封装到arrayList容器中**************************
    
            Car<String, Integer> benz = new Bus<String, Integer>("奔驰", 600, 4);
            Car<String, Integer> bmw = new Bus<String, Integer>("商务", 900, 10);
            Car<String, Integer> dayun = new Truck<String, Integer>("大运", 1200, 30);
            Car<String, Integer> fengtian = new Pickup<String, Integer>("丰田", 700, 4, 4);
            Encapsulation encapsulation = new Encapsulation();
            List<Car<String, Integer>> car = Encapsulation.getarrayList();
            car.add(benz);
            car.add(bmw);
            car.add(dayun);
            car.add(fengtian);
    
    
            //***********************开始界面**************************************
    
            System.out.println("欢迎您光临达达租车系统\n");
            System.out.println("您是否要租车?");
            System.out.println("y/是  n/否");
            while (true) try {
                String answer = input.nextLine();
                if (answer.charAt(0) == 'y') {
                    System.out.println("您可租车的类型及其价目表\n");
                    System.out.println("序号        汽车名称        租金        容量\n");
                    Encapsulation.getMenu(car);
                    System.out.println("请输入您要租车的数量");
                    int totalNumber; //租车的总数量
    
                    //************** 要租车的总数量***************************
                    ok:
                    while (true) {
                        try {
                            totalNumber =Integer.parseInt(input.nextLine().trim());
                            break ok;
                        } catch (NumberFormatException e) {
                            System.out.println("输入不正确,请重新输入");
                        }
                    }
                    List<String> capacityCarName = Encapsulation.getArrayList(); //载客车名称
                    List<String> tonnageCarName = Encapsulation.getArrayList();  //载货车名称
                    int totalCapacity = 0; //总载客量
                    int totalTonnage = 0; //总吨位
                    int totalPrice = 0;   //总价钱
                    int number; // 第i辆车的序号
                    //输入相应车型的序号
                    for (int i = 1; i <= totalNumber; i++) {
                        System.out.println("请输入您要租的第" + i + "辆车的序号");
                        number1:
                        while (true) {
                            try {
                                number = Integer.parseInt(input.nextLine().trim());
                                break number1 ;
                            } catch (NumberFormatException e) {
                                System.out.println("输入错误,重新输入");
                            }
                        }
                        if (number == 1) {
                            capacityCarName.add(benz.getName());
                            Bus car1 = (Bus) benz;
                            totalCapacity += (int)car1.getCapacity();
                            totalPrice += benz.getPrice();
                        }
                        if(number == 2){
                           capacityCarName.add(bmw.getName());
                            Bus car2 = (Bus) bmw;
                            totalCapacity += (int)car2.getCapacity();
                            totalPrice +=bmw.getPrice();
                        }
                        if(number ==3){
                            tonnageCarName.add(dayun.getName());
                            Truck car3 =(Truck) dayun;
                            totalTonnage += (int)car3.getTruck();
                            totalPrice +=dayun.getPrice();
                        }
                        if(number ==4){
                            tonnageCarName.add(fengtian.getName());
                            capacityCarName.add(fengtian.getName());
                            Pickup car4 = (Pickup)fengtian;
                             totalCapacity += (int)car4.getCapacity();
                             totalTonnage += (int)car4.getTonnage();
                        }
                    }
                    System.out.println("请输入租车的天数");
                    int day;
                    nubmer2:
                     while(true){
                         try {
                             day = Integer.parseInt(input.nextLine().trim());
                             break nubmer2;
                         }catch(NumberFormatException e){
                             System.out.println("输入有误,请重新输入");
                         }
                     }
                    System.out.println("您的账单\n");
                    System.out.println("*******可载人的车辆有******************");
                    Encapsulation.carBill(capacityCarName);
                    System.out.println("   "+"共载"+totalCapacity+"人\n");
    
                    System.out.println("*******可载人的车辆有******************");
                    Encapsulation.carBill(tonnageCarName);
                    System.out.println("   "+"共载"+totalTonnage+"吨\n");
                    System.out.println("租车总金额是:"+totalPrice*day);
                    System.out.println("谢谢惠顾");
                    System.exit(1);
                } else if (answer.charAt(0) == 'n') {
                    System.out.println("欢迎您下次光临");
                    System.exit(1);
                } else {
                    System.out.println("输入有误,请重新输入");
                }
            } catch (InputMismatchException e) {
                System.out.println("输入有误,请重新输入");
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("输入有误,请重新输入");
            }
        }
    }


写回答 关注

1回答

  • LooMing_Hou
    2017-05-29 20:45:55
    System.ou.println("answer what?");


Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530649 学习 · 6091 问题

查看课程

相似问题