2016-09-16 10 views
-4

アカウントクラスにアクセスする際の支援が必要です。プログラムを実行すると、ATMクラスのWriteLine( "Welcome/Enter Account/Exit");というプロンプトが表示されます。しかし、数字を入力すると、コマンドウィンドウが閉じます。私はここで何をすべきか分からない。私はこれがCシャープの私の最初のプログラムだと言及するべきです。また、私はサイトの初心者として、なぜ私の質問に投票していないのか分かりません。別のクラスの中からクラスにアクセスする方法。

Accountクラス:

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

あなたはまず、Accountオブジェクトにアクセスし、それをインスタンス化し、提供したい場合それは名前、ここでオブジェクトはtheAccと呼ばれます。

Account theAcc = new Account(); 

あなたは、そのオブジェクトのプロパティにアクセスしたい場合は、例えば、バランスは、あなたが何かを行うことができます:

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

まだ分かりません。 – user2187341

1

まず、Console.WriteLineをしてコンソールを入れないでください。 Accountクラス内のReadLineロジック。つまり、ユーザインタラクションコードはAtmクラス内にあるかもしれないが、独自の方法で独自の場所を持つべきです。 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 

、撤退の方法のように関連付けられたアカウントにアクセスする:入力は、3 & 1の間にある場合は、単にので、入力処理になるだろう

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

を呼び出します十分な残高があることを保証する必要があります(または、当座貸越が許可されている場合は、当座貸越限度額)。

+0

いいえ、あなたはそれらをreadlinesに追加しません。私はどのように表示するために私のコメントを編集します – Martheen

+0

アカウント[]アカウント=新しいアカウント[3]また、これはどこですか?今私は私のところに誤りがあります。 – user2187341

+1

詳細については、まず[C#プログラミングガイド](https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx)をお読みください。 – Martheen

関連する問題