Trying to create a bank management app. Not able to execute Login method after creating a new user. Feel something is wrong with the object creation for populating the Perons and Account arrays. Please help.
/Account class to hold account details/
public class Account extends Persons {
int accountNum;
int pin;
float checkingBal;
float savingBal;
float withDrawAmount;
float depositAmmount;
float transferAmount;
public Account(){ }
public Account(int accountNum, float checkingBal, float savingBal, int pin ){
this.accountNum = accountNum;
this.checkingBal = checkingBal;
this.savingBal = savingBal;
this.pin = pin;
}
public void setAccountNum(int accountNum){
this.accountNum = accountNum;
}
public void setPin(int pin){
this.pin = pin;
}
public int getAccountNum(){
return accountNum;
}
public int getPin(){
return pin;
}
public void withDraw(float withDrawAmount){
this.savingBal = this.savingBal - withDrawAmount;
}
public void deposit(float depositAmmount){
this.checkingBal = this.checkingBal + depositAmmount;
}
public void transfer(float transferAmount, String fromAccount, String toAccount){
if(fromAccount == "Checking" && toAccount == "Savings"){
this.checkingBal = this.checkingBal - transferAmount;
this.savingBal = this.savingBal + transferAmount;
}else if(fromAccount == "Savings" && toAccount == "Checking"){
this.savingBal = this.savingBal - transferAmount;
this.checkingBal = this.checkingBal + transferAmount;
}
System.out.print("Your New Checking Balance Is: "+this.checkingBal);
System.out.print("Your New Savings Balance Is: "+this.savingBal);
}
public float checkBalance(int accountNum){
float totalBalance = 0;
if(this.accountNum == accountNum){
totalBalance = this.checkingBal + this.savingBal;
}
return totalBalance;
}
}
/*******************************************************************************************************/
/Persons class to hold all the people having account./
import java.util.Date;
public class Persons {
String fname;
String lname;
int age;
String dob;
String address;
float yearlyIncome;
int accountNum;
public Persons(){}
public Persons(String fname, String lname, int age, String dob, String address, float yearlyIncome, int accountNum){
this.fname = fname;
this.lname = lname;
this.age = age;
this.dob = dob;
this.address = address;
this.yearlyIncome = yearlyIncome;
this.accountNum = accountNum;
}
public void setFname(String fname) {
this.fname = fname;
}
public void setLname(String lname) {
this.lname = lname;
}
public void setAge(int age) {
this.age = age;
}
public void setDob(String dob) {
this.dob = dob;
}
public void setAddress(String address) {
this.address = address;
}
public void setYearlyIncome(float yearlyIncome) {
this.yearlyIncome = yearlyIncome;
}
public void setAccountNum(int accountNum){
this.accountNum = accountNum;
}
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public int getAge() {
return age;
}
public String getDob() {
return dob;
}
public String getAddress() {
return address;
}
public float getYearlyIncome() {
return yearlyIncome;
}
public int getAccountNum(){
return accountNum;
}
}
/*****************************************************************************************************/
/Java Main Class/
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.Scanner;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Scanner scanf = new Scanner(System.in);
Persons people[] = new Persons[3];
Account personAccount[] = new Account[3];
float withdrawAmount;
float depositAmount;
float transferAmount;
int mainChoice;
int choice;
int counter = 0;
int userId = 0;
int loopBreak = 0;
int loginSuccess = -1;
while(loopBreak != -1){
System.out.println("================================================================================================");
System.out.println("Welcome To The California Bank :)");
System.out.println("1. Login..");
System.out.println("2. Create A New Customer.");
System.out.println("3. Done With Transactions.");
mainChoice = scanf.nextInt();
switch (mainChoice) {
case 1:
System.out.println("Please Enter Your Account Number:");
int accountNum = scanf.nextInt();
System.out.println("Please Enter Your Pin:");
int pin = scanf.nextInt();
for (int i = 0; i <= counter; i++) {
if (personAccount[i].getAccountNum() == accountNum && personAccount[i].getPin() == pin) {
System.out.println(personAccount[i].getAccountNum());
System.out.println(personAccount[i].getPin());
System.out.println("Customer Logged In and Validated.");
userId = i;
loginSuccess = 0;
} else {
System.out.println("Sorry You Are Not A Current Customer!");
loginSuccess = -1;
}
}
if(loginSuccess == 0){
System.out.println("======================================================================================");
System.out.println("What Else Would You Like To Do Today?");
System.out.println("1. Deposit.");
System.out.println("2. Withdraw.");
System.out.println("3. Transfer Money.");
System.out.println("4. Check Balance.");
choice = scanf.nextInt();
switch (choice) {
case 1:
System.out.println("How Much Money Would You Like To Deposit?");
depositAmount = scanf.nextFloat();
personAccount[userId].deposit(depositAmount);
break;
case 2:
System.out.println("How Much Money Would You Like To Withdraw Today?");
withdrawAmount = scanf.nextFloat();
personAccount[userId].withDraw(withdrawAmount);
break;
case 3:
System.out.println("How Much Money Would You Like To Withdraw Today?");
transferAmount = scanf.nextFloat();
System.out.println("1. Press 1 To Transfer From Checking to Savings.");
System.out.println("2. Press 2 To Transfer From Savings To Checking.");
int transferChoice;
transferChoice = scanf.nextInt();
if (transferChoice == 1) {
personAccount[userId].transfer(transferAmount, "Checking", "Savings");
} else {
personAccount[userId].transfer(transferAmount, "Savings", "Checking");
}
case 4:
System.out.println("Here Is Your Total Account Balance");
personAccount[userId].checkBalance(people[counter].accountNum);
break;
}
}
break;
case 2:
people[counter] = new Persons();
personAccount[counter] = new Account();
System.out.println("Please Enter Your First Name For The Account Creation:");
scanf.next();
String fName = scanf.nextLine();
System.out.println("Please Enter Your Last Name For The Account Creation:");
scanf.next();
String lName = scanf.nextLine();
people[counter].setFname(fName);
people[counter].setLname(lName);
System.out.println("Please Enter Your Age:");
int age = scanf.nextInt();
people[counter].setAge(age);
System.out.println("Please Enter Your Date Of Birth:");
String dob = scanf.next();
people[counter].setDob(dob);
System.out.println("Please Enter Your Address:");
scanf.next();
String address = scanf.nextLine();
people[counter].setAddress(address);
System.out.println("Please Enter Your Yearly Income:");
float annualIncome = scanf.nextFloat();
people[counter].setYearlyIncome(annualIncome);
System.out.println("Generating Your Account Number.....");
Random rand = new Random();
accountNum = rand.nextInt(100000);
people[counter].setAccountNum(accountNum);
personAccount[counter].setAccountNum(accountNum);
System.out.println("Your New Account Number Is: "+accountNum);
System.out.println("Please Enter A 4 Digit Pin");
pin = scanf.nextInt();
personAccount[counter].setPin(pin);
System.out.println("New Customer Created:");
counter++;
break;
case 3:
System.out.println("Thanks For Using The California Banking System. Goodbye!");
loopBreak = -1;
break;
}
}
}
}
Below is the error I am Getting after creating a user:
- Login..
- Create A New Customer.
- Done With Transactions. 1 Please Enter Your Account Number: 90806 Please Enter Your Pin: 3333 90806 3333 Customer Logged In and Validated. Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Account.getAccountNum()" because "personAccount[i]" is null at Main.main(Main.java:48)