Hello Daniweb.

I am trying to make an application for a ATM machine (class homework).

This is the schematic that I followed
http://www.screencast.com/users/haxtor21/folders/Jing/media/303e583a-8f90-46f3-8ed1-a35dde2200bb

My issue is that I'm trying to access a text file and search for a account ID. After the account ID i am doing a nextDouble and storing the next value, which is the balance, in the balance variable. I tried using findInLine to find the acctID but the compiler is telling me its only usable for strings.

So the text file would look like this
acct ID $
"
567 1020
897 2356
"

Here is what I have so far:

import java.io.*;
import javax.swing.*;
import java.util.*;
public class ATM{
  static Scanner in= new Scanner(System.in);
  static int acctID;
  static double balance;
  
  public static void main(String[] args){
    
    int identif=0;
    String tranType, findID, evalTran, transaction=null, findIden=null;
    double amount, dollars=0;
    
    //From customer
    acctID= getAcctID(identif);
    tranType= getTranType(transaction);

    
    //Bank ID validation and Balance declaration
    findID= validateID(findIden);
    
    //Evaluating the transaction
    evalTrans= evaluate();
    
  }
  
  public static int getAcctID(int acctID){
    System.out.println("Enter your account ID(###): ");
    acctID= in.nextInt();
    return acctID;}
  public static String getTranType(String transType){
    System.out.println("Would you like to Deposit(d) or Withdrawl(w) an amount?");
    String tranType= in.nextLine();
    return tranType;}
  public static String validateID(String findIden){   //Finds the BankIDs file, looks for the acctID
    File bankIds=new File("BankIDs.txt");             //and the cursor moves there
    Scanner id=new Scanner(bankIds);
    id.findInLine(acctID);
    if(id.hasNext()){                                 //If there is a number (the balance) after acctID
      balance= id.nextDouble();}                      //then use that number as the balance
    else{                                             //otherwise exit the program and the program will restart
      System.out.println("Sorry, your bank ID is invalid");//asking the user for his acctID again
      System.exit(0);}
      
    id.close();}
  public static void evaluate(){
    System.out.println("What amount would you like to transfer?");
    double amount= in.nextDouble();
    if(tranType.equalsCaseIgnore(w)){
        if(balance>=amount)
           balance-=amount;
        else
          System.out.println("Sorry, you do not have that many funds available");}
    else
           balance+=amount;  
  }  
}

Any other tips for my code would be appreciated.

Recommended Answers

All 10 Replies

Member Avatar for coil

You could try casting acctID to a string with Integer.toString .

well im kind of a noob at this point and idk what casting is.

Casting is to convert from one datatype/object to another. In this case, you would do Integer.toString(accID). Refer to Integer api for more information.

so I would do Integer.toString(accID) and then do id.findInLine(acctID); ?
this would work?
PS: im at school so I cant try this. thanks.

Hmm... Reread your code again, you don't need to convert it to Integer before using it. The reason is that you read in as a string and you are going to use it to search in a text file, there is no need to convert it back and forth.

Anyway, you need to iterate the scanner to search for the line which contains the account ID.

File bankIds=new File("BankIDs.txt");             //and the cursor moves there
Scanner id=new Scanner(bankIds);
double balance;
boolean validAccount = false;
while (id.hasNext()) {
  if(id.findInLine(acctID)!=null) {  // found the line
    // parse the line for the amount here
    validAccount = true;
    break;  // now no need to stay in the while loop
  }
}
id.close();

if (!validAccount) { //exit the program and the program will restart
  System.out.println("Sorry, your bank ID is invalid"); //asking the user for his acctID again
  System.exit(0);  // you said ask the user for account ID again, but you exit the program here???
}
if(id.findInLine(acctID)!=null) {  // found the line
    // parse the line for the amount here
    validAccount = true;
    break;  // now no need to stay in the while loop
  }
}
id.close();
 
if (!validAccount) { //exit the program and the program will restart
  System.out.println("Sorry, your bank ID is invalid"); //asking the user for his acctID again
  System.exit(0);  // you said ask the user for account ID again, but you exit the program here???
}

line 2:

balance= Double.parseDouble();

Would this be OK ?

line 9: why did you use the conditional !validAccount ? Does the validAccount boolean start off as 'true' in and of itself, then it gets negated and turns into a false ?

If so, how can anything be true inside the parentheses if it is alone? if you put some numerical value alone with no logical operators would this be considered true and the if statement would execute its statements ?

line 11: what i mean was that the ATM machine would have a script that would automatically run the program again. I dont see how you could ask the user to re-enter their account in that particular block of code. Is there some sort of jump method that spears countless repetition? Please do explain if you have time.

Thank you in advance.

In line 2, you need to split the line string and get the proper value. In this case, it could be something like...

String lineString = id.next();
String[] idAndBalance = lineString.split("/\\s+/");  // split at white spaces
Double balance = Double.parseDouble(idAndBalance[1]); // expect 2nd element is balance

In line 9, if you read it in English, it should be "if NOT validAccount." This should be clear enough for its meaning. You could initial validAccount to true if you want, but you will have to add 'else' statement to flip the flag if you do not find account ID in the loop. It would be wasted because the flag will turn true only once it is found.

In line 11, I am not sure why you expect the ATM to do that? Do you mean the ATM is an external program? If you are talking about your own Java Class, you are implementing it! In other words, if you just shut down the system after the first time, it means that the user (whoever uses your ATM class) has to implement the 2nd call himself/herself. I don't think that it is that way. You are implementing the ATM class, so you must handle the 2nd loop. The line should not be there until you display the failure on the 2nd time, and you need to check it.

import java.io.*;
import javax.swing.*;
import java.util.*;
public class ATM{
  static Scanner in= new Scanner(System.in);
  static int acctID;
  static double balance;
  
  public static void main(String[] args){
    
    int identif=0;
    String tranType, findID, evalTrans, transaction=null, findIden=null;
    double amount, dollars=0;
    
    //INPUT*******
    //From customer
    acctID= getAcctID(identif);
    
    //PROCESS*****
    //Bank ID validation and Balance declaration
    findID= validateID(findIden);
    
    //Evaluating the transaction
    evaluate();
    
    //OUTPUT
    
  }
  
  public static int getAcctID(int acctID){
    System.out.println("Enter your account ID(###): ");
    acctID= in.nextInt();
    return acctID;}
  public static String validateID(String findIden){   //Finds the BankIDs file, looks for the acctID
    File bankIds=new File("BankIDs.txt");             //and the cursor moves there
    Scanner id=new Scanner(bankIds);
    boolean validAccount=false;
    while(id.hasNext()) {
      if(id.findInLine(acctID) !=null){
        String lineString = id.next();
        String[] idAndBalance = lineString.split("/\\s+/");  // split at white spaces
        Double balance = Double.parseDouble(idAndBalance[1]); // expect 2nd element is balance
        validAccount=true;
        break;} }
    id.close();
    if(!validAccount) {
      System.out.println("Sorry, your bank ID is invalid"); //asking the user for his acctID again
      System.exit(0);  // you said ask the user for account ID again, but you exit the program here???
    }
      
    id.close();}
  public static void evaluate(){
    System.out.println("Would you like to Deposit(d) or Withdrawl(w) an amount?");
    String tranType= in.nextLine();
    System.out.println("What amount would you like to transfer?");
    double amount= in.nextDouble();
    if(tranType.equalsIgnoreCase("w")){
        if(balance>=amount)
           balance-=amount;
        else
          System.out.println("Sorry, you do not have that many funds available");
          System.exit(0);}
    else
           balance+=amount;  
  }  
}
1 error and 6 warnings found:
-------------
*** Error ***
-------------
File: F:\ATM.java  [line: 39]
Error: The method findInLine(java.lang.String) in the type java.util.Scanner is not applicable for the arguments (int)
--------------
** Warnings **
--------------
File: F:\ATM.java  [line: 12]
Warning: The local variable tranType is never read
File: F:\ATM.java  [line: 12]
Warning: The local variable findID is never read
File: F:\ATM.java  [line: 12]
Warning: The local variable evalTrans is never read
File: F:\ATM.java  [line: 12]
Warning: The local variable transaction is never read
File: F:\ATM.java  [line: 13]
Warning: The local variable amount is never read
File: F:\ATM.java  [line: 13]
Warning: The local variable dollars is never read

This is what I was talking about. Any thoughts ?

findInLind() require String as its argument. You could work around by replacing acctID with new Integer(acctID).toString() --> create a new string for acctID... Though, I don't really like the idea but it should work...

For all warnings, they are just for you to see that these variables you declared on those lines have never been used in your program.

Look at your code again, I see that you will get an error later on... There are many things going on here...

// in your main()
// declare another variable as integer and initiate to 0 for number of tries
int tryNumber=0;
// line 12, declare findID as boolean instead of String and initiate it to false
boolean findID=false;
// line 15~21, wrap them with a loop that count the number of tries including
// check for continue
  while (tryNumber>0 && !findID) {
    //INPUT*******
    //From customer
    acctID= getAcctID(identif);
      
    //PROCESS*****
    //Bank ID validation and Balance declaration
    findID = validateID(new Integer(acctID).toString());
    tryNumber--;  // ticking the number of tries
  }
  // Invalid input twice, no more try
  if (!findID) {
    System.out.println("Sorry, no more tries...");
    System.exit(0);  // exit the program here
  }

// This is my fault not to look at next() method of Scanner carefully.
// It actually returns each string that can be separated by a white space
// (any type of white space including new line). The method should be used
// here is nextLine() instead of next(), and the findInLine() may not be used.
// Also, attempting to open a file with Scanner require try & catch.
// So the correction for validateID() method is below...
  public static boolean validateID(String findIden){   //Finds the BankIDs file, looks for the acctID
    File bankIds=new File("BankIDs.txt");             //and the cursor moves there
    boolean validAccount=false;
    try {
      Scanner id=new Scanner(bankIds);
      String aString;
      while(id.hasNext()) {
        aString = id.nextLine();
        if(aString.indexOf(findIden)==0) {
          String[] idAndBalance = aString.split("\\s+");
          Double balance = Double.parseDouble(idAndBalance[1]); // expect 2nd element is balance
          validAccount=true;
          break;
        }
      }
      if(!validAccount) {
        System.out.println("Sorry, your bank ID is invalid"); //asking the user for his acctID again
      }
      id.close();
    }
    catch (FileNotFoundException e) {
      // do whatever you want if error occurs here
    }
    catch (Exception e) {
      // do whatever you want if error occurs here
    }
    return validAccount;
  }

OK, the rest you need to work on yourself. It involves calculation more...

I appreciate you effort Taywin, but actually my assignment is to write methods for all the boxes in red in the power point file from my first post. Getting the program to actually work is something extra that I choose to give a shot at. I am only just learning methods and classes now. But I will keep this code for future reference and digest through it. Thnak you for your time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.