如何使用继承在c#中创建一个简单的atm程序

时间:2015-07-08 18:56:42

标签: c#

我尝试使用继承使用C#创建e-ATM控制台应用程序,但每次调试时我都看到派生类值为null,而基类字段或属性已填充有价值的。为什么派生类即使在从基类继承之后也没有显示包含其数据的列表?

class CreateAccount
{
    string firstName, lastName, dateOfBirth, phoneNO, fathersName, mothersName;
    double initialBalance; int pinNo = 100, accountNo = 1234, age; DateTime yearOfBirth;

    protected static List<CreateAccount> data = new List<CreateAccount>();

    protected string FirstName
    {
        get { return this.firstName; }
        set
        {
            if (string.IsNullOrEmpty(value)) throw new Exception();
            else firstName = value;
        }
    }
    protected string LastName
    {
        get { return this.lastName; }
        set
        {
            if (string.IsNullOrEmpty(value)) throw new Exception();
            else lastName = value;
        }
    }
    protected string DateOfBirth
    {
        get { return this.dateOfBirth; }
        set
        {
            if (string.IsNullOrEmpty(value)) throw new Exception();
            else dateOfBirth = value;
        }
    }
    protected string PhoneNo
    {
        get { return this.phoneNO; }
        set
        {
            if ((string.IsNullOrEmpty(value)) || value.Length != 10)
                throw new Exception();
            else
                phoneNO = value;
        }
    }
    protected string FathersName
    {
        get { return this.fathersName; }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new Exception();
            else
                fathersName = value;
        }
    }
    protected string MothersName
    {
        get { return this.mothersName; }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new Exception();
            else
                mothersName = value;
        }
    }
    protected double InititailBalance
    {
        get { return this.initialBalance; }
        set
        {
            if (double.IsNaN(value))
                throw new Exception();

            else
                initialBalance = value;
        }
    }
    protected int PinNo
    {
        get { return this.pinNo; }
    }
    protected int AccountNo
    {
        get { return this.accountNo; }
    }
    public void GenerateAccount()
    { 
         // code for asking user for their details.
        data.Add(this);
    }
}


class ATM :CreateAccount
{
    public void Deposit()
    {
        Console.WriteLine("Enter your account number");
        int accountNo = int.Parse(Console.ReadLine());

        if (accountNo == AccountNo)
        {
            Console.WriteLine("Enter your amount you wish to deposit");
            int amount = int.Parse(Console.ReadLine());
            InititailBalance+= amount;
        }
    }
}   


class Program
{
    static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine("Menu");
            Console.WriteLine("1.Create Account");
            Console.WriteLine("2.ATM");
            Console.Write("Please enter your selections: ");
            int select = int.Parse(Console.ReadLine());

            switch (select)
            {
                case 1:
                    CreateAccount account = new CreateAccount();
                    account.GenerateAccount();
                    break;

                case 2:
                    ATM atm = new ATM();
                    atm.Deposit();
                    break;

            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您正在创建两个不同的对象:&#39; CreateAccount&#39;对象和ATM&#39;宾语。 ATM对象不会自动从先前创建的CreateAccount对象继承值,它们是两个完全不同的,不相关的实体。

因此,要使您的ATM对象具有与CreateAccount对象相同的值,您必须将CreateAccount对象复制到您的ATM对象。

CreateAccount account = new CreateAccount();
//set account variables here
ATM atm = (ATM)account;

答案 1 :(得分:0)

以下是正确使用继承的方法,实际上在这种情况下是无用的。 Dictionary是在这种情况下使用的正确数据结构,因为您可以避免重复使用它。同样,从此代码中,您可能希望从accountNo类中删除Account以避免重复的数字被保留,并询问它是否正在调用GenerateAccount()方法。所以这是完整的控制台应用程序:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ATM
{
    class Account
    {
        string firstName, lastName, dateOfBirth, phoneNO, fathersName, mothersName;
        double initialBalance; 
        int pinNo, accountNo, age; 
        DateTime yearOfBirth;

        public Account() 
        {
            pinNo = 100;
            accountNo = 1234;
        }

        public string FirstName
        {
            get { return this.firstName; }
            set
            {
                if (string.IsNullOrEmpty(value)) throw new Exception();
                else firstName = value;
            }
        }
        public string LastName
        {
            get { return this.lastName; }
            set
            {
                if (string.IsNullOrEmpty(value)) throw new Exception();
                else lastName = value;
            }
        }
        public string DateOfBirth
        {
            get { return this.dateOfBirth; }
            set
            {
                if (string.IsNullOrEmpty(value)) throw new Exception();
                else dateOfBirth = value;
            }
        }
        public string PhoneNo
        {
            get { return this.phoneNO; }
            set
            {
                if ((string.IsNullOrEmpty(value)) || value.Length != 10)
                    throw new Exception();
                else
                    phoneNO = value;
            }
        }
        public string FathersName
        {
            get { return this.fathersName; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    throw new Exception();
                else
                    fathersName = value;
            }
        }
        public string MothersName
        {
            get { return this.mothersName; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    throw new Exception();
                else
                    mothersName = value;
            }
        }
        public double InititailBalance
        {
            get { return this.initialBalance; }
            set
            {
                if (double.IsNaN(value))
                    throw new Exception();

                else
                    initialBalance = value;
            }
        }
        public int PinNo
        {
            get { return this.pinNo; }
        }
        public int AccountNo
        {
            get { return this.accountNo; }
        }
        public void GenerateAccount()
        {
            // code for asking user for their details.
        }
    }


    class ATM
    {
        public static Dictionary<int, Account> AccountsList;

        static ATM() 
        {
            AccountsList = new Dictionary<int, Account>();
        }

        public void CreateAccount()
        {
            Account acc = new Account();
            acc.GenerateAccount();
            AccountsList.Add(acc.AccountNo, acc);
        }

        public void Deposit()
        {
            Console.WriteLine("Enter your account number");
            int accountNo = int.Parse(Console.ReadLine());

            if (AccountsList.ContainsKey(accountNo))
            {
                Console.WriteLine("Enter your amount you wish to deposit");
                int amount = int.Parse(Console.ReadLine());
                AccountsList[accountNo].InititailBalance += amount;
            }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            ATM atm = new ATM();
            while (true)
            {
                Console.WriteLine("Menu");
                Console.WriteLine("1.Create Account");
                Console.WriteLine("2.ATM");
                Console.Write("Please enter your selections: ");
                int select = int.Parse(Console.ReadLine());

                switch (select)
                {
                    case 1:
                        atm.CreateAccount();
                        break;

                    case 2:
                        atm.Deposit();
                        break;
                    default:
                        Console.WriteLine("Invalid selection!");
                        break;
                }
            }
        }
    }
}

答案 2 :(得分:0)

FXMLLoaderImage的操作,这就是为什么我认为你不应该使用继承。我提出这个解决方案:

班级帐户:

// path is relative to working dir: use an absolute path if needed
File imageFile = new File("Library/risk-468289_640.png"); 
String imageURL = imageFile.toURI().toURL().toExternalForm(); 
helpPic.setImage(new Image(imageURL));

Class Atm:

CreateAccount

完整示例:

Atm