Search code examples
javamethodsparameters

Method declaration being treated as a parameter


public class TicketTest
{
    public static int COUNTER_TICKET_PRICE = 50; 
    public static int WEB_TICKET_PRICE = 30; 
    public static int WEB_TICKET_LT_WEEK_PRICE = 40; 
    public static double DISCOUNT = 0.5;
    int serial = 0;
    
    public static void main(String args[])
    {
        int total_sales = 0;
            tickets[] = {
                    new WebTicket(10),
                    new WebTicket(5),
                    new CounterTicket(),
                    new DiscountTicket(5, "Student"),
                    new DiscountTicket(10, "Senior"),
            };
            

            for(int i=0; i<tickets.length; i++) {
                    System.out.println( tickets[i] );
            }

            for(int i=0; i<tickets.length; i++) {
                total_sales += tickets[i].getPrice();
            }  

            System.out.println();
            System.out.print("Total sales: " + total_sales );


    }
    
    public void Ticket(int days) {
        public int getPrice(int days) {
    }
    }
}

On the 4th to last line, (public int getPrice..) it says that public isn't a valid modifier for the "parameter" getPrice. Did I not close some brackets or something?

I tried many things and even deleting everything else that mentions getPrice to see if that was influencing it, but I haven't gotten any change.


Solution

  •     public void Ticket(int days) {
            public int getPrice(int days) {
        }
        }
    

    You can't define a function inside of another function like this. You could split it:

        public void Ticket(int days) {
        }
        public int getPrice(int days) {
        }
    

    or put it inside a separate class, but you can't have one inside the other.