| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1108 人关注过本帖
标题:(求助)一道难题
只看楼主 加入收藏
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分: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;
    }
            
            
 }
 
         
                                    
 
2009-10-24 03:39
快速回复:(求助)一道难题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.068408 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved