2016-09-16 7 views
-4

계정 클래스에 액세스하는 데 도움이 필요합니다. 프로그램을 실행하면 ATM 클래스의 WriteLine ("Welcome/Enter Account/Exit"); 프롬프트가 표시됩니다. 그러나 숫자를 입력하면 명령 창이 닫힙니다. 나는 여기서 무엇을해야할지 모르겠다. 나는 이것이 C Sharp의 첫 번째 프로그램이라는 것을 언급해야한다. 또한, 내가 사이트에 처음 온 사람들이 왜 내 질문에 투표하지 않는지 잘 모르겠습니다.다른 클래스에서 클래스에 액세스하는 방법.

계정 클래스 :

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

namespace ConsoleApplication3 
{ 
    class Account //Within the Account class, we have balance, withdraw,and deposit 
    { 

     ////An account array to create 3 seperate accounts each with a default balance of $100.00. 
     //int[] myAccount = new int[3]; 
     Account[] account = new Account[3]; 


     public double balance; 

     public void deposit(double n) 
     { 
      balance += n; 
     } 
     public void withdraw(double n) 
     { 
      balance -= n; 
     } 
     public void calcInterest(double n) 
     { 
      //Here is where we calculate the interest! 
     } 

     public void menu() 
     { 
      { 
       { 

        int input = Convert.ToInt32(Console.ReadLine()); 
        var currAccount = account[input]; // Not sure what this code is for. 


        if (account[input] == null) 
        { 
         account[input] = new Account(); 
         account[input].balance = 100; //Set initial balance to $100 
        } 

        if (input != 4) 
        { 

         Console.WriteLine("1) Deposit"); 
         Console.WriteLine("2) Withdraw"); 
         Console.WriteLine("3) Get Balance"); 
         Console.WriteLine("4) Exit"); 
         if(input == 1) 
          { 
          Console.WriteLine("How much would you like to deposit today?"); 
          int moneyIn = Convert.ToInt32(Console.ReadLine()); 
          account[input].deposit(moneyIn); //access the deposit method and increase balance by the amount entered by user. 
          Console.WriteLine("Here is your current balance:" + account[input].balance); 
         } 

          if(input == 2) 
          { 

          Console.WriteLine("How much would you like to withdraw today?"); 
          int moneyOut = Convert.ToInt32(Console.ReadLine()); 
          account[input].withdraw(moneyOut); //Access the withdraw method and decrease balance by the amount entered by user. 
          Console.WriteLine("Here is your current balance:" + account[input].balance); 
         } 

         if (input == 3) 
          { 

          Console.WriteLine("Here is your current balance:"+account[input].balance); 
          //return account[input].balance; 
         } 

         if (input == 4) 
         { 
          //I want to exit the application here. 
         }     
        } 
       } 
      } 
     } 
    } 
} 

ATM 클래스 :

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

namespace ConsoleApplication3 
{ 
    class Atm //With the atm class we will have the atm menu 
    { 
     static void Main(string[] args) 
     { 

      Console.WriteLine("Welcome!"); 
      Console.WriteLine("Please enter your account number (1-3 or '4' to exit."); 
      int input = Convert.ToInt32(Console.ReadLine()); 

      { 



       if (input >= 1 && input <= 3) 
       { 
        Console.WriteLine("You have entered " + input); 
        Console.ReadLine(); 
        //ConsoleApplication3.Account[input]; // How do I access the account here? 
       } 
       else if (input == 4) 
       { 
        Console.WriteLine("Goodbye."); 
        //Exit Application 
       } 
       } 
      } 
     } 
    } 

프로그램 등급 :

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

namespace ConsoleApplication3 
{ 
    class Program 
    { 
     //Not really sure what this is for at the moment, or if it is even needed. 
    } 
} 

답변

0

먼저, 계정 개체에 액세스를 초기화하고 제공하려는 경우 그것의 이름, 여기에 개체라는 theAcc.

Account theAcc = new Account(); 

당신은, 예를 들어, 균형을 해당 개체의 속성에 액세스하려는 경우, 당신은 같은 것을 할 수있는 :

Console.Writeline("The account's balance is: " + theAcc.Balance); 
+0

아직도 이해하는 데 어려움이 있습니다. – user2187341

1

첫째, 넣지 않는 Console.WriteLine 및 콘솔. 계정 클래스 내의 ReadLine 논리. 즉, 사용자 상호 작용 코드는 Atm 클래스 내부에있는 자신의 위치에 있어야하지만 자신의 메서드에 있어야합니다. 현금 지급기 클래스 내부

는 메인 메뉴 입력에 대한 반복을 줄이

Accounts[] accounts = new Accounts[3] 

같은 계정에 대한 참조를 만들 수 있습니다. ,

int input = Convert.ToInt32(Console.ReadLine()); 
{ 
    if (input >=1 && input <= 3) 
    { 
     Console.WriteLine("You have entered "+input); 
     Console.ReadLine(); 
     //Access Account 
    }      
    else if (input == 4) 
    { 
     Console.WriteLine("Goodbye."); 
     //Exit Application 
    } 
} 

그럼 또한이

if (accounts[input]==null) 
{ 
    accounts[input] = new Account(); 
} 
currAccount = accounts[input]; 
//either call the account interaction menu or write it here 

같은 연관된 계정에 액세스하는 방법을 철회 : 입력 1 & (3) 사이에있는 경우, 단지

Console.WriteLine("You have selected account "+input); 

그래서 상기 입력의 처리가 될 것이다 호출 충분한 잔고가 있는지 확인해야합니다 (당좌 대월이 허용되는 경우 최대 초과 인출 금액).

+0

아니요, 해당 항목을 readlines에 추가하지 마십시오. 내 의견을 편집하여 방법을 보여 드리겠습니다. – Martheen

+0

Accounts [] accounts = 새 계정 [3] 지금 나는 내 문제가있다. – user2187341

+1

추가 질문을하기 전에 먼저 [C# 프로그래밍 가이드] (https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx)를 읽어보십시오. – Martheen

관련 문제