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

本文关键字:一个 简单 程序 atm 创建 继承 | 更新日期: 2023-09-27 18:01:35

我试图使用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;
            }
        }
    }
}

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

您正在创建两个不同的对象:一个'CreateAccount'对象和一个'ATM'对象。ATM对象不会自动继承先前创建的CreateAccount对象的值,它们是两个完全不同的、不相关的实体。

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

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

这是如何正确使用继承的,继承在这种情况下实际上是无用的。在这种情况下,Dictionary是合适的数据结构,因为可以避免使用它进行重复。同样,从这段代码中,您可能希望从Account类中删除accountNo,以避免保留重复的数字,并在调用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;
                }
            }
        }
    }
}

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

类账户:

class Account
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public string PhoneNumber { get; set; }
    public double Balance { get; set; }
    // More properties here
    ...
}

类Atm:

class Atm
{
    public List<Account> Accounts { get; set; }
    public Atm()
    {
        Accounts = new List<Account>();
    }
    public void CreateAccount()
    {
        var account = new Account();
        // Get details from user here:
        ...
        account.Balance = 0.0;
        account.Id = Accounts.Count + 1;
        Accounts.Add(account);
    }
    public void Deposit()
    {
        int accountId;
        // Get input from the user here:
        // --------------------------------
        // 1. Check that the account exists
        // 2. Deposit into the account.
        ...
    }

完整例子:

class Program
{
    static void Main()
    {
        var atm = new Atm();
        while (true)
        {
            int option;
            Console.WriteLine();
            Console.WriteLine("Menu:");
            Console.WriteLine("1. Create Account");
            Console.WriteLine("2. Deposit");
            Console.WriteLine();
            Console.Write("Please make a selection: ");
            var input = int.TryParse(Console.ReadLine(), out option);
            Console.WriteLine("-----------------");
            switch (option)
            {
                case 1:
                    atm.CreateAccount();
                    break;
                case 2:
                    atm.Deposit();
                    break;
            }
        }
    }
}
class Account
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public string PhoneNumber { get; set; }
    public double Balance { get; set; }
}
class Atm
{
    public List<Account> Accounts { get; set; }
    public Atm()
    {
        Accounts = new List<Account>();
    }
    public void CreateAccount()
    {
        var account = new Account();
        Console.WriteLine("Create a new account!");
        Console.WriteLine();
        Console.Write("Enter first name: ");
        account.FirstName = Console.ReadLine();
        Console.Write("Enter last name: ");
        account.LastName = Console.ReadLine();
        Console.Write("Enter date of birth: ");
        account.DateOfBirth = DateTime.Parse(Console.ReadLine());
        Console.Write("Enter phone number: ");
        account.PhoneNumber = Console.ReadLine();
        account.Balance = 0.0;
        account.Id = Accounts.Count + 1;
        Accounts.Add(account);
    }
    public void Deposit()
    {
        int accountId;
        Console.Write("Enter your account number: ");
        int.TryParse(Console.ReadLine(), out accountId);
        var account = Accounts.FirstOrDefault(a => a.Id == accountId);
        if (account != null)
        {
            double amount;
            Console.Write("Enter amount to deposit: ");
            double.TryParse(Console.ReadLine(), out amount);
            account.Balance += amount;
            Console.Write("Your new balance is {0}", account.Balance);
        }
        else
        {
            Console.WriteLine("That account does not exist!");
        }
    }
}