Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
java_dzienna_15_09
Commits
61259230
Commit
61259230
authored
Sep 16, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Kilka dodatkowych przykładów
parent
4dd19316
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
347 additions
and
0 deletions
+347
-0
Employee.java
...eElementyJavy/src/main/java/emps/comparable/Employee.java
+169
-0
ObslugaCSV.java
...lementyJavy/src/main/java/emps/comparable/ObslugaCSV.java
+39
-0
Porownywanie.java
...mentyJavy/src/main/java/emps/comparable/Porownywanie.java
+52
-0
Sortowanie_Lista.java
...yJavy/src/main/java/emps/comparable/Sortowanie_Lista.java
+17
-0
Sortowanie_Stream.java
...Javy/src/main/java/emps/comparable/Sortowanie_Stream.java
+16
-0
P2_FiltrowanieDoListyEmp.java
...main/java/emps/strumieniowo/P2_FiltrowanieDoListyEmp.java
+18
-0
P2_FiltrowanieDoListyString.java
...n/java/emps/strumieniowo/P2_FiltrowanieDoListyString.java
+20
-0
P6_UnikalneMiasta_Set.java
...rc/main/java/emps/strumieniowo/P6_UnikalneMiasta_Set.java
+16
-0
No files found.
PC22-ZaawansowaneElementyJavy/src/main/java/emps/comparable/Employee.java
0 → 100644
View file @
61259230
package
emps
.
comparable
;
import
java.time.LocalDate
;
import
java.util.Objects
;
public
class
Employee
implements
Comparable
<
Employee
>
{
private
int
id
;
private
String
firstName
;
private
String
lastName
;
private
String
jobTitle
;
private
double
salary
;
private
LocalDate
hireDate
;
private
String
departmentName
;
private
String
streetAddress
;
private
String
postalCode
;
private
String
city
;
private
String
country
;
public
Employee
(
int
id
,
String
firstName
,
String
lastName
,
String
jobTitle
,
double
salary
,
LocalDate
hireDate
,
String
departmentName
,
String
streetAddress
,
String
postalCode
,
String
city
,
String
country
)
{
this
.
id
=
id
;
this
.
firstName
=
firstName
;
this
.
lastName
=
lastName
;
this
.
jobTitle
=
jobTitle
;
this
.
salary
=
salary
;
this
.
hireDate
=
hireDate
;
this
.
departmentName
=
departmentName
;
this
.
streetAddress
=
streetAddress
;
this
.
postalCode
=
postalCode
;
this
.
city
=
city
;
this
.
country
=
country
;
}
public
int
getId
()
{
return
id
;
}
public
void
setId
(
int
id
)
{
this
.
id
=
id
;
}
public
String
getFirstName
()
{
return
firstName
;
}
public
void
setFirstName
(
String
firstName
)
{
this
.
firstName
=
firstName
;
}
public
String
getLastName
()
{
return
lastName
;
}
public
void
setLastName
(
String
lastName
)
{
this
.
lastName
=
lastName
;
}
public
String
getJobTitle
()
{
return
jobTitle
;
}
public
void
setJobTitle
(
String
jobTitle
)
{
this
.
jobTitle
=
jobTitle
;
}
public
double
getSalary
()
{
return
salary
;
}
public
void
setSalary
(
double
salary
)
{
this
.
salary
=
salary
;
}
public
LocalDate
getHireDate
()
{
return
hireDate
;
}
public
void
setHireDate
(
LocalDate
hireDate
)
{
this
.
hireDate
=
hireDate
;
}
public
String
getDepartmentName
()
{
return
departmentName
;
}
public
void
setDepartmentName
(
String
departmentName
)
{
this
.
departmentName
=
departmentName
;
}
public
String
getStreetAddress
()
{
return
streetAddress
;
}
public
void
setStreetAddress
(
String
streetAddress
)
{
this
.
streetAddress
=
streetAddress
;
}
public
String
getPostalCode
()
{
return
postalCode
;
}
public
void
setPostalCode
(
String
postalCode
)
{
this
.
postalCode
=
postalCode
;
}
public
String
getCity
()
{
return
city
;
}
public
void
setCity
(
String
city
)
{
this
.
city
=
city
;
}
public
String
getCountry
()
{
return
country
;
}
public
void
setCountry
(
String
country
)
{
this
.
country
=
country
;
}
@Override
public
String
toString
()
{
return
"Employee [id="
+
id
+
", firstName="
+
firstName
+
", lastName="
+
lastName
+
", jobTitle="
+
jobTitle
+
", salary="
+
salary
+
", hireDate="
+
hireDate
+
", departmentName="
+
departmentName
+
", streetAddress="
+
streetAddress
+
", postalCode="
+
postalCode
+
", city="
+
city
+
", country="
+
country
+
"]"
;
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
city
,
country
,
departmentName
,
firstName
,
hireDate
,
id
,
jobTitle
,
lastName
,
postalCode
,
salary
,
streetAddress
);
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
Employee
other
=
(
Employee
)
obj
;
return
Objects
.
equals
(
city
,
other
.
city
)
&&
Objects
.
equals
(
country
,
other
.
country
)
&&
Objects
.
equals
(
departmentName
,
other
.
departmentName
)
&&
Objects
.
equals
(
firstName
,
other
.
firstName
)
&&
Objects
.
equals
(
hireDate
,
other
.
hireDate
)
&&
id
==
other
.
id
&&
Objects
.
equals
(
jobTitle
,
other
.
jobTitle
)
&&
Objects
.
equals
(
lastName
,
other
.
lastName
)
&&
Objects
.
equals
(
postalCode
,
other
.
postalCode
)
&&
Double
.
doubleToLongBits
(
salary
)
==
Double
.
doubleToLongBits
(
other
.
salary
)
&&
Objects
.
equals
(
streetAddress
,
other
.
streetAddress
);
}
@Override
public
int
compareTo
(
Employee
other
)
{
// Metoda compareTo (i analogicznie compare w interfejsice Comparator)
// ma zwrócić wartość ujemną, jeśli lewy obiekt (tutaj this) jest mniejszy od prawego
// ma zwrócić wartość dodatnią, jeśli lewy obiekt jest większy od prawego
// ma zwrócić 0, jeśli oba obiekty są równe.
// To my, jako autorzy, decydujemy co to znaczy mniejszy/większy.
// W tej wersji porównujemy pensje pracowników. Aby było łatwo i poprawnie, korzystamy z gotowej metody
return
Double
.
compare
(
this
.
salary
,
other
.
salary
);
}
// Interfejs Comparable i metoda compareTo → domyślny sposób porównywania wpisany w definicję klasy, tzw. "natural order"
// Interfejs Comparator i metoda compare → dodatkowy, alternatywny sposób porównywania tworzony poza klasą, której dotyczy.
// "Maszykna porównująca obiekty", czyli taka "waga".
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/comparable/ObslugaCSV.java
0 → 100644
View file @
61259230
package
emps
.
comparable
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Scanner
;
// To jest przykład tzw. klasy narzędziowej (utility class),
// w której są zdefiniowane różne funkcje - w formie metod statycznych.
public
class
ObslugaCSV
{
public
static
List
<
Employee
>
wczytaj
(
File
plik
)
{
List
<
Employee
>
lista
=
Collections
.
synchronizedList
(
new
ArrayList
<>());
try
(
Scanner
scanner
=
new
Scanner
(
plik
))
{
scanner
.
nextLine
();
while
(
scanner
.
hasNextLine
())
{
String
linia
=
scanner
.
nextLine
();
String
[]
t
=
linia
.
split
(
";"
,
11
);
Employee
emp
=
new
Employee
(
Integer
.
parseInt
(
t
[
0
]),
t
[
1
],
t
[
2
],
t
[
3
],
Double
.
parseDouble
(
t
[
4
]),
LocalDate
.
parse
(
t
[
5
]),
t
[
6
],
t
[
7
],
t
[
8
],
t
[
9
],
t
[
10
]);
lista
.
add
(
emp
);
}
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
return
lista
;
}
public
static
List
<
Employee
>
wczytaj
(
String
sciezka
)
{
return
wczytaj
(
new
File
(
sciezka
));
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/comparable/Porownywanie.java
0 → 100644
View file @
61259230
package
emps
.
comparable
;
import
java.text.Collator
;
import
java.util.List
;
public
class
Porownywanie
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
Employee
emp110
=
emps
.
get
(
10
);
Employee
emp120
=
emps
.
get
(
20
);
System
.
out
.
println
(
emp110
);
System
.
out
.
println
(
emp120
);
System
.
out
.
println
();
// w Javie nie działa porównywanie za pomocą < > rzeczy innych niż liczby
// if(emp110 < emp120) {
// System.out.println("110 zarabia mniej");
// } else if(emp110 > emp120) {
// System.out.println("110 zarabia więcej");
// } else {
// System.out.println("zarabiają tyle samo");
// }
// zamiast tego, jeśli klasa jest Comparable, to wynik operacji compareTo porównujemy z zerem
if
(
emp110
.
compareTo
(
emp120
)
<
0
)
{
System
.
out
.
println
(
"110 zarabia mniej"
);
}
else
if
(
emp110
.
compareTo
(
emp120
)
>
0
)
{
System
.
out
.
println
(
"110 zarabia więcej"
);
}
else
{
System
.
out
.
println
(
"zarabiają tyle samo"
);
}
// to samo dla klas takich jak String, BigDecimal, LocalDate
String
ala
=
"Ala"
;
String
ola
=
"Ola"
;
System
.
out
.
println
(
ala
.
compareTo
(
ola
));
if
(
ala
.
compareTo
(
ola
)
<
0
)
{
System
.
out
.
println
(
"Ala < Ola"
);
}
System
.
out
.
println
();
System
.
out
.
println
(
"Łukasz"
.
compareTo
(
"Marek"
));
// >
Collator
collator
=
Collator
.
getInstance
();
System
.
out
.
println
(
collator
.
compare
(
"Łukasz"
,
"Marek"
));
// <
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/comparable/Sortowanie_Lista.java
0 → 100644
View file @
61259230
package
emps
.
comparable
;
import
java.util.List
;
public
class
Sortowanie_Lista
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
emps
.
sort
(
null
);
for
(
Employee
emp
:
emps
)
{
System
.
out
.
println
(
emp
);
}
}
}
\ No newline at end of file
PC22-ZaawansowaneElementyJavy/src/main/java/emps/comparable/Sortowanie_Stream.java
0 → 100644
View file @
61259230
package
emps
.
comparable
;
import
java.util.List
;
public
class
Sortowanie_Stream
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
emps
.
stream
()
.
sorted
()
.
forEachOrdered
(
emp
->
System
.
out
.
printf
(
"%s %s (%s) zarabia %.2f\n"
,
emp
.
getFirstName
(),
emp
.
getLastName
(),
emp
.
getJobTitle
(),
emp
.
getSalary
()));
}
}
\ No newline at end of file
PC22-ZaawansowaneElementyJavy/src/main/java/emps/strumieniowo/P2_FiltrowanieDoListyEmp.java
0 → 100644
View file @
61259230
package
emps
.
strumieniowo
;
import
java.util.List
;
import
java.util.stream.Collectors
;
public
class
P2_FiltrowanieDoListyEmp
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
List
<
Employee
>
wyniki
=
emps
.
stream
()
.
filter
(
emp
->
emp
.
getSalary
()
>=
10_000
)
.
collect
(
Collectors
.
toList
());
wyniki
.
forEach
(
System
.
out
::
println
);
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/strumieniowo/P2_FiltrowanieDoListyString.java
0 → 100644
View file @
61259230
package
emps
.
strumieniowo
;
import
java.util.List
;
import
java.util.stream.Collectors
;
public
class
P2_FiltrowanieDoListyString
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
List
<
String
>
wyniki
=
emps
.
stream
()
.
filter
(
emp
->
emp
.
getSalary
()
>=
10_000
)
.
map
(
emp
->
String
.
format
(
"Pracownik %s %s (%s) zarabia %.2f USD"
,
emp
.
getFirstName
(),
emp
.
getLastName
(),
emp
.
getJobTitle
(),
emp
.
getSalary
()))
.
collect
(
Collectors
.
toList
());
wyniki
.
forEach
(
System
.
out
::
println
);
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/strumieniowo/P6_UnikalneMiasta_Set.java
0 → 100644
View file @
61259230
package
emps
.
strumieniowo
;
import
java.util.List
;
public
class
P6_UnikalneMiasta_Set
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
emps
.
stream
()
.
map
(
Employee:
:
getCity
)
.
distinct
()
.
forEach
(
System
.
out
::
println
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment