Question

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

(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<String> 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\n1->add\n2->overwirte\n3->remove\n4->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

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Accounts.java
import java.util.ArrayList;
import java.util.Scanner;

public class Accounts {
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        ArrayList<String> accounts = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);

        int option = 0;
        do {
            System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display");
            System.out.println("Enter your option");
            option = scanner.nextInt();
            if (option == 0) {
                break;
            } else if (option == 1) {
                add(accounts);
            } else if (option == 2) {
                overwrite(accounts);
            } else if (option == 3) {
                remove(accounts);
            } else if (option == 4) {
                display(accounts);
            } else {
                System.out.println("Invalid option");
            }
        } while (true);
        System.out.println("You Quit The Program!");
    }//closes main method



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



    public static void overwrite(ArrayList<String> accounts) {
        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(ArrayList<String> accounts) {
        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(ArrayList<String> accounts) {
        System.out.println("Accounts :");
        for (String number : accounts) {
            System.out.println(number);
        }
    }//closes display method

}//closes Accounts class

0-quit 1->add 2-xoverwirte 3-remove 4->display Enter your option Enter acc no1 0-xquit 1->add 2-xoverwirte 3-remove 4->displa

\color{red}Please\; upvote\;the \;solution \;if \;it \;helped.\;Thanks!

Add a comment
Know the answer?
Add Answer to:
(How do I remove the STATIC ArrayList from the public class Accounts, and move it to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks {...

    Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + "."); else System.out.println("Invalid Entry"); System.out.print("Try again (Y/N): "); ch = scanner.next().charAt(0); }while(ch=='Y'); }...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • Write a Java method that should take an ArrayList as a parameter, print its element in...

    Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen {   public static void main(String[] args) {     Scanner input = new Scanner(System.in);       System.out.println("How many numbers do you want to input?");       int num = input.nextInt();       ArrayList<Double> d = new ArrayList<Double>(num);       for(int i = 0 ; i < num; i++){           System.out.print("Enter...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

  • Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...

    Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next();    String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word);    if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...

  • import java.util.Scanner; public class Chpt7_Project {    public static void bubbleSort(int[] list)    {    int...

    import java.util.Scanner; public class Chpt7_Project {    public static void bubbleSort(int[] list)    {    int temp;                       for (int i = list.length - 1; i > 0; i--)    {    for (int j = 0; j < i; j++)    {    if (list[j] > list[j + 1])    {    temp = list[j];    list[j] = list[j + 1];    list[j + 1] = temp;    }    }    }   ...

  • Can you help me rearrange my code to make it look cleaner but still give the...

    Can you help me rearrange my code to make it look cleaner but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string, double and int array containing * the command line arguments. * @exception Any exception * @return an arraylsit of...

  • Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])...

    Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])    {        Scanner s=new Scanner(System.in);        ScoresCircularDoubleLL score=new ScoresCircularDoubleLL();        while(true)        {            System.out.println("1--->Enter a number\n-1--->exit");            System.out.print("Enter your choice:");            int choice=s.nextInt();            if(choice!=-1)            {                System.out.print("Enter the score:");                int number=s.nextInt();                GameEntry entry=new GameEntry(number);   ...

  • Complete the following Java program by writing the missing methods. public class 02 { public static...

    Complete the following Java program by writing the missing methods. public class 02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number: int n = scan.nextInt(); if (isOdd (n)) { //Print n to 1 System.out.println("Print all numbers from "+n+" to 1"); printNumToOne (n); } else { System.out.println(n + " is //End of main()

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT