0

I'm writing a program that's supposed to act like an ATM machine. The Java program runs and asks the user for an ID (1-10) of an account. After I enter a valid ID, it brings the user to a menu with a list of possible choices:

  1. Check Balance
  2. Withdraw
  3. Deposit
  4. Exit.

The user enters a number that corresponds with the action that they would like to perform. The data is stored in a .csv file that is written like this:

ID,Balance

1,100

2,100

3,100

4,100

etc.

Basically I'm trying to get the program to look at the ID I enter, match it to the line of the same number in the .csv file, and then manipulate the balance accordingly. I'm trying to use a Scanner but I don't know where to go from here? My code is listed below.

import java.io.File;
import java.util.Date;
import java.util.Scanner;


public class AccountProg {
   public static void main(String[] args) {

    File accounts = new File("accounts.csv");
    Scanner input = new Scanner(System.in);

    //Ask for valid ID and check if valid
    System.out.println("Enter an ID (1-10): ");
    int id = input.nextInt();
    while(id < 1 || id > 10) {
        System.out.println("Invalid ID. Please input a valid ID: ");
        id = input.nextInt();
    }


    //Create menu
    System.out.println("Main Menu");
    System.out.println("1: Check Balance");
    System.out.println("2: Withdraw");
    System.out.println("3: Deposit");
    System.out.println("4: Exit");
    System.out.println("\nPlease enter a choice: ");

    //Ask for valid selection and check if valid
    int selection = input.nextInt();
    while(selection < 1 || selection > 4) {
        System.out.println("Invalid selection. Please input a valid       selection: ");
        selection = input.nextInt();
    }

    //Load CSV file into 
    Scanner inputStream = new Scanner(accounts);
    inputStream.useDelimiter(",");

    //switch statement for selection
    switch (selection) {
        case 1:
        case 2:
        case 3:
        case 4:
            System.out.println("Exiting...");
            break;
        default:
            break;
    }






}

}

class Account {
    //I will declare variables for the Account class
    private int id = 0;
    private double balance = 0;
    private Date dateCreated;

    //I will create a no arg constructor for a default Account
    Account(){
    }

    //I will pass the values from the main method to the Account class
    Account(int id, double balance) {
        this.id = id;
        this.balance = balance;
        dateCreated = new Date();
    }

    //I will create the accessor methods for id, balance, annualInterestRate, and     dateCreated
    public int getid() {
        return id;
    }
    public double getBalance() {
        return balance;
    }
    public Date getDateCreated() {
        return dateCreated;
    }

    //I will create the mutator method for id, balance, and annualInterestRate
    public void setid(int newId) {
        newId = id;
    }
    public  void setBalance(double newBalance) {
        newBalance = balance;
    }


    //I will write a method named withdraw
    public void withdraw(double w) {
        balance = balance - w;
    }

    //I will write a method named deposit
    public void deposit(double d) {
        balance = balance + d;
    }


}
  • while(scannerObject.hasNextLine()) { String lineInCSV = scannerObject.nextLine(); String [] eachElementOfLine = lineInCSV.split(" "); } – Prashant Ghimire May 10 '14 at 17:49

1 Answers1

0

I would use split to read every part of the string instead of another Scanner class.

You will use the scanner to read every line of the file, split it and save it values in ID and balance variables.

Scanner fileScanner = new Scanner(new File("file.csv"));
fileScanner.nextLine(); // skip the ID,Balance line
while (fileScanner.hasNextLine())
{
    String[] content = fileScanner.nextLine().split(",");

    if (content.length != 2)
    {
        System.out.println("Invalid line.");
        continue;
    }

    int ID = Integer.parseInt(content[0]);
    int balance = Integer.parseInt(content[1]);

    System.out.println("ID: " + ID + " Balance: " + balance);
}

The first fileScanner.nextLine(); make sure you don't parse ID,Balance line (if i don't exists in final file, delete this line.)

With this input

ID,Balance
1,100
2,100
3,100
4,100

you will get ID: 1 Balance: 100 for every file line.

In ID and balance you will have the readed values, so use it where you need.

This system could be OK for simple things, for more elaborate CSV you should use a library (search online for Java CSV)


If you want to get the balance of the ID inserted by the user, compare the readed id (ID) with the inserted id (insertedID) if equals do what you want, if not continue to read.

Marco Acierno
  • 13,987
  • 7
  • 40
  • 53