Hii everyone. I recently tried to make an ATM program. Stuck on the last step, which is to print the transactions into a file. I tried everything but nothing seems to work. There are 5 classes: UserDat-Contains account number, pin of 10 different users. NumToWords- Contains functions to convert nos. to words for the transactions. filread-To write into files(Here the problem lies.I guss) ,ATM machine-To compute withdraw,deposits,balance etc. To read data from UserDat and NumToWords class and to display a menu for options for the user. Finally, the mai class which contains the main method that starts the program. Any help will be much appreciated. Here's the syntax:
Code:package ATM;
import java.util.Scanner;
import java.io.*;
public class AtmMachine
{
int withdrawAmt;
int pos;
Scanner input = new Scanner(System.in);
Userdat comob=new Userdat();
double totalBal=0.00d;
double availableBal=0.00d;
double minbal=500.00;
NumToWords ob=new NumToWords();
public void anotherTrans()
{
System.out.println("Do you want another transaction");
System.out.println("If yes press 1 for no press 2");
int opt=input.nextInt();
if(opt==1)
{
drawMainMenu();
}
else if(opt==2)
{
System.exit(1);
}
else
{
System.out.println("Wrong input press 1 for yes or 2 for no");
anotherTrans();
}
}
public int userAccount()
{
System.out.print("Enter your account number: ");
int account=0;
account = input.nextInt();
int chk=0;
for(int i=0;i<10;i++)
{
if(comob.accno[i]== account)
{
chk=1;
pos=i;
break;
}
}
if(chk!=1)
{
userAccount();
}
return account;
}
public int userPin()
{
System.out.print("Enter your pin number: ");
int pin=0;
int flag=3;
int chk=0;
while(flag>0)
{
pin =input.nextInt();
for(int i=0;i<10;i++)
{
if(comob.pinno[i]== pin)
{
chk=1;
break;
}
}
if(chk==0)
{
flag--;
System.out.println("You have only "+flag+" chances left");
}
else
{
break;
}
}
if(flag==0)
{
System.out.println("Wrong pin entered thrice system exiting");
System.exit(1);
}
return pin;
}
public void startAtm()
{
userAccount();
userPin();
drawMainMenu();
}
public void drawMainMenu()
{
int selection;
System.out.println("\nATM main menu:");
System.out.println("1 - View account balance");
System.out.println("2 - Withdraw funds");
System.out.println("3 - Add funds");
System.out.println("4 - Terminate transaction");
System.out.print("Choice: ");
selection =input.nextInt();
switch(selection)
{
case 1:
viewAccountInfo();
break;
case 2:
withdraw();
break;
case 3:
deposit();
break;
case 4:
System.out.println("Thank you for using this ATM!!! goodbye");
System.exit(1);
}
}
public void viewAccountInfo()
{
System.out.println("Account Information:");
System.out.println("Welcome "+comob.name[pos]);
System.out.println("\t--Total balance: Rs"+comob.bal[pos]);
ob.main(comob.bal[pos]);
System.out.println("\t--Available balance: Rs"+(comob.bal[pos]-500));
ob.main(comob.bal[pos]-500);
anotherTrans();
}
public void deposit()
{
int depAmount;
System.out.println("Enter Deposit amount");
depAmount=input.nextInt();
System.out.println("\n***Please insert your money now...***");
comob.bal[pos] =comob.bal[pos] +depAmount;
availableBal =comob.bal[pos]-minbal;
anotherTrans();
}
public void checkNsf(int withdrawAmount)
{
if((comob.bal[pos]-minbal) -withdrawAmount < 0)
System.out.println("\n***ERROR!!! Insufficient funds in you accout***");
else
{
comob.bal[pos] =comob.bal[pos] -withdrawAmount;
availableBal =comob.bal[pos]-minbal;
System.out.println("\n***Please take your money now...");
}
}
public void withdraw()
{
System.out.println("Enter amount to withdraw");
withdrawAmt=input.nextInt();
int op= 0;
System.out.println("Confirm 1 for yes or 2 for no");
op=input.nextInt();
while(op!=0)
{
if(op==1)
{
checkNsf(withdrawAmt);
viewAccountInfo();
anotherTrans();
}
else if(op==2)
{
anotherTrans();
}
else
{
System.out.println("wrong choice enter yes or no");
op=input.nextInt();
}
}
}
public void readfile()throws IOException
{
File f=new File("Transactions.txt");
Scanner sc=new Scanner(f);
while(sc.hasNextLine())
{
System.out.println(sc.nextLine());
}
}
}
package ATM;
import java.util.Scanner;
import java.io.*;
public class filread extends AtmMachine
{
public void withdraw1()
{
try {
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
BufferedWriter buf=new BufferedWriter(new FileWriter("Transactions.txt",true));
PrintWriter pr=new PrintWriter(buf);
pr.println(withdrawAmt);
}
catch ( IOException e )
{
System.err.println(">>>>" + e.getMessage() );
e.printStackTrace();
}
}
}
package ATM;
class Userdat
{
String[] name={"Harish Jadeja","Harshit Sinha","Ram Kapoor","Mohan Singh","Jaykant Shikre","Shivaji Bhosale",
"Seema Iyer" ,"Priya Rajwade"," Archana Singh", "Balvinder Singh" };
int[] accno={1101,1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 1110};
int [] pinno={1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010};
int [] bal={4000,12000,74000,42988,57882,41104,7521,9865,41004,74852};
void main()
{
}
}
package ATM;
import java.util.*;
public class NumToWords {
String string;
String st1[] = { "", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", };
String st2[] = { "hundred", "thousand", "lakh", "crore" };
String st3[] = { "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "ninteen", };
String st4[] = { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy",
"eighty", "ninty" };
public String convert(int number) {
int n = 1;
int word;
string = "";
while (number != 0) {
switch (n) {
case 1:
word = number % 100;
pass(word);
if (number > 100 && number % 100 != 0) {
show("and ");
}
number /= 100;
break;
case 2:
word = number % 10;
if (word != 0) {
show(" ");
show(st2[0]);
show(" ");
pass(word);
}
number /= 10;
break;
case 3:
word = number % 100;
if (word != 0) {
show(" ");
show(st2[1]);
show(" ");
pass(word);
}
number /= 100;
break;
case 4:
word = number % 100;
if (word != 0) {
show(" ");
show(st2[2]);
show(" ");
pass(word);
}
number /= 100;
break;
case 5:
word = number % 100;
if (word != 0) {
show(" ");
show(st2[3]);
show(" ");
pass(word);
}
number /= 100;
break;
}
n++;
}
return string;
}
public void pass(int number) {
int word, q;
if (number < 10) {
show(st1[number]);
}
if (number > 9 && number < 20) {
show(st3[number - 10]);
}
if (number > 19) {
word = number % 10;
if (word == 0) {
q = number / 10;
show(st4[q - 2]);
} else {
q = number / 10;
show(st1[word]);
show(" ");
show(st4[q - 2]);
}
}
}
public void show(String s) {
String st;
st = string;
string = s;
string += st;
}
public static void main(int num) {
NumToWords w = new NumToWords();
String inwords = w.convert(num);
System.out.println("\t--"+inwords);
}
}
package ATM;
public class mai
{
public static void main(String args[])
{
AtmMachine myAtm = new AtmMachine();
myAtm.startAtm();
filread a=new filread();
a.withdraw1();
}
}
