Hello I’ve reached a stump that I cannot pull out.
My program records the number of bottles collected by four rooms. The program should prompt me to enter a room number and then how many bottles that room has collected. When ever the user types in “quit” the program will spit out the bottles collected by each room and calculate the room with the most bottles collected. I should be able to add bottles to each room as long as I haven’t typed in quit.
I cannot get my GetRoom (int room) working, that is the method that does not return a value.
How would I find the room with the most bottles collected? Math.Max?
I cannot use LINQ or arrays. Its part of the assignment rules.
Heres is my code:
namespace BottleDrive1
{
class Program
{//Initialize 4 rooms.
int room1 = 0;
int room2 = 0;
int room3 = 0;
int room4 = 0;
static void Main(string[] args)
{
//Start of while loop to ask what room your adding into.
while (true)
{
Console.Write("Enter the room you're in: ");
//If user enters quit at anytime, the code will jump out of while statement and enter for loop below
string quit = Console.ReadLine();
if (quit == "quit")
//Break statement allows quit to jump out of loop
break;
}
}
private void SetRoom(int room, int value)
{
switch (room)
{
case 1:
room1 = value;
break;
case 2:
room2 = value;
break;
case 3:
room3 = value;
break;
case 4:
room4 = value;
break;
}
}
public void GetRoom(int room)
{
int count = int.Parse(Console.ReadLine());
switch (room)
{
case 1:
room1 += count;
break;
case 2:
room2 += count;
break;
case 3:
room3 += count;
break;
case 4:
room4 += count;
break;
default:
throw new ArgumentException();
}
}
}
}
Here’s an example that uses a Class to hold the information for each room. The reason for using a class is so that if your program needs to change in the future to collect more information, you won’t have to track yet another array, you can just add properties to the class.
The individual rooms are now held in a list instead of an array just to show a different construct.
Here is the new Room class:
And here is the new version of the program. Note that additional checking of the values entered by the end user has been added in order to prevent exceptions when trying to get the current room or parse to an int the value entered by the user: