I’m a new programmer,
I’m terribly sorry for the walls of code, but I simply can’t find the error.
I am tring to create an arrayList that stores input values, but everytime I create the 2nd instance, the first instance gets overwritten. It does print two instances, but both instances have the same value.
Main block: The problem area is at the bottom of this code, “switch sc1” and “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 ShowRoomnnPress 1 to loginnPress 2 to create a usern"); 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 + "nnPress 1 to create a streamnPress 2 to view joinable streamsnPress 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:nnPress 0 for " + stream.genreArray[0] + "nPress 1 for " + stream.genreArray[1] + "nPress 2 for " + stream.genreArray[2] + "nnInput: "); 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:nnPress 0 for " + stream.typeArray[0] + "nPress 1 for " + stream.typeArray[1] + "nPress 2 for " + stream.typeArray[2] + "nnInput: "); 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: nnInsert time and date in this format: dd/mm-yyyy hh:mmnnInput: "); 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: nnInsert time and date in this format: dd/mm-yyyy hh:mmnnInput: "); 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?nnTitle: " + title + "nGenre: " + genre + "nType: " + type + "nPrice: " + price + "nStart date and time: " + startTimeDate + "nEnd date and time: " + endTimeDate + "nnPress 1 to confirmnPress 2 to go backnnInput: "); 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; } } } } }
Stream block: This block is used to inherit the properties for use in the class “List”
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;} }
List block: I think that the main issues are here, but I just can’t figure out what is wrong.
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"); } } }
All help is appreciated, thank you.
Advertisement
Answer
When I see object created once like this:
User user = new User(null, null, null, null, null);
And then, no new User
, I understand that you never create new instance of your object.
The rest of the code is in the same vein: you are (almost) creating only one instance of an object, rather than a new instance.
Therefore, when you change a property of said instance, it affect all objects.
You need to add somewhere in your code, when you feel that with the data entered by the user (and read by the Scanner
), you may create a new User
, Stream
, …
For example, after this:
list.addJoinableList(stream);
Do:
stream = new Stream(....);