-7

Please I have a c# assignment. To create a banking application that allows a teller open an account for a customer and perform transactions of deposit and withdrawal. No need for GUI. Ive used generics(I don't know if that is right). My code is below. But now I'm trying to add a .txt file so that any account created will be stored in that file because when I run the code and create an account, it goes away when I stop running. Also I'm hardcoding account numbers, I don't know how to go about that. I tried using random number generation, but when I run the code, it generates a new random number everytime. so I took it out, please help me!!!! This my code below.

The Generic Account class is below.

public class Account<T>: Person where T:class
{
    
    private static List<Account<T>> _accounts;
    
    
    private double _AccountBalance;

   
    private string _AccountName;

    
    private long _AccountNumber;


            public Account(string Name, long accountnumber, double initialBalance)
    {
        AccountBalance = initialBalance;
        AccountName = Name;
        AccountNumber = accountnumber;
    }

    public double AccountBalance
    {
        get { return _AccountBalance; }
        set { _AccountBalance = value; }
    }

    public string AccountName
    {
        get { return _AccountName; }
        set { _AccountName = value; }
    }

    public long AccountNumber
    {
        get { return _AccountNumber; }
        set { _AccountNumber = value; }
    }

    public static List<Account<T>> Accounts
    {
        get { return _accounts; }
        set { _accounts = value; }
    }

    public static List<Account<T>> saveAccount(Account<T> a)
    {
        Accounts.Add(a);
        return Accounts;
    }

   public virtual string getBalance(long Accountnumber)
   {
       
       return string.Format("{0:C}", AccountBalance);  //currency format
   } 


          public virtual double makeDeposit(long Accountnumber, double amount)
   {
       AccountBalance = AccountBalance + amount;

       return AccountBalance;
   }

   public virtual double makeWithdrawal(long Accountnumber, double amount)
   {
       
       AccountBalance = AccountBalance - amount;
       return AccountBalance;
   }
   public static StringBuilder AccountInfo(Account<T> a)
   {
       StringBuilder customerAccountInfo = new StringBuilder();

       customerAccountInfo.Append("\nAccount Name: " + a.AccountName);
       customerAccountInfo.Append("\nAccount Number: " + a.AccountNumber);   
       customerAccountInfo.Append("\nType of Account: " + typeof(T));      
       customerAccountInfo.Append("\nPresent account balance: " + a.getBalance(a.AccountNumber)); 

       return customerAccountInfo;
   }

    
}


public class Current : Account<Current>
{
    public Current(string accountName, long accountNumber, double accountBalance)
        : base(accountName, accountNumber, accountBalance)
    {
    }

    public static double overdraftWithdraw(Account<Current> a, long Accountnumber, double amount)
    {
                    a.AccountBalance = a.AccountBalance - amount;    
        return a.AccountBalance;
    }

    
}

}

public class Savings: Account<Savings>
{

    public Savings(string accountName, long accountNumber, double accountBalance)
        : base(accountName, accountNumber, accountBalance) { }


            public static double InterestOnAccount(Account<Savings> a,long accountnumber) 
    {
                     a.AccountBalance = a.AccountBalance + (BankPolicy<Savings>.interest_rate * a.AccountBalance);
         return a.AccountBalance;
    }
}

}

This bank policy class states the banks policy for the different account types.

public class BankPolicy<T> where T:class
{

    private static double MinimumAccBalance;

    private static double InterestRate;

    public static double minimumBalance
    {
        get { return MinimumAccBalance; }
        set { MinimumAccBalance = value; }
    }

    public static double interest_rate 
    {
        get { return InterestRate; }
        set { InterestRate = value; }
    }
    
}

}

Please help me. :)

I'm using a list to save right now. But it goes away every time I restart the program.

private static List<Account<T>> _accounts;
        
public static List<Account<T>> Accounts
        {
            get { return _accounts; }
            set { _accounts = value; }
        }

public static List<Account<T>> saveAccount(Account<T> a)
        {
            Accounts.Add(a);
            return Accounts;
        }
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Adora
  • 9
  • 4
  • 5
    You want people to go through this dump of code???? Post the relevant section rather – Rahul Sep 24 '15 at 14:30
  • 2
    I don't see any code where you try to save the account & read it back - can you show the bits where you attempt to do that - which appears to be what you are asking. – PaulF Sep 24 '15 at 14:32
  • Right now, I'm using a list. – Adora Sep 24 '15 at 14:39
  • 1
    looking at your classes i suggest you read up a view things on the basics of object oriented design? a person is a teller and an account is a person??? – Marco Forberg Sep 24 '15 at 14:42

2 Answers2

0

A simple google search should get you on your way. Using the following search query: C# Read and Write to a text file give you results such as the following:

How to read from and write to a text file by using Visual C# How to both read and write a file in C# - Stack Overflow c# - Easiest way to read from and write to files - Stack Overflow C# Using StreamWriter - Dot Net Perls

That should get you started. Just a side note, unless the use of generics is required for the assignment, it may be overkill for what you are trying to do.

Community
  • 1
  • 1
James Shaw
  • 839
  • 1
  • 6
  • 16
0

Cba going through your code, but if you put "using System.IO;" at the top, this following bit will work.

private void Save(string file, string text)
{
    File.WriteAllText(file,text);
}

This takes the path of a file (for example, a ".txt" file). And saves given text to it. I'd imagine that for each account you could concatenate all the information like so.

Person person; //example of an instance of "Person" class.
string text = person.PersonName+" : "+person.Gender;//etc
string filename = "C:/examplefilename.txt";
Save(filename, text);

Obviously you would tweak the value of "text" to your own use.

Sophie Coyne
  • 1,018
  • 7
  • 16