【问题标题】:Writing a method for selling Passes, and another Method for selling tickets in JAVA写一个卖Pass的方法,另一个用JAVA卖票的方法
【发布时间】:2014-02-23 12:42:43
【问题描述】:

您好,我正在尝试编写满足以下输出的代码:

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
        }


    }

感谢您的帮助!

【问题讨论】:

  • 您具体遇到了什么问题?
  • 我想你的 sellPass/Tickets 方法需要一个数量才能开始。您需要检查手头是否有所需的数量,如果没有,可能会引发某种错误。将“手头数量”值减去数量并将price x quantity 添加到赚到的钱...
  • 你怎么知道一张票多少钱一张通票?
  • @Rp- 我宣布了,票价是 0.50 美元,通票是 16.50 美元
  • @MadProgrammer 它以 5 张通行证和 50 张门票开始(对于展位 1),对于展位 2,它以 1 张门票和 10 张门票开始。在展位 1 中以 3 张通行证和 30 张门票结束,展位以 0 通行证和 2 张门票结束

标签: java object methods constructor drjava


【解决方案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
}

与您的sellTickets(int noOfTickets) 一样,您的方法应该允许您要出售多少张门票。

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

【讨论】:

  • 我认为编译时错误是故意存在的,因此 OP 有一些事情要自己解决。 ; )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多