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:
- Check Balance
- Withdraw
- Deposit
- 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;
}
}