Click here to Skip to main content
15,889,691 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
I want to make the search function but it is not working properly.

What I have tried:

C#
namespace Banking_system
{
    class Program
    {
        static string[] customer_name;
        static int b;
         static string[] notes = new string[] { "10", "20", "50", "100", "500", "1000", "5000" };
        //static int[] notes = new int[] { 10, 20, 50, 100, 500, 1000, 5000 };
        static int[] freq_counter = new int[] {0};
        static int[] bank_balance;

        static void Main(string[] args)
        {
            int[] deposit;
            int[] retrieve;
            int choice;
            char check;  
            int a;
            Console.WriteLine("Welcome the MIB Bank");
            Console.WriteLine("First Muslim International Bank");

            Console.WriteLine("Enter the value for no of Customer in our bank");
            a = Convert.ToInt32(Console.ReadLine());
            b=a;
            customer_name = new string[a];
            deposit = new int[a];
            retrieve = new int[a];
          
            bank_balance = new int[a];
            for(int i=0;i< a; i++)
            {
                Console.WriteLine("Enter the name of the Customer");
                customer_name[i] = Console.ReadLine();

                Console.WriteLine(customer_name[i]+" Enter the current balance of");
                bank_balance[i] = Convert.ToInt32(Console.ReadLine());
            }
            while (true)
            {
                Console.WriteLine("Welcome you are the vaulable customer in MIB Bank");
                Console.WriteLine("1- Deposit Amount\n 2- Retrieve Amount\n 3- Check Balance Amount");
                choice = Convert.ToInt32(Console.ReadLine());

                if (choice == 1)
                {
                    Program p = new Program();
                    p.deposit_amount(deposit);
                }
                else if (choice == 2)
                {
                    Program p = new Program();
                    p.retrieve_amount(retrieve);
                }
                else if(choice == 3)
                {
                    Program p = new Program();
                    p.total_bank_balance();
                }
                while (true)
                {
                    Console.WriteLine("Do you want to go to the menu again\n 1- Press y or Y for Yes\n 2- Press n or N for No");
                    check = Convert.ToChar(Console.ReadLine());
                    if(check=='y' || check == 'Y')
                    {
                        break;
                    }
                    else if(check=='n' || check == 'N')
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Please Enter the Vaild Key");
                    }
                }
                if(check=='n' || check == 'N')
                {
                    break;
                }
            }
            Console.ReadLine();
        }//End of main funcion
        
        int []deposit_amount(int []deposit)
        {
            deposit = new int[b];

            for (int k = 0; k < b; k++)
            {                
                string name;

                Console.WriteLine(customer_name[k] + " Please Enter your new Deposit Amount");
                deposit[k] = Convert.ToInt32(Console.ReadLine());
                bank_balance[k] = bank_balance[k] + deposit[k];

                Console.WriteLine(customer_name[k] + "Your New Deposit amount is " + bank_balance[k]);
            }
            return deposit;
        }

        
        int[] retrieve_amount(int[]retrieve)
        {
            retrieve = new int[b];
            
            for (int k = 0; k < b; k++)
            {
                Console.WriteLine(customer_name[k] + " Please Enter your new Retrieve Amount");
                retrieve[k] = Convert.ToInt32(Console.ReadLine());
                bank_balance[k] = bank_balance[k] - retrieve[k];
                Console.WriteLine(customer_name[k] + "Your New Amount is " + bank_balance[k]);
            }
            return retrieve;
        }

        void total_bank_balance()
        {
            string name;
            int balance;
          
            Console.WriteLine("Please Enter the name for check the total balance");
            name = Console.ReadLine();
            search(name);
            for (int k = 0; k < b; k++)
            {
                Console.WriteLine(name + " Your total bank balance is " + bank_balance[k]);
            }

        }// End of Total Balance
        string search(string name)
        {   
            for (int k = 0; k < b; k++)
            {               
                if (customer_name[k] == name)
                {                    
                    return name;                    
                }
                else
                {
                    Console.WriteLine("Client name is not in our database");                    
                }
               
            }
            return "null";
        }

    }// End of Class Program
}// End of namespace Banking System
Posted
Updated 18-Dec-19 0:07am
v3
Comments
F-ES Sitecore 1-Oct-19 9:56am    
"Not working" doesn't give anyone enough information to help you. You wouldn't phone a mechanic and say "my car isn't working, how do I fix it?" Use the debugger to step through your code and work out what it is doing that you don't want it to, or not doing that you do want it to. When you have worked that out try and think of how to amend the code to do what you want. If you do some debugging and find the cause then feel free to ask a specific question about your code, but we're not here to have you dump 100 lines of code and ask us to fix it when we have no idea what it is supposed to do.
Patrice T 1-Oct-19 10:07am    
We have no idea of what you expect or what you get wrong.

I give you a starting point:
C#
using System;

namespace Banking_system
{

    class Customer
    {
        string name;
        int balance;

        public Customer(string name, int balance)
        {
            this.name = name;
            this.balance = balance;
        }
        public string Name { get => name; }
        public int Balance { get => balance; }

        public int retrieve_amount(int amount)
        {
            balance -= amount;
            return balance;
        }
        public int deposit_amount(int amount)
        {
            balance += amount;
            return balance;
        }
    }

    class Bank
    {
        string name;
        Customer[] customer;
        public Bank(string name) { this.name = name; }
        public string Name { get => name; }

        public Customer search_customer(string name)
        {
            foreach (Customer c in customer)
            {
                if (c.Name == name) return c;
            }
            // not found
            return null;
        }
        public static void Main(string[] args)
        {
            Bank bank = new Bank("MIB");

            Console.WriteLine("Welcome the {0} Bank", bank.Name);
            Console.WriteLine("First Muslim International Bank");
            Console.WriteLine("Enter the value for no of Customers in our bank");

            int customers = Convert.ToInt32(Console.ReadLine());
            bank.customer = new Customer[customers];
            for (int n = 0; n < customers; ++n)
            {
                Console.WriteLine("Customer {0}/{1}", (n + 1), customers);
                Console.WriteLine("Please enter the  customer name");
                string name = Console.ReadLine();

                Console.WriteLine("Please enter the customer initial balance of {0}", name);
                int balance = Convert.ToInt32(Console.ReadLine());
                bank.customer[n] = new Customer(name, balance);
            }

            string answer = "";
            do
            {
                Console.WriteLine("Please enter your name");
                string name = Console.ReadLine();
                Customer current_customer = bank.search_customer(name);
                if (current_customer == null)
                {
                    Console.WriteLine("Sorry we cannot find {0} in our customer list", name);
                    continue;
                }
                Console.WriteLine("Welcome in MIB bank, Valuable customer. Please choose the operation");
                Console.WriteLine("1- Deposit Amount\n2- Retrieve Amount\n3- Check Balance Amount");
                int choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        Console.WriteLine("Please enter the amount to deposit");
                        current_customer.deposit_amount(Convert.ToInt32(Console.ReadLine()));
                        break;
                    case 2:
                        Console.WriteLine("Please enter the amount to retrieve");
                        current_customer.retrieve_amount(Convert.ToInt32(Console.ReadLine()));
                        break;
                    case 3:
                        break; // nothing to do
                    default:
                        Console.WriteLine("{0} is an invalid choice", choice);
                        break;
                }
                Console.WriteLine("Your updated balance is {0}", current_customer.Balance);
                Console.WriteLine("Enter Q or q to quit, any other key to continue");
                answer = Console.ReadLine().ToUpper();
            } while (answer != "Q");
        }
    }
}// End of namespace Banking System
 
Share this answer
 
Your search function should return either a boolean value of true or false, or better still, the array index of the name you are searching for. In your code you call the search function but ignore its return value. You then display the total bank balance for every customer.
 
Share this answer
 
Hello mutasadiq iqbal

Your question is not probably one which can be resolved as it's lengthy code and we do not know what the exact data all the involved variables holds.

But let me provide a guideline,
1. please debug your code carefully
2. check value of "b" variable, it might be not allowing to pass your criteria in for loop where you check "k<b" inside search method
3. check all values held in array "customer_name", it is possible that it is not properly derived and does not hold target value you want to compare with the "name"variable
4. check value held in variable "name" itself, it is possible that it is not containing expected value to compare


In short, Your best solution is to debug code carefully, and don't lose courage until you fix it!

Happy Coding! :)
 
Share this answer
 
In your search function is some static b. That is considered to be a sever flaw
or bug
. As you need the array size than add this value as parameter to your search function and also change the return value like:

C#
string searchName(string name, int count)
        {   
            for (int k = 0; k < count; k++)
            {               
                if (customer_name[k] == name)
                {                    
                    return name;                    
                }
                else
                {
                    Console.WriteLine("Client name is not in our database");                    
                }               
            }
            return "";
        }

   // usage
   string foundName = searchName(name, countToSearch/*your job*/);
   if( !foundName.isEmpty ) {
     // found => do stuff!!!
   } else {
     // no match => handle error
   }
 
Share this answer
 

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