2014-01-30 2 views
0

나는 다음과 같은 출력을 만족하는 코드를 작성하는 것을 시도하고 안녕하세요 : 내가 처음 세 줄을 위해 그것을 할 수 있었다패스를 판매하는 방법, 그리고 JAVA에서 티켓을 판매하는 다른 방법을 쓰기

Here are the booths at the start: 
   Ticket booth with 5 passes and 50 tickets 
   Ticket booth with 1 passes and 10 tickets 
Booth 1 has made $43.0 
Booth 2 has made $20.5 
Here are the booths at the end: 
   Ticket booth with 3 passes and 30 tickets 
   Ticket booth with 0 passes and 2 tickets 

을 sellPass(), sellTickets() 및 moneyMade()에 대한 내 메서드를 작성하는 데 문제가 있습니다.

public class TicketBoothTestProgram 
{ 
    public static void main(String args[]) 
    { 
     TicketBooth booth1, booth2; 

     booth1 = new TicketBooth(5, 50); 
     booth2 = new TicketBooth(1, 10); 

     System.out.println("Here are the booths at the start:"); 
     System.out.println(" " + booth1); 
     System.out.println(" " + booth2); 

     // Simulate selling 2 passes, 5 tickets, 12 tickets and 3 tickets from booth1 
     booth1.sellPass(); 
     booth1.sellPass(); 
     booth1.sellTickets(5); 
     booth1.sellTickets(12); 
     booth1.sellTickets(3); 

     // Simulate selling 2 passes, 5 tickets, 12 tickets and 3 tickets from booth2 
     booth2.sellPass(); 
     booth2.sellPass(); 
     booth2.sellTickets(5); 
     booth2.sellTickets(12); 
     booth2.sellTickets(3); 

     // Make sure that it all worked 
     System.out.println("\nBooth 1 has made $" + booth1.moneyMade); 
     System.out.println("Booth 2 has made $" + booth2.moneyMade); 
     System.out.println("\nHere are the booths at the end:"); 
     System.out.println(" " + booth1); 
     System.out.println(" " + booth2); 
    } 
} 

여기 내 방법을 쓰기 위해 노력하고있는 코드입니다 : 여기에 출력 내 코드에 예상되는 다음 테스터 코드는

public class TicketBooth 
    { 
     float moneyMade; 
     int availablePasses; 
     int availableTickets; 

     static float TICKET_PRICE = 0.50f; 
     static float PASS_PRICE = 16.50f; 

     public TicketBooth() 
     { 
     moneyMade = 0.0f; 
     availablePasses = 0; 
     availableTickets = 0; 
     } 

     public TicketBooth(int p) 
     { 
     moneyMade = 0.0f; 
     availablePasses = p; 
     availableTickets = 0; 
     } 

     public TicketBooth(int p, int t) 
     { 
     moneyMade = 0.0f; 
     availablePasses = p; 
     availableTickets = t; 
     } 

     public String toString() 
     { 
      return("Ticket booth with " + this.availablePasses + " passes and " + this.availableTickets + 
     " tickets"); 
     } 

     public sellPass() 
     { 
      //INSERT CODE HERE FOR SELLING A PASS 
     } 

     public sellTickets() 
     { 
      //INSERT CODE HERE FOR SELLING A TICKET 
     } 


    } 

어떤 도움 감사합니다 감사합니다!

+2

구체적으로 어떤 문제가 있습니까? – neminem

+1

당신의'sellPass/Tickets' 메쏘드가 처음부터 수량을 필요로한다고 생각합니다. 필요한 수량을 가지고 있는지 확인하고 그렇지 않은 경우 어떤 종류의 오류가 발생할 수 있습니다. 수량에 의한 "수작업"가치를 줄이고 돈에'가격 x 수량'을 더하십시오. – MadProgrammer

+0

얼마나 많은 표와 얼마나 많은 금액을 알 수 있습니까? –

답변

1

sellPass() 방법은 매우 간단합니다. boolean 반환 유형을 추가 했으므로 티켓이나 패스가 부족한 경우 false을 다시 실행합니다.

public boolean sellPass() { 
    //Check if you have a pass to sell 
    if (availablePasses > 0) { 
     //you have passes to sell 
     moneyMade += moneyPerPass; // add the money 
     availablePass--; //decrement pass counter 
     return true; 
    } 
    return false; // not enough passes to sell 
} 

와 같은 방식으로, 귀하의 방법은 판매하려는 티켓의 수를 허용해야합니다.

public boolean sellTickets(int noOfTickets) { 
    if(availableTickets >= noOfTickets) { 
     moneyMade += noOfTickets * pricePerTicket; 
     availableTickets -= noOfTickets; 
     return true; 
    } 
    return false; 
} 
+0

컴파일 타임 오류가 의도적으로 있다고 가정하므로 OP는 독자적으로 알아낼 항목이 있습니다. ;) – Radiodef

+0

고마워요! @ Rp- –