C# Programming Sample: Switch-Case Operator

C# TASK:

Create an application that allows the user to input money value for bank deposit, and deposit rate (in percents). Once the user provides this data, he should be able to derive the state of deposit based on amount of months he inputs. And vice versa, he should be able to derive amount of months required for the deposit to reach the amount of money he inputs. Use switch-case construct for handling the different choices of the user.

SOLUTION:

In this sample we will show how to use the switch-case operator, which is essentially equal to a set of if-else statements.

switch-case

  • includes one or more switch sections.
  • each switch section contains one or more case labels.
  • each case label is followed by one or more statements.
  • the code executed will belong to the scope of specific case whose case label matches the value of the switch expression.
  • if there is no case with label matching the switch expression, the code belonging to the scope of default section is executed instead.

For our application, we will use two switch-case statements: one for displaying different versions of main menu (since the sets of actions available to users will depend on whether or not the deposit money value and deposit rate value still stay at 0 or were already modified by the user), and the other for processing the actual choices made by the user.

For simplicity, we can use a single int variable to derive the cumulative state of both values:

int flag = 0 + (deposit > 0 ? 10 : 0) + (deposit_rate > 0 ? 20 : 0);

Case 1: deposit & deposit rate both equal to zero. Flag value: 0
Case 2: deposit is a number larger than zero, while deposit rate is still equal to zero. Flag value: 10
Case 3: deposit rate is a number larger than zero, while deposit is still equal to zero. Flag value: 20
Case 4: deposit & deposit rate are numbers larger than zero. Flag value: 30
Default case: a default error message

In cases 1-3 user should be presented with two options only: inputting deposit, and inputting deposit rate. An option to exit the application becomes 3rd. However in case 4, user is presented with two additional options (to derive deposit value from amount of months, and to derive amount of months from deposit value), and the option to exit the application becomes 5th.

This switch statement should look like this:

switch (flag)
{
    case 0:
        Console.WriteLine("Press 1 for inputting the initial bank deposit");
        Console.WriteLine("Press 2 for inputting the deposit rate");
        Console.WriteLine("Press 3 for exit");
        break;
    case 10:
        Console.WriteLine("Initial bank deposit: " + deposit);
        Console.WriteLine("Press 1 for modifying the initial bank deposit");
        Console.WriteLine("Press 2 for inputting the deposit rate");
        Console.WriteLine("Press 3 for exit");
        break;
    case 20:
        Console.WriteLine("Deposit rate: " + deposit_rate +"%");
        Console.WriteLine("Press 1 for inputting the initial bank deposit");
        Console.WriteLine("Press 2 for editing the deposit rate");
        Console.WriteLine("Press 3 for exit");
        break;
    case 30:    //everything required was entered, and now more actions became available
        Console.WriteLine("Initial bank deposit: " + deposit);
        Console.WriteLine("Deposit rate: " + deposit_rate + "%");
        Console.WriteLine("Press 1 for editing the initial bank deposit");
        Console.WriteLine("Press 2 for editing the deposit rate");
        Console.WriteLine("Press 3 for learning the state of deposit based on amount of months inputted");
        Console.WriteLine("Press 4 for learning the amount of months required for the deposit to reach the money value inputted");
        Console.WriteLine("Press 5 for exit");
        break;
    default:
        Console.WriteLine("An error has occurred");
        break;
}

After the user selects one of the presented options, a second switch-case statement is used, and now some of the cases perform different functions based on 3-item (flag != 30) or 5-item (flag = 30) menu presented to the user:
Case 1: deposit value update
Case 2: deposit rate update
Case 3 (3-item menu): exit the application
Case 3 (5-item menu): calculating deposit value based on amount of months
Case 4 (3-item menu): default error message
Case 4 (5-item menu): calculating amount of months based on deposit value
Case 5 (3-item menu): default error message
Case 5 (5-item menu): exit the application
Default case: a default error message

The structure of this switch statement would look like this:

switch (textvar)
{
    case "1":   
	 //place deposit value update code here
        break;
    case "2":  
	 //place deposit value rate update code here
        break;
    case "3":  
        if (flag == 30) //5-item menu
        {
		//place code for month-based deposit calculation here
        }
        else //3-item menu
        {
            //set flag for exiting the application
        }
        break;
    case "4": 
        if (flag == 30) //5-item menu
        {
 		//place code for deposit-based amount of months calculation here
        }
        else //3-item menu
        {   
            //place default error message here
        }
        break;
    case "5":
        if (flag == 30) //5-item menu
        {
            //set flag for exiting the application
        }
        else //3-item menu
        { 
            //place default error message here
        }
        break;
    default:
        //place default error message here
        break;

Note that we are going to reuse flag variable one more time, by setting its value to 40 as a marker of exiting the application, with main do..while statement being built like this:

do
{
}
while (flag != 40);

The full code for this application (the portion inside static void Main(string[] args)) looks like this:

string textvar = "anything goes";
int flag = 0;
int deposit = 0;
int deposit_rate = 0;

do
{
    //sets flag to 10 if only deposit value was entered
    //sets flag to 20 if only deposit_rate value was entered
    //sets flag to 30 if both deposit and deposit_rate were entered
    flag = 0 + (deposit > 0 ? 10 : 0) + (deposit_rate > 0 ? 20 : 0);

    //switch-case construction (based on flag value)
    switch (flag)
    {
        case 0:
            Console.WriteLine("Press 1 for inputting the initial bank deposit");
            Console.WriteLine("Press 2 for inputting the deposit rate");
            Console.WriteLine("Press 3 for exit");
            break;
        case 10:
            Console.WriteLine("Initial bank deposit: " + deposit);
            Console.WriteLine("Press 1 for modifying the initial bank deposit");
            Console.WriteLine("Press 2 for inputting the deposit rate");
            Console.WriteLine("Press 3 for exit");
            break;
        case 20:
            Console.WriteLine("Deposit rate: " + deposit_rate +"%");
            Console.WriteLine("Press 1 for inputting the initial bank deposit");
            Console.WriteLine("Press 2 for editing the deposit rate");
            Console.WriteLine("Press 3 for exit");
            break;
        case 30:    //everything required was entered, and now more actions became available
            Console.WriteLine("Initial bank deposit: " + deposit);
            Console.WriteLine("Deposit rate: " + deposit_rate + "%");
            Console.WriteLine("Press 1 for editing the initial bank deposit");
            Console.WriteLine("Press 2 for editing the deposit rate");
            Console.WriteLine("Press 3 for learning the state of deposit based on amount of months inputted");
            Console.WriteLine("Press 4 for learning the amount of months required for the deposit to reach the money value inputted");
            Console.WriteLine("Press 5 for exit");
            break;
        default:
            Console.WriteLine("An error has occurred");
            break;
    }

    //extra space between the parts of text on screen
    textvar = Console.ReadLine();

    //variables used
    string Result;
    int finaldeposit;
    int finalmonths;
    //extra space between the parts of text on screen
    Console.WriteLine();

    //switch-case construction (based on textvar value)
    switch (textvar)
    {
        case "1":   //deposit value update
            Console.WriteLine("Enter the new value for the bank deposit");
            Result = Console.ReadLine();
            //checks for the value being a positive digit
            while (!Int32.TryParse(Result, out deposit) || deposit <= 0) 
            {
                Console.WriteLine("Not a valid larger-than-zero number, try again.");
                Result = Console.ReadLine();
            }
            break;
        case "2":   //deposit_rate value update
            Console.WriteLine("Enter the new value for the deposit rate");
            Result = Console.ReadLine();
            //checks for the value being a positive digit
            while (!Int32.TryParse(Result, out deposit_rate) || deposit_rate <= 0) 
            {
                Console.WriteLine("Not a valid larger-than-zero number, try again.");
                Result = Console.ReadLine();
            }
            break;
        case "3":   //calculation of amount of money based on amount of months
            if (flag == 30) //only works if both deposit and deposit_rate were entered as positive digits
            {
                Console.WriteLine("Enter the amount of months");
                Result = Console.ReadLine();
                int months;
                //checks for the value being a positive digit
                while (!Int32.TryParse(Result, out months) || months <= 0) 
                {
                    Console.WriteLine("Not a valid larger-than-zero number, try again.");
                    Result = Console.ReadLine();
                }
                //Input amount of months and see the amount of money
                finaldeposit = deposit;
                finalmonths = 0;
                for (int i = 0; finalmonths < months; i++)
                {
                    finaldeposit += deposit_rate * finaldeposit / 100;
                    finalmonths++;
                }
                Console.WriteLine("After " + finalmonths + " months the initial deposit of " + deposit 
                    + " will reach the value of " + finaldeposit);
            }
            else //otherwise case 3 is used for different purpose (exiting the application)
            {
                //sets flag in position recognized by do..while cycle allowing to exit the cycle (and the application)
                flag = 40;
            }
            break;
        case "4":   //calculation of amount of months based on amount of money
            if (flag == 30) //only works if both deposit and deposit_rate were entered as positive digits
            {
                Console.WriteLine("Enter the amount of money");
                Result = Console.ReadLine();
                int money;
                //checks for the value being a positive digit
                while (!Int32.TryParse(Result, out money) || money <= 0)
                {
                    Console.WriteLine("Not a valid larger-than-zero number, try again.");
                    Result = Console.ReadLine();
                }
                finaldeposit = deposit;
                finalmonths = 0;
                for (int i = 0; finaldeposit < money; i++)
                {
                    finaldeposit += deposit_rate * finaldeposit / 100;
                    finalmonths++;
                }
                Console.WriteLine("After " + finalmonths + " months the initial deposit of " + deposit 
                    + " will reach the desired value of " + money + ", becoming equal to " + finaldeposit);
            }
            else
            {   
                //otherwise a default error message is shown
                Console.WriteLine("An error has occurred");
            }
            break;
        case "5":
            if (flag == 30) //only works if both deposit and deposit_rate were entered as positive digits
            {
                //sets flag in position recognized by do..while cycle allowing to exit the cycle (and the application)
                flag = 40;
            }
            else
            { 
                //otherwise a default error message is shown
                Console.WriteLine("An error has occurred");
            }
            break;
        default:
            //a default error message is shown
            Console.WriteLine("An error has occurred");
            break;
    }
    //extra space between the parts of text on screen
    Console.WriteLine();
}
while (flag != 40);

The C# programming sample was completed by one of our experts. If you need programming help and similar task to be completed as a part of your homework, we can assist you with ease. All you have to do is to place and order and choose and expert. Contact us with projects of any difficulty level and we will solve all your problems.

You can also read our another  IT sample – https://assignmentshark.com/blog/c-programming-example-random-number-from-1-to-5/

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.