import java.util.Scanner ;
public class Mal {
public static void main (String []args) {
System.out.println("Welcome") ;
Scanner myinput=new Scanner (System.in) ;
System.out.println("Make your choice. \n 1.Check a card number \n 2.Quit.");
int choise=myinput.nextInt();
switch(choise) {
case 1:System.out.println("Enter your credit card number: ");
break;
case 2:System.out.println("Are you sure?") ;
String answer=myinput.next();
if(answer.equals("yes")){
System.out.println("Byee :)") ;
System.exit(0);
}
break;
default: System.out.println("Idiot!") ;
break;
}
}
}
i want a program which starts again if user types something different than "yes"
Here's what you can do
have a flag like this
boolean quit = false;
and a while enclosing your options
while(!quit){
...
...
}
and
set quit = true if the user wants to quit.
EDIT:
Something like this
public static void main(String a[]) {
System.out.println("Welcome");
Scanner myinput = new Scanner(System.in);
boolean quit = false;
while (!quit) {
System.out
.println("Make your choise. \n 1.Check a card number \n 2.Quit.");
int choise = myinput.nextInt();
switch (choise) {
case 1:
System.out.println("Enter your credit card number: ");
break;
case 2:
System.out.println("Are you sure?");
String answer = myinput.next();
if (answer.equals("yes")) {
System.out.println("Byee :)");
quit= true;
}
break;
default:
System.out.println("Idiot!");
break;
}
}
Related
I need to make atm machine for school project.
I finished all and all is working fine, and i make the validation for the pin because it is string. So my problem is how to make validation for all other methods to check if anything else but numbers is entered to say the user that is wrong and to return him on the start of that method. All variables are stored into array as integers.
So here is my code please help i tried so many things and i cant make it work..
public class Banka {
static Scanner skener = new Scanner(System.in);
static String pin[] = {"1234","2345","3456","4567","5678"};
static String username[] = {"Mate","Nathan","John","Michelle","Angelina"};
static int balance[] = {200,100,250,150,300};
static boolean overdraft[] = {true,true,false,true,false};
static int index;
public static void main(String[] args) {
login();
}
public static void login() {
Date datum = new Date();
System.out.println("" +datum);
System.out.println("------------------------------");
System.out.println("Welcome to the Illuminati Bank \n Please log in with your PIN");
String login = skener.next();
checkpin(login);
for (int i = 0; i< pin.length; i++) {
if (login.matches(pin[i])) {
index = i;
System.out.println("\nWelcome " + username[index]);
Menu();
}
}
System.out.println("Wrong PIN entered, please login again \n");
login();
}
public static void Menu() {
System.out.println("Please select an option");
System.out.println("\n 1.View Bank Statement \n 2.Deposit \n 3.Withdraw \n 4.Change Pin \n 5.Exit \n");
int choice = skener.nextInt();
switch (choice) {
case 1: statement();
break;
case 2: deposit();
break;
case 3: withdraw();
break;
case 4: change();
break;
case 5: exit();
break;
default: System.out.println("Incorrect Choice ");
Menu();
}
}
public static void statement() {
switch(index) {
case 0: case 1: case 2: case 3: case 4:
System.out.println("" +username[index]+ ", your balance is: " +balance[index]+ "€");
if (overdraft[index] == true) {
System.out.println("You are entitled to overdraft");
}
else {
System.out.println("You are NOT entitled to overdraft");
}
Menu();
}
}
public static void deposit() {
System.out.println("" +username[index]+ ", how much you wish to deposit?");
int deposit = skener.nextInt();
balance[index] = balance[index] + deposit;
System.out.println("Thank you, you deposited " +deposit+ "€, now you have " +balance[index]+ "€ total");
depositm();
}
public static void depositm(){
System.out.println("\n 1.Deposit more \n 2.Exit to menu");
int more = skener.nextInt();
switch (more) {
case 1: deposit();
break;
case 2: Menu();
default: System.out.println("Wrong choice, please choose again");
depositm();
}
}
public static void withdraw() {
System.out.println("" +username[index]+ ", how much you wish to withdraw?");
int withdraw = skener.nextInt();
if (overdraft[index] == true) {
balance[index] = balance[index] - withdraw;
System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
Menu();
}
if(overdraft[index] == false && balance[index] >= withdraw)
{balance[index] = balance[index] - withdraw;
System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
Menu();
}
else {
System.out.println("You have insufficient funds \nPlease try again");
withdraw();
}
}
public static void change() {
System.out.println("" +username[index]+ ", do you want to change your pin?");
System.out.println("Press 1 to change or 2 to exit to menu");
int change = skener.nextInt();
switch (change) {
case 1: System.out.println("Please enter new PIN");
pin[index] = skener.next();
System.out.println("You successfully changed your PIN");
Menu();
case 2: System.out.println("Your PIN remains unchanged");
Menu();
default: System.out.println("Wrong choice, please choose again");
change();
}
}
public static void exit(){
System.out.println("Goodbye " +username[index]+ ", Illuminati Bank wish you all the best \n");
login();
}
public static int checkpin(String x){
while(!x.matches("\\d{4}")){
System.err.println("\n Error.\n Please enter 4 digit pin.");
login();
}
return 0;
}
}
so if any one can help me how to validate all other methods with user inputs where is INTs that would be great.
Try this
String input = "xxxx";
int pin;
try{
pin = Integer.parseInt(input);
}catch(NumberFormatException e){
// input contains letters or symbols.
}
Or here's another one using the Character class.
String input = "xxxx";
boolean allDigits = true;
for(char ch: input.toCharArray()){
if(!Character.isDigit(ch)) {
allDigits = false;
break;
}
}
if(allDigits){
// input contains only digits.
}
Edit: Answering this comment.
You can modify your method like this,
public static void checkpin(String x) {
if (x.length() == 4) {
try {
Integer.parseInt(x);
login();
} catch (NumberFormatException e) {
System.err.println("\n Error.\n Invalid pin.");
}
} else {
System.err.println("\n Error.\n Please enter 4 digit pin.");
}
}
this way the method login() is called only if the pin has 4 digits and all the digits are numbers.
import java.util.Scanner ;
public class Mal {
public static void main (String []args) {
System.out.println("Welcome") ;
Scanner myinput=new Scanner (System.in) ;
System.out.println("Make your choice. \n 1.Check a card number \n 2.Quit.");
int choise=myinput.nextInt();
switch(choise) {
case 1:System.out.println("Enter your credit card number: ");
break;
case 2:System.out.println("Are you sure?") ;
String answer=myinput.next();
if(answer.equals("yes")){
System.out.println("Byee :)") ;
System.exit(0);
}
break;
default: System.out.println("Idiot!") ;
break;
}
}
}
i want a program which starts again if user types something different than "yes"
Here's what you can do
have a flag like this
boolean quit = false;
and a while enclosing your options
while(!quit){
...
...
}
and
set quit = true if the user wants to quit.
EDIT:
Something like this
public static void main(String a[]) {
System.out.println("Welcome");
Scanner myinput = new Scanner(System.in);
boolean quit = false;
while (!quit) {
System.out
.println("Make your choise. \n 1.Check a card number \n 2.Quit.");
int choise = myinput.nextInt();
switch (choise) {
case 1:
System.out.println("Enter your credit card number: ");
break;
case 2:
System.out.println("Are you sure?");
String answer = myinput.next();
if (answer.equals("yes")) {
System.out.println("Byee :)");
quit= true;
}
break;
default:
System.out.println("Idiot!");
break;
}
}
I am having a bit difficulty with my code. After depositing an amount $30, it is suppose to bring back the Saving Menu
A- Deposit
B - Withdraw
C - Report
D-Return Main menu
Somehow it keeps scanning $30 instead of asking a new input.
Thanks to anyone who will try helping me out.
package Menu;
import java.util.Scanner;
enum Options {
A, B, C, D
}
public class Menu {
public Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Menu menu = new Menu();
menu.mainMenu();
}
public void mainMenu() {
String userChoice;
boolean quit = false;
do {
System.out.println("Bank Menu" + "\nA: Savings" + "\nB: Checking"
+ "\nC: Exit");
userChoice = sc.next().toUpperCase();
switch (userChoice) {
case "A":
savingMenu savMen = new savingMenu();
break;
case "B":
break;
case "C":
quit = true;
break;
default:
System.out.println("Wrong entry.");
break;
}
System.out.println();
} while (!quit);
sc.close();
System.exit(0);
}
}
package Menu;
import Savings.Savings;
public class savingMenu extends Menu {
Savings sav = new Savings();
public savingMenu() {
String userChoice;
boolean quit = false;
/**
* A - Deposit B - Withdraw C - Report D - Return Menu
*/
do {
System.out.println("Savings Menu" + "\nA: Deposit "
+ "\nB: Withdraw" + "\nC: Report"
+ "\nD: Return to Bank Menu");
userChoice = sc.nextLine().toUpperCase();
switch (userChoice) {
case "A":
sav.makeDeposit();
break;
case "B":
sav.makeWithdraw();
break;
case "C":
sav.doMonthlyReport();
case "D":
quit = true;
super.mainMenu();
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
}
}
With Scanner check there is a next line with hasNextLine()
Ok I found the error or I should say mis-understanding.
One of my class that I deleted in the post was actually causing the havoc.
public void validateDepositAmount() {
boolean isDoubleDigit = false;
System.out.println("How much would you like to deposit?");
//try (Scanner sc = new Scanner(System.in)) {
Scanner sc = new Scanner(System.in);
do {
if (sc.hasNextDouble()) {
totalDeposit = sc.nextDouble();
if(totalDeposit >= 0) {
isDoubleDigit = true;
} else {
System.out.println("Your input must higher than 0.");
sc.nextLine();
}
} else {
System.out.println("Please put an amount.");
sc.nextLine();
}
} while (isDoubleDigit == false);
//} catch (Exception e) {
//e.printStackTrace();
//}
}
So basically I was using Try with resources and it just kept going in a loop.
package thecashmachin;
import java.util.Scanner;
public class TheCashMachin {
public static void main(String[] args) {
int pin, proceed2=0, withdraw, dailydraw, Proceed, proceed3 = 0;
double balance;
Scanner pinnumber = new Scanner(System.in);
Scanner proc2 = new Scanner(System.in);
Scanner withd = new Scanner(System.in);
Scanner Next = new Scanner(System.in);
Scanner proc3 = new Scanner(System.in);
balance = 9999.99;
dailydraw = 1000;
System.out.println(
"text .");
System.out.println("1)Proceed");
System.out.println("2)Return Card");
Proceed = Next.nextInt();
switch (Proceed) {
case 1:// Proceed
System.out.println("Please enter your 5 digit pin below.");
Scanner Pin = new Scanner(System.in);
int Pincode = Pin.nextInt();
if (Pincode > 9999 && Pincode < 99999) {
System.out.println("1)Display Balance");
System.out.println("2)Withdraw Cash");
System.out.println("3)Other services");
proceed2 = proc2.nextInt();
} else {
System.err.println(
"text");
}
break;
case 2:// Return Card
System.err.println("text");
break;
default:
System.err.println(
"text");
break;}
switch (proceed2) {
case 1:
System.out.println("Your balance today is: 9999.99");
/*
* so right here the balance is shown and in real life you would have a go back button to display the other options but on my code after the balance is displayed you cant do anything else have to re run the the script i want a code that if selected goes back to the last option*/
break;
case 2:
System.out.println("Amount to withdraw");
withdraw = withd.nextInt();
System.out.println("Please take the cash");
System.out.println("Current Balance" + " " + (balance - withdraw));
System.out.println("Daily withdraw left:" + (dailydraw - withdraw));
if (withdraw > dailydraw) {
System.err.println("text");
}
case 3:
System.out.println("Would you like to;");
System.out.println("1)Order a check");
System.out.println("2)Order a Statement");
proceed3 = proc3.nextInt();
break;
default:
System.out.println("text");
}
switch (proceed3) {
case 1:
System.out.println("Your check has been orderd");
break;
case 2:
System.out.println("Your Statement has been orderd");
break;
}
}
}
DO
BOOLEAN = TRUE;
SWITCH() // WITH THE DIFFERENT CASES
DEFAULT BOOLEAN = FALSE;
WHILE BOOLEAN IS FALSE;
This should do. Use a simple do while loop.
Before entering the switch, your boolean is set to TRUE and if it comes to the default it turns it to FALSE and you loop until the boolean stays TRUE
An easy way I know is just making a Boolean before which is kept same as default..
boolean test = True;
while (test)
{
switch(Proceed)
{
case 1://Proceed
System.out.println("Please enter your 5 digit pin below.");
Scanner Pin=new Scanner(System.in);
int Pincode=Pin.nextInt();
test = false;
break;
case 2://Return Card
System.err.println("Your card is being ejected.\n Please Wait..");
test = false;
break;
default:
System.err.println("Sorry your request could not be processed.\n Please enter the pin again.\n")
// when neither case is true, keeps loop running.
break;
}
}
how to prompt user to loop the code yes to loop no to exit and wrong input print wrong input and go back to statement ie. "do you want to enter another name:"
import java.util.Scanner;
public class loop {
public static void main(String[] args){
Scanner kbd = new Scanner (System.in);
String decision;
boolean yn;
while(true){
System.out.println("please enter your name");
String name = kbd.nextLine();
System.out.println("you entered the name" + name );
System.out.println("enter another name : yes or no");
decision = kbd.nextLine();
switch(decision){
case "yes":
yn = false;
break;
case "no":
yn = true;
break;
default :
System.out.println("please enter again ");
return default;
}
}
}
}
If you don't use Java 7 you can't use switch-strings
Change while (true) with while (yn) so it will stop when he type "no", and change boolean yn; to boolean yn = true;
And change the rules inside the cases too.
case "yes":
yn = false;
break;
case "no":
yn = true;
break;
yn = true; if "yes";
yn = false; if "no";
You could change the condition inside the while, with while (!yn) but is more intuitive to let yn true if yes; false if no.
return default; don't make much sense, if you want to let user repeat in case of error well.. you should do a new while (true) to repeat until he writes a correct one. I would write another method.
This is how you could do it
Scanner kbd = new Scanner (System.in);
String decision;
boolean yn = true;
while(yn)
{
System.out.println("please enter your name");
String name = kbd.nextLine();
System.out.println("you entered the name" + name );
System.out.println("enter another name : yes or no");
decision = kbd.nextLine();
switch(decision)
{
case "yes":
yn = true;
break;
case "no":
yn = false;
break;
default:
System.out.println("please enter again ");
boolean repeat = true;
while (repeat)
{
System.out.println("enter another name : yes or no");
decision = kbd.nextLine();
switch (decision)
{
case "yes":
yn = true;
repeat = false;
break;
case "no":
yn = repeat = false;
break;
default:
repeat = true;
}
}
break;
}
}
Yes it will repeat decision code, but how it is created i think it is the only way to do it.
Yes, but you'd want while(yn), not while(true), which goes on forever. The break only breaks from the switch statement.
Alright, try this:
import java.util.Scanner;
public static void main(String args[]){
Scanner s = new Scanner(System.in);
boolean checking = true, valid = true;
String[] names = new String[50];
int i = 0;
while(checking){
System.out.println("Enter name...");
me = s.nextLine();
System.out.println("You entered " + me + ".");
while(valid){
System.out.println("Enter another? y/n");
you = s.nextLine();
if(you.equals("n")){
valid = false;
checking = false;
}else if you.equals("y")){
names[i] = you;
i++;
valid = false;
}else{
System.out.println("Sorry, try again (y/n)...");
}
}
}
}
boolean input = true;
while(input){
//ask for name
//print message saying you entered xyz
//ask if user wants to enter other name
//if user enters no
//set input = false;
//else continue
}
try doing this
Rather then break and while(true), you need to say while(!yn) as you set yn to false when the user input yes.
I just created a small class YesNoCommandPrompt that does just that:
private String prompt;
private Supplier<T> yesAction;
private Supplier<T> noAction;
public YesNoCommandPrompt(String prompt, Supplier<T> yesAction, Supplier<T> noAction) {
this.prompt = prompt;
this.yesAction = yesAction;
this.noAction = noAction;
}
// example usage
public static void main(String[] args) {
YesNoCommandPrompt<String> prompt = new YesNoCommandPrompt<>(
"Choose",
() -> {return "yes";},
() -> {return "no";});
System.out.println(prompt.run());
}
public T run() {
final String yesOption = "y";
final String noOption = "n";
try (Scanner scanner = new Scanner(System.in)) {
while (true) {
System.out.print(prompt + " [" + yesOption + "/" + noOption + "]: ");
String option = scanner.next();
if (yesOption.equalsIgnoreCase(option)){
return yesAction.get();
}
else if (noOption.equalsIgnoreCase(option)){
return noAction.get();
}
}
}
}