Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey! As your read in the beginning I need some help with my code. I was told to make a simple working switch statement that would be like a bank menu?

My original main looks like this

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BankAccount2
{

    class Program
    {
        static void Main(string[] args)
        {

            int bankMenu; // Making a variable that I will later use for a swtich statement

            Console.WriteLine("Welcome to Ernie's Bank");
            Console.WriteLine("Let's start of with your registery.\n");
            Console.Write("Your first name :");
            string firstName = Console.ReadLine(); // Letting the user assign his name to the variable firstName
            Console.Write("Your last name :");
            string lastName = Console.ReadLine(); // Repeating the process

            BankAcc user = new BankAcc(firstName, lastName); // I am using the class to 
            

            Console.Clear();

            Console.WriteLine("So your name is : " + firstName + "\nAnd your last name is : " + lastName); // Printing out the users name

            Console.Write("Press any key to continue");
            Console.ReadLine();
            Console.Clear();



            do 
            {
                Console.Clear();

                Console.WriteLine("This is Ernie's Bank Menu!");
                Console.WriteLine("1. Deposit\t2. Withdrawal\t3. Current Balance\n4. Exit"); // Printing out the options
                Console.Write("\n\nChoose an option from the menu : ");

                while(!Int32.TryParse(Console.ReadLine(), out bankMenu)) //Bullet proofing the code
                {
                    Console.Clear();

                    Console.WriteLine("So this is Ernie's Bank Menu!");
                    Console.WriteLine("1. Deposit\t2. Withdrawal\t3. Current Balance\n4. Exit\n\n");
                    Console.WriteLine("Valid option's only!");
                    Console.Write("Choose an option from the menu : ");
                }

                switch (bankMenu) // Making a switch statement since I am letting the user choose what he wants to do 
                {
                    case 1:
                        
                        break;
                    case 2:
                        Console.WriteLine("You chose Withdrawal!");
                        
                        break;
                    case 3: // This case is to display the user's balance
                        user.DisplayBalance();
                        Console.Write("Press any key to continue...");
                        Console.ReadLine();
                        break;
                    case 4: // This statement is the exit
                        Console.Clear();
                        Console.WriteLine("You chose Exit");
                        Console.WriteLine("\nHave a nice day " + firstName + "!");
                        Console.Write("Press any key to continue...");
                        Console.ReadKey();
                        break;
                    default:
                        Console.WriteLine("Hey! That ain't a option my man");
                        Console.WriteLine("\nPress any key to continue...");
                        Console.ReadKey();
                        break;
                }
            } while (bankMenu !=4);

And I made a class that looks like this : 

namespace BankAccount2
{
    class BankAcc
    {
        private string firstName = "";
        private string lastName = ""; //Making the variables
        private float balance;

        public BankAcc(string firstName, string lastName)
        {
            this.firstName = firstName;
            this.lastName = lastName;
            balance = 50000; // Giving the user a start balance of 50000

        }

        public BankAcc(string firstName, string lastName, float balance)
        {
            this.firstName = firstName;
            this.lastName = lastName;
            this.balance = balance;

            
        }

        public string FirstName
        {
            get // Getting his first name and then returning
            {
                return firstName;
            }
             // Setting his first name as a value
            set
            {
                firstName = value;
            }
        }

        public string LastName
        {
            get // Repeating the process from the FirstName
            {
                return lastName;
            }

            set
            {
                lastName = value;
            }
        }

        public float Balance
        {
            get // Getting the balance and then returning it. Later on I give a balance a initial value
            {
                return balance; 
            }

            set
            {
                balance = value;
            }
        }

        public void Deposit(float amount)
        {
            Console.Write("Choose an amount to deposit :");

            balance += amount;
        }

        public void Withdraw(float amount)
        {
            Console.Write("Choose an amount to withdraw");

            if (amount > balance) // If the user tries to withdraw more than his funds it will tell him insufficient funds
            {
                Console.WriteLine("Insufficient funds...");
            }
            else
            {
                balance -= amount;
            }
        }   

        public void DisplayBalance()
        {
            Console.WriteLine($"Your current balance is {balance}ISK."); // Displaying his balance
        }

    }
}


So what I want to do is to get the public void withdraw and deposit and put into into my switch statement in my main code. But I just don't know how to do that?
Would really appreciate some help since I am new to C#

What I have tried:

I've tried googling a lot and nothing came up
I tried texting some of my friends but no one is responding...
Posted
Updated 24-Nov-21 9:42am
v2

BTW, give this to your instructor and tell him to teach this stuff correctly, so experienced programmers already in the industry don't have to re-teach correct technique to new programmers fresh out of school. Just because it's a simple homework assignment does NOT mean the code he gives you can be crap.

This version of the BankAcc class allows for teachable moments for several aspects of .Net in general, and C#:

- basic class structure
- overloading constructors
- how fields and properties work together
- how regions are used for code organization
- how to use Intellisense comments, and their importance
- using return values in methods
- return void in methods
- sanity checking parameters and throwing exceptions
- industry-accepted best practice with object naming, and code commenting/formatting
- separation of concerns - avoid putting UI or presentation code in an entity

C#
public class BankAccount
{
    #region  fields

    private string  firstName = "";
    private string  lastName  = ""; 
    // ALWAYS use decimal values when dealing with money, because small math
    // errors are almost always introduced when using floating point numbers 
    private decimal balance;

    #endregion  fields

    #region  properties

    /// <summary>
    /// Get/set first name
    /// </summary>
    public string FirstName
    {
        get { return this.firstName;  }
        set { this.firstName = value; }
    }

    /// <summary>
    /// Get/set last name
    /// </summary>
    public string LastName
    {
        get { return this.lastName;  }
        set { this.lastName = value; }
    }

    /// <summary>
    /// Get/set the account balance
    /// </summary>
    public decimal Balance
    {
        get { return this.balance;  }
        set { this.balance = value; }
    }

    #endregion  properties

    #region  constructors

    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="firstName">The account holder's first name</param>
    /// <param name="lastName">The account holder's last name</param>
    /// <remarks>Calls the overloaded constructor to do actual intiialization with a balance of 50000</remarks>
    public BankAccount(string firstName, string lastName) : this(firstName, lastName, 50000)
    {
        // This constructor calls the overloaded constructor with a specific
        // balance value. Doing it this way allows us to intialize the object
        // the same way every time. It illustrates the concept of overloading,
        // as well as calling another constructor in the same class.
    }

    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="firstName">The account holder's first name</param>
    /// <param name="lastName">The account holder's last name</param>
    /// <param name="balance">The balance with which to start the account</param>
    public BankAccount(string firstName, string lastName, decimal balance)
    {
        // sanity checks - ensure that the user enters both a first and last name.
        if (string.IsNullOrEmpty(firstName))
        {
            throw new ArgumentNullException("firstName");
        }
        if (string.IsNullOrEmpty(lastName))
        {
            throw new ArgumentNullException("lastName");
        }

        this.FirstName = firstName;
        this.LastName  = lastName;
        this.Balance   = balance;
    }

    #endregion  constructors

    #region  methods

    /// <summary>
    /// Add money to the account balance
    /// </summary>
    /// <param name="amount">The anmount to add</param>
    public void MakeDeposit(decimal amount)
    {
        this.Balance += amount;
    }

    /// <summary>
    /// Take money from the account
    /// </summary>
    /// <param name="amount">The amount to take</param>
    /// <returns>True is there were sufficient funds, otherwise, false</returns>
    public bool MakeWithdrawal(decimal amount)
    {
        bool result = false;
        if (amount <= this.Balance) 
        {
            this.Balance -= amount;
            result = true;
        }
        return result;
    }   

    #endregion  fields

}
 
Share this answer
 
v3
0) We don't generally do people's homework for them. Before you try to claim that this isn't homework, don't waste your time - many of us have had this very assignment.

1) I'm assuming that the code you have so far was provided as an example by your instructor.

2) You have all of the hints in your code to do what you need to do. You have an example of how to request and process data, and you even have an example of accessing the "user" object.

3) If it were me, I would implement a nested switch statement:
C#
switch (bankMenu)
{
    case 4:
    {
        // exit
    }
    break;
    default:
    {
        switch (bankMenu)
        {
            case 1 : 
            {
                // deposit
            }
            case 2 : 
            {
                // withdrawal
            }
        }
        // show balance
    }
}


I would also add a line to the startup info display that shows the customer's beginning balance.
 
Share this answer
 
v2
Comments
Ernie25 24-Nov-21 14:26pm    
Hey, I do claim this is a assignment that I have to do, the main method is all hand written by me, but on the other hand the BankAcc Class is fully written by my instructor
#realJSOP 24-Nov-21 15:08pm    
That still doesn't invalidate my statement that we don't do people's homework for them.Didn't your instructor teach you about object-oriented programming before he gave you this assignment?
Ernie25 25-Nov-21 6:01am    
The thing is my instructor is just telling me to read the book, which still doesn't explain to me how do I put my public void Withdraw and Deposit into my Main...
#realJSOP 25-Nov-21 6:54am    
We can't compensate for a bad instructor and CodeProject is not really an instruction web site. However, there are several articles here that explain the basics.

My advice is to find appropriate articles here, AND find some online programming lessons (youtube?) that explains the basics. Your instructor is failing you if he isn't teaching the basics.

BTW, did you even *open* the book he gave you? What's the title?

If you search this site (I used "C# getting started" in the search box), several articles will come up. I don't know how good or bad they might be, but it's at least a starting point.

You may as well get used to the fact that you're going to have to read A LOT of stuff to learn not only the "how", but the "why" of things that need to be done/considered. If you feel like your instructor isn't doing an adequate job, confront him with your concerns. If he doesn't change his teaching technique, drop the class and find another one. The point is that YOU have to take control of your instruction. If need be, teach yourself. Initiative is the name of the game.
Hint: The BankAcc class should not have any user interaction (Console) statements at all. The class should be responsible for maintaining the account, nothing more. If you start throwing Console statements in there, you restrict the class to being used only in a Console application. It can no longer be used in any Windows Forms, WPF, ASP.NET, or any other application type.

The methods and properties exposed by the BankAcc class should do nothing but maintain the account properties and balance. They should check to see if the operation requested is valid, like you cannot withdraw more than the balance of the account, and return either true/false values for success/failure or throw exceptions if the data passed into the methods is bad or causes an invalid operation to happen. That choice is up to you.
 
Share this answer
 
Comments
#realJSOP 24-Nov-21 15:07pm    
It's a homework assignment, and is, in fact, a console app. My guess is that he is not tasked with modifying the account class (it was provided by the instructor), but I agree - interface code should not be in the model.
Ernie25 25-Nov-21 6:04am    
Indeed this is supposed to be a console app that my constructor is supposed to run in Visual studio while pressing F5. Most of the code is the code that I wrote and the BankAcc Class is fully written by my constructor
Dave Kreskowiak 25-Nov-21 11:36am    
Oh great! An instructor who is showing how not to do things.

Take a look at the code in Solution 3 from #realJSOP. That is how that BankAccount class should have been written, by your instructor.

What he should have been teaching is called "separation of concerns", and it makes learning this stuff a bit easier. The BankAccount class #realJSOP wrote is responsible for nothing but the data associated with an account. That's it. No user interaction at all. It's just maintaining the data and exposing the methods to manipulate it.

Understand this concept also makes it easier for you in future classes as it helps teach how to break problems down into smaller and smaller, solvable chunks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900