首页 > 解决方案 > 尝试在没有“硬编码”的情况下创建多个用户

问题描述

好的,所以对于一个学校项目,我需要“重新制作 twitter”。所以我的问题是……每次我想注册时,有没有办法创建一个新用户。在我当前的程序中,它只会创建一个用户,因为我对其进行了硬编码,但我希望它能够创建一个用户而无需我对其进行硬编码,这样我就可以拥有多个用户。

编辑:为澄清起见,我希望能够制作用户列表。我将如何去做。

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
        // make it so u can log in to an existing account
        // make it double check the password and email

        Scanner sc = new Scanner(System.in);

        int age;

        System.out.println("-------------------------------------------------------------------------");
        System.out.println("|                               Twitter                                 |");
        System.out.println("| Sign Up                                                        Log in |");
        System.out.println("-------------------------------------------------------------------------");

        System.out.println("Would you like to Log In or Sign Up?");
        String responseString = sc.nextLine();
        responseString = responseString.toUpperCase();
        char responseSL = responseString.charAt(0);


        if (responseSL == 'S') {

            User user1 = new User(); //this creates the first user

            System.out.println("Alright, lets get started by setting up your profile!");

            System.out.println("Please enter a username.");
            user1.setUserName(sc.nextLine());

            System.out.println("Please enter your email address.");
            user1.setEmailAddress(sc.nextLine());

            System.out.println("Please enter a password.");
            user1.setPassWord(sc.nextLine());

            System.out.println("Alright we got your account all setup. Just take a moment to review everthing. Don't worry you can change this stuff later if you want!");
            user1.printUserProfile();

        } //end signup if

        else {

            // make sign in thing here.

        } //end else
    } //end main
} //end main class

class User {
    // this class sets up the users profile when they are signing up
    private String userName;
    private String emailAddress;
    private String passWord;

    //Username, Age, Email Address, Tweets[ ]
    //Methods
    //Setters and getters, Create tweet
    public User () {

    }

    public String getUserName() {return this.userName;}
    public void setUserName(String userName) {this.userName = userName;}

    public String getEmailAddress() {return this.emailAddress;}
    public void setEmailAddress(String emailAddress) {this.emailAddress = emailAddress;}

    public String getPassWord() {return this.passWord;}
    public void setPassWord(String passWord) {this.passWord = passWord;}

    public void printUserProfile() {
        System.out.println("Username: " + this.userName);
        System.out.println("Email Address: " + this.emailAddress);
        System.out.println("Password (remember not to share this to anyone) " + this.passWord);
    }


}

class Tweet {
    private String tweet;

    //Fields
    //messageBody, hashtag, messageLength (less than 240 characters)
    //Constructor
    //Method containsHashtag

    public Tweet () {

    }

    public String getTweet() {return this.tweet;}
    public void setTweet(String tweet) {this.tweet = tweet;}

    public static boolean checkHashTag(String tweet, String hashTag) {
        String [] tweetArray = tweet.split(" ");

        boolean hasHashTag = false;

        for (int i = 0; i < tweetArray.length; i++) {

            if (tweetArray[i].equals(hashTag)) {
                hasHashTag = true;
            }

        }

        return hasHashTag;
    }
}

标签: java

解决方案


您只能存储一个User实例,因为您只有一个类型的变量User,即user1. 为了能够存储可变数量的用户,您应该查看java.util.List接口和实现它的类。这将允许您存储多个用户,如下所示:

LinkedList<User> users = new LinkedList<User>();
users.add(user1);

推荐阅读