查看对象和打印详细信息

时间:2020-05-19 20:48:12

标签: c# oop

我有一个程序可以创建3个帐户,储蓄帐户,借记卡帐户和信用卡帐户。 在主程序中创建对象后,我希望用户显示所创建的帐户。

Account.cs

using System;

namespace Bank
{
    public abstract class Account{


        protected string _type;
        protected int _initbal;


        public Account(string type, int initbal){

            _type=type;
            _initbal=initbal;

        }

        public virtual int Deposit(int amount_depo){

            if(amount_depo<0){
                Console.WriteLine("Amount must be more than 0");
            }

            else{
                _initbal=amount_depo+_initbal;

            }

            return _initbal;


        }
        public virtual int Withdraw(int amount_with){

            if(_initbal<amount_with){
                Console.WriteLine("Insufficient funds");
            }

            else if(amount_with<0){
                Console.WriteLine("Amount must be more than 0");

            }

            else{

                _initbal=amount_with-_initbal;
            }

            return _initbal;

        }

        public abstract void Display();
    }

}

SavingsAccount.cs

using System;

namespace Bank
{
    public class Saving:Account{

        public int _number{get;}

        private static int Number=160140000;
        public Saving(string type, int initbal):base(type,initbal){ 

            Number++;
            _number=Number;
        }

        public override void Display(){

            Console.WriteLine("Account number: " + _number);
            Console.WriteLine("Account type: " + base._type);
            Console.WriteLine("Balance: RM" + base._initbal);


        }

   }
}

DebitAccount.cs

using System;

namespace Bank
{
    public class Debit:Saving{

        public int cardnum{get;}

        private static int Number=46360000;
        public Debit(string type, int initbal):base(type,initbal){

            Number++;
            cardnum=Number;
         }

        public override void Display(){

            Console.WriteLine("Card Number: " + cardnum);
            Console.WriteLine("Current balance: RM" + base._initbal);

        }

   }
}

CreditAccount.cs

using System;

namespace Bank
{
    public class Credit{

        public int cardnum{get;}

        private static int Number=52110000;
        public int _limit {get;}
        private static int Limit=5000;
        private string _type;
        public Credit(string type){ 
            _type=type;
            _limit=Limit;
            Number++;
            cardnum=Number;
        }

        public void Withdraw(int cred_amount_with){

            if(cred_amount_with > _limit){
                Console.WriteLine("Withdrawal limit exceeded!");

            }

            else if(cred_amount_with < 0){
                Console.WriteLine("Invalid, must be more than 0!");

            }

            else{

                Console.WriteLine("Transaction success (from credit card)");
            }



        }

        public void Display(){

            Console.WriteLine("Card Number: " + cardnum);
            Console.WriteLine("Current credit limit: RM" + _limit);


        }


   }
}

和我的 Main 程序

using System;
using System.Collections.Generic;  

namespace Bank
{
    Console.WriteLine("1.Make new saving/debit/credit account");
    Console.WriteLine("2.Delete account");
    Console.WriteLine("3.View accounts");
    int card_select = Convert.ToInt32(Console.ReadLine());


     if (card_select == 1){

             Console.WriteLine("\n|  Savings Account  |");
             Saving save1=new Saving("Saving",0);
             save1.Display();

             Console.WriteLine("\n");

             Console.WriteLine("\n|  Debit Card  |");
             Debit deb1=new Debit("Debit",0);
             save1.Display();
             Console.WriteLine("\n");

             Console.WriteLine("|  Credit Card  |");
             Credit cred1=new Credit("Credit");
             cred1.Display();

             Console.WriteLine("\nSuccess, your accounts has been created\n");

         }

我试图将帐户添加到列表中,然后在用户选择3时打印出帐户详细信息(我很确定不会起作用)

关于如何执行此操作的任何建议?

1 个答案:

答案 0 :(得分:3)

  1. 创建类型帐户对象列表的集合
  2. 在主类中添加各种帐户,只要所有不同的帐户都源自基本帐户,我们就可以做到
  3. 然后使用该列表在用户可以从控制台输入的特定索引位置中选择对象。在那里,我们必须为特定对象使用索引,或者在列表中进行迭代以找到特定帐户并用于显示!