settingsAccountsettings
Menusettings

Q: SoftUni Palatka Conf - Java Task

+8 votes

It’s time for fun and business and the SoftUni team is organizing the coolest event in the year – the Palatka Conf. Unfortunately, there are some questions left unanswered – will there be enough room and food for everyone? Your task is to write a program that calculates the number of meals and beds and decides if they will be enough.

Normally, you know how many rooms and how much food you have, and the only variable is the number of people. At the Palatka Conf, the case is completely opposite – you know the number of people, but random hotels, tents and meals are coming every which way.

The input you receive will be in format {tents/food/rooms} {quantity} {type}. Tents are two types: normal and firstClass -> for two and three people, accordingly. Rooms are single, double and triple -> for 1, 2 and 3 people. Meals are only two types: musaka and zakuska. The musaka can feed two, while the zakuska is enough for 0. Calculate the total amount and print on the console if everyone is well fed and had a place to sleep.

Input:

  • On the first line you will receive an integer – the number of people coming to the Palatka Conf.
  • On the second line, you will receive another integer N – the number of lines to follow.
  • On the next N lines, you receive input in the format described above.

Output:

  • There are two lines of output. On the first line, print either:
    • Everyone is happy and sleeping well. Beds left: {number of beds left}
    • Some people are freezing cold. Beds needed: {number of beds needed}
  • On the second line, one of two choices:
    • Nobody left hungry. Meals left: {number of meals left}
    • People are starving. Meals needed: {number of meals needed}

Constraints:

  • N is an integer in range [1 .. 20]
  • All other numbers in the input are integers in range [0 … 100]

Examples:

example softuni palatka java task

asked in Java category by user eiorgert
edited by user golearnweb

2 Answers

+3 votes

Here's my solution, mate:

package com.tutorials7.java.exam;

import java.util.Scanner;

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

        int totalPeople = console.nextInt();
        console.nextLine();
        int inputCount = console.nextInt();
        console.nextLine();

        int totalBeds = 0;
        int totalMeals = 0;
        for (int i = 0; i < inputCount; i++) {
            String[] line = console.nextLine().split("\\s+");
            String something = line[0];
            int count = Integer.parseInt(line[1]);
            String type = line[2];

            if (something.equals("tents")) {//TENTS
                if (type.equals("normal")) {
                    totalBeds += 2 * count;
                } else {//for firstClass tent
                    totalBeds += 3 * count;
                }
            } else if (something.equals("rooms")) {//ROOMS
                if (type.equals("single")) {
                    totalBeds += count;
                } else if (type.equals("double")) {
                    totalBeds += 2 * count;
                } else {//for triple type room
                    totalBeds += 3 * count;
                }
            } else {//for FOOD
                if (type.equals("musaka")) {
                    totalMeals += 2 * count;
                } else {//for zakuska
                    totalMeals += 0;
                }
            }
        }
        if (totalPeople > totalBeds) {
            System.out.printf(
                    "Some people are freezing cold. Beds needed: %d%n",
                    totalPeople - totalBeds);
        } else {
            System.out.printf(
                    "Everyone is happy and sleeping well. Beds left: %d%n",
                    totalBeds - totalPeople);
        }
        if (totalPeople > totalMeals) {
            System.out.printf(
                    "People are starving. Meals needed: %d%n",
                    totalPeople - totalMeals);
        } else {
            System.out.printf(
                    "Nobody left hungry. Meals left: %d%n",
                    totalMeals - totalPeople);
        }
    }
}
answered by user hues
+2 votes

...and here's mine (with some Bulgarian comments :-):

package com.tutorials7.java.exam;

import java.util.Scanner;

public class Uprajnenie {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        int peopleToCome = console.nextInt();
        console.nextLine();//PISHE SE ZARADI TOVA CHE KATO SE VZIMA ZIFRA - NE OTIVA SAMO NA SLEDVASHTIYA RED - POMAGAME MU!!!
        int rows = console.nextInt();
        console.nextLine();//PISHE SE ZARADI TOVA CHE KATO SE VZIMA ZIFRA - NE OTIVA SAMO NA SLEDVASHTIYA RED - POMAGAME MU!!!

        Integer peopleToAccT = 0;//BROYACH ZA HORA V PALATKITE
        Integer peopleToAccR = 0;//BROYACH ZA HORA V ROOM-OVETE
        Integer peopleToFeedZ = 0;//BROYACH ZA ZAKUSKITE
        Integer peopleToFeedM = 0;//BROYACH ZA MUSAKITE
        for (int i = 0; i < rows; i++) {
            String[] data = console.nextLine().split("\\s+");
            String stuff = data[0];
            Integer quantity = Integer.parseInt(data[1]);
            String type = data[2];

            //FOR TENTS
            if (stuff.equals("tents") & type.equals("normal")) {
                peopleToAccT = peopleToAccT + (2 * quantity);
            } else if (stuff.equals("tents") & type.equals("firstClass")) {
                peopleToAccT = peopleToAccT + (3 * quantity);
            }

            //FOR ROOMS
            if (stuff.equals("rooms") & type.equals("single")) {
                peopleToAccR = peopleToAccR + (1 * quantity);
            } else if (stuff.equals("rooms") & type.equals("double")) {
                peopleToAccR = peopleToAccR + (2 * quantity);
            } else if (stuff.equals("rooms") & type.equals("triple")) {
                peopleToAccR = peopleToAccR + (3 * quantity);
            }

            //FOR FOOD
            if (stuff.equals("food") & type.equals("zakuska")) {
                peopleToFeedZ = 0;
            } else if (stuff.equals("food") & type.equals("musaka")) {
                peopleToFeedM = peopleToFeedM + (2 * quantity);
            }

        }
        Integer peopleAllTogether = peopleToAccR + peopleToAccT;
        Integer feedAllTogether = peopleToFeedM + peopleToFeedZ;

        if (peopleAllTogether >= peopleToCome) {
            System.out.printf("Everyone is happy and sleeping well. Beds left: %d%n", peopleAllTogether - peopleToCome);//%n E ZA NOV RED
        } else {
            System.out.printf("Some people are freezing cold. Beds needed: %d%n", peopleToCome - peopleAllTogether);
        }

        if (feedAllTogether >= peopleToCome) {
            System.out.printf("Nobody left hungry. Meals left: %d", feedAllTogether - peopleToCome);
        } else {
            System.out.printf("People are starving. Meals needed: %d", peopleToCome - feedAllTogether);
        }
    }
}

 

answered by user sam
...