逻辑在此方法重载示例

时间:2016-09-03 21:51:18

标签: java overloading

我在Pluralsigh.com上发布java。以下代码由我撰写,以关注视频课程。主题是方法重载。一旦实现hasCarryOnSpace方法,代码似乎不会增加乘客数量。我在if语句之后的add1Passanger(int bags,int carryOns)方法中调用此方法。我也在add1Passenger(Passanger p,int carryOns)方法中调用它。请告诉我我的逻辑有什么问题......

public class Flight {

    //fields
    public int passengers;
    private int seats = 150;
    private int checkedBags;
    private int maxCarryOns = checkedBags*2, totalCarryOns;
    private int freeCheckedBags;

    //getter/setters
    public int getSeats(){return 150;}
    public int getCheckedBags() {return this.checkedBags;}

    //constructors
    public Flight (){}  
    public Flight(int freeCheckedBags) {
        this.freeCheckedBags=freeCheckedBags;
    }   
    public Flight(int freeCheckedBags, int checkedBags) {
        this(freeCheckedBags); 
        this.checkedBags=checkedBags;
    }


    //methods   
    public void addPassengers(Passenger... list) {
        if(hasSeats(list.length)) {
            passengers += list.length;
            for(Passenger passanger: list) {
                checkedBags += passanger.getCheckedBags();
            }
        }
        else tooMany(); 
    }

    public void add1Passenger(){
        if(hasSeats())
            passengers +=1;
        else tooMany();
        return;
    }

    public void add1Passanger(int bags){
        if(hasSeats()) {
            add1Passenger();
            this.checkedBags+=bags;
        }
    }

    public void add1Passenger(Passenger p){
        add1Passanger(p.getCheckedBags());
    }

    public void add1Passenger(int bags, int carryOns){
        if(hasSeats() && hasCarryOnSpace(carryOns)){
            add1Passanger(bags);
            totalCarryOns+=carryOns;
        }
    }

    public void add1Passenger(Passenger p, int carryOns){
        add1Passenger(p.getCheckedBags(), carryOns);
    }

    public boolean hasCarryOnSpace(int carryOns){
        return totalCarryOns+carryOns < maxCarryOns;
    }

    public boolean hasSeats(){
        return passengers < getSeats();
    }

    private boolean hasSeats(int count) {
        return passengers+count <= seats;
    }

    private void tooMany(){
        System.out.println("no more seats available");
    }

    //main method
    public static void main(String[] args ){
        Flight usAir = new Flight();
        //i have a separate Passenger class created w/ freeBags as first
        //parameter and checkedBags as the second in its constructors
        Passenger bob = new Passenger();
        Passenger jean = new Passenger(0, 1);       
        Passenger nick = new Passenger(0, 2);
        Passenger dan = new Passenger(2,2);

        usAir.addPassengers(bob, jean);
        usAir.add1Passenger(dan);
        usAir.add1Passenger();
        usAir.add1Passanger(2);
        //calls below don't increment the passenger count
        usAir.add1Passenger(nick, 2);
        usAir.add1Passenger(1, 1);
        System.out.println("usAir has " + usAir.passengers 
                + " passengers " + "with " + usAir.getCheckedBags()+
                " checked bags,"+" "+usAir.freeCheckedBags
                +" free bags, and "+usAir.totalCarryOns+" carryOns on board");
   }
}

2 个答案:

答案 0 :(得分:0)

请参阅以下代码。阅读评论以获得解释。代码的目的不仅是回答问题,还展示了MCVE的想法。
通过发布MCVE,您更有可能获得更好,更快的帮助,更重要的是:在许多情况下,它可以帮助您自己找到问题。

public class Flight {

    //fields
    public int passengers;
    private int seats = 150;
    private int checkedBags;

    //The problem is here:  maxCarryOns in intialized to 0.
    //private int maxCarryOns = checkedBags*2;
    //change to say:
    private int maxCarryOns = seats*2;

    private int totalCarryOns;
    private int freeCheckedBags;

    //getter/setters
    public int getSeats(){return 150;}
    public int getCheckedBags() {return checkedBags;}

    //constructors
    public Flight (){}

   //removed 2 more constructors , not needed to demonstrate the problem


    //methods

    //removed, not needed to demonstrate the problem
    //public void addPassengers(Passenger... list) {

    public void add1Passenger(){
        if(hasSeats()) {
            passengers +=1;
        } else {
            tooMany();
        }
        return;
    }

    public void add1Passanger(int bags){
        if(hasSeats()) {
            add1Passenger();
            checkedBags+=bags;
        }
    }

    //removed, not needed to demonstrate the problem
    //public void add1Passenger(Passenger p)

    public void add1Passenger(int bags, int carryOns){

        if(hasSeats() && hasCarryOnSpace(carryOns)){

            add1Passanger(bags);
            totalCarryOns+=carryOns;
        }
    }

    public void add1Passenger(Passenger p, int carryOns){

        add1Passenger(p.getCheckedBags(), carryOns);
    }

    public boolean hasCarryOnSpace(int carryOns){

        //a simple printout here would hae revealed that 
        //maxCarryOns was 0, hencethis methos always returned false.
        return (totalCarryOns+carryOns) < maxCarryOns;
    }

    public boolean hasSeats(){

        return passengers < getSeats();
    }

    //removed, not needed to demonstrate the problem
    //private boolean hasSeats(int count) {

    private void tooMany(){
        System.out.println("no more seats available");
    }

    //main method
    public static void main(String[] args ){

        Flight usAir = new Flight();

        //removed all statements which are not needed to demonstrate the problem
        Passenger nick = new Passenger(0,2);

        //calls below don't increment the passenger count
        usAir.add1Passenger(nick, 2);
        usAir.add1Passenger(1, 1);
        System.out.println("usAir has " + usAir.passengers
                + " passengers " + "with " + usAir.getCheckedBags()+
                " checked bags,"+" "+usAir.freeCheckedBags
                +" free bags, and "+usAir.totalCarryOns+" carryOns on board");
   }
}

//include all needed to run the code
class Passenger {

    int freeBags =0, checkedBags =0;

    Passenger(int freeBags, int checkedBags){

        this.freeBags = freeBags;
        this.checkedBags = checkedBags;
    }

    /**
     *@return
     */
    public int getCheckedBags() {

        return  checkedBags;
    }
}

答案 1 :(得分:-1)

虽然这个场景的代码应该简洁明了..它只是首先猜测在passenger构造中将0分配给Flight