Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
graphic-2020-TP7
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
MANSOUR Chadi
graphic-2020-TP7
Commits
d21a1951
Commit
d21a1951
authored
5 months ago
by
MANSOUR Chadi
Browse files
Options
Downloads
Patches
Plain Diff
corrected the ShapeWriter class
parent
0784c6fd
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.idea/vcs.xml
+1
-1
1 addition, 1 deletion
.idea/vcs.xml
src/main/java/serializer/ShapeWriter.java
+24
-3
24 additions, 3 deletions
src/main/java/serializer/ShapeWriter.java
with
25 additions
and
4 deletions
.idea/vcs.xml
+
1
−
1
View file @
d21a1951
<?xml version="1.0" encoding="UTF-8"?>
<project
version=
"4"
>
<component
name=
"VcsDirectoryMappings"
>
<mapping
directory=
"
$PROJECT_DIR$
"
vcs=
"Git"
/>
<mapping
directory=
""
vcs=
"Git"
/>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/main/java/serializer/ShapeWriter.java
+
24
−
3
View file @
d21a1951
...
...
@@ -7,17 +7,26 @@ import shape.Shape;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
ShapeWriter
implements
ShapeVisitor
{
private
PrintWriter
printWriter
;
private
static
final
Map
<
Class
<?
extends
Shape
>,
ShapeSerializer
<?
extends
Shape
>>
serializers
=
new
HashMap
<>();
static
{
serializers
.
put
(
Circle
.
class
,
new
CircleSerializer
());
serializers
.
put
(
Rectangle
.
class
,
new
RectangleSerializer
());
}
public
ShapeWriter
(
PrintWriter
printWriter
)
{
this
.
printWriter
=
printWriter
;
}
public
static
void
write
(
File
file
,
List
<
Shape
>
shapes
)
throws
IOException
{
public
static
void
write
(
File
file
,
Iterable
<
Shape
>
shapes
)
throws
IOException
{
try
(
PrintWriter
printWriter
=
new
PrintWriter
(
file
)){
ShapeWriter
shapeWriter
=
new
ShapeWriter
(
printWriter
);
...
...
@@ -29,11 +38,23 @@ public class ShapeWriter implements ShapeVisitor {
@Override
public
void
visit
(
Circle
circle
)
{
printWriter
.
printf
(
"Circle %f %f %f%n"
,
circle
.
getX
(),
circle
.
getY
(),
circle
.
getRadius
());
ShapeSerializer
<
Circle
>
serializer
=
(
ShapeSerializer
<
Circle
>)
serializers
.
get
(
Circle
.
class
);
if
(
serializer
!=
null
)
{
String
serializedData
=
serializer
.
serialize
(
circle
);
printWriter
.
println
(
serializedData
);
}
else
{
throw
new
IllegalArgumentException
(
"No serializer found for Circle"
);
}
}
@Override
public
void
visit
(
Rectangle
rectangle
)
{
printWriter
.
printf
(
"Rectangle %f %f %f %f%n"
,
rectangle
.
getX
(),
rectangle
.
getY
(),
rectangle
.
getWidth
()
,
rectangle
.
getHeight
());
ShapeSerializer
<
Rectangle
>
serializer
=
(
ShapeSerializer
<
Rectangle
>)
serializers
.
get
(
Rectangle
.
class
);
if
(
serializer
!=
null
)
{
String
serializedData
=
serializer
.
serialize
(
rectangle
);
printWriter
.
println
(
serializedData
);
}
else
{
throw
new
IllegalArgumentException
(
"No serializer found for Rectangle"
);
}
}
}
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