#little help with project
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
Get the reference to the user associated with the request from memory and pass that into thr constructor. Need more context though if u want clearer suggestions
One message removed from a suspended account.
One message removed from a suspended account.
that would be a decent idea for the CLI
One message removed from a suspended account.
One message removed from a suspended account.
well and another option would be to have a currently logged in user then each request made from there is under that user until they sign out and back in to a different user
One message removed from a suspended account.
unless u have some requirement that each time a request is made then a user is selected during the request, not before
One message removed from a suspended account.
having a persisted login you mean? Or per request
(it wont make a huge difference either way)
One message removed from a suspended account.
One message removed from a suspended account.
wdym cant
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
.java has syntax highlighting
One message removed from a suspended account.
One message removed from a suspended account.
so a gui complicates things a little more than a CLI in some regards, but both login systems would work in either case
One message removed from a suspended account.
in either case you're going to have to perform a user search and store that user (whether u use a hashmap or not)
You could, Map<Integer, User> or whatever the key on user is
but you could also do a for loop to find them
if this is for a class thats picky then probably just use a forloop
unless they dont care what code you use, then a hashmap is technically better
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
the map wouldnt be part of a user or manifestation
the user list could be a hashmap i suppose yeah
but the User object itself wouldnt
do u understand how maps work?
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
this for example would be better as a hashmap fyi
oh wait does the JOptionPane API expect that
One message removed from a suspended account.
One message removed from a suspended account.
oh it kinda makes sense
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
ok. Well a map is a "key-value" pair. Keys are something unique, values are something that doesnt necessarily need to be unique
One message removed from a suspended account.
so a userID might be unique, and that "key" would link to the value of the User
Map<User, Manifesto> would indicate that a user can only ever have 1 manifesto
One message removed from a suspended account.
One message removed from a suspended account.
do you have something like
public class User {
private int id;
private String firstName;
private String lastName;
}``` for example
One message removed from a suspended account.
One message removed from a suspended account.
whats cpf mean
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
ok yeah then that would be the ID for them
so you could have Map<String, Usuario>
Why is that an abstract class?
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
ok that makes sense yeah
One message removed from a suspended account.
im guessing options are different per user type
One message removed from a suspended account.
One message removed from a suspended account.
so each user can perform the same tasks within the program
One message removed from a suspended account.
ok
would it make sense for a user to make consecutive requests?
then you wouldnt need to have the user ID for each request type
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
You're saying you believe the code is more complicated by doing?
If you want to keep it with user ID per request thats fine since they all have access to the same requests
(You wouldnt want them to start filling out a request and then not have access to it)
One message removed from a suspended account.
One message removed from a suspended account.
basically so you dont have 8 levels of nested control flow I would make each form its own method at least
One message removed from a suspended account.
well the dialog boxes
but like you could roughly simplify it down here
theres more but then its at least readable
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
(easier said than done haha) yeah
One message removed from a suspended account.
I would just call it Controller (or whatever that translates to)
its a fairly standard name
heres a simple template with the variables you had before ```java
public class Controller {
private List<Manifestacao> manifestacaoList;
private List<Usuario> usuarioList;
public Controller() {
manifestacaoList = new ArrayList<>();
usuarioList = new ArrayList<>();
}
}```
then you can create a public void run() method that does basically why main(String[] args) did before, but without the variable declarations in it
One message removed from a suspended account.
then each switch case can be a method
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
yeah but the controller can call them themselves
so main would just do controller.run();
its just a way of getting out of static context
and then every method in the class will have access to private List<Manifestacao> manifestacaoList; private List<Usuario> usuarioList;
One message removed from a suspended account.
One message removed from a suspended account.
well the key would be String (the type of cpf) and the value would be Usuario not manifests
you're wanting to map user IDs to users
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
either you have a shared list of all manifests, or each user could maintain their own list of manifests
based on ur original post the manifest had a User object in it. you could do that and just keep the list of manifestos as u have and it'd be fine
One message removed from a suspended account.
it doesnt need a map for storing manifests
if you had a map it would have to be Map<UserKey, List<Manifest>>, which is not great
One message removed from a suspended account.
its whoever wrote it
it would be like having a book with an author
just replac book with manifest and author with user
One message removed from a suspended account.
its just not necesary to have that complexity since the manifest already has the user. If you want to filter a users manifests you could just iterate across the List<Manifest> and find matching users
One message removed from a suspended account.
are you able to add the List<Manifest> to the Usario class?
One message removed from a suspended account.
basically chop off the map part and just directly store each uses manifests on their instance
so a user would have their ID, name, birthday, and manifests
One message removed from a suspended account.
One message removed from a suspended account.
ok, so they have an empty list
meaning they have no manifests at the time a user is created
One message removed from a suspended account.
yeah. user.addManifest(newManifest)
then that method can just add to the list, and perhaps establish the bi-directional link from manifest to user
One message removed from a suspended account.
yeah like this one java Manifestacao manifestacao = new Manifestacao(); manifestacao.setDataManifestacao(dateFormat); manifestacao.setDescricao(descricaoInput.getText());
One message removed from a suspended account.
and at this point you'd be able to remove the shared manifest list
or keep it
but if u keep the list you'll have to maintain it
meaning you would have to add the manifest to the user, and also add it to the shared controller list
unless u need all manifestos for all users often
One message removed from a suspended account.
public class Controller {
private List<Manifestacao> manifestacaoList;```
the previously local variables
One message removed from a suspended account.
One message removed from a suspended account.
it would be a List/ArrayList
One message removed from a suspended account.
a user has multiple manifests, so instead of private Manifest manifest it would be a private List<Manifest> manifests. (and then = new ArrayList<>() )
One message removed from a suspended account.
also your variables should start with lowercase letters
yes
each user will have its own array
also a tip, you had manifestacaoArray.size() == 0. There is a method for this. manifestacaoArray.isEmpty()
One message removed from a suspended account.
One message removed from a suspended account.
it is
One message removed from a suspended account.
One message removed from a suspended account.
You can either maintain the list in the controller, or have a method that iterates over the users and adds them to a combined list
List<Manifest> allManifests = new ArrayList<>();
for (User u : users) {
allManifests.addAll(u.getManifests());
}
return allManifests;
technically maintaining the list directly is better performance, but for this it doesnt matter
and then you can change it later if you want
but just get the 'easy' version of it working
because if you add and remove manifests from a user you also would need to remove it from the shared list, but by recreating the shared list everytime it is needed it'll always be correct
One message removed from a suspended account.
One message removed from a suspended account.
not too complex. it would just be
private List<Manifest> getAllManifests() {
List<Manifest> allManifests = new ArrayList<>();
for (User u : users) {
allManifests.addAll(u.getManifests());
}
return allManifests;
}```
then anytime you need all the manifests just call that method
One message removed from a suspended account.
i'll be back in a little bit but just have a controller with one variable (the list of users) and go from there. (Since users have the manifests)
~15 min
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
the controller is responsible for actions
the user interacts with the view, all interactions gets forwarded to the controller
the controller interacts with the model, determines if any view changes need to be made
hence the term "controller". it is the controller
its not a true controller since its technically not listening for any events, but its executing branches based on user inputs
i guess maybe thats not even a criteria for it, but either way it doesnt matter too much on the specific terminology
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
because the controller needs state. the state here is non-static (or else EVERYTHING using it needs to be static)
static should only* be used for things that dont depend on any state
e.g. Math.abs(-1) == 1
static method, doesnt depend on state, only inputs
and since all methods of a class have access to that classes variables, you can have the program's state scoped to a smaller size
your main file will just be ```java
public class Main {
public static void main(String[] args) {
Controller controller = new Controller();
controller.run();
}
}
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
yeah, its just a way of getting us away from static
One message removed from a suspended account.
One message removed from a suspended account.
also, you currently have java int menuInput = 0; while (menuInput != 2) { // get menu input switch (menuInput) { ... } }
a way to make this simpler to understand is using while (true). java while (true) { int menuInput = // get input if (menuInput == 2) { break; // Exit while true } switch (menuInput) { ... } }
they are actually doing something similar, but they are using main to create an object of the Main class's type, and using instance (non static) from there. In what I was suggesting with the Controller it just hides it away completely
roughly like ```java
class Controller {
private List<Manifest> manifestacaoList;
private List<User> usuarioList;
public Controller() {
manifestacaoList = new ArrayList<>();
usuarioList = new ArrayList<>();
}
public static void main(String[] args) {
Controller controller = new Controller();
controller.run();
}
public void run() {```
but its best practice that main is, well, main
just a pure entry point
Also they're usingSystem.exit(0); which isn't recommended to terminate when you have other ways
which is basically what you were doing with this original way of exiting
One message removed from a suspended account.
correct, this is the part i was mentioning about maintaining a shared list in addition to each users' list
Leaving it out for now and just having a getter method handle the combining will be a simpler start
One message removed from a suspended account.
yes that was the idea, but just leave it out for now and get the list the other way
One message removed from a suspended account.
and revisit it at the end if you wish, it wouldnt be a large change, just not something to be concerned about
Wdym "all"
One message removed from a suspended account.
One message removed from a suspended account.
Just like User is your abstract class to the 3 types available, List can be thought of as being very similar, and ArrayList is a specific type of List. You can type everything as ArrayList<> as you want but its not necessary. Its better practice for the declared type to be List, and less to type
you would still say private List<User> usarioList = new ArrayList<>(); though
you can't say new List<>() with a few asterisks**
One message removed from a suspended account.
The difference isnt super important to understand right now. Also just forget about the Map<userid, user> for now, and just make it a method that iterates over the users. Can change that easily later on
An arraylist is a list but not all lists are arraylist
One message removed from a suspended account.
i would hope the instructor for this wouldnt care if List<> __ = new ArrayList<>() is used, since it literally is an arraylist
but if you want to be safe, just say ArrayList<> ... = new ArrayList<>()
it makes practically no difference here
in large applications its better to use the supertype so you dont have to specifically be a subtype
like how some of your methods will take in a User and not a Professor
like if the method applies to all users, it shouldnt be subtyped, but i'm off track
also the reason i changed it from userArray to userList in the name is because userArray implies User[] as the type
(arraylist is a list that uses arrays [] internally)
so in terms of a template, you should have ```java
public class Main {
public static void main(String[] args) {
Controller controller = new Controller();
controller.run();
}
}
And ```java
public class Controller {
private ArrayList<User> userList = new ArrayList<>();
public Controller() {}
public void run() {
// TODO!
}
}```
One message removed from a suspended account.
not really
theres some copy pasting you can do with ur old switch cases
but they'll just be like 50 line methods instead of a 200 lines method
One message removed from a suspended account.
this
cause rn you only have a few paths covered and its hundreds of lines, and very hard to tell whats happening
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
this part is within run
so at a super high level with no implementations: ```java
public void run() {
int userInput = 1;
switch (userInput) {
case 1 -> part1();
case 2 -> part2();
case 3 -> part3();
}
}
private void part1() {
}
private void part2() {
}
private void part3() {
}```
rename them to something different, but thats the idea
(thats java 14+ switch syntax btw)
One message removed from a suspended account.
yes
and it should have a method for each of its main things it controlls
so the first set of options is register/list/exit. Exit doesnt require a method, thats just a break
but i'm not sure on the nesting of register/list
One message removed from a suspended account.
One message removed from a suspended account.
the exact same way, but im saying to take that switch inside of a switch and make that a private method
One message removed from a suspended account.
yeah the while would wrap that switch
so the first layer of menu options are Register something, list something, or exit?
One message removed from a suspended account.
so using what you had before I'll update the outer switches on the template: ```java
public class Controller {
private ArrayList<User> userList = new ArrayList<>();
public Controller() {}
public void run() {
JOptionPane.showMessageDialog(null, "Bem vindo! Voce está no sistema da ouvidoria!", "Ouvidoria", 1);
while (true) {
String[] menu = { "Register", "List", "Exit"};
int menuInput = JOptionPane.showOptionDialog(null, "Escolha uma opção", "Ouvidoria - Menu", 0, 3, null, menu, menu[0]);
if (menuInput == 2) {
break;
}
switch (menuInput) {
case 0 -> startRegistration();
case 1 -> listInformation();
}
}
}
private void startRegistration() {
}
private void listInformation() {
}``` that all make sense?
One message removed from a suspended account.
One message removed from a suspended account.
i updated mine to remove the static option constants, but similar. You should still have the menuinput dialog box inside the loop as you originally had
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
yup seems good. and yeah for now
yeah im looking at it and your if condition seems backwards
you check if there are no manifestos, then say there are no registered users
You should be checking users, which is the local userList.isEmpty()
and since you're scoped to a method you can simplify it to java if (userList.isEmpty()) { JOptionPane.showMessageDialog(null, "Nenhum usuário cadastrado! \nVocê precisa cadastrar um usuário primeiro.", "Ouvidoria - Manifestação", 2); return; } // normal processing String userList = "Lista de usuários"; for (Usuario user : manifestacaoList){ userList += "\n- " + user.getNome(); } JOptionPane.showMessageDialog(null, userList, "Ouvidoria - Manifestação", JOptionPane.WARNING_MESSAGE); ...
One message removed from a suspended account.
One message removed from a suspended account.
yeah. and crap yeah i forgot to mention a step
One message removed from a suspended account.
separate manifesto from user registration
private private void createUser() and private void createManifesto()
or whatever you want to name them
One message removed from a suspended account.
then that case 0: // Cadastrar uma manifestação
would go inside createManifesto
private void startRegistration() {
int menuInput2 = 0;
String[] menu2 = { "Manifestação", "Usuário", "Voltar"};
menuInput2 = JOptionPane.showOptionDialog(null, "Escolha uma opção", "Ouvidoria - Cadastrar", 0, JOptionPane.QUESTION_MESSAGE, null, menu2, menu2[0]);
switch (menuInput2){
case 0: // Cadastrar uma manifestação
registerManifest();
break;
case 1: // Cadastrar um usuário
registerUser();
break;
case 2: // Voltar para o menu principal
break;
}
}```
roughly
and then copy the cases again into their own method
using the original code:
private void registerManifesto() {
if(manifestacaoList.size() == 0){
JOptionPane.showMessageDialog(null, "Nenhum usuário cadastrado! \nVocê precisa cadastrar um usuário primeiro.", "Ouvidoria - Manifestação", 2);
} else {
String userList = "Lista de usuários";
for (Usuario user : manifestacaoList){
userList += "\n- " + user.getNome();
}
JOptionPane.showMessageDialog(null, userList, "Ouvidoria - Manifestação", JOptionPane.WARNING_MESSAGE);
int menuInput3 = 0;
String[] menu3 = { "Reclamação", "Sugestão", "Elogio"};
menuInput2 = JOptionPane.showOptionDialog(null, "Escolha uma opção", "Ouvidoria - Cadastrar", 0, 3, null, menu3, menu3[0]);
JTextField nomeUserInput = new JTextField();
JTextField descricaoInput = new JTextField();
Object[] message4 = {
"Nome do usuário:", nomeUserInput,
"Descrição:", descricaoInput,
};
JOptionPane.showConfirmDialog(null, message4, "Ouvidoria - Usuário - Professor", JOptionPane.OK_CANCEL_OPTION);
if (nomeUserInput.getText().equals("") || descricaoInput.getText().equals("") ){
JOptionPane.showMessageDialog(null, "Erro ao cadastrar usuário! \n Campo de texto vázio!", "Ouvidoria", 0);
}
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Manifestacao manifestacao = new Manifestacao();
manifestacao.setDataManifestacao(dateFormat);
manifestacao.setDescricao(descricaoInput.getText());
switch (menuInput3){
case 0: // Reclamação
manifestacao.setCategoria(Categoria.CATEGORIA_RECLAMACAO);
break;
case 1: // Sugestao
manifestacao.setCategoria(Categoria.CATEGORIA_SUGESTAO);
break;
case 2: // Elogio
manifestacao.setCategoria(Categoria.CATEGORIA_ELOGIO);
break;
}
}
}```
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
yes
which will bring you back to the main menu
also .isEmpty() works on strings
if (nomeUserInput.getText().isEmpty() || descricaoInput.getText().isEmpty()) {
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
it shouldnt be in the if
One message removed from a suspended account.
after the return just put that copy paste into the normal part of the method (no indentation/nesting)
e.g.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
within this method its a little confusing / inforrect i think
the title at least
"Ouvidoria - Usuário - Professor"
One message removed from a suspended account.
One message removed from a suspended account.
also the empty input should probably return, unless you want to loop until valid inputs
this part will have to change a little bit to fetch the user and add the manifest to them
One message removed from a suspended account.
make a method to return a user by ID or name (it seems the popup box is asking for name, not ID though)
i believe in ur ability to create a private User getUserByName(String name) in the controller
One message removed from a suspended account.
i meant that if the user left one of the fields blank you could wrap that section in another loop until its non-blank, but if its not a requirement just return early (since they didnt provide enough data)
One message removed from a suspended account.
One message removed from a suspended account.
yeah so it just goes back a menu if it fails
One message removed from a suspended account.
well only the previous step
it wont return to the main menu
eh maybe it would
i guess it depends on how you wrap things in loops or not
One message removed from a suspended account.
also i made u a helper method if you want to use it java private static boolean anyEmpty(JTextField... fields) { for (JTextField field : fields) { if (field.getText().isEmpty()) { return true; } } return false; }
then you can just say if (anyEmpty(nomeInput, cpfInput, dataNasciInput, cargaHoraInput))
Detected code, here are some useful tools:
kinda, the variable name should start with a lowercase letter
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
well its defined in the user class already so thats redundant
One message removed from a suspended account.
just manifestacaos is enough
well you havent used it yet. will have to possibly create a getter/setter for it. Realistically you only need a getter, then an 'adder'
addManifesto(Manifesto)
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
yeah its roughtly what you had at the start. Theres still a few issues though from the original version
(like manifestos are created and then forgotten)
also the indenting on registrarManifestacao is messed up
it can also use seVazio
One message removed from a suspended account.
yeah
here you need to find the user and add it to their manifestos
and also in that snippet seVazio can shorten the if
One message removed from a suspended account.
One message removed from a suspended account.
yeah it wont change the execution of the code but u should fix it
One message removed from a suspended account.
One message removed from a suspended account.
if (nomeUserInput.getText().equals("") || descricaoInput.getText().equals("") ){
JOptionPane.showMessageDialog(null, "Erro ao cadastrar usuário! \n Campo de texto vázio!", "Ouvidoria", 0);
}``` the if condition
the little helper method to OR them together can be used there
if (seVazio(nomeUserInput, descricaoInput)) {
JOptionPane.showMessageDialog(null, "Erro ao cadastrar usuário! \n Campo de texto vázio!", "Ouvidoria", 0);
}```
u used it in the other spots
One message removed from a suspended account.
One message removed from a suspended account.
thats called varargs in english btw. Its nice to use every now and then
for variable number of arguments
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
yes thats what its currently doing
One message removed from a suspended account.
One message removed from a suspended account.
i think the user should just remember their username
One message removed from a suspended account.
u cant assign a single manifest to a list of manifests, but that shouldnt be in the constructor
only have the first 3 args
the default initialization to new arraylist will just make an empty list for each new user
One message removed from a suspended account.
One message removed from a suspended account.
yes none of the users should take a manifesto on creation
when u first create them they shouldnt have any
that would be a different request
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
no it has a reference to the user object
One message removed from a suspended account.
u still need a findUserById/Cpf/Username whatever, user.addManifest and user.getManifests at least
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
yes
One message removed from a suspended account.
yes but pedro should be in the form of a variable
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
pass a string to the method and replace nomeUserInput.getText() with that parameter string. also Usuario should just be user
the return type shouldnt be void either
One message removed from a suspended account.
findUser(String name)
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
u dnt need a boolean method u want a user returning method
i mean yeah thats an existance search but u need the specific user, assuming they exist
One message removed from a suspended account.
private Usuario findUser(String name)
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
private Usuario findUser(String nome) {
for (Usuario u : usuarioArray) {
if (u.getNome().equals(nome)) {
return u;
}
}
return null;
}```
I need to go to sleep
Usuario user = findUser(inputNome);
if (user == null) {
// say user not found, return early
}
user.addManifesto(manifest);
I'd use the Stream filter method, and you can then use Optional.
this guy is 1 month into programming that's not going to be a realistic option, but yes that is a nice way to filter
Streaming indeed, Optional is a bit harder to tell. But that's a different (interesting) discussion.
