FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/generics/Store.java at master · CSapprenticeWT/OnJava8-Examples · GitHub
CSapprenticeWT
OnJava8-Examples
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
generics
/
Store.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
87 lines (82 loc) · 2.24 KB
Breadcrumbs
OnJava8-Examples
/
generics
/
Store.java
Copy path
File metadata and controls
87 lines (82 loc) · 2.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// generics/Store.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Building a complex model using generic collections
import
java
.
util
.*;
import
java
.
util
.
function
.*;
import
onjava
.*;
class
Product
{
private
final
int
id
;
private
String
description
;
private
double
price
;
Product
(
int
idNumber
,
String
descr
,
double
price
) {
id
=
idNumber
;
description
=
descr
;
this
.
price
=
price
;
System
.
out
.
println
(
toString
());
}
@
Override
public
String
toString
() {
return
id
+
": "
+
description
+
", price: $"
+
price
;
}
public
void
priceChange
(
double
change
) {
price
+=
change
;
}
public
static
Supplier
<
Product
>
generator
=
new
Supplier
<
Product
>() {
private
Random
rand
=
new
Random
(
47
);
@
Override
public
Product
get
() {
return
new
Product
(
rand
.
nextInt
(
1000
),
"Test"
,
Math
.
round
(
rand
.
nextDouble
() *
1000.0
) +
0.99
);
}
};
}
class
Shelf
extends
ArrayList
<
Product
> {
Shelf
(
int
nProducts
) {
Suppliers
.
fill
(
this
,
Product
.
generator
,
nProducts
);
}
}
class
Aisle
extends
ArrayList
<
Shelf
> {
Aisle
(
int
nShelves
,
int
nProducts
) {
for
(
int
i
=
0
;
i
<
nShelves
;
i
++)
add
(
new
Shelf
(
nProducts
));
}
}
class
CheckoutStand
{}
class
Office
{}
public
class
Store
extends
ArrayList
<
Aisle
> {
private
ArrayList
<
CheckoutStand
>
checkouts
=
new
ArrayList
<>();
private
Office
office
=
new
Office
();
public
Store
(
int
nAisles
,
int
nShelves
,
int
nProducts
) {
for
(
int
i
=
0
;
i
<
nAisles
;
i
++)
add
(
new
Aisle
(
nShelves
,
nProducts
));
}
@
Override
public
String
toString
() {
StringBuilder
result
=
new
StringBuilder
();
for
(
Aisle
a
:
this
)
for
(
Shelf
s
:
a
)
for
(
Product
p
:
s
) {
result
.
append
(
p
);
result
.
append
(
"
\n
"
);
}
return
result
.
toString
();
}
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
new
Store
(
5
,
4
,
3
));
}
}
/* Output: (First 8 Lines)
258: Test, price: $400.99
861: Test, price: $160.99
868: Test, price: $417.99
207: Test, price: $268.99
551: Test, price: $114.99
278: Test, price: $804.99
520: Test, price: $554.99
140: Test, price: $530.99
...
*/
Back
|
FazBrowse Home
|
New Git URL