C
C#8mo ago
waler

Help with program logic

So I'm currently doing an exercise for OOP, where I'm designing a database for a classroom of students. So basically I just have two objects "Student" and "Classroom" with specific behaviours like adding a student or removing a student, etc... For context related to my question, the "Student" class has 5 attributes to it being: - Student ID - Student social security number - Student's name - Date of birth - City of birth Note: every parameters name in the screenshot is also mentioned in this order (ID is excluded) The main thing I want to ask here is: I'm trying to implement this new behaviour in the Student class where I can update a student's credentials (Student's ID can not be changed). I know that sometimes, we only need to update a few information like their name and birthday or sometimes all of their credentials (except the ID) At first, I thought I can declare all of the parameters and set a default value for them so any credentials that need not be updated can be left blank (as in not inputted when calling this method). However, I then realise that it was not that simple since I have a case where I need to update the security number and birthdate, but not their name or birthplace. So calling the method as student1.UpdateInfo("new security number", "new birthdate") would not work, since the "new birthdate" is understood as the input for the parameter hoten_new Another solution that I came up with this is to overload this method with all of the possible combinations, but then that would mean I need to have (2^4 - 1) UpdateInfo methods with different parameters. I need some suggestions on what to do here in this case. Code is not written in english so please excuse the inconvenience
No description
8 Replies
Jimmacle
Jimmacle8mo ago
instead of making lots of overloads with different combinations, have you considered making one method to update each attribute?
waler
waler8mo ago
I did actually, then I thought that seems inconvenience when I need to update multiple attributes...but, now that thought seems silly of me hmmm maybe I should have done that
Jimmacle
Jimmacle8mo ago
don't think about it too hard 😛
waler
waler8mo ago
alright, thank you very much. Weird that I overcomplicated something so kekw
Jimmacle
Jimmacle8mo ago
unless there is more validation needed than i can see in your code, you don't actually need any methods at all you can do all the access control you need with properties
waler
waler8mo ago
oooh so kind of like defining a local property for the class? I haven't looked through this, I'll go read about that
Jimmacle
Jimmacle8mo ago
not sure what you mean by local, but for example for the student id you can make it a property with { get; } or { get; private set; } so it can't be modified after the class is instantiated (first) or only by the class itself (second)
waler
waler8mo ago
ah, I think I have read over an example that used get - set but did not look into it, I'll do some search on this then