AT Machine

Labels:
import java.util.*;
import java.io.*;

public class ATM {
Vector balance;
Vector name;
Vector password;
File file = new File("data.txt");

public ATM(){
balance = new Vector();
name = new Vector();
password = new Vector();
//init();
}

public void init(){
int option = 0;

loadFile();

Scanner in = new Scanner(System.in);

do {
System.out.println("[1] Create new Account");
System.out.println("[2] Access Existing Account");
System.out.println("[3] Exit");

System.out.print("> ");

String input = in.nextLine();

try {
option = Integer.parseInt(input);
} catch (Exception e){
option = 0;
}


if(option > 3 || option < 1){
System.out.println("Invalid input.");
continue;
}

if(option == 1){
create();
}

if(option == 2){
open();
}
} while (option != 3);

in.close();
writeFile();
}

private void create(){
Scanner in = new Scanner(System.in);

System.out.print("Enter Account Name: ");

String actname = in.nextLine();

if(accountSearch(actname)>=0){
System.out.println("Account Already Exists.");
} else {

name.add(actname);

System.out.print("Enter Password: ");
password.add(in.nextLine());

balance.add(0.0);
System.out.println("Account " + name.lastElement() + " created!");
}

in.close();
}

private void open(){
Scanner in = new Scanner(System.in);

System.out.print("Enter Account Name: ");

int accountNumber = accountSearch(in.nextLine());

if(accountNumber >= 0){
System.out.print("Enter Password: ");
if(password.get(accountNumber).equals(in.nextLine())){

int option = 0;
double amount = 0;

do {
System.out.println("[1] Deposit Funds");
System.out.println("[2] Withdraw Funds");
System.out.println("[3] Balance Inquiry");
System.out.println("[4] Exit");

System.out.print("> ");
option = in.nextInt();

if(option > 4 || option < 1){
System.out.println("Invalid input.");
continue;
}

if(option == 1){
deposit(accountNumber);
}

if(option == 2){
withdraw(accountNumber);
}

if(option == 3){
balance(accountNumber);
}
} while (option != 4);
} else {
System.out.println("Account and password does not match.");
}
} else {
System.out.println("Account Name does not exist!");
}


in.close();
}

private void deposit(int accountNumber){
double amount = 0;
Scanner in = new Scanner(System.in);

System.out.print("Enter amount to deposit: ");
String amt = in.nextLine();

try {
amount = Double.parseDouble(amt);
} catch (Exception e){
System.out.println("Invalid input.");
}

double bal = balance.get(accountNumber);
bal = bal + amount;
balance.set(accountNumber,bal);

in.close();


}

private void withdraw(int accountNumber){
double amount = 0;

Scanner in = new Scanner(System.in);

System.out.print("Enter amount to withdraw: ");
String amt = in.nextLine();

try{
amount = Double.parseDouble(amt);
} catch (Exception e) {
System.out.println("You must input a number.");
}

double bal = balance.get(accountNumber);

if(amount > bal){
System.out.println("Insufficient funds.");
} else {
bal = bal - amount;
balance.set(accountNumber,bal);
}

in.close();


}

private void balance(int accountNumber){
double bal = balance.get(accountNumber);
System.out.println("Balance is " + bal + ".");


}

private int accountSearch(String s){
for(int i=0;i if(s.equalsIgnoreCase(name.get(i))){
return i;
}
}

return -1;
}

private void loadFile(){

Scanner inFile = null;

try {
inFile = new Scanner(file);
String acct = "";
String pass = "";
double bal = 0;

while (inFile.hasNextLine()){ //loops through the file as long as there is a next line
String line = inFile.nextLine(); //reads the next lineof the file

acct = line.substring(0,line.indexOf("|")); //extracts all characters to the left of the |
pass =
line.substring(line.indexOf("|")+1,line.indexOf("|",line.indexOf("|")+1));
//extracts all chars between the two | symbols
bal =
Double.parseDouble(line.substring(line.lastIndexOf("|")+1,line.length()));
//extraces all chars from the second | to the end of the line thenparses it

name.add(acct); //add to vector of names
password.add(pass); //add to vector of passwords
balance.add(bal); //add to vector of balances
}

inFile.close();
} catch (Exception e) {
System.out.println("Could not find file.");
}
}

private void writeFile(){

FileOutputStream fos = null;
DataOutputStream dos = null;

try {
fos = new FileOutputStream(file,false); //true to append,false to overwrite
dos = new DataOutputStream(fos);

for(int i=0;i dos.writeBytes(name.get(i) + "|" + password.get(i) +
"|" + balance.get(i) + "\n");
// write data to file in this format: name|password|balance
}

fos.close();
dos.close();

} catch (Exception e) {
System.out.println("Could not write file.");
}
}
}
0 comments:

Post a Comment

Hi. :-h


Chem2

Recent Entries

Recent Comments