In: Computer Science
Convert this java code from hashmap into arraylist.
import java.io.*;
import java.util.*;
public class Solution {
        public static void main(String[] args) throws IOException {
                Scanner sc = new Scanner(System.in);
                HashMap labs = new HashMap();
                while (true) {
                        System.out.println("Choose operation : ");
                        System.out.println("1. Create a Lab");
                        System.out.println("2. Modify a Lab");
                        System.out.println("3. Delete a Lab");
                        System.out.println("4. Assign a pc to a Lab");
                        System.out.println("5. Remove a pc from a Lab");
                        System.out.println("6. Quit");
                        int choice = sc.nextInt();
                        String name=sc.nextLine();
                        switch (choice) {
                        case 1:
                                System.out.println("Enter Lab name (i.e. LAB A or LAB B) : ");
                                name = sc.nextLine();
                                labs.put(name, new Lab(name));
                                break;
                        case 2:
                                System.out.println("Enter name of lab to modify : ");
                                name = sc.nextLine();
                                
                                if (labs.containsKey(name)) {
                                        System.out.println("Enter new name to modify : ");
                                        String name2 = sc.nextLine();
                                        Lab lab = labs.get(name);
                                        labs.remove(name);
                                        labs.put(name2, lab);
                                } else {
                                        System.out.println("Invalid name");
                                }
                                break;
                        case 3:
                                System.out.println("Enter name of lab to delete : ");
                                name = sc.nextLine();
                                if (labs.containsKey(name)) {
                                        Lab lab = labs.get(name);
                                        System.out.println("Total pc in this lab are : " + lab.pc_count);
                                        labs.remove(name);
                                } else {
                                        System.out.println("Invalid name");
                                }
                                break;
                        case 4:
                                System.out.println("Enter name of lab to assign a pc : ");
                                name = sc.nextLine();
                                if (labs.containsKey(name)) {
                                        if (labs.get(name).pc_count >= 50) {
                                                System.out.println("cannot assign more pc");
                                        } else {
                                                labs.get(name).pc_count++;
                                        }
                                } else {
                                        System.out.println("Invalid name");
                                }
                                break;
                        case 5:
                                System.out.println("Enter name of lab to assign a pc : ");
                                name = sc.nextLine();
                                if (labs.containsKey(name)) {
                                        if (labs.get(name).pc_count <= 0) {
                                                System.out.println("cannot remove pc");
                                        } else {
                                                labs.get(name).pc_count--;
                                        }
                                } else {
                                        System.out.println("Invalid name");
                                }
                                break;
                        case 6:
                                return;
                        default:
                                System.out.println("Invalid choice choose again.");
                                break;
                        }
                }
        }
        static class Lab {
                String name;
                int pc_count;
                Lab(String name) {
                        this.name = name;
                        pc_count = 0;
                }
        }
}
THE CODE IS:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Solution {
    // In stead of hashmap we will use two
arraylist.
    // One arraylist of type String to act as the
Key arraylist
    // Second arraylist to store the Lab to act as
the Value arraylist
    static ArrayList<String> key = new
ArrayList<>();
    static ArrayList<Lab> value = new
ArrayList<>();
    public static void put(String name, Lab lab)
{
        // if key is already
there in hashmap then replace the previous lab value
        if (key.contains(name))
{
           
int index = key.indexOf(name);
           
value.set(index, lab);
        }// else if key is not
present then add the key-value pair to the hashmap
        else {
           
key.add(name);
           
value.add(lab);
        }
    }
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
        while (true) {
           
System.out.println("Choose operation : ");
           
System.out.println("1. Create a Lab");
           
System.out.println("2. Modify a Lab");
           
System.out.println("3. Delete a Lab");
           
System.out.println("4. Assign a pc to a Lab");
           
System.out.println("5. Remove a pc from a Lab");
           
System.out.println("6. Quit");
           
int choice = sc.nextInt();
           
String name = sc.nextLine();
           
switch (choice) {
               
case 1:
                   
System.out.println("Enter Lab name (i.e. LAB A or LAB B) :
");
                   
name = sc.nextLine();
                   
put(name, new Lab(name));
                   
break;
               
case 2:
                   
System.out.println("Enter name of lab to modify : ");
                   
name = sc.nextLine();
                   
if (key.contains(name)) {
                       
System.out.println("Enter new name to modify : ");
                       
String name2 = sc.nextLine();
                       
// getting index of current position of key and updating the
name
                       
int index = key.indexOf(name);
                       
key.set(index, name2);
                   
} else {
                       
System.out.println("Invalid name");
                   
}
                   
break;
               
case 3:
                   
System.out.println("Enter name of lab to delete : ");
                   
name = sc.nextLine();
                   
if (key.contains(name)) {
                       
// getting lab
                       
int index = key.indexOf(name);
                       
Lab lab = value.get(index);
                       
System.out.println("Total pc in this lab are : " +
lab.pc_count);
                       
// removing the lab from the hashmap
                       
key.remove(index);
                       
value.remove(index);
                   
} else {
                       
System.out.println("Invalid name");
                   
}
                   
break;
               
case 4:
                   
System.out.println("Enter name of lab to assign a pc : ");
                   
name = sc.nextLine();
                   
if (key.contains(name)) {
                       
// checking if number of pc in the lab exceeds 50
                       
if (value.get(key.indexOf(name)).pc_count >= 50) {
                           
System.out.println("cannot assign more pc");
                       
}// if it doesn't then increment the value of pc in the lab
                       
else {
                           
value.get(key.indexOf(name)).pc_count++;
                       
}
                   
} else {
                       
System.out.println("Invalid name");
                   
}
                   
break;
               
case 5:
                   
System.out.println("Enter name of lab to remove a pc : ");
                   
name = sc.nextLine();
                   
if (key.contains(name)) {
                       
if (value.get(key.indexOf(name)).pc_count <= 0) {
                           
System.out.println("cannot remove pc");
                       
} else {
                           
value.get(key.indexOf(name)).pc_count--;
                       
}
                   
} else {
                       
System.out.println("Invalid name");
                   
}
                   
break;
               
case 6:
                   
return;
               
default:
                   
System.out.println("Invalid choice choose again.");
                   
break;
           
}
        }
}
static class Lab {
        String name;
        int pc_count;
        Lab(String name)
{
           
this.name = name;
           
pc_count = 0;
        }
    }
}