settingsAccountsettings
Menusettings

Q: Beer Stock - C# Task

+8 votes

Before the exam starts the students who will want a beer will tell you, so on the first line you’ll get the amount of reserved beers, then the shipments will start coming in.

Each shipment will be in the format “{amount} {type}”, where type is the type of package received – either beers, sixpacks or cases and amount is the amount received, each sixpack holds exactly 6 beers and each case holds exactly 24 beers. However since the world isn’t perfect in every 100 beers exactly the 100th is always broken so it gets discarded. On the last line you’ll receive the text “Exam Over”, at that point you have to calculate all the beers received and if they are more or equal to the amount of reserved beers print “Cheers! Beer left: {amount of cases left} cases, {amount of sixpacks left} sixpacks and {amount of beers left} beers.” or

if they are less print “Not enough beer. Beer needed: {amount of cases needed} cases, {amount of sixpacks needed} sixpacks and {amount of beers needed} beers.” Where {amount of cases left/needed}, {amount of sixpacks left/needed} and {amount of beers left/needed} represent respectively the amount of beer left/needed, represented so that the most amount of cases are used, after which the most amount of sixpacks are used (see the Examples to get a better idea).

Input

The input data should be read from the console.

  • On the first line you’ll receive the amount of reserved beers.
  • On each of the next lines you will be given a string representing a shipment in the format
    {amount} {type}”*, until you receive the command “Exam Over”.
  • The names will always be given in plural regardless of amount “beers”, ”sixpacks”, ”cases”.
  • The amount and the type will be separated by exactly one space, there will be no leading or trailing spaces in the input.

The input data will always be valid and in the format described. There is no need to check it explicitly.

*HINT: Use string.Split() to separate {amount} from {type}.

Output

The output should be printed on the console.

  • If the amount of beer is more or equal to the amount of reserved beer:
    • “Cheers! Beer left: {amount of cases left} cases, {amount of sixpacks left} sixpacks and {amount of beers left} beers.”
  • If the amount of beer is less than the amount of reserved beer:
    • “Not enough beer. Beer needed: {amount of cases needed} cases, {amount of sixpacks needed} sixpacks and {amount of beers needed} beers.”
  • The names must always be printed in in plural regardless of amount “beers”, ”sixpacks”, ”cases”.

Constraints

  • The input lines will be in the range: [1… 100].
  • The amount of reserved beer, and the amount in a shipment will be valid integers in the range
    [0... 2,147,483,647]
    .
  • Allowed working time for your program: 0.25 seconds.
  • Allowed memory: 16 MB.

Examples

beer stock c# task example 1

beer stock c# task example 2

asked in C# category by user mitko
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

mitko, here is my solution:

using System;

class Beers
{
    static void Main()
    {
        ulong howManyBeers = ulong.Parse(Console.ReadLine());

        string line = Console.ReadLine();

        ulong beerCount = 0;
        while (line != "Exam Over")
        {
            string[] data = line.Split(' ');
            ulong count = ulong.Parse(data[0]);
            string type = data[1];

            if (type == "beers")
            {
                beerCount += count;
            }
            else if (type == "cases")
            {
                beerCount += count * 24;
            }
            else if (type == "sixpacks")
            {
                beerCount += count * 6;
            }
            line = Console.ReadLine();
        }

        if (beerCount >= 100)
        {
            beerCount = beerCount - (beerCount / 100);
        }

        ulong moreBeer = howManyBeers - beerCount;
        ulong casesLeft = moreBeer / 24;
        ulong beersLeft1 = moreBeer % 24;
        ulong sixPacksLeft = beersLeft1 / 6;
        ulong beersLeft2 = beersLeft1 % 6;

        ulong difBeer = beerCount - howManyBeers;
        ulong casesLeftD = difBeer / 24;
        ulong beersLeft1D = difBeer % 24;
        ulong sixPacksLeftD = beersLeft1D / 6;
        ulong beersLeft2D = beersLeft1D % 6;

        if (beerCount < howManyBeers)
        {
            Console.WriteLine("Not enough beer. Beer needed: {0} cases, {1} sixpacks and {2} beers.", casesLeft, sixPacksLeft, beersLeft2);
        }

        else if (beerCount >= howManyBeers)
        {
            Console.WriteLine("Cheers! Beer left: {0} cases, {1} sixpacks and {2} beers.", casesLeftD, sixPacksLeftD, beersLeft2D);
        }
    }
}

 

answered by user nikole
selected by user golearnweb
...