Ticket Price Calculation – Static Program in java

In this post, we will learn Ticket Price Calculation – Static Program in java Programming language.

Question:

Create a class Ticket with the following private variables
int ticketid;
int price;
static int availableTickets;

Include getters and setters methods in the Ticket class.

AvailableTickets should hold only positive value. Zero and negative values are not allowed.(This logic should be checked inside the corresponding setter method)

Write the following method in the Ticket class:

public int calculateTicketCost(int nooftickets) —this method should check the ticket availability, If the tickets are available, reduce the nooftickets from availableTickets and calculate the total amount as nooftickets*price  and return the total amount.  If the tickets are not available, this method should return -1.

Write a main method in the Main class to test the application.

Sample input and output 1:

Enter no of bookings:
2
Enter the available tickets:
25
Enter the ticketid:
123
Enter the price:
100
Enter the no of tickets:
5
Available tickets: 25

Total amount:500

Available ticket after booking:20

Enter the ticketid:
124
Enter the price:
100
Enter the no of tickets:
2
Available tickets: 20

Total amount:200

Available ticket after booking:18

Sample input and output 2:

Enter no of bookings:
1
Enter the available tickets:
25
Enter the ticketid:
123
Enter the price:
100
Enter the no of tickets:
26
Tickets not sufficient / available

CODE:–

```import java.util.Scanner;
public class Main
{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
Ticket obj=new Ticket();
System.out.println("Enter no of bookings:");
int no_bookings=sc.nextInt();
System.out.println("Enter the available tickets:");
obj.setAvailableTickets(sc.nextInt());
while(no_bookings>0)
{
System.out.println("Enter the ticketid:");
obj.setTicketid(sc.nextInt());
System.out.println("Enter the price:");
obj.setPrice(sc.nextInt());
System.out.println("Enter the no of tickets:");
int no_tickets=sc.nextInt();
if(obj.calculateTicketCost(no_tickets)==-1)
{
continue;
}
System.out.println("Available tickets: "+obj.getAvailableTickets());
System.out.println("Total amount:"+obj.calculateTicketCost(no_tickets));
System.out.println("Available ticket after booking:"+obj.getAvailableTickets());
no_bookings--;
}
}
}```
```public class Ticket
{
private int ticketid;
private int price;
private static int availableTickets;
public void setTicketid(int ticketid)
{
this.ticketid=ticketid;
}
public int getTicketid()
{
return this.ticketid;
}
public void setPrice(int price)
{
this.price=price;
}
public int getPrice()
{
return this.price;
}
public static void setAvailableTickets(int availableTickets1)
{   if(availableTickets1>=0)
{
availableTickets=availableTickets1;
}
}
public static int getAvailableTickets()
{
return availableTickets;
}
public int calculateTicketCost(int nooftickets)
{
if(availableTickets>=nooftickets)
{
availableTickets-=nooftickets;
return nooftickets*this.price;
}
else
{
return -1;
}
}
}```
Next:
Sharing Is Caring