美文网首页
iMooc-Java入门第二季达达租车系统

iMooc-Java入门第二季达达租车系统

作者: SpringWolfM | 来源:发表于2017-04-18 15:06 被阅读0次

    最近在跟着慕课网的java教程回顾和复习java,同时也在查漏补缺中对本科阶段的知识做一个小型的总结。
    第二季的视频用了三天看完,大作业用了一个上午写完。看视频过程中学习了eclipse强大的生成(generalize)和改错功能。
    对java的面向对象特性和控制台IO做了复习,也新学了ArrayList的功能,收获比较大。

    先贴上大作业要求的输出。


    Paste_Image.png Paste_Image.png Paste_Image.png
    代码借鉴了来自以下网页的代码

    http://www.imooc.com/article/16652%20%20%20Java
    http://blog.csdn.net/Joseph_Cherry/article/details/60530978

    • 主要思想是:建立一个Car父类,MotorCar是载人汽车子类,Truck是载货汽车子类,bothCar是皮卡(可载人也可载货)子类。用ArrayList存储各类车的名称、租金、容量。重写toString方法来解决输出各类车的信息价格表。

    我的代码:

    Car.java
    package com.DADACar;
    public class Car {
        public String name;
        public int price;
        
        public String getName() {
            return name;
        }
        public int getPrice() {
            return price;
        }
        public int getHumanNum() {
            // TODO Auto-generated method stub
            return 0;
        }
        public int getGoodsNum() {
            // TODO Auto-generated method stub
            return 0;
        }
    }
    
    motorCar.java
    package com.DADACar;
    
    public class motorCar extends Car {
        private int humanNum;
        
        public int getHumanNum() {
            return this.humanNum;
        }
        public motorCar(String name, int price, int humanNum){
            this.name=name;
            this.price=price;
            this.humanNum=humanNum;
        }
        public String toString(){
            return(name+"\t"+price+"元\\天    "+"载客量:"+humanNum+"人");
        }
    }
    
    trunckCar.java
    package com.DADACar;
    public class truckCar extends Car {
        private int goodsNum;
    
        public int getGoodsNum() {
            return this.goodsNum;
        }
    
        public truckCar(String name, int price, int goodsNum) {
            this.name=name;
            this.price=price;
            this.goodsNum=goodsNum;
            
        }
        public String toString(){
            return(name+"\t"+price+"元\\天    "+"载货量:"+goodsNum+"吨");
        }
    }
    
    bothCar.java
    package com.DADACar;
    
    public class bothCar extends Car {
        private int goodsNum;
        private int humanNum;
        public int getGoodsNum() {
            return this.goodsNum;
        }
        public int getHumanNum() {
            return this.humanNum;
        }
        public bothCar(String name, int price, int goodsNum, int humanNum){
            this.name=name;
            this.price=price;
            this.goodsNum=goodsNum;
            this.humanNum=humanNum;
        }
        
        public String toString(){
            return(name+"\t"+price+"元\\天\t"+"    载货量:"+goodsNum+"吨    载人量"+humanNum+"人");
        }
    }
    
    rentCarSystem.java //主函数在这里!!
    package com.DADACar;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class rentCarSystem {
        
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("欢迎使用哒哒租车系统");
            System.out.println("您是否需要租车? 1-是 0-否");
            Scanner statusScanner = new Scanner(System.in);
            String begin = statusScanner.next();
            //statusScanner.close();
            while(true){
            switch(begin){
            case "1":
                systemInfoPrint();
                break;
            case "0":
                System.out.println("谢谢使用本系统,再见!");
                System.exit(0);
                break;
            default:
                System.out.println("输入的值有误!");
                }
            }
        }
        public static void systemInfoPrint(){
            ArrayList<Car> carlist = new ArrayList<Car>(6);
            carlist.add(new motorCar("奥迪A4",500,4));
            carlist.add(new motorCar("马自达6",400,4));
            carlist.add(new bothCar("皮卡雪6",450,2,4)); //货量,人量
            carlist.add(new motorCar("金龙",800,20));
            carlist.add(new truckCar("松花江",400,4));
            carlist.add(new truckCar("依维柯",1000,20));
            
            System.out.println("可供租用的车型信息有:");
            System.out.println("******************************************");
            System.out.println("序号\t车型        单价            容量");
            for(int i = 0;i<carlist.size();i++){
            System.out.print((i+1)+"\t");//不换行
            System.out.println(carlist.get(i));//换行,这里不知道行不行//行,get返回值是String,重写了Car的toString函数
            }
            
            int rentNum = 0;
            System.out.println("请输入租车数量:");
            Scanner rentNumScanner = new Scanner(System.in);
            rentNum = rentNumScanner.nextInt();
            
            Scanner carNoSC=new Scanner(System.in);
            int [] rentNumList = new int[rentNum];
            
            for(int i=0;i<rentNum;i++){
            System.out.println("请输入第"+(i+1)+"辆车的序号:");
            rentNumList[i]=carNoSC.nextInt();
            }
            
            System.out.println("请输入租用天数:");
            Scanner rentDaySC = new Scanner(System.in);
            int rentDayNum = rentDaySC.nextInt();
            
            
            int totalMan=0;
            int totalGoods=0;
            int totalPrice=0;
            StringBuilder humanOutput = new StringBuilder();
            StringBuilder goodsOutput = new StringBuilder();
            
            for(int i =0;i<rentNumList.length;i++){
                if(carlist.get(rentNumList[i]-1) instanceof motorCar){
                    humanOutput.append(carlist.get(rentNumList[i]-1).getName());
                    humanOutput.append(" ");
                    totalMan = totalMan+carlist.get(rentNumList[i]-1).getHumanNum();
                    totalPrice = totalPrice+rentDayNum*carlist.get(rentNumList[i]-1).getPrice();
                }
                else if(carlist.get(rentNumList[i]-1) instanceof truckCar){
                    goodsOutput.append(carlist.get(rentNumList[i]-1).getName());
                    goodsOutput.append(" ");
                    totalGoods = totalGoods+carlist.get(rentNumList[i]-1).getGoodsNum();
                    totalPrice = totalPrice+rentDayNum*carlist.get(rentNumList[i]-1).getPrice();
                }
                else{
                    humanOutput.append(carlist.get(rentNumList[i]-1).getName());
                    humanOutput.append(" ");
                    goodsOutput.append(carlist.get(rentNumList[i]-1).getName());
                    goodsOutput.append(" ");
                    totalPrice = totalPrice+rentDayNum*carlist.get(rentNumList[i]-1).getPrice();
                    totalMan = totalMan+carlist.get(rentNumList[i]-1).getHumanNum();
                    totalGoods = totalGoods+carlist.get(rentNumList[i]-1).getGoodsNum();
                }
                
                
            }
            
            System.out.println("您的账单如下:");
            //重头戏
            System.out.println("**可载人的车有:");
            System.out.println(humanOutput+"\t共载人:"+totalMan+"人");
            System.out.println("**可载货的车有:");
            System.out.println(goodsOutput+"\t共载货:"+totalGoods+"吨");
            System.out.println("租车的总价是:");
            System.out.println(totalPrice+"元");
    
            
            //rentNumScanner.close();
            //carNoSC.close();
            //rentDaySC.close();
        }
    }
    
    • 另外值得注意的是

    Scanner输入流如果close了,就会出现如下所示的NoSuchElementException错误。

    >Exception in thread "main" [Java](http://lib.csdn.net/base/javase).util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)at java.util.Scanner.next(Scanner.java:1416)
    

    csdn博客中有分析说注释掉close()那一行就不报错了

    相关文章

      网友评论

          本文标题:iMooc-Java入门第二季达达租车系统

          本文链接:https://www.haomeiwen.com/subject/jwgfzttx.html