[Solved] Profit or Loss with Java

Profit or Loss: We are going to create a console application that can estimate whether the booking is a profit or loss, thereby enabling hall owners to reduce or increase expenses depending on the status. Hence if several Booking details are given, compute whether the bookings are profitable or not. Use Threads to compute for each booking, Finally display the details along with the profit/loss status.

Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names and method names should be the same as specified in the problem statement.
 

Create a class Event with the following private attributes

AttributesDatatype
nameString
hallbookingHallBooking

Include appropriate getters and setters.
Create default constructor and a parameterized constructor with arguments in order Event(String name,HallBooking hallbooking).

Create a class HallBooking with following private attributes

AttributesDatatype
hallNameString
costDouble
hallCapacityInteger
seatsBookedInteger

Include appropriate getters and setters.
Create default constructor and a parameterized constructor with arguments in order
HallBooking(String hallName, Double cost, Integer hallCapacity,Integer seatsBooked).

Create a class ComputeStatus that implements Runnable interface with List<Event> eventList attribute.

Include following methods.
Override run() method which displays event name along with their status (i.e) Profit or Loss. 
If  (seats booked / hall capacity) * 100 >= 60 then it is a profit else loss .

Create a driver class Main which creates ThreadGroup with two threads. Each thread will have half of the event details. After the first half of event details are obtained, invoke the first thread and after the other half is obtained invoke the second thread. The Threads print the status of the events. Use the join method appropriately to ensure printing the status in the correct order.

Input Format:

The first line of input corresponds to the number of events ‘n’.
The next ‘n’ line of input corresponds to the event details in CSV format of (Event Name,Hall Name,Cost,Hall Capacity,Seats Booked).
Refer to sample input for formatting specifications.

Output Format:

The output consists of event names with their status (Profit or Loss).
Refer to sample output for formatting specifications.

Problem Constraints:
If n>0 and n then even. Otherwise, display as “Invalid Input“.

Sample Input and Output 1:
[All text in bold corresponds to input and rest corresponds to output]

Enter the number of events
4
Enter event details in CSV
Party,Le Meridian,12000,400,250
Wedding,MS mahal,500000,1000,400
Alumni meet,Ramans,10000,600,300
Plaza party,Rizzodous,30000,1200,1000

Party yields profit
Wedding yields loss
Alumni meet yields loss
Plaza party yields profit

Solution


import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String args[]) throws IOException
    {
    	BufferedReader br=new BufferedReader(new
		InputStreamReader(System.in));
		System.out.println("Enter the number of events");
		int n=Integer.parseInt(br.readLine());
		if(n>0 && n%2==0) {
			ArrayList<Event>eList=new ArrayList<Event>();
			String ar[]=new String[10];
			System.out.println("Enter event details in CSV");
			for(int i=0;i<n;i++) {
				ar=br.readLine().split(",");
				HallBooking hb=new HallBooking(ar[1],
						Double.parseDouble(ar[2]),
						Integer.parseInt(ar[3]),
						Integer.parseInt(ar[4]));
				Event e=new Event(ar[0],hb);
				eList.add(e);
			}
			ComputeStatus cs=new ComputeStatus(eList);
			Thread t=new Thread(cs);
			t.start();
		}
		else {
			System.out.println("Invalid Input");
		}
    }
}
public class Event {
    private String name;    
	private HallBooking hallbooking;
	public Event() {}
	public Event(String name,HallBooking hallbooking) {
		this.name=name;
		this.hallbooking=hallbooking;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public HallBooking getHallbooking() {
		return hallbooking;
	}
	public void setHallbooking(HallBooking hallbooking) {
		this.hallbooking = hallbooking;
	}
	
}
import java.util.*;
public class ComputeStatus implements Runnable {
    List<Event> eventList;
    public ComputeStatus(List<Event> eList) {
		eventList=eList;
	}
	public void run() {
		for(Event e:eventList) {
			boolean result=(e.getHallbooking().getSeatsBooked()/
					(float)e.getHallbooking().getHallCapacity()) * 100 >= 60;
			if(result) {
				System.out.println(e.getName()+" yields profit");
			}
			else {
				System.out.println(e.getName()+" yields loss");
			}
		}
	}
}
public class HallBooking {
    private    String hallName;
	private Double cost;
	private	Integer hallCapacity,seatsBooked;
	public HallBooking() {}
	public HallBooking(String hallName, Double cost, Integer hallCapacity,Integer seatsBooked) {
		this.hallName=hallName;
		this.cost=cost;
		this.hallCapacity=hallCapacity;
		this.seatsBooked=seatsBooked;
	}
	public String getHallName() {
		return hallName;
	}
	public void setHallName(String hallName) {
		this.hallName = hallName;
	}
	public Double getCost() {
		return cost;
	}
	public void setCost(Double cost) {
		this.cost = cost;
	}
	public Integer getHallCapacity() {
		return hallCapacity;
	}
	public void setHallCapacity(Integer hallCapacity) {
		this.hallCapacity = hallCapacity;
	}
	public Integer getSeatsBooked() {
		return seatsBooked;
	}
	public void setSeatsBooked(Integer seatsBooked) {
		this.seatsBooked = seatsBooked;
	}
	
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *