📄 validation.java
字号:
package HotelSystem;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class Validation {
int input;
Scanner scan;
// 控制台输入校验,得到字符串
public String getString() {
boolean success = false;
String input = "";
while (!success) {
success = false;
scan = new Scanner(System.in);
input = scan.nextLine();
if (!input.equals("")) {
success = true;
break;
}
System.out.print("不能为空,请重新输入:");
}
return input;
}
// 控制台输入校验,得到指定的字符或字符串
public String getString(String[] range) {
boolean success = false;
String result = null;
test:
while (!success) {
result = getString();
for (int i = 0; i < range.length; i++) {
if (result.equals(range[i])) {
System.out.println(result.equals(range[i]));
success = true;
break test;
}
}
System.out.print("输入超出指定范围,请重新输入:");
}
return result;
}
// 控制台输入校验,得到指定的数字
public int getInt(int[] range) {
boolean success = false;
int result = 0;
while (!success) {
scan = new Scanner(System.in);
result = getInt();
if (result >= range[0] & result <= range[1]) {
success = true;
} else {
System.out.print("输入超出指定范围" + "(" + range[0] + "-" + range[1]
+ "),请重新输入:");
}
}
return result;
}
// 控制台输入校验,得到指定ID对应Houses集合中的下标
public int getInt(LinkedList<Integer> range, LinkedList<Houses> houses) {
boolean success = false;
int result = 0;
while (!success) {
result = getInt();
for (int i = 0; i < range.size(); i++) {
if (result == range.get(i)) {
for (int j = 0; i < houses.size(); j++) {
if (houses.get(j) instanceof Hotel) {
Hotel hotel = (Hotel) houses.get(j);
if (result == hotel.getId()) {
result = j;
success = true;
break;
}
} else {
Resort resort = (Resort) houses.get(j);
if (result == resort.getId()) {
result = j;
success = true;
break;
}
}
}
}
}
if (success == false) {
System.out.print("没有这个ID,请重新输入:");
}
}
return result;
}
/*
* public int getInt(LinkedList<Orders> houses) { boolean success=false;
* int result=0; while(!success){ result = getInt(); success=true; for(int
* i=0; i<houses.size(); i++) { } } return result; }
*/
// 控制台输入校验,得到数字输入
public int getInt() {
boolean success = false;
while (!success) {
scan = new Scanner(System.in);
if (scan.hasNextInt()) {
input = scan.nextInt();
success = true;
break;
} else {
System.out.print("输入的不是数字,请重新输入:");
}
}
return input;
}
public String getEmail() {
String input = null;
boolean success = false;
while (!success) {
Scanner scan = new Scanner(System.in);
if (scan.hasNextLine()) {
input = scan.nextLine();
if (!(input.indexOf("@") == -1)) {
success = true;
} else {
System.out.print("不是一个有效的E-mail地址,请重新输入:");
}
}
}
return input;
}
public int getOrdersId(LinkedList<Orders> Orders) {
int input = -1;
boolean success = false;
test:
while (!success) {
input = getInt();
for (int i = 0; i < Orders.size(); i++) {
Orders order = Orders.get(i);
if (order.getOrdersId() == input) {
success = true;
input = i;
System.out.println(i);
break test;
}
}
if (input==-1)
{
success = true;
}
if (success == false) {
System.out.print("没有符合的订单,输入(0)退出菜单,请重新输入:");
}
}
return input;
}
public int getInput() {
return input;
}
public void setInput(int input) {
this.input = input;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -