I have assignment task to write ATM app, with following requirements: to have classes, Methods, Constructors, Decision making, Collections, Iterration and Error handling.
Below are my classes.
1st class is User class with his attributes, constructor, and get and set methods.
package atm.rcl;
public class User {
static String userName;
static String password;
private double balance;
private String accType;
public User() {
}
public User(String userName, String password, double balance, String accType) {
super();
this.userName = userName;
this.password = password;
this.balance = balance;
this.accType = accType;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getAccType() {
return accType;
}
public void setAccType(String accType) {
this.accType = accType;
}
}
Second class contains methods which app will use.
package atm.rcl;
import java.util.List;
public interface UserRipo {
List<User> checkBalance();
void withdrawMoney();
void depositMoney();
void passwordChange();
public List<User> getDetails();
}
Third class is where methods are implemented. I have created Arraylist named userList with user details(name, pass etc.) and I want to check if user input for Username and Pass matches any from userList . But everytime I run the program it goes in Else , and skip If statement ?? Why is that, where I am wrong ?? What to fix ?? Please please anyone who can help ??
package atm.rcl;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class UserRipoImplement implements UserRipo {
List<User> userList;
public UserRipoImplement() {
userList = new ArrayList<>();
User george = new User("George", "1234", 2000, "Savings");
User colin = new User("Colin", "2356", 3000, "Business");
User michael = new User("Michael", "password", 5000, "Current");
User lorena = new User("Lorena", "parola", 4000, "Checkings");
userList.add(george);
userList.add(colin);
userList.add(michael);
userList.add(lorena);
}
private User firstUser;
private Scanner scanner;;
{
scanner = new Scanner(System.in);
}
@SuppressWarnings("unlikely-arg-type")
public void login() {
System.out.println("Please enter username: ");
String input = scanner.nextLine();
System.out.println("Enter your password");
String inputPass = scanner.nextLine();
//if(input.equals(userList.get(0)) && inputPass.equals(userList.get(1)))
//if(input.equals(userList.get(0).getUserName()) && inputPass.equals(userList.get(1).getPassword()))
//if(userList.contains(input) && userList.contains(inputPass))
//THIS IS THE IF STATEMENT WHICH IS NOT MATCHING userInput for userName and Pass with userList arraylist???
if(userList.contains(input) && userList.contains(inputPass)) {
System.out.println("You logged in successfully ");
}
else {
System.out.println("Incorrect details, try again!");
}
}
@Override
public List<User> getDetails() {
return userList;
}
@Override
public List<User> checkBalance() {
// TODO Auto-generated method stub
return null;
}
@Override
public void withdrawMoney() {
// TODO Auto-generated method stub
}
@Override
public void depositMoney() {
// TODO Auto-generated method stub
}
@Override
public void passwordChange() {
// TODO Auto-generated method stub
}
}
And 4th Class is the runner code class
package atm.rcl;
public class Main {
public static void main(String[] args) {
UserRipoImplement atm = new UserRipoImplement();
while (true) {
atm.login();
}
// for (User user : atm.getDetails()) {
// System.out.println(user.getUserName() + " "+user.getAccType() + " " + user.getBalance());
// }
}
}
I am sorry if my question has been answered but it didn't find it at all. Please Help me with suggestions and how can improve it and make code running?? Really important is to check User input for pass and username with Arraylist userList ?? Pleeease.
I tried to try with different condition syntax for If statement but didn't worked.
//if(input.equals(userList.get(0)) && inputPass.equals(userList.get(1)))
//if(input.equals(userList.get(0).getUserName()) && inputPass.equals(userList.get(1).getPassword()))
//if(userList.contains(input) && userList.contains(inputPass))
if(userList.contains(input) && userList.contains(inputPass)) {
System.out.println("You logged in successfully ");
}
else {
System.out.println("Incorrect details, try again!");
}