• Post Reply Bookmark Topic Watch Topic
  • New Topic

keeping the balance amount  RSS feed

 
Ergo Bodur
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I have to make  a program that reads in a file for 3 things, ID ( numbers 2,1,3,4 and 8 ) , 0 or 1 ( 0 is deposit and 1 is withdraw ) and the balance being put in ( double )
I have made 4 programs trying to figure it out but the program doesnt seem to do subtract when I need it to. Any help would be appreciated but working on this for a few days now.

 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would suggest you restrict yourself to one thing; once you have done that you can easily move on to the rest of the project.
Well done with the indentation () but please get rid of the excess empty lines e.g. lines 35 27 29 41 and 42.
You have some bad design, I am afraid. You are nowhere creating a bank account object, which would encapsulate the balance. I would start with a bank account class, with a toString method. Then you can write:-Get that working. Once that is working correctly, you can move on to something different, maybe a menu, maybe the file input.
Note that you aren't using the File object in line 11.
Why did you write continue in lines 55 64 and 75?
 
Ergo Bodur
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the response! If you mean the output is a mess, yes at this point I just wanted to see if I could get it working before making it neater since it is my fourth attempt at this program. The continues I placed in the program, don't really know how they work completely, but i'm sure it makes the program skip back to the beginning. Looking at them now I probably didn't need them.

How come you want me to make it into a string? I have a file with these values.

2 0 400
2 1 500
2 1 150
1 1 300
1 0 250
1 1 150
3 0 125
4 0 145
8 0 250
8 1 125

once again it being, ID , 0 deposit and 1 withdraw. final number being the amount being put in or out.

I thought if i initialized an array starting with 0's I would be able to put numbers into it. Still new to this java programming so forgive me if my brain cant wrap around your suggestions.
My output works.... but it just keeps going gives me the final results without subtracting. For example account 2 deposits 400 than tries to withdraw 500 so it should give an error and not process it. then once again it tries to withdraw 150 which this time should work and leave the account with 250.

my output looks like this if you need to see mine.

2 0 400.0
Invalid amount...
Account: 2 tried to withdraw: 500.0
Invalid amount...
Account: 2 tried to withdraw: 150.0
Invalid amount...
Account: 1 tried to withdraw: 300.0
1 0 250.0
Invalid amount...
Account: 1 tried to withdraw: 150.0
3 0 125.0
4 0 145.0
8 0 250.0
Invalid amount...
Account: 8 tried to withdraw: 125.0



 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at line 24.
Every time you go around that loop you set the value of the current account to 0.

I'm guessing this was put in to ensure the balance was zero to begin with, but your array of doubles will be all zeros by default, so this is not necessary.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea of continue is to break out of the middle of a loop; since you put it at the end of the executable code, it does nothing.
I didn't say to make a String; I said to give your account class a toString method. All classes have a toString method inherited from Object, but it doesn't do what you want here.
Forget about the file for a bit. Simply verify that you are adding and removing money correctly. Come back to the file later.
And why are you reading from the terminal with that Scanner?
 
Ergo Bodur
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the declaring it 0 messing it up? all the accounts have to start at zero ( assuming ) and the balances are added or deducted.

I have no use for the scanner it is just there, and as for making a method I was thinking about that but didnt know how to go about it. We recently learned methods but they are confusing to me

public static void string test( )
{

}

like that right? but I get confused with the variable passing and such. sorry for late response.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about the main method for the time being. Have a look at the Java™ Tutorials where you should find some simple information about what methods are.
I can't see where you are declaring 0; but an array of primitives is automatically filled with 0s until you put other values into it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!