尝试使多个用户没有“硬编码”

时间:2019-01-15 14:32:38

标签: java

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

edit:为澄清起见,我希望能够列出用户列表。我将如何去做。

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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

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