Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.util.HashMap;
- import java.util.Scanner;
- import java.util.TreeSet;
- import model.Student;
- /**
- *
- * @author phuon
- */
- public class DataInput {
- public int inputInt(String mode, int min, int max) {
- Scanner scanner = new Scanner(System.in);
- while (true) {
- String raw = scanner.nextLine().trim();
- if (!raw.isEmpty()) {
- try {
- int number = Integer.parseInt(raw);
- if (number >= min && number <= max) {
- return number;
- } else {
- System.out.print(mode + " must in range "
- + "[" + min + ", " + max + "], enter again: ");
- }
- } catch (NumberFormatException e) {
- System.out.print(mode + " must be a number, enter again: ");
- }
- } else {
- System.out.print(mode + " can not be empty, enter again: ");
- }
- }
- }
- public String inputID() {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter ID: ");
- while (true) {
- String id = scanner.nextLine().trim();
- if (!id.isEmpty()) {
- return id;
- } else {
- System.out.print("ID can not be empty, enter again: ");
- }
- }
- }
- public String inputExistedID(HashMap<String, TreeSet<Student>> records) {
- Scanner scanner = new Scanner(System.in);
- while (true) {
- System.out.print("Enter ID: ");
- String id = scanner.nextLine().trim();
- if (!id.isEmpty()) {
- if (records.containsKey(id)) {
- return id;
- } else {
- System.out.print("ID " + id + " is not exist, enter again: ");
- }
- } else {
- System.out.print("ID can not be empty, enter again: ");
- }
- }
- }
- public String inputName() {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter Name: ");
- while (true) {
- String name = scanner.nextLine().trim();
- if (!name.isEmpty()) {
- if (isValidName(name)) {
- return name;
- } else {
- System.out.print("Name can not contains special character or digits, enter again: ");
- }
- } else {
- System.out.print("Name can not be empty, enter again: ");
- }
- }
- }
- public String inputCourseName() {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter course name: ");
- while (true) {
- String courseName = scanner.nextLine().trim();
- if (!courseName.isEmpty()) {
- boolean isValid = false;
- if (courseName.equals("Java") || courseName.equals(".Net") || courseName.equals("C/C++")) {
- return courseName;
- } else {
- System.out.print("Course name only one of three courses Java, .Net or C/C++\n"
- + "Enter again: ");
- }
- } else {
- System.out.print("Course name can not be empty, enter again: ");
- }
- }
- }
- public boolean isAOrB(String a, String b) {
- Scanner scanner = new Scanner(System.in);
- while (true) {
- String choice = scanner.nextLine().trim();
- if (!choice.isEmpty()) {
- if (choice.equals(a)) {
- return true;
- } else if (choice.equals(b)) {
- return false;
- } else {
- System.out.print("You can only enter \"" + a + "\" or \"" + b + "\": ");
- }
- } else {
- System.out.print("Choice can not be empty, enter again: ");
- }
- }
- }
- private boolean isValidName(String name) {
- for (int i = 0; i < name.length(); i++) {
- char c = name.charAt(i);
- if (!Character.isLetter(c) && c != ' ') {
- return false;
- }
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment