info@meritcampus.com    +91-85006-22255
...
`

# Weekend Hack - 31 Aug 2013 - Results

Thanks to Pramod Jain for spending so much time, coming up with original and innovative answers and for submitting them. Thanks to many others who have tried to get the solution.
We have reviewed the solutions as per the criteria mentioned at Weekend Hack and included the comments below. Given the experience of the participants, they were really innovative and clean. Few minor comments found are included below in the order of the submission. These comments are not intended to point out the mistakes in the solutions but to learn from them.

The winner of the Weekend Hack 31 Aug 2013 - 1 is Pramod Jain. He will get a recharge or gift voucher worth Rs. 150.

# To book the seats in the given bus service

Write a program to book the seats in the given bus service. In the service, the number of rows, the seat pattern and the ticket price are given.

More Details:

1. If the seat pattern is 3-2 and the number of rows is 4, then the seat numbers will be
`AAA AAAAA AAAAA AAAAA AA` 2. The ticket price is depending upon the row of the seat, for first row an additional 50 is charged and if it is the last row a discount of 50 is provided. For other rows the price will be as given in the service definition.
3. Every booked ticket should have the information about the seat number, price, whether window or not and the location of the seat.
4. While printing the availability if the seat is available you need to print A, if it is booked print B. A space should be printed to show the aisle (path).
5. If the seat pattern is 1-2 and the number of rows is 3, then the seat numbers will be
`1 234 567 89`
6. When booking the tickets we need to fill the top most left seat available. That is the seat with the smallest number should be booked first.
7. The seat location should be provided for every ticket. If the seat pattern is 4-1 and the number of rows is three and the allocated seat number is 8, then the seat location details should be
`.... ...X. ..... .`

For a service with seat pattern 3-2, with 4 rows and ticket price 300, the details will be as shown below.

Action/Status Availability/Result
Initial Availability `AAA AAAAA AAAAA AAAAA AA`
If two tickets are booked, the ticket details are [1~350~W, 2~350~A]
Availability after booking two tickets `BBA AAAAA AAAAA AAAAA AA`
If five more tickets are booked, the ticket details are [3~350~A, 4~350~A, 5~350~W, 6~300~W, 7~300~A]
Availability after booking five more tickets `BBB BBBBA AAAAA AAAAA AA`

## Solution 1 - Submitted on Sun Sep 1 14:40:05 2013

``` import java.util.ArrayList;import java.util.List;class SeatBooking {    public static void main(String s[]) {        IService bangalore2030 = createService("BNG-2030", 4, "2-3", 300);        System.out.println("##########################");        System.out.print(bangalore2030.getAvailability());        System.out.println("##########################");        Double price = bangalore2030.bookSeats(2);        System.out.println("Booking Cost For Two Tickets is : " + price);        List tickets = bangalore2030.getLastBookedTickets();        System.out.println("The details of two tickets are :");        System.out.println("--------------------------------");        printTickets(tickets);        System.out.println("--------------------------------");        System.out.println("##########################");        System.out.print(bangalore2030.getAvailability());        System.out.println("##########################");        price = bangalore2030.bookSeats(10);        System.out.println("Booking Cost For Ten Tickets is :  " + price);        tickets = bangalore2030.getLastBookedTickets();        System.out.println("The details of ten tickets are :");        System.out.println("--------------------------------");        printTickets(tickets);        System.out.println("--------------------------------");        System.out.println("##########################");        System.out.print(bangalore2030.getAvailability());        System.out.println("##########################");    }    public static IService createService(String serviceName, int numberOfRows, String pattern, int ticketPrice) {        IService service = new Service(serviceName, numberOfRows, pattern, ticketPrice);        return service;    }    public static void printTickets(List tickets) {        if (tickets != null) {            for (ITicket ticket : tickets) {                System.out.println(ticket);                System.out.println();                System.out.print(ticket.getLocation());                System.out.println();            }        }    }}interface IService {    String getServiceName();    Double bookSeats(int numberOfSeats);    List getLastBookedTickets();    String getAvailability();}interface ITicket {    String getSeatNumber();    String getLocation();    int getPrice();    boolean isWindow();    String toString();}class Service implements IService {    String serviceName;    int numberOfRows;    String pattern;    int ticketPrice;    String availability = "";    List lastTicket;    int total;    int remainingSeats;    int currentSeatNumber;    Service(String serviceName, int numberOfRows, String pattern, int ticketPrice) {        this.serviceName = serviceName;        this.numberOfRows = numberOfRows;        this.pattern = pattern;        this.ticketPrice = ticketPrice;        remainingSeats = total * numberOfRows;        setAvailability(pattern, numberOfRows);    }    public void setAvailability(String pattern, int numberOfRows) {        String rowsAndCol[] = pattern.split("-");        // getting number of seats present at left and right in each row        int left = Integer.parseInt(rowsAndCol[0]);        int right = Integer.parseInt(rowsAndCol[1]);        total = left + right;        // forming availability string        for (int i = 0; i < numberOfRows; i++) {            for (int j = 0; j < total; j++) {                availability += "A";                if (j == left - 1)                    availability += " ";            }            availability += "\n";        }    }    public String getServiceName() {        return serviceName;    }    public Double bookSeats(int numberOfSeats) {        // if number of seats ordered are less than remaining number of seats return null        if (numberOfSeats < remainingSeats) {            return null;        }        // make each ticket ticket with its features nad adding it to list        List ticket = new ArrayList();        for (int i = 0; i < numberOfSeats; i++) {            ITicket temporary = makeTicket();            ticket.add(temporary);            // for each ticket allocated remaining seats gets reduced by 1 and availability string gets modified            remainingSeats--;            availability = availability.replaceFirst("A", "B");        }        double totalAmount = 0;        // returning total amount of all tickets ordered        for (int i = 0; i < ticket.size(); i++) {            totalAmount += ticket.get(i).getPrice();        }        // last ticket sold till now is the present ticket        lastTicket = ticket;        return totalAmount;    }    //function to make the ticket    public Ticket makeTicket() {        // seat number is the nu,ber of next seat being alloted         int seatNumber = ++currentSeatNumber;        int price = ticketPrice;        boolean isWindow = false;        // copying availability string into position string        String position = getAvailability();        // the first 'A' marks the seat position , so replacing it with 'X'        position = position.replaceFirst("A", "X");        // making others characters '.'        position = position.replaceAll("A|B", ".");        // if seat is present in first row then ticket price is casual ticket price + 50        if ((seatNumber - 1) / total == 0) {            price = price + 50;        }        // if seat is present in last row then ticket price is casual ticket price -50        else if ((seatNumber - 1) / total == numberOfRows - 1) {            price = price - 50;        }        // if seat number is leftmost or right position in the bus then it is window seat else isWindow remains false        if (seatNumber % total == 1 || seatNumber % total == 0) {            isWindow = true;        }        Ticket ticket = new Ticket(String.valueOf(seatNumber), position, price, isWindow);        return ticket;    }    public List getLastBookedTickets() {        return lastTicket;    }    public String getAvailability() {        return availability;    }}class Ticket implements ITicket {    String seatNumber;    String location;    int price;    boolean isWindow;    Ticket(String seatNumber, String location, int price, boolean isWindow) {        this.seatNumber = seatNumber;        this.location = location;        this.price = price;        this.isWindow = isWindow;    }    public String getSeatNumber() {        return seatNumber;    }    public String getLocation() {        return location;    }    public int getPrice() {        return price;    }    public boolean isWindow() {        return isWindow;    }    // function to print the ticket in the required format    @Override    public String toString() {        String string = "";        string += seatNumber + "~" + price + "~";        if (isWindow) {            string += "W";        } else {            string += "A";        }        return string;    }} ```

• It is not a good idea to name the variable as `string` in `toString` method.