Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
SzkolenieALX_ProjektPatryka
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
SzkolenieALX_ProjektPatryka
Commits
91bf7f37
Commit
91bf7f37
authored
Jun 13, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Liczenie średniej i filtrowanie Jobów
parent
716e1a62
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
114 additions
and
7 deletions
+114
-7
F4_SredniaWszystkich.java
src/emps/F4_SredniaWszystkich.java
+5
-5
F5_SredniaProgramistow.java
src/emps/F5_SredniaProgramistow.java
+19
-0
F6a_SredniaWybranych.java
src/emps/F6a_SredniaWybranych.java
+31
-0
F6b_SredniaWybranychInteraktywnie.java
src/emps/F6b_SredniaWybranychInteraktywnie.java
+50
-0
ObslugaCSV.java
src/emps/ObslugaCSV.java
+9
-2
No files found.
src/emps/F4_SredniaWszystkich.java
View file @
91bf7f37
...
@@ -8,15 +8,15 @@ public class F4_SredniaWszystkich {
...
@@ -8,15 +8,15 @@ public class F4_SredniaWszystkich {
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
double
avg
=
emps
.
stream
()
double
avg
=
emps
.
stream
()
//
.
mapToInt
(
Employee:
:
getSalary
)
// równoważne lambdzie: emp -> emp.getSalary()
.
mapToInt
(
Employee:
:
getSalary
)
// równoważne lambdzie: emp -> emp.getSalary()
.
average
()
.
average
()
//
.
orElse
(
0.0
);
.
orElse
(
0.0
);
//
System
.
out
.
println
(
avg
);
System
.
out
.
println
(
avg
);
double
avg2
=
emps
.
stream
().
collect
(
Collectors
.
averagingInt
(
Employee:
:
getSalary
));
double
avg2
=
emps
.
stream
().
collect
(
Collectors
.
averagingInt
(
Employee:
:
getSalary
));
System
.
out
.
println
(
avg2
);
System
.
out
.
println
(
avg2
);
}
}
}
}
...
...
src/emps/F5_SredniaProgramistow.java
0 → 100644
View file @
91bf7f37
package
emps
;
import
java.util.List
;
public
class
F5_SredniaProgramistow
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
double
avg
=
emps
.
stream
()
.
filter
(
emp
->
"Programmer"
.
equals
(
emp
.
getJobTitle
()))
.
mapToInt
(
Employee:
:
getSalary
)
.
average
()
.
orElse
(
0.0
);
System
.
out
.
println
(
avg
);
}
}
src/emps/F6a_SredniaWybranych.java
0 → 100644
View file @
91bf7f37
package
emps
;
import
java.util.List
;
import
java.util.OptionalDouble
;
import
javax.swing.JOptionPane
;
public
class
F6a_SredniaWybranych
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
String
szukanyJob
=
JOptionPane
.
showInputDialog
(
"Podaj nazwę stanowiska"
);
if
(
szukanyJob
==
null
)
{
return
;
}
OptionalDouble
avg
=
emps
.
stream
()
.
filter
(
emp
->
szukanyJob
.
equalsIgnoreCase
(
emp
.
getJobTitle
()))
.
mapToInt
(
Employee:
:
getSalary
)
.
average
();
if
(
avg
.
isPresent
())
{
JOptionPane
.
showMessageDialog
(
null
,
"Średnia wynosi "
+
avg
.
getAsDouble
());
}
else
{
JOptionPane
.
showMessageDialog
(
null
,
"Nikt nie pracuje na takim stanowisku"
,
"Brak danych"
,
JOptionPane
.
WARNING_MESSAGE
);
}
}
}
src/emps/F6b_SredniaWybranychInteraktywnie.java
0 → 100644
View file @
91bf7f37
package
emps
;
import
java.io.File
;
import
java.util.List
;
import
java.util.OptionalDouble
;
import
javax.swing.JFileChooser
;
import
javax.swing.JOptionPane
;
import
javax.swing.filechooser.FileNameExtensionFilter
;
public
class
F6b_SredniaWybranychInteraktywnie
{
public
static
void
main
(
String
[]
args
)
{
JFileChooser
chooser
=
new
JFileChooser
(
"."
);
chooser
.
setFileFilter
(
new
FileNameExtensionFilter
(
"pliki CSV"
,
"csv"
,
"txt"
));
int
coSieStalo
=
chooser
.
showOpenDialog
(
null
);
if
(
coSieStalo
!=
JFileChooser
.
APPROVE_OPTION
)
{
// jeśli było Cancel lub wystąpił błąd
return
;
}
File
plik
=
chooser
.
getSelectedFile
();
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
plik
);
Object
[]
jobs
=
emps
.
stream
()
.
map
(
Employee:
:
getJobTitle
)
.
distinct
()
.
sorted
()
.
toArray
();
Object
szukanyJob
=
JOptionPane
.
showInputDialog
(
null
,
"Wybierz stanowisko"
,
"Pytanie"
,
JOptionPane
.
QUESTION_MESSAGE
,
null
,
jobs
,
null
);
if
(
szukanyJob
==
null
)
{
return
;
}
OptionalDouble
avg
=
emps
.
stream
()
.
filter
(
emp
->
szukanyJob
.
equals
(
emp
.
getJobTitle
()))
.
mapToInt
(
Employee:
:
getSalary
)
.
average
();
if
(
avg
.
isPresent
())
{
JOptionPane
.
showMessageDialog
(
null
,
"Średnia wynosi "
+
avg
.
getAsDouble
());
}
else
{
JOptionPane
.
showMessageDialog
(
null
,
"Nikt nie pracuje na takim stanowisku"
,
"Brak danych"
,
JOptionPane
.
WARNING_MESSAGE
);
}
}
}
src/emps/ObslugaCSV.java
View file @
91bf7f37
...
@@ -11,10 +11,17 @@ import java.util.Scanner;
...
@@ -11,10 +11,17 @@ import java.util.Scanner;
// Jest to zestaw metod statycznych. Jak moduł w Pythonie czy C++.
// Jest to zestaw metod statycznych. Jak moduł w Pythonie czy C++.
public
class
ObslugaCSV
{
public
class
ObslugaCSV
{
// Przeciążanie (overloading) to taka sytuacja, że w klasie mamy kilka wersji metody o tej samej nazwie.
// Poszczególne wersje różnią się liczbą lub typem parametrów.
public
static
List
<
Employee
>
wczytaj
(
String
sciezka
)
{
public
static
List
<
Employee
>
wczytaj
(
String
sciezka
)
{
return
wczytaj
(
new
File
(
sciezka
));
}
public
static
List
<
Employee
>
wczytaj
(
File
plik
)
{
List
<
Employee
>
emps
=
new
ArrayList
<>();
List
<
Employee
>
emps
=
new
ArrayList
<>();
// try with resources - od Java 7 - automatyczne zamykanie plików i innych zasobów
// try with resources - od Java 7 - automatyczne zamykanie plików i innych zasobów
try
(
Scanner
scanner
=
new
Scanner
(
new
File
(
sciezka
)
))
{
try
(
Scanner
scanner
=
new
Scanner
(
plik
))
{
scanner
.
nextLine
();
// pomijamy pierwszą linię z nagłówkami
scanner
.
nextLine
();
// pomijamy pierwszą linię z nagłówkami
while
(
scanner
.
hasNextLine
())
{
while
(
scanner
.
hasNextLine
())
{
String
linia
=
scanner
.
nextLine
();
String
linia
=
scanner
.
nextLine
();
...
@@ -33,6 +40,6 @@ public class ObslugaCSV {
...
@@ -33,6 +40,6 @@ public class ObslugaCSV {
return
emps
;
return
emps
;
}
}
}
}
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