Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
exercises
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
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
ISC2
oop
exercises
Commits
865eb9ac
Commit
865eb9ac
authored
1 year ago
by
iliya
Browse files
Options
Downloads
Patches
Plain Diff
feat: ex3 pt2 serie2 finished
parent
dd5c966f
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
serie2/ex3_2/.gitignore
+1
-0
1 addition, 0 deletions
serie2/ex3_2/.gitignore
serie2/ex3_2/Container.java
+84
-0
84 additions, 0 deletions
serie2/ex3_2/Container.java
serie2/ex3_2/MainContainer.java
+22
-0
22 additions, 0 deletions
serie2/ex3_2/MainContainer.java
with
107 additions
and
0 deletions
serie2/ex3_2/.gitignore
0 → 100644
+
1
−
0
View file @
865eb9ac
*.class
This diff is collapsed.
Click to expand it.
serie2/ex3_2/Container.java
0 → 100644
+
84
−
0
View file @
865eb9ac
public
class
Container
{
private
int
length
;
private
int
capacity
;
public
static
void
ensurePositiveValue
(
int
...
value
)
{
for
(
int
i
:
value
)
{
if
(
i
<
0
)
{
throw
new
RuntimeException
(
"Value must be greater or equal to zero\tCurrently value = "
+
i
);
}
}
}
public
Container
(
int
capacity
)
{
ensurePositiveValue
(
capacity
);
this
.
capacity
=
capacity
;
this
.
length
=
0
;
}
public
static
Container
withCapacity
(
int
capacity
)
{
return
new
Container
(
capacity
);
}
public
void
fillToTheMax
()
{
this
.
length
=
this
.
capacity
;
}
public
void
fillWith
(
int
value
)
{
ensurePositiveValue
(
value
);
if
(
this
.
length
+
value
>
this
.
capacity
)
{
throw
new
RuntimeException
(
"Container overflowed"
);
}
this
.
length
+=
value
;
}
public
int
quantity
()
{
return
this
.
length
;
}
public
boolean
isFull
()
{
return
this
.
length
==
this
.
capacity
;
}
public
int
remaining
()
{
return
this
.
capacity
-
this
.
length
;
}
public
void
remove
(
int
value
)
{
ensurePositiveValue
(
this
.
length
-
value
);
this
.
length
-=
value
;
}
public
void
flush
()
{
this
.
length
=
0
;
}
public
void
fillTo
(
Container
baseContainer
,
Container
...
containers
)
{
int
toFillBase
=
baseContainer
.
remaining
();
if
(
toFillBase
<
this
.
length
)
{
baseContainer
.
fillWith
(
toFillBase
);
this
.
remove
(
toFillBase
);
}
else
{
int
toFill
=
this
.
length
;
this
.
remove
(
this
.
length
);
baseContainer
.
fillWith
(
toFill
);
}
for
(
Container
container
:
containers
)
{
int
toFillContainer
=
container
.
remaining
();
System
.
out
.
println
(
this
.
length
);
if
(
this
.
length
>
toFillContainer
)
{
container
.
fillWith
(
toFillContainer
);
this
.
remove
(
toFillContainer
);
}
else
{
int
toFill
=
this
.
length
;
this
.
remove
(
this
.
length
);
container
.
fillWith
(
toFill
);
}
}
}
}
This diff is collapsed.
Click to expand it.
serie2/ex3_2/MainContainer.java
0 → 100644
+
22
−
0
View file @
865eb9ac
public
class
MainContainer
{
public
static
void
main
(
String
[]
args
)
{
Container
origin
=
Container
.
withCapacity
(
10
);
origin
.
fillToTheMax
();
Container
destination1
=
Container
.
withCapacity
(
5
);
destination1
.
fillWith
(
2
);
Container
destination2
=
Container
.
withCapacity
(
3
);
Container
destination3
=
Container
.
withCapacity
(
10
);
origin
.
fillTo
(
destination1
,
destination2
,
destination3
);
assert
destination1
.
isFull
();
assert
destination2
.
isFull
();
assert
destination2
.
remaining
()
==
0
;
destination2
.
remove
(
2
);
assert
destination2
.
remaining
()
==
2
;
assert
!
destination3
.
isFull
();
System
.
out
.
println
(
destination3
.
quantity
());
assert
destination3
.
quantity
()
==
4
;
destination3
.
flush
();
assert
destination3
.
quantity
()
==
0
;
}
}
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