需要帮助从另一个类访问一个类。 C锐利 [英] Need help accessing a class from another class. C sharp

查看:53
本文介绍了需要帮助从另一个类访问一个类。 C锐利的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要协助才能访问帐户类。当我运行程序时,它会提示ATM类的WriteLine(欢迎/输入帐户/退出);.但是,输入数字后,命令窗口才会关闭。我不知道该怎么做。我还要提一下这是我与C Sharp合作的第一个程序。



账户类:



使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

命名空间ConsoleApplication3
{
类帐户//在帐户类中,我们有余额,提取和存款
{

/ ///一个帐户数组,用于创建3个单独的帐户,每个帐户的默认余额为$ 100.00。
// int [] myAccount = new int [3];
账户[]账户=新账户[3];


公共双重余额;

公共无效存款(双n)
{
余额+​​ = n;
}
public void withdraw(double n)
{
balance - = n;
}
public void calcInterest(double n)
{
//这是我们计算利息的地方!
}

public void menu()
{
{
{

int input = Convert.ToInt32(Console。的ReadLine());
var currAccount = account [input]; //不确定这段代码的用途。


if(account [input] == null)
{
account [input] = new Account();
account [input] .balance = 100; //将初始余额设为$ 100
}

if(输入!= 4)
{

Console.WriteLine(1)存款) ;
Console.WriteLine(2)Withdraw);
Console.WriteLine(3)Get Balance);
Console.WriteLine(4)退出);
if(输入== 1)
{
Console.WriteLine(你今天要存多少钱?);
int moneyIn = Convert.ToInt32(Console.ReadLine());
account [input] .deposit(moneyIn); //访问存款方法并按用户输入的金额增加余额。
Console.WriteLine(这是您当前的余额:+帐户[输入] .balance);
}

if(输入== 2)
{

Console.WriteLine(你今天要退出多少钱?);
int moneyOut = Convert.ToInt32(Console.ReadLine());
account [input] .withdraw(moneyOut); //访问withdraw方法并按用户输入的金额减少余额。
Console.WriteLine(这是您当前的余额:+帐户[输入] .balance);
}

if(输入== 3)
{

Console.WriteLine(这是你当前的余额:+账户[输入] 。平衡);
//返回帐户[输入] .balance;
}

if(输入== 4)
{
//我想在这里退出应用程序。
}
}
}
}
}
}
}









ATM等级:





使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

命名空间ConsoleApplication3
{
类Atm //使用atm类我们将有atm菜单
{
static void Main(string [] args )
{
int input = -1;
do
{
Console.WriteLine(Welcome!);
Console.WriteLine(请输入您的帐号(1-3或'4'退出。);
if(Int32.TryParse(Console.ReadLine(),out input))
{
if(输入> = 1&& input< = 3)
{
Console.WriteLine(您输入了+输入);
Console.ReadLine();

//ConsoleApplication3.Account [输入]; //如何在此处访问该帐户?
}
}
}
while(输入!= 4);
{
Console.WriteLine(Goodbye。);
}

}
}
}











































计划等级:





使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

命名空间ConsoleApplication3
{
class Program
{
//目前还不确定这是什么,或者甚至是否需要它。
}
}





我的尝试:



我已经在网上搜索了很长一段时间,但似乎找不到任何与我遇到的问题类似的东西。这是我在课堂上独奏的第一个程序。

解决方案

100.00。
// int [] myAccount = new int [3];
账户[]账户=新账户[3];


公共双重余额;

公共无效存款(双n)
{
余额+​​ = n;
}
public void withdraw(double n)
{
balance - = n;
}
public void calcInterest(double n)
{
//这是我们计算利息的地方!
}

public void menu()
{
{
{

int input = Convert.ToInt32(Console。的ReadLine());
var currAccount = account [input]; //不确定这段代码的用途。


if(account [input] == null)
{
account [input] = new Account();
account [input] .balance = 100; //将初始余额设为


100
}

if(输入!= 4)
{

Console.WriteLine(1)Deposit);
Console.WriteLine(2)Withdraw);
Console.WriteLine(3)Get Balance);
Console.WriteLine(4)退出);
if(输入== 1)
{
Console.WriteLine(你今天要存多少钱?);
int moneyIn = Convert.ToInt32(Console.ReadLine());
account [input] .deposit(moneyIn); //访问存款方法并按用户输入的金额增加余额。
Console.WriteLine(这是您当前的余额:+帐户[输入] .balance);
}

if(输入== 2)
{

Console.WriteLine(你今天要退出多少钱?);
int moneyOut = Convert.ToInt32(Console.ReadLine());
account [input] .withdraw(moneyOut); //访问withdraw方法并按用户输入的金额减少余额。
Console.WriteLine(这是您当前的余额:+帐户[输入] .balance);
}

if(输入== 3)
{

Console.WriteLine(这是你当前的余额:+账户[输入] 。平衡);
//返回帐户[输入] .balance;
}

if(输入== 4)
{
//我想在这里退出应用程序。
}
}
}
}
}
}
}









ATM等级:





使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

命名空间ConsoleApplication3
{
类Atm //使用atm类我们将有atm菜单
{
static void Main(string [] args )
{
int input = -1;
do
{
Console.WriteLine(Welcome!);
Console.WriteLine(请输入您的帐号(1-3或'4'退出。);
if(Int32.TryParse(Console.ReadLine(),out input))
{
if(输入> = 1&& input< = 3)
{
Console.WriteLine(您输入了+输入);
Console.ReadLine();

//ConsoleApplication3.Account [输入]; //如何在此处访问该帐户?
}
}
}
while(输入!= 4);
{
Console.WriteLine(Goodbye。);
}

}
}
}











































计划等级:





使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

命名空间ConsoleApplication3
{
class Program
{
//目前还不确定这是什么,或者甚至是否需要它。
}
}





我的尝试:



我已经在网上搜索了很长一段时间,但似乎找不到任何与我遇到的问题类似的东西。这是我在课堂上独奏的第一个程序。


Becasue你的Main方法做得很少:

 static void Main( string [] args)
{
Console.WriteLine(Welcome!);
Console.WriteLine(请输入您的帐号(1-3或'4'退出。);
int input = Convert.ToInt32(Console.ReadLine());
if(输入> = 1&& input< = 3)
{
Console.WriteLine(您输入了+输入);
Console.ReadLine();
//ConsoleApplication3.Account [input]; //如何在此处访问该帐户?
}
else if(input == 4)
{
Console。 WriteLine(再见。);
//退出应用程序
}
}

一旦输入它退出的数字,窗口关闭,你就可以'什么都看不见。

所以首先改变它并添加一个循环:

 static void Main(string [] args)
{
int input = -1;
do
{
Console.WriteLine(Welcome!);
Console.WriteLine(请输入您的帐号(1) -3或'4'退出。);
if(int32.TryParse(Conso le.ReadLine(),out input))
{
if(input> = 1&&输入< = 3)
{
Console.WriteLine(你输入了+输入);
Console.ReadLine();
//ConsoleApplication3.Account [输入]; //我如何在此处访问该帐户?输入时
}
}
}!= 4)
Console.WriteLine(Goodbye。);
}

我还用TryParse替换了Convert - 这意味着如果输入错误的东西你的应用程序不会崩溃!



你的另一个问题是我想在这里使用账户这一点 - 对于你需要的东西有点混乱但是尝试:

账户acc =新账户(); 
acc.menu();

但是你不应该把控制台的东西放在Account类中 - 它应该只关注操纵帐户而不是处理用户输入。 main方法应该处理用户输入,并将数据传递给Account类,并在用户要求发生事件时调用其中的方法。



该帐户与接口有关,因为它需要知道要调用的方法.ATM需要能够调用帐户然后通过按键选择操作。所以我需要将接口移动到ATM部分因为它包含主要方法吗?



ATM是一个界面,就像通过互联网登录一样 - 但你不要因为他们访问同一个帐户,所以我希望这两个是相同的,对吗?首先,您要将银行卡插入计算机的哪个位置? :笑:

ATM通过询问帐户本身的帐户列表(或其他)并将帐号传递给帐户,构建帐户的实例(具有正确的帐号)。

免责声明:您可能不想使用此代码(实际上我建议您不要这样做)但它可以解释我的意思:

 public类账户
{
私人静态账户[]账户=新账户[3];
私人小数余额= 0.0M;
公共账户(decimal openingDeposit,int accountNumber)
{
if(accountNumber> = 0&& accountNumber< accounts.Length)
{
余额= openingDeposit;
accounts [accountNumber] = this;
}
}
公共账户GetAccount(int index)
{
if(index> = 0&& index< accounts.Length)
{
返回账户[指数];
}
返回null;
}
公共小数MakeDeposit(十进制存款)
{
余额+​​ =存款;
返还余额;
}
公共小数MakeWithdrawl(十进制存款)
{
余额 - =存款;
返还余额;
}
公共小数GetBalance()
{
返还余额;
}
}



现在,您的Main方法可以填空:

 public static void Main(string [] args)
{
Account a = new account(123.88M,0);
账户b =新账户(456.99M,1);
账户c =新账户(1.24M,2);
...
if(int.TryParse(Console.ReadLine(),out input))
{
if(input> = 1&& input< = 3)
{
账户acc = Account.GetAccount(输入-1);
Console.WriteLine(帐户余额为:{0},acc.GetBalance());
}
}
}

这里,账户正在处理自己,这意味着它知道它必须访问哪个账户 - 这是调用Account类的实例,每个实例彼此分开。他们有不同的帐号,不同的余额 - 一个帐户不能影响另一个帐户。

那里可能有很多没有多大意义,但试着稍微掩饰一下对所发生的事情有一种感觉。



ATM(您的主要方法)知道如何通过屏幕和几个键来处理用户,但它不会影响您的帐户本身 - 它要求您的银行的主要系统执行此操作,并且真实世界的分离会反映在软件中。



有想法,并且看看你是否明白了我的意思 - 我现在已经离开,因为她自己坚持要过生活,这里对不服从的后果可能会变得严重! :笑:


Need assistance accessing the account class. When I run the program it prompts with the ATM class's WriteLine("Welcome/Enter Account/Exit");. After I enter a number however, the command window just closes. I'm not sure what to do here. I should also mention this is my first program with C Sharp.

Account Class:

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 Class:


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)
        {
                int input = -1;
            do
            {
                Console.WriteLine("Welcome!");
                Console.WriteLine("Please enter your account number (1-3 or '4' to exit.");
                if (Int32.TryParse(Console.ReadLine(), out input))
                {
                    if (input >= 1 && input <= 3)
                    {
                        Console.WriteLine("You have entered " + input);
                        Console.ReadLine();

                        //ConsoleApplication3.Account[input]; // How do I access the account here? 
                    }
                }
            }
            while (input != 4); 
                { 
                                    Console.WriteLine("Goodbye.");
                }

        }
    }
}






















Program Class:


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.
    }
}



What I have tried:

I've been searching online for quite some time but can't seem to find anything similar to the problem I'm having. This is my first program I am working on solo for class.

解决方案

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 Class:


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)
        {
                int input = -1;
            do
            {
                Console.WriteLine("Welcome!");
                Console.WriteLine("Please enter your account number (1-3 or '4' to exit.");
                if (Int32.TryParse(Console.ReadLine(), out input))
                {
                    if (input >= 1 && input <= 3)
                    {
                        Console.WriteLine("You have entered " + input);
                        Console.ReadLine();

                        //ConsoleApplication3.Account[input]; // How do I access the account here? 
                    }
                }
            }
            while (input != 4); 
                { 
                                    Console.WriteLine("Goodbye.");
                }

        }
    }
}






















Program Class:


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.
    }
}



What I have tried:

I've been searching online for quite some time but can't seem to find anything similar to the problem I'm having. This is my first program I am working on solo for class.


Becasue your Main method does very little:

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
        }
    }

As soon as you enter the number it exits and the window closes so you can't see anything.
So start by changing that and adding a loop:

static void Main(string[] args)
    {
    int input = -1;
    do
        {
        Console.WriteLine("Welcome!");
        Console.WriteLine("Please enter your account number (1-3 or '4' to exit.");
        if (int32.TryParse(Console.ReadLine(), out input))
            {
            if (input >= 1 && input <= 3)
                {
                Console.WriteLine("You have entered " + input);
                Console.ReadLine();
                //ConsoleApplication3.Account[input]; // How do I access the account here? 
                }
            }
        } while input != 4)
    Console.WriteLine("Goodbye.");
    }

I've also replaced the Convert with TryParse - which means your app doesn't crash if you type the wrong thing!

You other problem is the "I want to use Account here" bit - it's a bit muddled as to exactly what you need but try:

Account acc = new Account();
acc.menu();

But you shouldn't really be putting the console stuff in the Account class - it should be concerned only with manipulating accounts, not handling user input. You main method should handle user input, and pass data to the Account class, and call methods inside it as the user asks for things to happen.

"The account is concerned with the interface because it needs to know what method to call. The ATM needs to be able to call the account and then later choose an action, by pressing the key. So I need to move the interface to the ATM section because it contains the main method?"

The ATM is "an interface" in the same way that login via the internet is - but you don't expect the two to be the same just because they access the same Account, do you? For one thing, where are you going to insert the bank card in your computer? :laugh:
The ATM constructs an instance of the account (which has the right account number) by asking the Account List (or whatever) for the Account itself and passing it the account number.
Disclaimer: you may not want to use this code (in fact I'd recommend you don't) but it may explain what I mean:

public class Account
    {
    private static Account[] accounts = new Account[3];
    private decimal balance = 0.0M;
    public Account(decimal openingDeposit, int accountNumber)
        {
        if (accountNumber >= 0 && accountNumber < accounts.Length)
            {
            balance = openingDeposit;
            accounts[accountNumber] = this;
            }
        }
    public Account GetAccount(int index)
        {
        if (index >= 0 && index < accounts.Length)
            {
            return accounts[index];
            }
        return null;
        }
    public decimal MakeDeposit(decimal deposit)
        {
        balance += deposit;
        return balance;
        }
    public decimal MakeWithdrawl(decimal deposit)
        {
        balance -= deposit;
        return balance;
        }
    public decimal GetBalance()
        {
        return balance;
        }
    }


Now, your Main method can "fill in the blanks":

public static void Main(string[] args)
    {
    Account a = new Account(123.88M, 0);
    Account b = new Account(456.99M, 1);
    Account c = new Account(1.24M, 2);
    ...
    if (int.TryParse(Console.ReadLine(), out input))
        {
        if (input >= 1 && input <= 3)
            {
            Account acc = Account.GetAccount(input - 1);
            Console.WriteLine("The account balance is: {0}", acc.GetBalance());
            }
        }
    }

Here, the Account is dealing with itself, which means it "knows" which account it's got to access - this is called an instance of the Account class, and each instance is separate from each other. They have different account numbers, different balances - and one account can't affect another.
There's probably quite a lot there that doesn't make much sense, but try to gloss over it for a bit and get a "feel" for what happens.

The ATM (your Main method) knows how to deal with the user via a screen and a couple of keys, but it doesn't affect your account itself - it asks your bank's main systems to do that and that "real world" separation is reflected in the software.

Have a think, and see if your get what I mean - I'm off now as Herself is insisting I "get a life" and the consequences for disobedience here could get serious! :laugh:


这篇关于需要帮助从另一个类访问一个类。 C锐利的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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