Java ~ 用户一个用户
Java ~ User by user
我希望用户通过扫描仪提供数据并将其添加到列表中。稍后,我想获取此列表的大小,例如用户名、姓氏。我仍在尝试使用构造函数和异常,现在我想尝试从用户输入数据。
检查构造函数的是Human Service,如果name少于3个字母,lastname少于5个字母会抛出异常。
import java.util.ArrayList;
import java.util.List;
public class HumanService {
List<Human> humans;
public HumanService() {
humans = new ArrayList<>();
}
public void addHuman(String name, String lastName) throws HumanNameWrongFormat, HumanLastNameWrongFormat {
if(HumanValidator.humanValidatorName(name) && HumanValidator.humanValidatorLastName(lastName)) {
Human human = new Human(sizeOfList(), name, lastName);
humans.add(human);
}
}
public int sizeOfList() {
return humans.size();
}
public Human getHumanByLastName(String lastName) throws HumanNotFoundException {
for (Human human : humans) {
if (human.getLastName().equals(lastName)) {
return human;
}
}
throw new HumanNotFoundException(lastName + " not found");
}
public Human getHumanById (Integer id) throws HumanNotFoundException {
for (Human human : humans) {
if (human.getId().equals(id)) {
return human;
}
}
throw new HumanNotFoundException(id + " not found");
}
}
我想按用户列表获取数据。
例如。
请告诉我你的名字和姓氏。
这是您的名字和姓氏,通过扫描仪将被添加到列表中并进行检查。
这也是我的主要class。
public class main {
public static void main(String[] args) throws HumanNotFoundException {
HumanService humanService = new HumanService();
try {
humanService.addHuman("John", "Walker");
humanService.addHuman("Steve", "Williams");
humanService.addHuman("Gregor", "Wroten");
}
catch (HumanNameWrongFormat | HumanLastNameWrongFormat e) {
System.out.println(e.getMessage());
}
System.out.println(humanService.sizeOfList());
try {
humanService.getHumanByLastName("Wroten");
}
catch (HumanNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
这里是您可以使用 Scanner
获取所有必需的用户名并将它们添加到列表以供进一步处理的方法
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Human> humans = new ArrayList();
while (true) {
System.out.println("Please give your first name");
String firstName = scanner.nextLine();
System.out.println("Please give your last name");
String lastName = scanner.nextLine();
humans.add(new Human(firstName, lastName)); // use your humanService here
boolean breakOut = false;
String input;
do {
System.out.println("Do you want to enter more names? (Y/N)");
input = scanner.nextLine();
if (input.equalsIgnoreCase("Y") || input.equalsIgnoreCase("N")) {
breakOut = true;
} else {
System.out.println("Invalid input. try again");
}
} while (!breakOut);
if (input.equalsIgnoreCase("Y")) {
break;
}
}
System.out.println(humans);
}
}
我假设您在 Human
中有两个字段 firstName
、lastName
class Human {
private String firstName;
private String lastName;
public Human(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "first name " + firstName + " and last name " + lastName;
}
}
输入:
Please give your first name
John
Please give your last name
Smith
Do you want to enter more names? (Y/N)
y
Please give your first name
Will
Please give your last name
Smith
Do you want to enter more names? (Y/N)
n
输出:
[first name John and last name Smith, first name Will and last name Smith]
在上面的代码中,replace
list
用你的humanService
加上humans
我希望用户通过扫描仪提供数据并将其添加到列表中。稍后,我想获取此列表的大小,例如用户名、姓氏。我仍在尝试使用构造函数和异常,现在我想尝试从用户输入数据。
检查构造函数的是Human Service,如果name少于3个字母,lastname少于5个字母会抛出异常。
import java.util.ArrayList;
import java.util.List;
public class HumanService {
List<Human> humans;
public HumanService() {
humans = new ArrayList<>();
}
public void addHuman(String name, String lastName) throws HumanNameWrongFormat, HumanLastNameWrongFormat {
if(HumanValidator.humanValidatorName(name) && HumanValidator.humanValidatorLastName(lastName)) {
Human human = new Human(sizeOfList(), name, lastName);
humans.add(human);
}
}
public int sizeOfList() {
return humans.size();
}
public Human getHumanByLastName(String lastName) throws HumanNotFoundException {
for (Human human : humans) {
if (human.getLastName().equals(lastName)) {
return human;
}
}
throw new HumanNotFoundException(lastName + " not found");
}
public Human getHumanById (Integer id) throws HumanNotFoundException {
for (Human human : humans) {
if (human.getId().equals(id)) {
return human;
}
}
throw new HumanNotFoundException(id + " not found");
}
}
我想按用户列表获取数据。 例如。 请告诉我你的名字和姓氏。 这是您的名字和姓氏,通过扫描仪将被添加到列表中并进行检查。
这也是我的主要class。
public class main {
public static void main(String[] args) throws HumanNotFoundException {
HumanService humanService = new HumanService();
try {
humanService.addHuman("John", "Walker");
humanService.addHuman("Steve", "Williams");
humanService.addHuman("Gregor", "Wroten");
}
catch (HumanNameWrongFormat | HumanLastNameWrongFormat e) {
System.out.println(e.getMessage());
}
System.out.println(humanService.sizeOfList());
try {
humanService.getHumanByLastName("Wroten");
}
catch (HumanNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
这里是您可以使用 Scanner
获取所有必需的用户名并将它们添加到列表以供进一步处理的方法
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Human> humans = new ArrayList();
while (true) {
System.out.println("Please give your first name");
String firstName = scanner.nextLine();
System.out.println("Please give your last name");
String lastName = scanner.nextLine();
humans.add(new Human(firstName, lastName)); // use your humanService here
boolean breakOut = false;
String input;
do {
System.out.println("Do you want to enter more names? (Y/N)");
input = scanner.nextLine();
if (input.equalsIgnoreCase("Y") || input.equalsIgnoreCase("N")) {
breakOut = true;
} else {
System.out.println("Invalid input. try again");
}
} while (!breakOut);
if (input.equalsIgnoreCase("Y")) {
break;
}
}
System.out.println(humans);
}
}
我假设您在 Human
firstName
、lastName
class Human {
private String firstName;
private String lastName;
public Human(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "first name " + firstName + " and last name " + lastName;
}
}
输入:
Please give your first name
John
Please give your last name
Smith
Do you want to enter more names? (Y/N)
y
Please give your first name
Will
Please give your last name
Smith
Do you want to enter more names? (Y/N)
n
输出:
[first name John and last name Smith, first name Will and last name Smith]
在上面的代码中,replace
list
用你的humanService
加上humans