Collections:

TREEMAP

To assist Event organizers, you need to develop a console application that shows the number of tickets sold in a particular price category.

Sample Input and Output :
[All Texts in bold corresponds to the input and rest are output]

Enter the number of events:
3
Enter event details in CSV(Customer Name,Ticket Price,No of Seats Booked)
Emily
,100,5
Natalie
,200,10
Grace
,100,3
Ticket Price    Tickets Booked
100                 8
200                 10

CODE:

//package com.mycom.collections.map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class Main {

    public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of events:");
int num = Integer.parseInt(sc.nextLine());
TreeMap<Integer,Integer> tm = new TreeMap<Integer,Integer>();
System.out.println("Enter event details in CSV(Customer Name,Ticket Price,No of Seats Booked)");
for(int i =0; i<num; i++) {
String user = sc.nextLine();
String arr[]=user.split(",");
            int price=Integer.parseInt(arr[1]);
            int seats=Integer.parseInt(arr[2]);
            if(tm.containsKey(price)){
                Integer s1 = tm.get(price);
                seats=seats+s1;   
                tm.replace(price,s1,seats);     //replace(key,old value,new value);
            }else{
                tm.put(Integer.parseInt(arr[1]),Integer.parseInt(arr[2]));
            }
}
System.out.println(String.format("%-15s %s","Ticket Price","Tickets Booked"));
Set<Integer> keys = tm.keySet();
for(Integer k:keys) {
System.out.println(String.format("%-15s %s",k,tm.get(k)));
}

}

}

 

Comments

Popular posts from this blog