patterncsharpMinor
Calculating revenue from roller coaster rides
Viewed 0 times
rollerrevenuecoastercalculatingridesfrom
Problem
Problem description:
The task is to calculate the amount of money earned from roller
coaster rides on any given day.
attraction.
the same group will want to ride together.
the roller coaster runs out of sufficient space for the next group.
in the same order.
Here is my code:
```
public static class RollerCoaster
{
private static int CountDirhamsEarnedInOneOperation(int numAvailablePlaces, LinkedList numPeopleInEachGroup)
{
//Each passenger pays one dirham to take the ride once.
int dirhamsEarned = 0;
int numGroupsInQueue = numPeopleInEachGroup.Count();
while (numPeopleInEachGroup.First() 0)
{
dirhamsEarned += numPeopleInEachGroup.First();
numAvailablePlaces -= numPeopleInEachGroup.First();
numPeopleInEachGroup.AddLast(numPeopleInEachGroup.First());
numPeopleInEachGroup.RemoveFirst();
numGroupsInQueue--;
}
return dirhamsEarned;
}
public static long CalculateDirhamsEarned(int numAvailablePlaces, int numOperationsPerDay, LinkedList numPeopleInEachGroup)
{
long dirhamsEarned = 0;
while (numOperationsPerDay > 0)
{
dirhamsEarned += (long)CountDirhamsEarnedInOneOperation(numAvailablePlaces, numPeopleInEachGroup);
numOperationsPerDay--;
}
return dirhamsEarned;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter:\nThe number of places available;\nThe number of times the attraction can function per day;\nThe number of groups in the queue.");
string[] inputs = Console
The task is to calculate the amount of money earned from roller
coaster rides on any given day.
- There is a queue in front of the
attraction.
- Visitors might be alone or in groups. All members of
the same group will want to ride together.
- The ride begins once
the roller coaster runs out of sufficient space for the next group.
- Once a ride is over, passengers go back to the end of the queue
in the same order.
- Each passenger pays one dirham to ride the roller coaster once.
- The roller coaster can only operate a limited number of times each day.
Here is my code:
```
public static class RollerCoaster
{
private static int CountDirhamsEarnedInOneOperation(int numAvailablePlaces, LinkedList numPeopleInEachGroup)
{
//Each passenger pays one dirham to take the ride once.
int dirhamsEarned = 0;
int numGroupsInQueue = numPeopleInEachGroup.Count();
while (numPeopleInEachGroup.First() 0)
{
dirhamsEarned += numPeopleInEachGroup.First();
numAvailablePlaces -= numPeopleInEachGroup.First();
numPeopleInEachGroup.AddLast(numPeopleInEachGroup.First());
numPeopleInEachGroup.RemoveFirst();
numGroupsInQueue--;
}
return dirhamsEarned;
}
public static long CalculateDirhamsEarned(int numAvailablePlaces, int numOperationsPerDay, LinkedList numPeopleInEachGroup)
{
long dirhamsEarned = 0;
while (numOperationsPerDay > 0)
{
dirhamsEarned += (long)CountDirhamsEarnedInOneOperation(numAvailablePlaces, numPeopleInEachGroup);
numOperationsPerDay--;
}
return dirhamsEarned;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter:\nThe number of places available;\nThe number of times the attraction can function per day;\nThe number of groups in the queue.");
string[] inputs = Console
Solution
One of the suggestions I would make is to improve your input handling. You don't handle invalid integers well at all.
And similarly for the other int's you grab. (You could also do them all in one loop.)
This method:
Does too much. Break the inside of that
Other than that, there's little to say in my opinion.
string input = "invalid int";
int numAvailablePlaces = 0;
while (!int.TryParse(input, out numAvailablePlaces))
{
Console.WriteLine("Please enter a valid integer for the number of places available.");
input = Console.ReadLine();
}And similarly for the other int's you grab. (You could also do them all in one loop.)
This method:
private static int CountDirhamsEarnedInOneOperation(int numAvailablePlaces, LinkedList numPeopleInEachGroup)
{
//Each passenger pays one dirham to take the ride once.
int dirhamsEarned = 0;
int numGroupsInQueue = numPeopleInEachGroup.Count();
while (numPeopleInEachGroup.First() 0)
{
dirhamsEarned += numPeopleInEachGroup.First();
numAvailablePlaces -= numPeopleInEachGroup.First();
numPeopleInEachGroup.AddLast(numPeopleInEachGroup.First());
numPeopleInEachGroup.RemoveFirst();
numGroupsInQueue--;
}
return dirhamsEarned;
}Does too much. Break the inside of that
while loop to a new method.Other than that, there's little to say in my opinion.
Code Snippets
string input = "invalid int";
int numAvailablePlaces = 0;
while (!int.TryParse(input, out numAvailablePlaces))
{
Console.WriteLine("Please enter a valid integer for the number of places available.");
input = Console.ReadLine();
}private static int CountDirhamsEarnedInOneOperation(int numAvailablePlaces, LinkedList<int> numPeopleInEachGroup)
{
//Each passenger pays one dirham to take the ride once.
int dirhamsEarned = 0;
int numGroupsInQueue = numPeopleInEachGroup.Count();
while (numPeopleInEachGroup.First() <= numAvailablePlaces && numGroupsInQueue > 0)
{
dirhamsEarned += numPeopleInEachGroup.First();
numAvailablePlaces -= numPeopleInEachGroup.First();
numPeopleInEachGroup.AddLast(numPeopleInEachGroup.First());
numPeopleInEachGroup.RemoveFirst();
numGroupsInQueue--;
}
return dirhamsEarned;
}Context
StackExchange Code Review Q#106741, answer score: 5
Revisions (0)
No revisions yet.