Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class DictionaryExercise {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int id;
- String name;
- String menu = "Please choose a command:ADD, REM, CNT, PRN or END \n";
- System.out.print(menu);
- String command = scan.next();
- Dictionary<Integer, String> table = new Hashtable();
- while (!command.equals("END")){
- if (command.equals("ADD")){
- System.out.print("Enter the ID: ");
- id = scan.nextInt();
- System.out.print("Enter the name: ");
- name = scan.next();
- add(id, name, table);
- }
- else if (command.equals("REM")){
- System.out.print("Enter the ID: ");
- id = scan.nextInt();
- rem(id, table);
- }
- else if (command.equals("CNT")){
- cnt(table);
- }
- else if (command.equals("PRN")){
- prn(table);
- }
- System.out.print(menu);
- command = scan.next();
- }
- System.exit(0);
- }
- public static void add(int id, String name, Dictionary table){
- table.put(id, name);
- }
- public static void rem(int id, Dictionary table){
- table.remove(id);
- }
- public static void cnt(Dictionary table){
- System.out.println(table.size());
- }
- public static void prn(Dictionary table){
- System.out.println(table);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement