What I want to achieve:
I'm currently diving deeper into Java by trying to create a program that reads .csv exports from bank accounts and uses that to do cool stuff.
Since different banks have different file exports, I created an abstract class Bank with only universally present data fields, like money transferred in any transaction. Then, I created a subclass for each individual bank, each extending the abstract Bank class. In each subclass I created a file loader method for managing their specific .csv standard.
My Problem: I want the program to dynamically decide which Bank subclass to use at runtime when reading any given file. That subclass then uses its methods to read the file, and what data to transfer to its Superclass. However, I don't want to add a new if(inputString == bankSubclassName) { bankSubclass.loadFile() } each time a new subclass gets added.
Is it possible to create a system that reads an argument at runtime, eg. a string, and then to uses a method from a subclass "linked" to that argument? Without having to edit the main program each time a new subclass gets added?
Currently, I seem to have a mental block and I'm totally stuck. Maybe there is a better way?
Thanks in advance!
Map<String, BankFactory>, whereinterface BankFactory { Bank create(); }. You would populate the map asmap.put("subclass", new SubclassFactory()), whereclass SubclassFactory implements BankFactory { public Bank create() { return new Subclass(); } }. You should always sanatize your input before processing. Allowing clients to dynamically load any class they want could lead to exploits similar to SQL injection.