用java写注册火车票系统的方法
时间: 2023-12-29 19:04:22 浏览: 36
以下是一个基本的Java代码示例,用于注册火车票系统:
```java
import java.util.Scanner;
public class TrainTicketRegistrationSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String username, password, confirmPassword, email;
int age;
System.out.println("Welcome to the Train Ticket Registration System!\n");
// Get user input for registration information
System.out.print("Enter your username: ");
username = scanner.nextLine();
System.out.print("Enter your password: ");
password = scanner.nextLine();
System.out.print("Confirm your password: ");
confirmPassword = scanner.nextLine();
while (!password.equals(confirmPassword)) {
System.out.println("Passwords do not match. Please try again.");
System.out.print("Enter your password: ");
password = scanner.nextLine();
System.out.print("Confirm your password: ");
confirmPassword = scanner.nextLine();
}
System.out.print("Enter your email: ");
email = scanner.nextLine();
System.out.print("Enter your age: ");
age = Integer.parseInt(scanner.nextLine());
// Create new user object with input information
User newUser = new User(username, password, email, age);
// Add new user to database or file system
// Example: UserDatabase.addUser(newUser);
System.out.println("\nThank you for registering with the Train Ticket Registration System!");
}
}
class User {
private String username;
private String password;
private String email;
private int age;
public User(String username, String password, String email, int age) {
this.username = username;
this.password = password;
this.email = email;
this.age = age;
}
// Getters and setters for user information
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
这个示例程序可以让用户输入他们的注册信息,包括用户名、密码、电子邮件和年龄。它还包括一个用户类,其中包含有关用户的信息,例如用户名、密码、电子邮件和年龄。在此示例中,我们只是将新用户添加到一个名为`UserDatabase`的虚构数据库中,但实际应用将使用更适合其需求的持久化存储方法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)