Advertisement
atanasovetr

DictionaryExercise

Apr 30th, 2020
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. import java.util.*;
  2. public class DictionaryExercise {
  3.     public static void main(String[] args) {
  4.         Scanner scan = new Scanner(System.in);
  5.         int id;
  6.         String name;
  7.         String menu = "Please choose a command:ADD, REM, CNT, PRN or END \n";
  8.         System.out.print(menu);
  9.         String command = scan.next();
  10.         Dictionary<Integer, String> table = new Hashtable();
  11.  
  12.         while (!command.equals("END")){
  13.             if (command.equals("ADD")){
  14.                 System.out.print("Enter the ID: ");
  15.                 id = scan.nextInt();
  16.                 System.out.print("Enter the name: ");
  17.                 name = scan.next();
  18.                 add(id, name, table);
  19.             }
  20.             else if (command.equals("REM")){
  21.                 System.out.print("Enter the ID: ");
  22.                 id = scan.nextInt();
  23.                 rem(id, table);
  24.             }
  25.             else if (command.equals("CNT")){
  26.                 cnt(table);
  27.             }
  28.             else if (command.equals("PRN")){
  29.                 prn(table);
  30.             }
  31.             System.out.print(menu);
  32.             command = scan.next();
  33.         }
  34.         System.exit(0);
  35.     }
  36.  
  37.     public static void add(int id, String name, Dictionary table){
  38.         table.put(id, name);
  39.     }
  40.     public static void rem(int id, Dictionary table){
  41.         table.remove(id);
  42.     }
  43.     public static void cnt(Dictionary table){
  44.         System.out.println(table.size());
  45.     }
  46.     public static void prn(Dictionary table){
  47.         System.out.println(table);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement