When I try to log all my failed attempts with PrintWriter it just overwrites each line in the log instead of making multiple lines in the log with each failed attempt. How do I remedy this? What's going on with it?
package HW1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Banking {
public static void main(String[] args) throws FileNotFoundException {
File fi1 = new File("ClientInfo.txt");
Scanner sc1 = new Scanner(fi1);
Scanner sc2 = new Scanner(System.in);
Scanner sc3 = new Scanner(System.in);
Scanner loginScan = new Scanner(System.in);
ClientRecords[] clientRecordArray = new ClientRecords[100];
PrintWriter logTransaction1 = new PrintWriter("BankLogs.txt");
String firstName;
String middleName;
String lastName;
String userId = "";
String userIdTyped;
int pinNumber;
double balance;
String choice1;
int choice2;
int logSize = 0;
int failedUserId = 0;
while(sc1.hasNext())
{
firstName = sc1.next();
middleName = sc1.next();
lastName = sc1.next();
userId = sc1.next();
pinNumber = sc1.nextInt();
balance = sc1.nextDouble();
ClientRecords client1 = new ClientRecords(firstName, middleName, lastName, userId, pinNumber, balance);
clientRecordArray[logSize] = client1;
logSize++;
}
System.out.println("Please enter your user ID:");
userIdTyped = loginScan.nextLine();
while(!clientRecordArray[0].getUserId().equals(userIdTyped))
{
userIdTyped = null;
System.out.println("Wrong user ID please try again:");
failedUserId++;
userIdTyped = loginScan.nextLine();
}
logTransaction1.println("Failed to enter user ID " + failedUserId + " times.");
logTransaction1.close();
menu1(sc2);
menu2(sc3);
}
public static void menu1(Scanner sc2)
{
ClientRecords client1 = new ClientRecords("N", "xx", "uygg", "PinkFloyd111", 0007, 51234.99);
System.out.println("Please enter the corresponding pin number:");
int userPin = sc2.nextInt();
while(userPin != client1.getUserPin())
{
System.out.println("Sorry wrong PIN please try again.");
userPin = sc2.nextInt();
}
}
public static void menu2(Scanner sc3) throws FileNotFoundException
{
PrintWriter logTransaction = new PrintWriter("BankLogs.txt");
ClientRecords client1 = new ClientRecords("hjhh", "hjh", "hh", "PinkFloyd111", 0007, 51234.99);
int choice = 0;
double depositAmount = 0;
double withdrawAmount = 0;
double balance = client1.getUserBalance();
System.out.println("Welcome to your bank!");
System.out.println("Press (1) to deposit to your account. ");
System.out.println("Press (2) to withdraw from your account. ");
System.out.println("Press (3) to check balance of your account. ");
System.out.println("Press (4) to exit the system.");
System.out.println("Enter the number for your choice here: ");
choice = sc3.nextInt();
switch (choice) {
case 1:
System.out.println("Please enter how much you'd like to desposit here:");
depositAmount = sc3.nextDouble();
balance += depositAmount;
System.out.println("You have deposited " + "$" + depositAmount);
logTransaction.println("Deposit of: " + "$" + depositAmount);
logTransaction.close();
break;
case 2:
System.out.println("Please enter how much you'd like to withdraw here:");
withdrawAmount = sc3.nextDouble();
balance -= withdrawAmount;
System.out.println("You have withdrawn " + "$" + withdrawAmount);
logTransaction.println("Withdrawal of: " + "$" + withdrawAmount);
logTransaction.close();
break;
case 3:
System.out.println("The balance of your account is: " + "$" + balance);
break;
case 4:
System.out.println("Thank you and have a great day!");
}
}
}