当前位置: 代码迷 >> Java相关 >> (求助)一道难题
  详细解决方案

(求助)一道难题

热度:103   发布时间:2009-10-24 03:39:22.0
class Person {  
    float height;  
    float money;  
    int ticketCount;  
    boolean hasPass;     


    Person() {  
       this.height = 0.0f;  
       this.money = 0.0f;  
       this.ticketCount = 0;  
       this.hasPass = false;  
    }  

    Person(float tempHeight, float tempMoney) {  
        this.height = tempHeight;  
        this.money = tempMoney;  
    }  

    void useTickets(int tempTicketCount) {  
        this.ticketCount -= tempTicketCount;  
    }  

    public String toString() {  
                       
        if (this.hasPass == true)  
            return height + "'person with $" + money + " and " + "a pass";  
                 
         else
            return height + "'person with $" + money + " and " + ticketCount  
                    + " " +"tickets";  
               
          }
           
      // (4) starts here, took me 4 hours ~~~~~~~~~  
         
    //buy passes method
    boolean buyPass(TicketBooth booth) {
        if(booth.availablePasses >= 1 && this.money >= booth.PASS_PRICE){
            this.money -= booth.PASS_PRICE;
            booth.sellPass();
            this.hasPass = true;
            return true;
           }
         
        return false;
    }
     
     
    //buy tickets method
    int buyTickets(int number, TicketBooth booth) {
        if (booth.availableTickets >= number) {
            if(this.money >= number*booth.TICKET_PRICE) {
                booth.sellTickets(number);
                this.money -= number*booth.TICKET_PRICE;
                this.ticketCount += number;
                return number;
            }
            else {
                int canAfford = (int)(this.money/booth.TICKET_PRICE);
                booth.sellTickets(canAfford);
                this.money -= (canAfford*booth.TICKET_PRICE);
                this.ticketCount =+ canAfford;
                return canAfford;
            }
        }
        else if (this.money >= booth.availableTickets*booth.TICKET_PRICE) {
               booth.sellTickets(booth.availableTickets);   
               this.money -= (booth.availableTickets * booth.TICKET_PRICE);
               this.ticketCount =+ booth.availableTickets;
               return booth.availableTickets;
        }
        return 0;
    }


    //allowed method
    boolean allowedToRide(Ride aRide) {
        if(this.height >= aRide.heightRequirement && (this.hasPass || this.ticketCount >= aRide.ticketsRequired))
            return true;
        else
            return false;
    }     
     
    //geton method
    boolean getOn(Ride aRide) {
        if (this.allowedToRide(aRide))    {            
                if (!this.hasPass) {
                this.ticketCount -= aRide.ticketsRequired;     
                aRide.numberOfRiders += 1;                 
                return true;
               }
                else if (this.hasPass)  
                    aRide.numberOfRiders += 1;
        return true;                        
            }
                     
        return false;
    }
            
            
}

         
                                    


----------------解决方案--------------------------------------------------------
  相关解决方案