该名称在当前上下文中不存在 [英] name does not exist in current context

查看:90
本文介绍了该名称在当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Text;


class Bank
{
   static List <account> accounts = new List <account> ();
   static Account active_account; // this will be an active account (reference) retrived from 
				// the bank. This will need to initialised either by 
				// CreateAccount or by RetrieveAccount.
   static void Main()
   {
       bool running = true;
       bool valid_account = false;
       bool valid_PIN = false;
       int user_option = 0;
       string account_name;
       int account_number;
       int account_pin; 
       
            while (running)
	{
			Console.WriteLine("Main Menu: Choose one of the 6 options:\n");
			Console.WriteLine("1. Create Account\n");
			Console.WriteLine("2. View account details\n"); 
			Console.WriteLine("3. Deposit\n");
			Console.WriteLine("4. Withdraw\n");
			Console.WriteLine("5. Remove Account\n");
			Console.WriteLine("6. Display all accounts\n");
			Console.WriteLine("7. Exit\n");
			Console.WriteLine("Enter a choice:");
            user_option = SafeReadInteger(0);

            if (user_option == 1)
            {
                Console.WriteLine("You wish to create a new account");
                Console.WriteLine("Please enter the name of the new account:");
                account_name = Console.ReadLine();
                Console.WriteLine("Please enter an account number:");
                account_number = SafeReadInteger(0);
                Console.WriteLine("Please enter a pin for your account:");
                account_pin = SafeReadInteger(0);
            }
            else if (user_option == 2)
            {
              
                Console.WriteLine("Please enter account number:");
                account_number = SafeReadInteger(0);
                active_account = RetrieveAccount(account_number);
                if (valid_account == null)
                {
                    Console.WriteLine("Invalid");
                }
                else
                {
                    Console.WriteLine("Please enter your pin:");
                    account_pin = SafeReadShort(0);
                    valid_PIN = active_account.Check_PIN(account_pin);
                    while (valid_PIN == false)
                    {
                        Console.WriteLine("Please enter your pin:");
                        account_pin = SafeReadShort(0);
                        valid_PIN = active_account.Check_PIN(account_pin);
                    }
                }
            }
            else if (user_option == 3)
            {
                Console.WriteLine("Please enter your account number:");
                account_number = SafeReadInteger(0);
                Console.WriteLine("Please enter the amount you wish to deposit:");
            }
       }
    }
   static Account CreateAccount(int acc_num, short pin, string owner, decimal balance)
   {
       active_account = new Account(acc_num, pin, owner, balance);
       accounts.Add(active_account);
       return active_account;
   }
    private static Account RetrieveAccount(int acc_num)
    {
        foreach (Account acc in accounts)
        {
            if (acc.Check_Account(acc_num))
            {
                return acc;
            }
        }
        return null;
 
    }
    static void DisplayAccount(int acc_num)
    {
        {
            foreach (Account acc in accounts)
                Console.WriteLine("Account Number: {0} Account Balance: {1} Account Owner:  {2}",acc.Get_AccNum(), acc.Get_Balance(), acc.Get_Owner());
        }
    }
    static void BankDeposit(int acc_num, decimal amount)
    {
        active_account = RetrieveAccount(acc_num);
        active_account.Deposit(amount);
    }
    //static bool BankWithdraw(int acc_num, decimal amount)
    //{
    //    active_account = RetrieveAccount(acc_num);
    //    active_account.Withdraw(amount);
    //}
    
    //static void DeleteAccount(int acc_num)
    //{
    //    active_account = RetrieveAccount(acc_num);
           
    //            accounts.Remove(accounts);

    //            return;
    //}
}
    //static void DisplayAccounts(int acc_num)
    //{
    //      foreach (int acc_num in accounts)
    //        {
    //            Console.Writeline(acc_nums);
    //        }
    //}
    
 

class Account
{
    private int acc_num;
    private short acc_pin;
    private string acc_owner;
    private decimal acc_balance;
    // The following Constructors are used to initialise the attributes above
    public Account(int acc_num_value, short acc_pin_value, string acc_owner_value, decimal acc_balance_value)
    {
        acc_num = acc_num_value;
        acc_pin = acc_pin_value;
        acc_owner = acc_owner_value;
        acc_balance = acc_balance_value;
    }


    public Account(int acc_num_value, short acc_pin_value, string acc_owner_value)
    {
        acc_num = acc_num_value;
        acc_pin = acc_pin_value;
        acc_owner = acc_owner_value;
    }
    public bool Check_Account(int acc_num_value)
    {
        if (acc_num_value == acc_num)
        {
            return true;
        }
        else return false;
    }
    public bool Check_PIN(short acc_pin_value)
    {
        if (acc_pin_value == acc_pin)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void Deposit(decimal amount_value)
    {
        if (amount_value >= 0)
        {
            acc_balance += amount_value;
        }
        else if (amount_value < 0)
        {
            Console.WriteLine("Wrong Input");
        }
    }
    public bool Withdraw(decimal amount_value)
    {
        if (amount_value > acc_balance)
        {
            return false;
        }
        else
        {
            acc_balance -= amount_value;
            return true;
        }
    }
    public string Get_Owner()
    {
        return this.acc_owner;
    }
    public decimal Get_Balance()
    {
        return this.acc_balance;
    }
    public int Get_AccNum()
    {
        return this.acc_num;
    }
    public static int SafeReadInteger1 (int defaultVal)
    {
        try
        {
            return int.Parse(System.Console.ReadLine());
        }
        catch
        {
            return defaultVal;
        }
    }
    static short SafeReadShort(short default_value)
    {
        try
        {
            return short.Parse(Console.ReadLine());
        }
        catch
        {
            return default_value;
        }
    }
}</account></account>

推荐答案

此代码存在很多问题.
1)没有称为"SafeReadInteger1"的方法,即SafeReadInteger1,其次是在错误的类中定义的,或者将其移至主函数所在的同一类,或使用该类的名称来调用它.
2)其他一些方法会因参数错误而导致编译时错误(例如active_account.Check_PIN)
这就是我无需编译您的代码即可完成的全部工作.希望对您有帮助
There are quite a few problems with this code.
1) There is no method called "SafeReadInteger" it’s called SafeReadInteger1 and second it’s defined in wrong class either move it to same class in which the main function is or use the name of the class to call it .
2) Some other method will give you compile time error because of wrong parameter(like active_account.Check_PIN)
That is all I can do without compiling your code. Hope this helps


这篇关于该名称在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆