Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MOBAPP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
THONG Sylvain
MOBAPP
Commits
76903570
Commit
76903570
authored
3 months ago
by
s22thong
Browse files
Options
Downloads
Patches
Plain Diff
fix(DX): setters with empty checks for attributes in User class
parent
b468cb9d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/src/main/java/fr/imt_atlantique/myfirstapplication/User.java
+88
-7
88 additions, 7 deletions
.../main/java/fr/imt_atlantique/myfirstapplication/User.java
with
88 additions
and
7 deletions
app/src/main/java/fr/imt_atlantique/myfirstapplication/User.java
+
88
−
7
View file @
76903570
...
...
@@ -4,10 +4,14 @@ import android.os.Build;
import
android.os.Parcel
;
import
android.os.Parcelable
;
import
androidx.annotation.NonNull
;
import
java.text.DateFormat
;
import
java.time.LocalDate
;
import
java.time.format.DateTimeFormatter
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Date
;
public
class
User
implements
Parcelable
{
private
String
firstName
;
...
...
@@ -17,15 +21,21 @@ public class User implements Parcelable {
private
LocalDate
birthDate
;
private
String
[]
phoneNumbers
;
public
User
(
String
firstName
,
String
lastName
,
String
birthCity
,
String
birthDept
,
LocalDate
birthDate
,
String
[]
phoneNumbers
)
{
this
.
firstName
=
firstName
;
this
.
lastName
=
lastName
;
this
.
birthCity
=
birthCity
;
this
.
birthDept
=
birthDept
;
this
.
birthDate
=
birthDate
;
this
.
phoneNumbers
=
phoneNumbers
;
public
User
(
String
firstName
,
String
lastName
,
String
birthCity
,
String
birthDept
,
LocalDate
birthDate
,
String
[]
phoneNumbers
)
throws
Exception
{
try
{
setFirstName
(
firstName
);
setLastName
(
lastName
);
setBirthCity
(
birthCity
);
setBirthDept
(
birthDept
);
setBirthDate
(
birthDate
);
setPhoneNumbers
(
phoneNumbers
);
}
catch
(
Exception
e
)
{
throw
new
IllegalArgumentException
(
e
.
getMessage
());
}
}
protected
User
(
Parcel
in
)
{
firstName
=
in
.
readString
();
lastName
=
in
.
readString
();
...
...
@@ -86,4 +96,75 @@ public class User implements Parcelable {
public
String
[]
getPhoneNumbers
()
{
return
phoneNumbers
;
}
public
void
setFirstName
(
String
firstName
)
{
if
(
firstName
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"The first name can not be empty."
);
}
this
.
firstName
=
capitalizeFirstLetter
(
firstName
);
}
public
void
setLastName
(
String
lastName
)
{
if
(
lastName
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"The last name can not be empty."
);
}
this
.
lastName
=
capitalizeFirstLetter
(
lastName
);
}
public
void
setBirthCity
(
String
birthCity
)
{
if
(
birthCity
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"The birth city can not be unspecified."
);
}
this
.
birthCity
=
capitalizeFirstLetter
(
birthCity
);
}
public
void
setBirthDept
(
String
birthDept
)
{
if
(
birthDept
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"The birth department can not be unspecified."
);
}
this
.
birthDept
=
capitalizeFirstLetter
(
birthDept
);
}
public
void
setBirthDate
(
LocalDate
birthDate
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
if
(
birthDate
!=
null
&&
birthDate
.
isAfter
(
LocalDate
.
now
()))
{
throw
new
IllegalArgumentException
(
"The birth date cannot be in the future."
);
}
}
this
.
birthDate
=
birthDate
;
}
public
void
setPhoneNumbers
(
String
[]
phoneNumbers
)
{
this
.
phoneNumbers
=
phoneNumbers
;
}
@NonNull
@Override
public
String
toString
()
{
return
"User{"
+
"firstName='"
+
firstName
+
'\''
+
", lastName='"
+
lastName
+
'\''
+
", birthCity='"
+
birthCity
+
'\''
+
", birthDept='"
+
birthDept
+
'\''
+
", birthDate="
+
birthDate
+
", phoneNumbers="
+
Arrays
.
toString
(
phoneNumbers
)
+
'}'
;
}
private
String
capitalizeFirstLetter
(
String
input
)
{
if
(
input
==
null
||
input
.
isEmpty
())
{
return
input
;
}
return
input
.
substring
(
0
,
1
).
toUpperCase
()
+
input
.
substring
(
1
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment