-3

Okay so for a school project I need to "remake twitter". So my question is... is there a way to create a new user every time I want to sign up. In my current program it will only create one user because I hard coded it, but I want it to be able to create a user without my hard coding it, so that I can have more than just one user.

edit: For clarification I want to be able to make a List of Users. How would I go about doing that.

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;
    }
}
Mateo Lini
  • 7
  • 1
  • 3
  • you did not hardcode the user. you are asking for input. how are oyu persisting the users? – Philipp Sander Jan 15 '19 at 14:35
  • 1
    See [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – khelwood Jan 15 '19 at 14:36
  • You want List of Users. [How to make a new List in Java](//stackoverflow.com/q/858572) – 001 Jan 15 '19 at 14:36
  • `signUp == "SIGNUP"` will probably not work -> have a look at how to compare strings – Lino Jan 15 '19 at 14:37
  • You are making a well-known Java mistake by using `==` to compare strings. Use `equals()` instead. See: [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jesper Jan 15 '19 at 14:37
  • Ok I fixed that issue. But I need help with the main problem – Mateo Lini Jan 15 '19 at 14:49

1 Answers1

0

You can only store one User instance because you only have a single variable of type User, namely user1. To be able to store a variable amount of users you should have a look at the java.util.List interface and the classes implementing it. This will allow you to store multiple users, like the following:

LinkedList<User> users = new LinkedList<User>();
users.add(user1);
Ahorn
  • 649
  • 4
  • 19
  • Yea I know that I created a user myself, but what I'm trying to do is make a List or something that will automatically create users. And I just call an Index from that List and it will be a user. – Mateo Lini Jan 15 '19 at 14:54
  • Only arrays can be accessed by index, but they always have a predefined size, so you can't store a variable amount of users in them. A list doesn't have an index but a variable size and is probably the better choice for this problem. – Ahorn Jan 15 '19 at 14:58
  • Ok so how would I go about doing that? – Mateo Lini Jan 16 '19 at 01:20
  • Have a look at the java documentation https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html – Ahorn Jan 17 '19 at 07:27