Help improving this code

ok so when i query a favorite row i know its id because its already been inserted, problem arises when i try inserting a favoritedto that i made in the program, i cant know what the id will be for it, but i dont have to either because the add method of my dao will just insert it and sql will generate a key for it. problem is when im trying to save a favorite ride i need to pass a favoritedto as argument but a favoritedto requires a id to be constructed, so i just put garbage number to construct it, there is no issue then all fine. problem is i dont think this is good written and i would refer to this as spaghetti code
83 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @userexit! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
userexit
userexitOP7mo ago
package mvp.model.db.dto;

import mvp.model.db.tablepk.FavoritesPK;

public class FavoritesDto extends FavoritesPK {
private final StationsDto origin;
private final StationsDto destination;
private final String name;

public FavoritesDto(String name, StationsDto origin, StationsDto destination, Integer id) {
super(id);
this.name = name;
this.origin = origin;
this.destination = destination;
}

public StationsDto getOrigin() {
return origin;
}

public StationsDto getDestination() {
return destination;
}

@Override
public String toString() {
return name + "\n" + origin + "\n" + destination + "\n" + getId();
}

public String getName() {
return name;
}
}
package mvp.model.db.dto;

import mvp.model.db.tablepk.FavoritesPK;

public class FavoritesDto extends FavoritesPK {
private final StationsDto origin;
private final StationsDto destination;
private final String name;

public FavoritesDto(String name, StationsDto origin, StationsDto destination, Integer id) {
super(id);
this.name = name;
this.origin = origin;
this.destination = destination;
}

public StationsDto getOrigin() {
return origin;
}

public StationsDto getDestination() {
return destination;
}

@Override
public String toString() {
return name + "\n" + origin + "\n" + destination + "\n" + getId();
}

public String getName() {
return name;
}
}
in my favoritesDto i require a key representing the id in the table, thats so when a user wants to edit a favoritedto we can know which one he is editing and use a update query problem arises when adding a favoritesDto to the table itself, my constructor requires a id, but when adding a id is not needed as the sql will generate one for me (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
yeah I know it doesnt matter what I put in ID in the constructor when inserting but for me its useful to know the id cuz when i update the db i need the id @borgrel for better understanding
userexit
userexitOP7mo ago
No description
userexit
userexitOP7mo ago
this is when I add an element to the db i dont need the primary key as it will be generated
userexit
userexitOP7mo ago
problem is here when i modify a row in favorites table
No description
userexit
userexitOP7mo ago
i need the id here so thats why i have an id attribute in favoritesDto
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
but then i need 2 different dto's classes ok so things go like this: when i go in the saved rides table: I get all the rows from the database -> i create a favoriteDto instance that has the name, origin, destination, id -> presenter will ask the view to display all the favoriteDtos -> they get displayed -> now the user can change the name, the origin and the destination station when user clicks save button -> presenter asks the model to save the changes -> model talks to repository -> repository uses dao to update (the id in favoritedto is useful for this)
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
my problem arises when: user wants to save a ride to his favorite rides, selects destination and origin and name, clicks button -> presenter asks model to save, issue is i cant use same favoriteDto cuz that one requires a key but bruh i need the id to update the row no ?
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
its not for the displaying purpose that can be filtered
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
the only reason i gave it an id is so i can update them
userexit
userexitOP7mo ago
No description
userexit
userexitOP7mo ago
if i click save here what should i do ? go through each row and update it right but i need the primary key for each then maybe im not understanding you or you are not understanding me
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
bro thats the point he can change those
userexit
userexitOP7mo ago
No description
userexit
userexitOP7mo ago
he can change everything name origni destination there is ok wait :
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
DROP TABLE IF EXISTS FAVORITES;
CREATE TABLE IF NOT EXISTS FAVORITES (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
id_origin INTEGER NOT NULL,
id_destination INTEGER NOT NULL,
FOREIGN KEY (id_origin) REFERENCES STATIONS(id),
FOREIGN KEY (id_destination) REFERENCES STATIONS(id),
UNIQUE (id_origin, id_destination)
);
DROP TABLE IF EXISTS FAVORITES;
CREATE TABLE IF NOT EXISTS FAVORITES (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
id_origin INTEGER NOT NULL,
id_destination INTEGER NOT NULL,
FOREIGN KEY (id_origin) REFERENCES STATIONS(id),
FOREIGN KEY (id_destination) REFERENCES STATIONS(id),
UNIQUE (id_origin, id_destination)
);
yh i guess
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
wdym
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
the app doesnt allow login system none of that just a basic app to calculate shortest path
userexit
userexitOP7mo ago
like this
No description
userexit
userexitOP7mo ago
wdym with bridging table ill show u what a row of my table looks like
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
i mean there is a way i can get old destination and old origin u think thats better ?
userexit
userexitOP7mo ago
No description
userexit
userexitOP7mo ago
when I load values in my table: presenter asks model for all rows in favorites table -> model gives them -> presenterp asses them to the view -> makes a favoritetablerow -> adds it to the table
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
yh yeah because of dtos i need a dto with the key and a dto where i dont care about the key maybe i give a default value to the key and i add a setter ? if the dto is not in the table and will be added it can keep the default random value, if its read from the favorites table then set its id
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
I know how I can ignore the key I can save in the FavoriteTableRow the old origin and old destination and use those for updating like u mentioned before with ur query here
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
i thought i needed the key but i just realised no no i didnt think i needed the key thats why i gave it a random value in my code for inserting because i knew it wouldnt matter fori nserting because i coded it but thats spaghetti code i think I just didnt know how to do it the other way but now I realise in my own definition of the table . UNIQUE (id_origin, id_destination) means I can identify a row in the table with those 2 and when I make a new FavoriteTableRow I can hold those 2 ids in there and use them for the update query what do u think @borgrel
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
yeah but which one do u think is better give a default for int to favoritedto and then a setter or simply not use it and use origin and destination for update imo latter one looks more correct but if you dont agree we can debate on it
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
modify the old one but i can always make a copy of it when instantiating the favoritetablerows
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
idk i cant see it as a normal user because i made it and i know waht it does
userexit
userexitOP7mo ago
No description
userexit
userexitOP7mo ago
i mean he has to click save its intuitive no ? i coded it this way because it was intuitive for me
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
ill jut put in the button SAVE CHANGES this way its more straightforward ig
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
and maybe a button to add a new one it will just add an entry to the table
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
idk what a listview is i just made this because this is the first thing that came up to my mind lmao
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
they can compute it as much as they want
No description
userexit
userexitOP7mo ago
then they have the option to save it too
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
yh
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
they are metro lines bro STIB is a metro company
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
yeah MOVIB STIB LOGO
userexit
userexitOP7mo ago
No description
userexit
userexitOP7mo ago
there is the lines in the top also the metro network @borgrel
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
@borgrel
userexit
userexitOP7mo ago
No description
userexit
userexitOP7mo ago
yeah thats how they are known 1 is stockel 2 is simonis 5 is erasmus 6 is elisabeth
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
no they are shown without the square brackets they are shown as 1,5 instead of [1,5]
userexit
userexitOP7mo ago
userexit
userexitOP7mo ago
just realised while recording this that there was an error we should be able to compute a ride before saving changes the issue relies here
favRidesTable.getItems().clear();
favorites.forEach((f) -> {
FavoriteTableRow favoriteRow = new FavoriteTableRow(f, allStations);
favoriteRow.getDeleteButton().setOnAction((e) -> {
presentation.deleteFavoriteRide(f);
});
favoriteRow.getRunButton().setOnAction((e) -> {
presentation.search(f);
});
favRidesTable.getItems().add(favoriteRow);
});
favRidesTable.getItems().clear();
favorites.forEach((f) -> {
FavoriteTableRow favoriteRow = new FavoriteTableRow(f, allStations);
favoriteRow.getDeleteButton().setOnAction((e) -> {
presentation.deleteFavoriteRide(f);
});
favoriteRow.getRunButton().setOnAction((e) -> {
presentation.search(f);
});
favRidesTable.getItems().add(favoriteRow);
});
im doing presentation.search(f) except f is at this poiint a favoritedto with no origin nor destination
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
its hard when u make the program and the view i should make the view one day ' and the other the model issue is im doing everything at once so i look at it more like a puzzle putting things together to get soemthing that works in the end but ig that just spaghettifies the code
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
@borgrel so what about the error handling lmao do i use observer observable
userexit
userexitOP7mo ago
@borgrel
No description
userexit
userexitOP7mo ago
im going to add a red background maybe on the origin and destination searchablecomboboxes the ones that interfere
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
No description
userexit
userexitOP7mo ago
how does it look
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server