Question: (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts

(How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?)

import java.util.ArrayList; import java.util.Scanner;

public class Accounts { static ArrayList accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int option = 0; do { System.out.println("0->quit 1->add 2->overwirte 3->remove 4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else if (option == 2) { overwrite(); } else if (option == 3) { remove(); } else if (option == 4) { display(); } else { System.out.println("Invalid option"); } } while (true); System.out.println("You Quit The Program!"); }//closes main method

public static void add() { System.out.print("Enter acc no : "); String number = scanner.next(); accounts.add(number); }//closes add method

public static void overwrite() { System.out.print("Enter acc no : "); String number = scanner.next(); int accPos = -1; for (int i = 0; i < accounts.size(); i++) { if (accounts.get(i).equalsIgnoreCase(number)) { accPos = i; } } if (accPos == -1) { System.out.println("No Account is present with given number"); return; } else { System.out.print("Enter new acc no : "); String newAccNumber = scanner.next(); accounts.set(accPos, newAccNumber); System.out.println("Successfully overwrite"); } }//closes overwrite method

public static void remove() { System.out.print("Enter acc no : "); String number = scanner.next(); if (accounts.remove(number)) { System.out.println("Successfully removed"); } else { System.out.println("Account not present"); } }//closes remove method

public static void display() { System.out.println("Accounts :"); for (String number : accounts) { System.out.println(number); } }//closes display method

}//closes Accounts class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!