java - How to go back to steps in a program? -
i have bank account program rewriting when wrote in school , wondering how go step within program.
so, after create account, , choose option withdrawl, go , prompt option once again, how done? (see comment in code) thanks..
main class:
import java.text.*; public class bankaccounttest { public static void main (string args[]){ numberformat formatter = numberformat.getnumberinstance(); formatter.setmaximumfractiondigits(2); // helps formatter format final output formatter.setminimumfractiondigits(2); consolereader console = new consolereader(system.in); system.out.println("hello, make new bank account?"); string newa = console.readline(); if(newa.equalsignorecase("yes")){ system.out.println("how deposit initially?"); double init = console.readdouble(); bankaccount account = new bankaccount(init); system.out.println("your account created, do? /n 1: balance /n 2: account id /n 3: make withdrawl /n 4: make deposit?"); string option = console.readline(); if(option.equalsignorecase("get balance")){ system.out.println(account.getbalance()); //go if after excecutes } } } }
bank account class:
public class bankaccount { public static int bankid = 0; //constructor called bankaccount michaelsbank = new bankaccount(); public bankaccount(){ balance = 0; accnum = bankid++; } //constructs bank account initial deposit, used if given number parameter public bankaccount(double initialbalance){ balance = initialbalance; } public void deposit(double amount){ balance = balance + amount; } public void withdraw(double amount){ balance = balance - amount; } public double getbalance(){ return balance; } public int getid(){ return accnum; } private int accnum; private double balance; }
console reader class:
import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.io.ioexception; /** class read strings , numbers input stream. class suitable beginning java programmers. constructs necessary buffered reader, handles i/o exceptions, , converts strings numbers. */ public class consolereader { /** constructs console reader input stream such system.in @param instream input stream */ public consolereader(inputstream instream) { reader = new bufferedreader (new inputstreamreader(instream)); } /** reads line of input , converts integer. input line must contain nothing integer. not added white space allowed. @return integer user typed */ public int readint() { string inputstring = readline(); int n = integer.parseint(inputstring); return n; } /** reads line of input , converts floating- point number. input line must contain nothing nunber. not added white space allowed. @return number user typed */ public double readdouble() { string inputstring = readline(); double x = double.parsedouble(inputstring); return x; } /** reads line of input. in (unlikely) event of ioexception, program terminates. @return line of input user typed, null @ end of input */ public string readline() { string inputline = ""; try { inputline = reader.readline(); } catch(ioexception e) { system.out.println(e); system.exit(1); } return inputline; } private bufferedreader reader; }
string option = console.readline(); while(option.equalsignorecase("get balance")){ system.out.println(account.getbalance()); //go if after excecutes option = console.readline(); }
simple loop should ok solve it. please notice should read console once again after line executes prevent infinite loop.
Comments
Post a Comment