Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

go back to top of class and write/read to file

 
Caleb Jones
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering if i could take this code and at the end of if(deposit.equals("y") statement whether or not it is possible to start over at the top of the class (and still keep the new balance). Also, is it possible to get the current balance from a file and then save it to the file when im done with the application?


import static java.lang.System.*;
import java.util.Scanner;

class bank {
public static void main(String args[]) {
double balance;
balance = 0.00;

out.println("Your current balance is $" + balance + ".");
out.println("Do you want to make a deposit? (y\\n)");
Scanner depositScanner = new Scanner(System.in);
String deposit = depositScanner.next();
if (deposit.equals("y")) {
out.println("Please enter your deposit amount:");
Scanner depositamountScanner = new Scanner(System.in);
double depositamount = depositamountScanner.nextDouble();
balance = balance + depositamount;
out.println("Your new deposit amount is: $" + balance + ".");
}
else {
("Please come again.")
}



}
}

[ EJFH: Remove exhortation to take discussions offline; we don't do that here. ]
[ June 15, 2005: Message edited by: Ernest Friedman-Hill ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

You're probably looking for a looping construct, maybe "while". We like to hint at answers here and let you have the joy of working out the details (no sarcasm intended, honest!) so this isn't even Java...

Another hint - I'd try to make a routine that is exactly that many lines of Java. A line like "get the deposit amount" turns into a call to a new method that does the kind of prompt and read you already have.

After you get that going, think about the file bit. Read from file (if it exists), do the stuff above, rewrite the file.

Give this a try, feel encouraged to post more code. Have fun!
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just the phrase "go back to top of class" reeks of bad Object Oriented design.

Follow the advice Stan is giving you and while you're doing it try to think more object oriented. In java programs don't start at the top of a class per se. You have an entry point, such as the main method in a stand alone app, and from there you call other objects and methods to achieve what you need. in fact I'd take most of your code out of the main method and just call a constructor from there.
[ June 15, 2005: Message edited by: Hentay Duke ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic