简体   繁体   English

尝试在没有“硬编码”的情况下让多个用户

[英]Try to make multiple users without "hard coding" it

Okay so for a school project I need to "remake twitter".好的,对于学校项目,我需要“重新制作 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;
    }
}

You can only store one User instance because you only have a single variable of type User , namely user1 .你只能存储一个User实例,因为你只有类型的单变量User ,即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.为了能够存储可变数量的用户,您应该查看java.util.List接口和实现它的类。 This will allow you to store multiple users, like the following:这将允许您存储多个用户,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有硬编码大小的Java数组声明 - Java array declaration without hard coding the size 调用没有硬编码参数的构造函数(耦合) - Calling a constructor without hard coding parameters (coupling) 有没有办法在代码中使用没有硬编码属性键的属性文件? - Are there way to use properties files without hard coding property keys in code? 从工厂请求项目,而无需对每种情况进行硬编码 - Requesting item from Factory without hard-coding every case 以编程方式指定Java类文字(不进行硬编码)?反射? - Specify a Java class literal programmatically (without hard coding it)? reflection? 如何在不对目录进行硬编码的情况下将某些内容保存到桌面? - How to save something to the desktop without hard-coding the directory? 从 txt 文件创建 Object,无需对每个 object 进行硬编码 - Create Object from txt file without hard coding every object 用Java硬编码@PathVariable - Hard coding @PathVariable in Java 在Java 8中,如何在不在我的环境中对其进行硬编码的情况下获取主机名? - In Java 8, how do I get my hostname without hard-coding it in my environment? 从 android 应用程序访问 AWS S3,无需硬编码访问密钥 - Accessing AWS S3 from android app without hard-coding access keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM