Hello, I have troubles with my coding right now. It would be great if you can ta
ID: 3843406 • Letter: H
Question
Hello, I have troubles with my coding right now.
It would be great if you can take a look of my code and help me fix the problems.
Very appreciate.
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
class ATM_Machine
{
public ArrayList <String> identity = new ArrayList<String>();
public int[] balance = new int [100];
public int count = 0;
public void register(){
Scanner scan = new Scanner(System.in);
for(int i = 0; i<40;i++){
System.out.print("-");
}
System.out.println();
String set="";
System.out.println("First Name:");
String fName = scan.nextLine();
set += fName + " ";
System.out.println("Last Name:");
String lName = scan.nextLine();
set += lName + " ";
System.out.println("account number: " + count+" Welcome! " + set);
for(int i = 0; i<40;i++){
System.out.print("-");
}
System.out.println();
identity.add(count,set);
balance[count]= 0;
count++;
}
public void username(){
Scanner scan = new Scanner(System.in);
for(int i = 0; i<40;i++){
System.out.print("-");
}
System.out.println();
System.out.println("username:");
String uName = scan.nextLine();
System.out.println("password:");
String pWord = scan.nextLine();
System.out.println("account number:");
int aNumber2 = scan.nextInt();
for(int i = 0; i<40;i++){
System.out.print("-");
}
System.out.println();
if(aNumber2<=0){
System.out.println("Incorrect input");
username();
}
else if(aNumber2 <=count){
}
else{
System.out.println("The number is incorrect, please register first");
register();
}
}
public static void main(String arg[]) throws IOException
{
Scanner scan = new Scanner(System.in);
boolean n = true;
while(n){
System.out.println("Welcome to HRA ATM Machine!");
System.out.println("Enter 1 to login");
System.out.println("Enter 2 to register");
System.out.println("Enter any letter to access next menu");
String x = scan.nextLine();
for(int i = 0; i<40;i++){
System.out.print("-");
}
System.out.println();
if(x.equals("1")||x.equals("2")){
for(int i = 0; i<40;i++){
System.out.print("-");
}
System.out.println();
if(x.equals("1")){
username();
}
else if(x.equals("2")){
register();
username();
}
}
else{
System.out.println("Do you want to startover?(Yes/No)");
String cycle = scan.nextLine();
boolean n2 = true;
while(n2){
if(cycle.equals("Yes")){
n2 = false;
}
else if(cycle.equals("No")){
n = false;
n2 = false;
for(int i = 0; i<40;i++){
System.out.print("-");
}
System.out.println(" Thanks for using HRA ATM machine, have a nice day! ");
for(int i = 0; i<40;i++){
System.out.print("-");
}
}
else{
System.out.println("Please retype");
cycle = scan.nextLine();
}
}
}
}
}
}
Explanation / Answer
Here is your fixed code. I have added the comments wherever I changed it, So that you can understand what and why I changed.
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
class ATM_Machine {
// As there is no object level concept here, and we need all the data shared by the class
// i.e. shared by all the methods and instances, we need to make these data members static.
public static ArrayList<String> identity = new ArrayList<String>();
public static int[] balance = new int[100];
public static int count = 0;
// As we need to call the method without any object reference,
// we need to make it static , so that It can be referenced directly
public static void register() {
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 40; i++) {
System.out.print("-");
}
System.out.println();
String set = "";
System.out.println("First Name:");
String fName = scan.nextLine();
set += fName + " ";
System.out.println("Last Name:");
String lName = scan.nextLine();
set += lName + " ";
System.out.println("account number: " + count + " Welcome! " + set);
for (int i = 0; i < 40; i++) {
System.out.print("-");
}
System.out.println();
identity.add(count, set);
balance[count] = 0;
count++;
}
//As we need to call the method without any object reference,
// we need to make it static , so that It can be referenced directly
public static void username() {
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 40; i++) {
System.out.print("-");
}
System.out.println();
System.out.println("username:");
String uName = scan.nextLine();
System.out.println("password:");
String pWord = scan.nextLine();
System.out.println("account number:");
int aNumber2 = scan.nextInt();
for (int i = 0; i < 40; i++) {
System.out.print("-");
}
System.out.println();
if (aNumber2 <= 0) {
System.out.println("Incorrect input");
username();
} else if (aNumber2 <= count) {
} else {
System.out.println("The number is incorrect, please register first");
register();
}
}
public static void main(String arg[]) throws IOException {
Scanner scan = new Scanner(System.in);
boolean n = true;
while (n) {
System.out.println("Welcome to HRA ATM Machine!");
System.out.println("Enter 1 to login");
System.out.println("Enter 2 to register");
System.out.println("Enter any letter to access next menu");
String x = scan.nextLine();
for (int i = 0; i < 40; i++) {
System.out.print("-");
}
System.out.println();
if (x.equals("1") || x.equals("2")) {
for (int i = 0; i < 40; i++) {
System.out.print("-");
}
System.out.println();
if (x.equals("1")) {
username();
} else if (x.equals("2")) {
register();
username();
}
} else {
System.out.println("Do you want to startover?(Yes/No)");
String cycle = scan.nextLine();
boolean n2 = true;
while (n2) {
if (cycle.equals("Yes")) {
n2 = false;
} else if (cycle.equals("No")) {
n = false;
n2 = false;
for (int i = 0; i < 40; i++) {
System.out.print("-");
}
System.out.println(" Thanks for using HRA ATM machine, have a nice day! ");
for (int i = 0; i < 40; i++) {
System.out.print("-");
}
} else {
System.out.println("Please retype");
cycle = scan.nextLine();
}
}
}
}
}
}
Sample run: -
Welcome to HRA ATM Machine!
Enter 1 to login
Enter 2 to register
Enter any letter to access next menu
2
----------------------------------------
----------------------------------------
----------------------------------------
First Name:
Salil
Last Name:
Bansal
account number: 0
Welcome! Salil Bansal
----------------------------------------
----------------------------------------
username:
salil2603
password:
26031992
account number:
1212121
----------------------------------------
The number is incorrect, please register first
----------------------------------------
First Name:
Salil
Last Name:
Bansal
account number: 1
Welcome! Salil Bansal
----------------------------------------
Welcome to HRA ATM Machine!
Enter 1 to login
Enter 2 to register
Enter any letter to access next menu
1
----------------------------------------
----------------------------------------
----------------------------------------
username:
salil2603
password:
26031992
account number:
1
----------------------------------------
Welcome to HRA ATM Machine!
Enter 1 to login
Enter 2 to register
Enter any letter to access next menu
2
----------------------------------------
----------------------------------------
----------------------------------------
First Name:
Sal
Last Name:
Ban
account number: 2
Welcome! Sal Ban
----------------------------------------
----------------------------------------
username:
sa
password:
s
account number:
2
----------------------------------------
Welcome to HRA ATM Machine!
Enter 1 to login
Enter 2 to register
Enter any letter to access next menu
k
----------------------------------------
Do you want to startover?(Yes/No)
y
Please retype
y
Please retype
Yes
Welcome to HRA ATM Machine!
Enter 1 to login
Enter 2 to register
Enter any letter to access next menu
m
----------------------------------------
Do you want to startover?(Yes/No)
No
----------------------------------------
Thanks for using HRA ATM machine, have a nice day!
----------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.