Jump to content

Hello, so I am stuck again.

 

package BankAccount;
import java.util.Scanner;
 

public class mainClass {public static void main(String[]args){//Create an account methodcreatingAnAccount createAccount = new creatingAnAccount();//Method that shows balancemainMenu showBalance = new mainMenu();//Method that adds funds to your balancemainMenu addFunds = new mainMenu();//Method that adds funds to your balancemainMenu withdrawFunds = new mainMenu();//Method that adds funds to your balancemainMenu threeOptions = new mainMenu();//Method that adds funds to your balancecreatingAnAccount enterCredentials = new creatingAnAccount();Scanner Question1 = new Scanner (System.in);System.out.println("Do you have an account?");String Answer1 = Question1.nextLine();if (Answer1.equals("yes")){     enterCredentials.enterCredentials();}else{     Scanner createAcc = new Scanner(System.in);     System.out.println("Do you want to create an account?");     String accCreate = createAcc.nextLine();if(accCreate.equals("yes")){      createAccount.createAccount();      enterCredentials.enterCredentials();}else{    System.exit(0);   }}

so if they answer yes to wanting to create an account it will take them to this method...

public void createAccount(){String username;String password;Scanner enterUsername = new Scanner (System.in);System.out.println("Enter a username: ");username = enterUsername.nextLine();userNames.add(username);Scanner enterPassword = new Scanner (System.in);System.out.println("Enter a password: ");password = enterPassword.nextLine();userPasswords.add(password);}

That then adds the scanner input to this arraylist:

 

ArrayList<String> userNames = new ArrayList<String>();ArrayList<String> userPasswords = new ArrayList<String>();

Once there username and password are saved to the array, they will then be asked to re enter them to access the program like so:

 

public void enterCredentials(){mainMenu threeOptions = new mainMenu();Scanner enterUsername = new Scanner (System.in);System.out.println("Please enter your username: ");String username = enterUsername.nextLine();Scanner enterPassword = new Scanner (System.in);System.out.println("Please enter your password: ");String password = enterPassword.nextLine();if(userNames.contains(username) && userPasswords.contains(password)){threeOptions.threeOptions();}

after that they should be taken to the threeOptions(); class, however they are not and the program ends there, why is this?

 

I know this is super long and hard to read

 

Thanks

 

 

 

 

Link to comment
https://linustechtips.com/topic/30474-java-help-please/
Share on other sites

Link to post
Share on other sites

If I understand correctly your userNames and userPasswords array lists are declared in the class creatingAnAccount right?

Either way, just from looking at the names of your classes and variables I believe you haven't fully understood the concept of object oriented programming.

 

What you are doing in your main method is create two different instances of the class creatingAnAccount and assigning them to different variables. Every time you do "new Foo();" a new instance is created, each with its own class fields. So, when you are calling createAccount.createAccount(); in the main method which then saves the password and user name in an array that does not mean that every instance sees the same array. In fact, when you call enterCredentials.enterCredentials(), despite being the same class, it is not the same object and the arrays in this one are empty. That is why threeOptions.threeOptions() in enterCredentials() is never called. The same thing is happening with all the different instances of mainMenu you have created and assigned to each variable in your main method.

 

The way to program in java (and any OOP language) is to design classes that represent objects or concepts and instantiate as many objects of those classes as entities you want to work with. For instance you could have a class BankAccount with a field balance, a method deposit and a method withdraw and then create two instances (one representing Foo's bank account and the other representing Bar's bank account) and invoke those methods on each one to change their balances independently of one another. The methods are shared among the instances but the fields are not (unless they are static).

Link to comment
https://linustechtips.com/topic/30474-java-help-please/#findComment-391441
Share on other sites

Link to post
Share on other sites

I agree with MikeD, it seems you have not fully understood OOP. Which is fair as it can be a complicated subject.

 

I advise reading this: http://educationjelly.com/downloads/ebooks/object-oriented-programming-using-java.pdf

 

It explains the concepts of OOP using real world terms and makes it much easier to understand so give that a go, and then come back to your program with a better understanding.

Case Bitfenix Shinobi | CPU - i5 3570K @ 4.2Ghz | Motherboard -  Asus P8Z77 | GPU - 7870 | PSU - Corsair CXM 600w | 
Harddrive - Seagate Barracuda 1Tb x 2 | SSD - Samsung 840 256Gb | Cooling - Custom 2x Dual 120mm Radiator, Watercooling Loop |

Ultimate Programming Resources Thread ||| CompSci Masters Degree Student and Professional Java and C# Programmer

Link to comment
https://linustechtips.com/topic/30474-java-help-please/#findComment-392472
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×