Vasilena

ТЕМА 16

Mar 20th, 2021 (edited)
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. //ТЕМА_16//
  2.  
  3. import java.util.Dictionary;
  4. import java.util.Scanner;
  5. import java.util.Hashtable;
  6. import java.util.Enumeration;
  7.  
  8. public class UASD_2_200321 {
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.         //MAKING A DICTIONARY, BY USING HASHTABLE//
  12.         Dictionary<Integer, String> MonthsDictionary = new Hashtable<>();
  13.  
  14.         //                   KEY : VALUE//
  15.         MonthsDictionary.put(1, "JANUARY");
  16.         MonthsDictionary.put(2, "FEBRUARY");
  17.         MonthsDictionary.put(3, "MARCH");
  18.         MonthsDictionary.put(4, "APRIL");
  19.         MonthsDictionary.put(5, "MAY");
  20.         MonthsDictionary.put(6, "JUNE");
  21.         MonthsDictionary.put(7, "JULY");
  22.         MonthsDictionary.put(8, "AUGUST");
  23.         MonthsDictionary.put(9, "SEPTEMBER");
  24.         MonthsDictionary.put(10, "OCTOBER");
  25.         MonthsDictionary.put(11, "NOVEMBER");
  26.         MonthsDictionary.put(12, "DECEMBER");
  27.  
  28.         //ASKING THE USER//
  29.         System.out.println("This is a program that allows you to search for month's name by it's number and the opposite.");
  30.         System.out.println("Please, choose:\nNUMBER | NAME");
  31.         String user = sc.nextLine().toLowerCase();
  32.  
  33.         //CHECKING FOR CONDITION//
  34.         if(user.equals("number")) { //BY NUMBER//
  35.             Enumeration<Integer> itemsKey = MonthsDictionary.keys(); //Enumerations of keys//
  36.             Enumeration<String> itemsValue = MonthsDictionary.elements(); //Enumerations of values//
  37.  
  38.             System.out.println("Please, enter the month's number: ");
  39.             String month_number = sc.nextLine();
  40.  
  41.             while (itemsKey.hasMoreElements()) {
  42.                 String currentKey = itemsKey.nextElement().toString();
  43.                 String currentValue = itemsValue.nextElement();
  44.  
  45.                 if(currentKey.equals(month_number)){
  46.                     System.out.println("This month name is: " + currentValue);
  47.                 }
  48.             }
  49.         }else if(user.equals("name")){ //BY NAME//
  50.             Enumeration<Integer> itemsKey = MonthsDictionary.keys(); //Enumerations of keys//
  51.             Enumeration<String> itemsValue = MonthsDictionary.elements(); //Enumerations of values//
  52.  
  53.             System.out.println("Please, enter the month's name: ");
  54.             String month_name = sc.nextLine().toUpperCase();
  55.  
  56.             while (itemsKey.hasMoreElements()) {
  57.                 String currentKey = itemsKey.nextElement().toString();
  58.                 String currentValue = itemsValue.nextElement();
  59.  
  60.                 if(currentValue.equals(month_name)){
  61.                     System.out.println("This month number is: " + currentKey);
  62.                 }
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68.  
Add Comment
Please, Sign In to add comment