每次创建新实例时,ArrayList都会被覆盖

时间:2020-05-19 21:38:03

标签: java

我是新程序员,

对于代码的使用,我深感抱歉,但我根本找不到错误。

我正试图创建一个存储输入值的arrayList,但是每次创建第二个实例时,第一个实例都会被覆盖。它确实打印两个实例,但是两个实例具有相同的值。

主块: 问题区域位于此代码的底部,“ switch sc1”和“ switch s2,case 2”

package com.company;

import java.io.FileNotFoundException;
import java.util.Scanner;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

        User user = new User(null, null, null, null, null);

        Login login = new Login(null, null, null, null, null);

        Stream stream = new Stream(null, null, null, null, 0, null, null);

        List list = new List(null, null, null, null, 0, null, null);

        ArrayList<Stream> joinableList = new ArrayList<Stream>();

        ArrayList<Stream> completedList = new ArrayList<Stream>();

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM-yyyy HH:mm");

        boolean showRoom = false;

        while (showRoom == false) {

        System.out.println("\nWelcome to ShowRoom\n\nPress 1 to login\nPress 2 to create a user\n");

        Scanner choice1 = new Scanner(System.in);

        System.out.print("Input: ");

        int s1 = choice1.nextInt();

        switch (s1) {
            case 1:
                System.out.print("\nEnter Username: ");
                Scanner scanUsername = new Scanner(System.in);
                String username = scanUsername.nextLine();
                user.setUsername(username);

                System.out.print("\nEnter Password: ");
                Scanner scanPassword = new Scanner(System.in);
                String password = scanPassword.nextLine();
                user.setPassword(password);

                login.verifyLogin(username, password);

                break;

            case 2:
                System.out.print("\nChoose Username: ");
                Scanner scanChooseUsername = new Scanner(System.in);
                username = scanChooseUsername.nextLine();
                user.setUsername(username);
                user.saveUsername();

                System.out.print("\nChoose Password: ");
                Scanner scanChoosePassword = new Scanner(System.in);
                password = scanChoosePassword.nextLine();
                user.setPassword(password);
                user.savePassword();

                break;
            }

            boolean loggedIn = false;

            while (loggedIn == false) {

                System.out.println("\nWelcome to your dashboard " + user.username + "\n\nPress 1 to create a stream\nPress 2 to view joinable streams\nPress 3 to view completed streams");
                Scanner choice2 = new Scanner(System.in);
                System.out.print("\nInput: ");

                int s2 = choice2.nextInt();

                switch (s2) {
                    case 1:
                        String listUsername = user.username;
                        stream.setListUsername(user.username);

                        Scanner chooseTitle = new Scanner(System.in);
                        System.out.print("\nChoose title: ");
                        String title = chooseTitle.nextLine();
                        stream.setTitle(title);
                        System.out.println("\nYou have chosen: " + stream.title);

                        Scanner chooseGenre = new Scanner(System.in);
                        System.out.print("\nChoose genre:\n\nPress 0 for " + stream.genreArray[0] + "\nPress 1 for " + stream.genreArray[1] + "\nPress 2 for " + stream.genreArray[2] + "\n\nInput: ");
                        int chosenGenre = chooseGenre.nextInt();
                        String genre = stream.genreArray[chosenGenre];
                        stream.setGenre(genre);
                        System.out.println("\nYou have chosen: " + stream.genre);

                        Scanner chooseType = new Scanner(System.in);
                        System.out.print("\nChoose type:\n\nPress 0 for " + stream.typeArray[0] + "\nPress 1 for " + stream.typeArray[1] + "\nPress 2 for " + stream.typeArray[2] + "\n\nInput: ");
                        int chosenType = chooseType.nextInt();
                        String type = stream.typeArray[chosenType];
                        stream.setType(type);
                        System.out.println("\nYou have chosen: " + stream.type);

                        Scanner choosePrice = new Scanner(System.in);
                        System.out.print("\nChoose price: ");
                        double price = choosePrice.nextDouble();
                        stream.setPrice(price);
                        System.out.println("\nYou have chosen: " + stream.price);

                        Scanner chooseStartTimeDate = new Scanner(System.in);
                        System.out.print("\nChoose start time and date: \n\nInsert time and date in this format: dd/mm-yyyy hh:mm\n\nInput: ");
                        String startTimeDate = chooseStartTimeDate.nextLine();
                        stream.setStartTimeDate(startTimeDate);
                        System.out.println("\nYou have chosen " + stream.startTimeDate);

                        Scanner chooseEndTimeDate = new Scanner(System.in);
                        System.out.print("\nChoose end time and date: \n\nInsert time and date in this format: dd/mm-yyyy hh:mm\n\nInput: ");
                        String endTimeDate = chooseEndTimeDate.nextLine();
                        stream.setEndTimeDate(endTimeDate);
                        System.out.println("\nYou have chosen " + stream.endTimeDate);

                        Scanner confirmStream = new Scanner(System.in);
                        System.out.print("\nDo you want to create a stream, with the following details?\n\nTitle: " + title + "\nGenre: " + genre + "\nType: " + type + "\nPrice: " + price + "\nStart date and time: " + startTimeDate + "\nEnd date and time: " + endTimeDate + "\n\nPress 1 to confirm\nPress 2 to go back\n\nInput: ");

                        int sc1 = confirmStream.nextInt();

                    switch (sc1) {
                        case 1:

                            list.addJoinableList(stream);

                            System.out.println("\nStream has been created and added to list");

                            loggedIn = false;

                            break;

                        case 2:
                            loggedIn = false;

                            break;
                    }

                    case 2:

                        list.printJoinableList();

                        break;

                    case 3:

                        System.out.println("\nCompleted stream list");

                        break;
                }
            }
        }
    }
}

流块: 此块用于继承在“列表”类中使用的属性

package com.company;

import java.time.LocalDateTime;

public class Stream {
    protected String listUsername;
    protected String title;
    protected String genre;
    protected String type;
    protected double price;
    protected String startTimeDate;
    protected String endTimeDate;

    public  Stream (String listUsername, String title, String genre, String type, double price, String startTimeDate, String endTimeDate) {
        this.listUsername = listUsername;
        this.title = title;
        this.genre = genre;
        this.type = type;
        this.price = price;
        this.startTimeDate = startTimeDate;
        this.endTimeDate = endTimeDate;
    }

    String genreArray[] = {"Comedy", "Lifestyle", "Music"};

    String typeArray[] = {"Entertainment", "Familiy", "Work"};

    public String getListUsername() { return listUsername; }

    public void setListUsername(String listUsername) { this.listUsername = listUsername; }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getStartTimeDate() {
        return startTimeDate;
    }

    public void setStartTimeDate(String startTimeDate) {
        this.startTimeDate = startTimeDate;
    }

    public String getEndTimeDate() { return endTimeDate; }

    public void setEndTimeDate(String endTimeDate) { this.endTimeDate = endTimeDate;}
}

列表块: 我认为主要问题就在这里,但我只是想不出什么错。

package com.company;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

public class List extends Stream {

    public List(String listUsername, String title, String genre, String type, double price, String startTimeDate, String endTimeDate) {

        super(listUsername, title, genre, type, price, startTimeDate, endTimeDate);
    }

    ArrayList<Stream> joinableList = new ArrayList<Stream>();
    ArrayList<Stream> completedList = new ArrayList<Stream>();

    public void addJoinableList(Stream stream) {

        joinableList.add(stream);

    }

    public void printJoinableList() {

        for (Stream s : joinableList) {
            System.out.print("\nUsername: " + s.getListUsername());
            System.out.print("\nTitle: " + s.getTitle());
            System.out.print("\nGenre: " + s.getGenre());
            System.out.print("\nType: " + s.getType());
            System.out.print("\nPrice: " + s.getPrice());
            System.out.print("\nStart time and date: " + s.getStartTimeDate());
            System.out.print("\nEnd time and date: " + s.getEndTimeDate() + "\n");
        }
    }
}

感谢所有帮助,谢谢。

1 个答案:

答案 0 :(得分:3)

当我看到这样创建的对象一次:

User user = new User(null, null, null, null, null);

然后,没有new User,我了解到您从不创建对象的新实例。

其余的代码是一样的:(几乎)您只创建一个对象实例,而不是一个新实例。

因此,当您更改所述实例的属性时,它将影响所有对象。

您需要在代码中添加一个位置,当您感觉到用户输入的数据(并由Scanner读取)时,可以创建一个新的User,{{1} },...

例如,在此之后:

Stream

要做:

list.addJoinableList(stream);