Java program using while loops changing it to use methods
I'm a first year computer science student and I'm running into some problems. We were asked to write a simple banking program using loops then asked to write the same program using methods. I'm currently stuck on changing it to a method program.
Here is what I have for the loop program.
import java.util.Scanner;
public class Prog6
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String act = "MUSH";
double bal = 0.0;
double inc = 0.0;
while (act.charAt(0) != 'Q')
{
System.out.print((char) 27 + "[2J");
System.out.print("## IT\'S A BANK ##\nD - Deposit\nW - Withdraw\nI - Interest\nB - Balance\nQ - Quit\n\nAction:");
act = stdIn.next();
act = act.toUpperCase();
switch (act.charAt(0))
{
case 'D':
System.out.print((char) 27 + "[2J");
System.out.print("Deposit how much?:");
inc = stdIn.nextDouble();
while (inc < 0)
{
System.out.print("Deposits must be non-negative. Please try again:");
inc = stdIn.nextDouble();
}
开发者_如何学C bal += inc;
break;
case 'W':
System.out.print((char) 27 + "[2J");
System.out.print("Withdraw how much?:");
inc = stdIn.nextDouble();
while (inc < 0)
{
System.out.print("Withdrawalas must be non-negative. Please try again:");
inc = stdIn.nextDouble();
}
while (inc > bal)
{
System.out.print("Insufficient funds. Please try a lower amount:");
inc = stdIn.nextDouble();
}
bal -= inc;
break;
case 'I':
System.out.print((char) 27 + "[2J");
inc = bal * .04;
bal += inc;
System.out.print("Interest accrued: $" + inc
+ "; press enter key to return to menu.");
stdIn.nextLine();
stdIn.nextLine();
break;
case 'B':
System.out.print((char) 27 + "[2J");
System.out.print("Balance = $" + bal + "; press enter key to return to menu.");
stdIn.nextLine();
stdIn.nextLine();
break;
case 'Q':
System.out.print((char) 27 + "[2J");
break;
default:
System.out.print("Invalid input, try again:");
break;
}
}
}
}
I presume that I should make a method for getbalence
, withdraw
, deposit
, interest
but it's unclear to me how I should go about this...
I apologize if I didn't include some other information that I should have.
I really look forward to reading your responses!
Okay, I'll start you off
static void deposit(Scanner stdIn) {
System.out.print((char)27 + "[2J");
System.out.print("Deposit how much?:");
inc = stdIn.nextDouble();
while (inc < 0){
System.out.print("Deposits must be non-negative. Please try again:");
inc = stdIn.nextDouble();
}
bal += inc;
}
your switch statement will not look like this
switch (act.charAt(0)){
case 'D':
deposit(stdIn);
break;
case 'W':
// the rest of your code
What I have done, is to take a complete, re-usable function, and made a method out of it. And where the code was in your switch statement, I have replaced it with a call to that method. This makes the code easier to read, debug maintain etc, because everything is nicely separated.
You should be able to finish the rest of the application yourself.
I would also say, that doing it all from a main method and calling static methods is bad practice. It would be better to create objects to represent your Bank, but I have simply extended what you have already got, and did not want to overcomplicate for you at the first instance.
精彩评论