ex 2

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.8 kB
1
Indexable
Never
import java.util.Arrays; //imported util.array
import java.util.Scanner;

public class EventoAttendance {
    private static final int SPACE_SIZE_6 = 6;
    private static final int SPACE_SIZE_8 = 8;
    private static final int PLACE_CAPACITY = 56;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the total number of people attending the event: ");
        int totalAttendees = scanner.nextInt();

        System.out.print("Enter the number of groups: ");
        int numGroups = scanner.nextInt();

        int[] groupSizes = new int[numGroups];

        for (int i = 0; i < numGroups; i++) {
            System.out.print("Enter the size of group " + (i + 1) + ": ");
            groupSizes[i] = scanner.nextInt();
        }

        System.out.println("Total Attendees: " + totalAttendees);
        System.out.println("Number of Groups: " + numGroups);
        System.out.println("Group sizes:");
        for (int i = 0; i < numGroups; i++) {
            System.out.println("Group " + (i + 1) + ": " + groupSizes[i]);
        }

        int[] seatingPlan = createSeatingPlan(groupSizes);
        displaySeatingPlan(seatingPlan);

        scanner.close();
    }
    private static int[] createSeatingPlan(int[] groupSizes) {
        int totalVacantSeats = PLACE_CAPACITY;
        int table6Seats = 0;
        int table8Seats = 0;

        int[] sortedGroupSizes = Arrays.copyOf(groupSizes, groupSizes.length);
        Arrays.sort(sortedGroupSizes);

        for (int i = groupSizes.length - 1; i >= 0; i--) {
            int size = sortedGroupSizes[i];
            if (size >= SPACE_SIZE_8 && table8Seats + SPACE_SIZE_8 <= PLACE_CAPACITY) {
                table8Seats += SPACE_SIZE_8;
                totalVacantSeats -= SPACE_SIZE_8;
            } else if (size >= SPACE_SIZE_6 && table6Seats + SPACE_SIZE_6 <= PLACE_CAPACITY) {
                table6Seats += SPACE_SIZE_6;
                totalVacantSeats -= SPACE_SIZE_6;
            }
        }
        return new int[]{table6Seats, table8Seats, totalVacantSeats};
    }
    private static void displaySeatingPlan(int[] seatingPlan) {
        int table6Seats = seatingPlan[0];
        int table8Seats = seatingPlan[1];
        int totalVacantSeats = seatingPlan[2];

        System.out.println("\nSeating Plan:");
        System.out.println("Table 6:");
        for (int i = 0; i < table6Seats / SPACE_SIZE_6; i++) {
            System.out.println("Group size: " + SPACE_SIZE_6);
        }

        System.out.println("Table 8:");
        for (int i = 0; i < table8Seats / SPACE_SIZE_8; i++) {
            System.out.println("Group size: " + SPACE_SIZE_8);
        }
        System.out.println("Total vacant seats: " + totalVacantSeats);
    }
}