0

I'm doing a simple school project but I'm stuck on something that's probably really simple.

The program is basically for a "bank owner" who has access to people's accounts and the ability to create multiple accounts.

All I need to do is allow the user to input the name of the account which they'd like to get information on

Assume I made a new account as follows:

static Account account01 = new Account( "Account 1" , 25.0 );

where "Account1" is the name of the account and "25.0" is the balance, in dollars.

Now I have the following code

System.out.println("Enter the name of the account that you want to print the    balance of");
    String nameInput = scan.nextLine();
    nameInput.toString();

I want to run the toString method for account01 which prints the account balance, but how do I do that? I tried doing nameInput.toString(); but that just prints the name I typed in. I need to substitute nameInput with the name of the account entered.

Any help is appreciated.

Bardya
  • 1
  • 1. Get a list of the accounts, 2. search for the account maching the given name, 3. print account. – Tamas Hegedus Dec 13 '15 at 03:15
  • Sorry, forgot to mention: No arrays or collection are allowed in this project. I was thinking of doing that but it's not allowed. – Bardya Dec 13 '15 at 03:22
  • There is no way to get the account from a string without searching a collection of accounts or using reflection (which is probably not something you've learned) – OneCricketeer Dec 13 '15 at 03:24
  • 1
    can't you just have a getter method which returns account balance ? – Fafore Tunde Dec 13 '15 at 03:25
  • @FaforeTunde - OP would still need to get the Account object instance from the input. – OneCricketeer Dec 13 '15 at 03:34
  • Yes I do have a getBal() method which returns the balance, but I still need to find a way to use the string text as the name of the class. – Bardya Dec 13 '15 at 03:48
  • @Bardya yes that is what I was thinking. So if there is a way to loop through your accounts (which I'm not sure there is) then you can lookup each of their properties. – Fafore Tunde Dec 13 '15 at 03:50

2 Answers2

1

You seem to be asking if you can (somehow) have the user enter the name of a variable. That is next to impossible (for a local variable) and almost certainly NOT what your instructor envisages.

That leaves you with using collections (preferable) or arrays ... or hard-wiring the accounts into the code, which seems to be a contradiction of your requirements.

"No arrays or collection are allowed in this project."

That's an extraordinary restriction1, given the requirement that the bank manager can create multiple accounts. (I guess, it is possible that you are suppose to put the information into a database and access it via JDBC, but that's not a simple exercise.)

I strongly suspect that either you have misunderstood the requirements, or that you haven't explained them to us properly.


1 - It is not impossible to achieve though. You could implement your own list or array type from scratch. Just ... a lot of work, and it seems to be "beside the point* in the context of your assignment, from what I can make out.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Afaik you can't implement an array-like type without arrays in java. The closest you can get is a linked list or a binary tree. – Tamas Hegedus Dec 13 '15 at 13:52
  • @hege_hegedus - That's what I meant. A linked list or tree or something similar could be used to simulate an array .... inefficiently. – Stephen C Dec 13 '15 at 13:58
-1

You can override the toString() of you defined Account class, to print all attribute values:

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
public String toString() {
   return ReflectionToStringBuilder.toString(this);
}

Referenced from here

You can override toString() to print your required value.

But searching for Account based on Name would require you to store the Account Objects in some Collection.

Community
  • 1
  • 1
Abhijeet
  • 8,561
  • 5
  • 70
  • 76
  • OP says `toString` is already implemented. You searching statement is more of a comment than an answer, and in the comments it is mentioned collections can't be used. – OneCricketeer Dec 13 '15 at 03:38
  • @cricket_007 `toString()` in the code is invoked on `String nameInput` & not on Account class(to which I was pointing at). //logic Override `toString()` to print Balance Loop through all the objects based on no. of Account Object created(No Collection mean limited no. of objects created). Check for scanner value against each Account Name Invoke `toString()` – Abhijeet Dec 13 '15 at 03:49
  • I understand the problem. How exactly do you loop over all the Account objects without a collection? Also, in the post *I want to run the toString method for account01 which prints the account balance*, which I took to imply `toString()` is already defined in Account – OneCricketeer Dec 13 '15 at 03:53
  • @cricket_007 , looping over objects would require to have custom implementation to manage indices of object created. Stephen mentions about custom implementation of List or Array [below](http://stackoverflow.com/a/34247818/452708) – Abhijeet Dec 13 '15 at 04:42