ppupil2

DataInput

Nov 17th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1.  
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8. import java.util.HashMap;
  9. import java.util.Scanner;
  10. import java.util.TreeSet;
  11. import model.Student;
  12.  
  13. /**
  14.  *
  15.  * @author phuon
  16.  */
  17. public class DataInput {
  18.  
  19.     public int inputInt(String mode, int min, int max) {
  20.         Scanner scanner = new Scanner(System.in);
  21.         while (true) {
  22.             String raw = scanner.nextLine().trim();
  23.             if (!raw.isEmpty()) {
  24.                 try {
  25.                     int number = Integer.parseInt(raw);
  26.                     if (number >= min && number <= max) {
  27.                         return number;
  28.                     } else {
  29.                         System.out.print(mode + " must in range "
  30.                                 + "[" + min + ", " + max + "], enter again: ");
  31.                     }
  32.                 } catch (NumberFormatException e) {
  33.                     System.out.print(mode + " must be a number, enter again: ");
  34.                 }
  35.             } else {
  36.                 System.out.print(mode + " can not be empty, enter again: ");
  37.             }
  38.         }
  39.     }
  40.  
  41.     public String inputID() {
  42.         Scanner scanner = new Scanner(System.in);
  43.  
  44.         System.out.print("Enter ID: ");
  45.         while (true) {
  46.             String id = scanner.nextLine().trim();
  47.             if (!id.isEmpty()) {
  48.                 return id;
  49.             } else {
  50.                 System.out.print("ID can not be empty, enter again: ");
  51.             }
  52.         }
  53.     }
  54.  
  55.     public String inputExistedID(HashMap<String, TreeSet<Student>> records) {
  56.         Scanner scanner = new Scanner(System.in);
  57.  
  58.         while (true) {
  59.             System.out.print("Enter ID: ");
  60.             String id = scanner.nextLine().trim();
  61.             if (!id.isEmpty()) {
  62.                 if (records.containsKey(id)) {
  63.                     return id;
  64.                 } else {
  65.                     System.out.print("ID " + id + " is not exist, enter again: ");
  66.                 }
  67.             } else {
  68.                 System.out.print("ID can not be empty, enter again: ");
  69.             }
  70.         }
  71.     }
  72.  
  73.     public String inputName() {
  74.         Scanner scanner = new Scanner(System.in);
  75.  
  76.         System.out.print("Enter Name: ");
  77.         while (true) {
  78.             String name = scanner.nextLine().trim();
  79.             if (!name.isEmpty()) {
  80.                 if (isValidName(name)) {
  81.                     return name;
  82.                 } else {
  83.                     System.out.print("Name can not contains special character or digits, enter again: ");
  84.                 }
  85.             } else {
  86.                 System.out.print("Name can not be empty, enter again: ");
  87.             }
  88.         }
  89.     }
  90.  
  91.     public String inputCourseName() {
  92.         Scanner scanner = new Scanner(System.in);
  93.  
  94.         System.out.print("Enter course name: ");
  95.         while (true) {
  96.             String courseName = scanner.nextLine().trim();
  97.             if (!courseName.isEmpty()) {
  98.                 boolean isValid = false;
  99.                 if (courseName.equals("Java") || courseName.equals(".Net") || courseName.equals("C/C++")) {
  100.                     return courseName;
  101.                 } else {
  102.                     System.out.print("Course name only one of three courses Java, .Net or C/C++\n"
  103.                             + "Enter again: ");
  104.                 }
  105.             } else {
  106.                 System.out.print("Course name can not be empty, enter again: ");
  107.             }
  108.         }
  109.     }
  110.  
  111.     public boolean isAOrB(String a, String b) {
  112.         Scanner scanner = new Scanner(System.in);
  113.  
  114.         while (true) {
  115.             String choice = scanner.nextLine().trim();
  116.             if (!choice.isEmpty()) {
  117.                 if (choice.equals(a)) {
  118.                     return true;
  119.                 } else if (choice.equals(b)) {
  120.                     return false;
  121.                 } else {
  122.                     System.out.print("You can only enter \"" + a + "\" or \"" + b + "\": ");
  123.                 }
  124.             } else {
  125.                 System.out.print("Choice can not be empty, enter again: ");
  126.             }
  127.  
  128.         }
  129.     }
  130.  
  131.     private boolean isValidName(String name) {
  132.         for (int i = 0; i < name.length(); i++) {
  133.             char c = name.charAt(i);
  134.             if (!Character.isLetter(c) && c != ' ') {
  135.                 return false;
  136.             }
  137.         }
  138.         return true;
  139.     }
  140. }
  141.  
Add Comment
Please, Sign In to add comment