Example C # bank - a class for customers - for withdrawing money, deposits, etc.

I am learning C # and I am trying to figure out when to use classes and when not.

If I write an application for a bank, I know that I will use classes for clients, which will include their name, account number, balance, etc. I would use a static class for methods that will be credited to their account, withdraw, change their address, etc., since I only need to write them once?

In addition, what will I use to track each client’s property? Having 2,000 customers:

exampleName = new Customer(); 

doesn't seem correct in my code. I'm not ready to study the database yet and am just learning classes.

+6
source share
6 answers

Having a database would be ideal, but at the same time, you could use IEnumerable to store Customer objects, for example:

 List<Customer> myCustomers = new List<Customer>(); myCustomers.Add(new Customer {Name = "Bob", Address = "123 Anywhere St." }); 

Then you can simply transfer the list to where it is needed.

Usually you will have a property of the Customer class in which the accounts are stored:

 public class Customer { public Customer() { _accounts = new List<Account>(); } public List<Account> Accounts { get { return _accounts; } set { _accounts = value; } } private List<Account> _accounts; } 

Etc. Please note that I keep it simple and makes things longer and deceiving, like you are a beginner.

Using item lists in this way is a good way to get started because you will use them when using the database; You will receive result sets from the database, and then translate these result sets into lists of business objects.

Regarding the use of static methods for conducting business logic, such as adjusting balances, changing addresses, etc., it does not matter to you at this stage. If you use tools like Resharper, this will fool you with such suggestions, but in your case, you can safely ignore it. What you should look for is to keep everything as self-sufficient as possible, to avoid data leakage and responsibility leakage between objects - this is just a good coding discipline and a good way to prevent errors caused by lack of coding.

After you make money and work, you may want to move some functions to the static classes of the "helper" style. This is absolutely normal, but be careful - helper classes are fantastic and that’s all, but can quickly turn into an anti-pattern if you do not support this coding discipline.

+5
source

You do not need to use a static class or static methods to write methods only once. This may or may not make sense, but this is the absolutely correct way to write methods without repeating :

 public class Customer { //properties, constructors, etc. public virtual void Deposit(decimal amount) { } public virtual void Withdraw(decimal amount) { } //etc } 

It also allows the use of polymorphism , e.g.

 public class BusinessCustomer : Customer { public override void Deposit(decimal amount) { //some other implementation } } 
+3
source

that for withdrawals, deposits, etc.

Those will be called Transactions .

0
source

Static classes are used when you are not going to create objects. You get one “instance” of this class - you cannot do things like:

 MyStaticClass m = new MyStaticClass(); m.SomeFunc(); 

when you have a static class. Instead, you will use it using the class name. Sort of:

 MyStaticClass.SomeFunc(); 

What will you use to track each Customer object? You can use some kind of collection to store them. Of course, in a real system for storing data there may be some kind of emphasis, probably a database. But you could just do something like:

 IEnumerable<Customer> Customers = new List<Customer>(); 

And then add your customers to this list.

 Customers.Add(new Customer() { ... }); 

Back to the question about static methods ...

So, the point here is that you are not going to refer to instance members in the static method, so you will not use static methods to update a specific client address. Assuming your Customer class is as follows:

 public class Customer { public string Address { get; set; } } 

You cannot use a static method like

 public static void SetAddress() 

because each Client (presumably) has a different address. You cannot access the client address there because it is not static. Get it? Instead, you should use a static method if you want to do something that does not need to be processed with instance data. I use static methods for functions such as utility functions.

 public static double ComputeSomething(double someVal) { ... } 

Here, the ComputeSomething function can be called by anyone who:

 var result = MyStaticClass.ComputeSomething(3.15); 

The conclusion here is that static classes are not used to create objects, but they are really used as a convenient container for storing functions. Static functions are those that can be in a non-static class, but cannot access any instance data.

One place where the static function will be used will be for the Singleton template. You make the constructor non-public so that people cannot call it, and instead provide a static method for the class to return a single instance of the class. Sort of:

 public class MySingleton { private static MySingleton instance; private MySingleton() {} public static MySingleton Instance { get { if (instance == null) { instance = new MySingleton(); } return instance; } } } 
0
source

This should be in addition to the other answers. This is an example of polymorphism with interfaces.

 public interface IDeposit { void Deposit(decimal amount); } public interface IWithdraw { void Withdraw(decimal amount); } public class Customer : IDeposit, IWithdraw { public void Deposit(decimal amount) { throw new NotImplementedException(); } public void Withdraw(decimal amount) { throw new NotImplementedException(); } } public class DepositOnlyATM : IDeposit { public void Deposit(decimal amount) { throw new NotImplementedException(); } } 

Saves concepts separately and allows you to implement multiple interfaces or just one. With class inheritance approaches, you get only one, and you get everything. Inevitably you get into spaghetti in my experience, because subclasses want some kind of behavior, but not all.

0
source

I would recommend, instead of going straight into the implementation details, that you first write down some simple user stories for your banking example. For instance,

  • As a client, I would like to open a new account so that I can make deposits and withdraw

In this requirement, we can introduce a couple of classes (client and account). From there, simply functionally decompose what the client should do and what the account should do.

I found that the book "Object-Oriented Thought Process" is well read and will help answer some of the questions: "When I do it against this."

Good luck and have fun!

0
source

Source: https://habr.com/ru/post/927615/


All Articles