使用Java创建注册和登录页面

时间:2018-11-07 07:36:46

标签: java

我是Java的初学者,我正在尝试创建两个页面,该页面需要注册并使用Java登录页面。我要创建的只是一个控制台,因此不需要GUI。现在,我正在创建一个注册页面,您可以是学生也可以是补习生,使用数组存储用户输入,当用户登录时,它将在注册页面中检查用户输入。

注册所需的信息(获取用户输入):电子邮件,密码,用户名,联系方式,凭据

当前,我被困在switch语句中的数组部分,我不太确定如何在switch中实现arrays / arraylist。这就是我得到的:

import java.util.ArrayList;
 import java.util.Scanner;


 public class assgn {

        public static void  main(String[] args) {
            ArrayList<userType> users = new ArrayList<userType>(); // store and hold user input temporary
            Scanner input = new Scanner(System.in); // to obtain input from user

            String email, password, username, subject, credentials;
            int contact;

            System.out.println("========== SIGN UP PAGE ==========");
            System.out.println("Pick your choice:  ");
            System.out.println("1) Student" + "\n" + "2) Tutor");
            int choice = input.nextInt();

            switch(choice) {
                case 1:
                    System.out.println("========== Student Account ==========");
                    //arrays goes here
                    break;

                case 2:
                    System.out.println("========== Tutor Account ==========");
                    //arrays goes here
                    break;

                default:
                    System.out.println("Please pick your account type");
            }
        }

    }

和下面是类文件:

public class userType {
    private String email, password, username, subject, credentials;
    private int contact;

    public String getEmail() {
        return email;
    }

    public void newEmail (String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void newPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void newUsername (String username) {
        this.username = username;
    }

    public String getSubject (){
        return subject;
    }

    public void newSubject (String subject){
        this.subject = subject;
    }

    public String getCredentials() {
        return credentials;
    }

    public void newCredentials(){
        this.credentials = credentials;
    }

    public int getContact() {
        return contact;
    }

    public void newContact (int contact){
        this.contact = contact;
    }

    public userType(String email, String password, String username, String subject, String credentials){
        this.email = email;
        this.password = password;
        this.username = username;
        this.subject = subject;
        this.credentials = credentials;
    }

    public userType(int contact){
        this.contact = contact;
    }
}

任何帮助将不胜感激,在此先感谢您的欢呼

2 个答案:

答案 0 :(得分:2)

您可以使用已经完成的操作将它们添加到users ArrayList中。

switch (choice) {
case 1:
    System.out.println("========== Student Account ==========");
    users.add(new userType(email, password, username, subject, credentials));
    break;

case 2:
    System.out.println("========== Tutor Account ==========");
    users.add(new userType(email, password, username, subject, credentials));
    break;

default:
    System.out.println("Please pick your account type");
}

您仍然需要让用户填写其课程emailpasswordusernamesubjectcredentials。除此之外,我还会在userType的构造函数中添加一些内容,以便您可以确定用户是导师还是学生。

然后将类名userType更改为UserType,这是Java的命名约定。

答案 1 :(得分:0)

您可以尝试使用此代码。

        ArrayList<userType> student = new ArrayList<userType>(); // store and hold user(student) input temporary
        ArrayList<userType> tutor = new ArrayList<userType>(); // store and hold user(tutor) input temporary

        switch (choice) {
        case 1:
            System.out.println("========== Student Account ==========");

            System.out.print("email : "); // input userType information (email, password ...)
            email = input.next();
            System.out.print("password : ");
            password = input.next();
            System.out.print("username : ");
            username = input.next();
            System.out.print("subject : ");
            subject = input.next();
            System.out.print("credentials : ");
            credentials = input.next();

            student.add(new userType(email, password, username, subject, credentials));
            break;

        case 2:
            System.out.println("========== Tutor Account ==========");

            System.out.print("email : "); // input userType information (email, password ...)
            email = input.next();
            System.out.print("password : ");
            password = input.next();
            System.out.print("username : ");
            username = input.next();
            System.out.print("subject : ");
            subject = input.next();
            System.out.print("credentials : ");
            credentials = input.next();

            tutor.add(new userType(email, password, username, subject, credentials));
            break;

        default:
            System.out.println("Please pick your account type");
        }

用户可以输入自己的信息。然后,您可以确定用户是导师还是学生。