My system is a banking system and I want my program to loop after the user has interacted with one of the processes (withdraw, view balance etc).
After user get result I want to give a chance to continue with another process:
package smartcode.atm.h.w;
import java.util.Scanner;
public class SmartCodeATMHW {
public static void main(String[] args) {
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
do{
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
return;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
break;
default:
System.out.println("Error \nInvalid number");
break;
}
return;
}while (end == false);
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
}
I can see from the code you have already handled the requirement using end==false.
You need to remove the return; statement used after the switch case.
Also, for case 1 use break; instead of return;
public static void main(String[] args) {
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
do{
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
break;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
break;
default:
System.out.println("Error \nInvalid number");
break;
}
}while (end == false);
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
You can also take input from the User whether he wants to continue or not and depending on the response, display the options again.
This here:
for (int i = 0; i < 3; i++) {
Around that other loop:
} while (end == false)
is simply bogus.
You want one loop that goes around your "menu"; and that loop ends when the user wants to end.
End of story.
just put all cases into a infinite loop and when user select case 4 then system.exit(0); will terminate program otherwise it will loop.
import java.util.Scanner;
public class Test {
public static void main(String [] args){
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
while(true){
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
return;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
System.exit(0);
break;
default:
System.out.println("Error \nInvalid number");
break;
}
}
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.