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
8b7188e6
Commit
8b7188e6
authored
Sep 15, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady emps z poprzednich zajęć
parent
2929e8e6
Show whitespace changes
Inline
Side-by-side
Showing
30 changed files
with
1376 additions
and
0 deletions
+1376
-0
emps.csv
PC22-ZaawansowaneElementyJavy/emps.csv
+108
-0
P1_WypiszDane.java
...tyJavy/src/main/java/emps/nieobiektowo/P1_WypiszDane.java
+29
-0
P3_SredniaPensja.java
...avy/src/main/java/emps/nieobiektowo/P3_SredniaPensja.java
+34
-0
zadania.txt
...eElementyJavy/src/main/java/emps/nieobiektowo/zadania.txt
+2
-0
Employee.java
...neElementyJavy/src/main/java/emps/obiektowo/Employee.java
+155
-0
Grupowanie1.java
...lementyJavy/src/main/java/emps/obiektowo/Grupowanie1.java
+37
-0
Grupowanie2.java
...lementyJavy/src/main/java/emps/obiektowo/Grupowanie2.java
+41
-0
Grupowanie2a.java
...ementyJavy/src/main/java/emps/obiektowo/Grupowanie2a.java
+35
-0
Grupowanie3.java
...lementyJavy/src/main/java/emps/obiektowo/Grupowanie3.java
+30
-0
Grupowanie4.java
...lementyJavy/src/main/java/emps/obiektowo/Grupowanie4.java
+31
-0
Grupowanie5.java
...lementyJavy/src/main/java/emps/obiektowo/Grupowanie5.java
+33
-0
MergeDodatkowe.java
...entyJavy/src/main/java/emps/obiektowo/MergeDodatkowe.java
+24
-0
ObslugaCSV.java
...ElementyJavy/src/main/java/emps/obiektowo/ObslugaCSV.java
+81
-0
P0_WczytajDane.java
...entyJavy/src/main/java/emps/obiektowo/P0_WczytajDane.java
+15
-0
P1_WypiszWybranePola.java
...vy/src/main/java/emps/obiektowo/P1_WypiszWybranePola.java
+19
-0
P2_WypiszBogatych.java
...yJavy/src/main/java/emps/obiektowo/P2_WypiszBogatych.java
+22
-0
P3_SredniaPensja.java
...tyJavy/src/main/java/emps/obiektowo/P3_SredniaPensja.java
+20
-0
P4_SredniaProgramistow.java
.../src/main/java/emps/obiektowo/P4_SredniaProgramistow.java
+24
-0
P4a_SredniaWybranych_Interaktywnie.java
...va/emps/obiektowo/P4a_SredniaWybranych_Interaktywnie.java
+40
-0
P4b_SredniaWybranych_Interaktywnie.java
...va/emps/obiektowo/P4b_SredniaWybranych_Interaktywnie.java
+51
-0
P5_MinMax_v1.java
...ementyJavy/src/main/java/emps/obiektowo/P5_MinMax_v1.java
+48
-0
P5_MinMax_v2.java
...ementyJavy/src/main/java/emps/obiektowo/P5_MinMax_v2.java
+35
-0
P5_MinMax_v3.java
...ementyJavy/src/main/java/emps/obiektowo/P5_MinMax_v3.java
+31
-0
P5_MinMax_v4.java
...ementyJavy/src/main/java/emps/obiektowo/P5_MinMax_v4.java
+31
-0
P6_WypiszUnikalneMiasta.java
...src/main/java/emps/obiektowo/P6_WypiszUnikalneMiasta.java
+24
-0
P6a_WypiszUnikalneMiasta_Lista.java
...n/java/emps/obiektowo/P6a_WypiszUnikalneMiasta_Lista.java
+27
-0
P7a_Sortowanie.java
...entyJavy/src/main/java/emps/obiektowo/P7a_Sortowanie.java
+27
-0
P7b_Sortowanie.java
...entyJavy/src/main/java/emps/obiektowo/P7b_Sortowanie.java
+22
-0
P7c_Sortowanie.java
...entyJavy/src/main/java/emps/obiektowo/P7c_Sortowanie.java
+27
-0
PodwyzkaOkno.java
...ementyJavy/src/main/java/emps/obiektowo/PodwyzkaOkno.java
+273
-0
No files found.
PC22-ZaawansowaneElementyJavy/emps.csv
0 → 100644
View file @
8b7188e6
employee_id;first_name;last_name;job_title;salary;hire_date;department_name;address;postal_code;city;country
100;Steven;King;President;24000;1997-06-17;Executive;2004 Charade Rd;98199;Seattle;United States of America
101;Neena;Kochhar;Administration Vice President;17000;1999-09-21;Executive;2004 Charade Rd;98199;Seattle;United States of America
102;Lex;De Haan;Administration Vice President;17000;2003-01-13;Executive;2004 Charade Rd;98199;Seattle;United States of America
103;Alexander;Hunold;Programmer;9000;2000-01-03;IT;2014 Jabberwocky Rd;26192;Southlake;United States of America
104;Bruce;Ernst;Programmer;6000;2001-05-21;IT;2014 Jabberwocky Rd;26192;Southlake;United States of America
105;David;Austin;Programmer;4800;2007-06-25;IT;2014 Jabberwocky Rd;26192;Southlake;United States of America
106;Valli;Pataballa;Programmer;4800;2008-02-05;IT;2014 Jabberwocky Rd;26192;Southlake;United States of America
107;Diana;Lorentz;Programmer;4200;2009-02-07;IT;2014 Jabberwocky Rd;26192;Southlake;United States of America
108;Nancy;Greenberg;Finance Manager;12000;2004-08-17;Finance;2004 Charade Rd;98199;Seattle;United States of America
109;Daniel;Faviet;Accountant;9000;2004-08-16;Finance;2004 Charade Rd;98199;Seattle;United States of America
110;John;Chen;Accountant;8200;2007-09-28;Finance;2004 Charade Rd;98199;Seattle;United States of America
111;Ismael;Sciarra;Accountant;7700;2007-09-30;Finance;2004 Charade Rd;98199;Seattle;United States of America
112;Jose Manuel;Urman;Accountant;7800;1998-03-07;Finance;2004 Charade Rd;98199;Seattle;United States of America
113;Luis;Popp;Accountant;6900;2009-12-07;Finance;2004 Charade Rd;98199;Seattle;United States of America
114;Den;Raphaely;Purchasing Manager;11000;2004-12-07;Purchasing;2004 Charade Rd;98199;Seattle;United States of America
115;Alexander;Khoo;Purchasing Clerk;3100;2005-05-18;Purchasing;2004 Charade Rd;98199;Seattle;United States of America
116;Shelli;Baida;Purchasing Clerk;2900;2007-12-24;Purchasing;2004 Charade Rd;98199;Seattle;United States of America
117;Sigal;Tobias;Purchasing Clerk;2800;2007-07-24;Purchasing;2004 Charade Rd;98199;Seattle;United States of America
118;Guy;Himuro;Purchasing Clerk;2600;2008-11-15;Purchasing;2004 Charade Rd;98199;Seattle;United States of America
119;Karen;Colmenares;Purchasing Clerk;2500;2009-08-10;Purchasing;2004 Charade Rd;98199;Seattle;United States of America
120;Matthew;Weiss;Stock Manager;8000;2006-07-18;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
121;Adam;Fripp;Stock Manager;8200;2007-04-10;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
122;Payam;Kaufling;Stock Manager;7900;2005-05-01;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
123;Shanta;Vollman;Stock Manager;6500;2007-10-10;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
124;Kevin;Mourgos;Stock Manager;5800;2009-11-16;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
125;Julia;Nayer;Stock Clerk;3200;2007-07-16;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
126;Irene;Mikkilineni;Stock Clerk;2700;2008-09-28;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
127;James;Landry;Stock Clerk;2400;2009-01-14;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
128;Steven;Markle;Stock Clerk;2200;2010-03-08;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
129;Laura;Bissot;Stock Clerk;3300;2007-08-20;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
130;Mozhe;Atkinson;Stock Clerk;2800;2007-10-30;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
131;James;Marlow;Stock Clerk;2500;2007-02-16;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
132;TJ;Olson;Stock Clerk;2100;2009-04-10;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
133;Jason;Mallin;Stock Clerk;3300;2006-06-14;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
134;Michael;Rogers;Stock Clerk;2900;2008-08-26;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
135;Ki;Gee;Stock Clerk;2400;2009-12-12;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
136;Hazel;Philtanker;Stock Clerk;2200;2011-02-06;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
137;Renske;Ladwig;Stock Clerk;3600;2005-07-14;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
138;Stephen;Stiles;Stock Clerk;3200;2007-10-26;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
139;John;Seo;Stock Clerk;2700;2008-02-12;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
140;Joshua;Patel;Stock Clerk;2500;2008-04-06;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
141;Trenna;Rajs;Stock Clerk;3500;2005-10-17;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
142;Curtis;Davies;Stock Clerk;3100;2007-01-29;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
143;Randall;Matos;Stock Clerk;2600;2008-03-15;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
144;Peter;Vargas;Stock Clerk;2500;2008-07-09;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
145;John;Russell;Sales Manager;14000;2006-10-01;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
146;Karen;Partners;Sales Manager;13500;2007-01-05;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
147;Alberto;Errazuriz;Sales Manager;12000;2007-03-10;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
148;Gerald;Cambrault;Sales Manager;11000;2009-10-15;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
149;Eleni;Zlotkey;Sales Manager;10500;2000-01-29;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
150;Peter;Tucker;Sales Representative;10000;2007-01-30;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
151;David;Bernstein;Sales Representative;9500;2007-03-24;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
152;Peter;Hall;Sales Representative;9000;2007-08-20;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
153;Christopher;Olsen;Sales Representative;8000;2008-03-30;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
154;Nanette;Cambrault;Sales Representative;7500;2008-12-09;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
155;Oliver;Tuvault;Sales Representative;7000;2009-11-23;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
156;Janette;King;Sales Representative;10000;2006-01-30;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
157;Patrick;Sully;Sales Representative;9500;2006-03-04;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
158;Allan;McEwen;Sales Representative;9000;2006-08-01;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
159;Lindsey;Smith;Sales Representative;8000;2007-03-10;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
160;Louise;Doran;Sales Representative;7500;2007-12-15;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
161;Sarath;Sewall;Sales Representative;7000;2008-11-03;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
162;Clara;Vishney;Sales Representative;10500;2007-11-11;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
163;Danielle;Greene;Sales Representative;9500;2009-03-19;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
164;Mattea;Marvins;Sales Representative;7200;2010-01-24;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
165;David;Lee;Sales Representative;6800;2000-02-23;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
166;Sundar;Ande;Sales Representative;6400;2000-03-24;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
167;Amit;Banda;Sales Representative;6200;2000-04-21;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
168;Lisa;Ozer;Sales Representative;11500;2007-03-11;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
169;Harrison;Bloom;Sales Representative;10000;2008-03-23;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
170;Tayler;Fox;Sales Representative;9600;2008-01-24;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
171;William;Smith;Sales Representative;7400;2009-02-23;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
172;Elizabeth;Bates;Sales Representative;7300;2009-03-24;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
173;Sundita;Kumar;Sales Representative;6100;2010-04-21;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
174;Ellen;Abel;Sales Representative;11000;2006-05-11;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
175;Alyssa;Hutton;Sales Representative;8800;2007-03-19;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
176;Jonathon;Taylor;Sales Representative;8600;2008-03-24;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
177;Jack;Livingston;Sales Representative;8400;2008-04-23;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
178;Kimberely;Grant;Sales Representative;7000;2009-05-24;;;;;
179;Charles;Johnson;Sales Representative;6200;2011-01-04;Sales;Magdalen Centre, The Oxford Science Park;OX9 9ZB;Oxford;United Kingdom
180;Winston;Taylor;Shipping Clerk;3200;2008-01-24;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
181;Jean;Fleaur;Shipping Clerk;3100;2008-02-23;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
182;Martha;Sullivan;Shipping Clerk;2500;2009-06-21;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
183;Girard;Geoni;Shipping Clerk;2800;2000-02-03;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
184;Nandita;Sarchand;Shipping Clerk;4200;2006-01-27;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
185;Alexis;Bull;Shipping Clerk;4100;2007-02-20;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
186;Julia;Dellinger;Shipping Clerk;3400;2008-06-24;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
187;Anthony;Cabrio;Shipping Clerk;3000;2009-02-07;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
188;Kelly;Chung;Shipping Clerk;3800;2007-06-14;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
189;Jennifer;Dilly;Shipping Clerk;3600;2007-08-13;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
190;Timothy;Gates;Shipping Clerk;2900;2008-07-11;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
191;Randall;Perkins;Shipping Clerk;2500;2009-12-19;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
192;Sarah;Bell;Shipping Clerk;4000;2006-02-04;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
193;Britney;Everett;Shipping Clerk;3900;2007-03-03;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
194;Samuel;McCain;Shipping Clerk;3200;2008-07-01;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
195;Vance;Jones;Shipping Clerk;2800;2009-03-17;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
196;Alana;Walsh;Shipping Clerk;3100;2008-04-24;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
197;Kevin;Feeney;Shipping Clerk;3000;2008-05-23;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
198;Donald;OConnell;Shipping Clerk;2600;2009-06-21;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
199;Douglas;Grant;Shipping Clerk;2600;2010-01-13;Shipping;2011 Interiors Blvd;99236;South San Francisco;United States of America
200;Jennifer;Whalen;Administration Assistant;4400;1987-09-17;Administration;2004 Charade Rd;98199;Seattle;United States of America
201;Michael;Hartstein;Marketing Manager;13000;2006-02-17;Marketing;147 Spadina Ave;M5V 2L7;Toronto;Canada
202;Pat;Fay;Marketing Representative;6000;2007-08-17;Marketing;147 Spadina Ave;M5V 2L7;Toronto;Canada
203;Susan;Mavris;Human Resources Representative;6500;2004-06-07;Human Resources;8204 Arthur St;;London;United Kingdom
204;Hermann;Baer;Public Relations Representative;10000;2004-06-07;Public Relations;Schwanthalerstr. 7031;80925;Munich;Germany
205;Shelley;Higgins;Accounting Manager;12000;2004-06-07;Accounting;2004 Charade Rd;98199;Seattle;United States of America
206;William;Gietz;Public Accountant;8300;2004-06-07;Accounting;2004 Charade Rd;98199;Seattle;United States of America
PC22-ZaawansowaneElementyJavy/src/main/java/emps/nieobiektowo/P1_WypiszDane.java
0 → 100644
View file @
8b7188e6
package
emps
.
nieobiektowo
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.util.Arrays
;
import
java.util.Scanner
;
public
class
P1_WypiszDane
{
public
static
void
main
(
String
[]
args
)
{
// Jak odczytać dane z pliku emps.csv i wypisać dane z poszczególnych rekordów?
try
(
Scanner
scanner
=
new
Scanner
(
new
File
(
"emps.csv"
)))
{
while
(
scanner
.
hasNextLine
())
{
String
linia
=
scanner
.
nextLine
();
// System.out.println(linia);
String
[]
t
=
linia
.
split
(
";"
);
// System.out.println(Arrays.toString(t));
// wypisanie wybranych pól z rekordu:
System
.
out
.
printf
(
"Pracownik nr %s: %s %s (%s) zarabia %s\n"
,
t
[
0
],
t
[
1
],
t
[
2
],
t
[
3
],
t
[
4
]);
}
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/nieobiektowo/P3_SredniaPensja.java
0 → 100644
View file @
8b7188e6
package
emps
.
nieobiektowo
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.util.Scanner
;
public
class
P3_SredniaPensja
{
public
static
void
main
(
String
[]
args
)
{
try
(
Scanner
scanner
=
new
Scanner
(
new
File
(
"emps.csv"
)))
{
// wczytuję pierwsza linię, aby ją zignorować , bo tam są nagłówki kolumn
scanner
.
nextLine
();
int
suma
=
0
;
int
ile
=
0
;
while
(
scanner
.
hasNextLine
())
{
String
linia
=
scanner
.
nextLine
();
String
[]
t
=
linia
.
split
(
";"
);
suma
+=
Integer
.
parseInt
(
t
[
4
]);
ile
++;
}
double
srednia
=
(
double
)
suma
/
ile
;
System
.
out
.
println
(
"suma: "
+
suma
);
System
.
out
.
println
(
"średnia: "
+
srednia
);
System
.
out
.
printf
(
"suma: %d, ilość: %d, średnia: %.2f\n"
,
suma
,
ile
,
srednia
);
long
sredniaWCentach
=
Math
.
round
(
srednia
*
100
);
System
.
out
.
println
(
"średnia to "
+
(
sredniaWCentach
/
100
)
+
" dolarów i "
+
(
sredniaWCentach
%
100
)
+
" centów"
);
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/nieobiektowo/zadania.txt
0 → 100644
View file @
8b7188e6
P3_Srednia - oblicz średnią wszystkich pracowników razem wziętych
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/Employee.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.time.LocalDate
;
import
java.util.Objects
;
// To jest przykład "normalnej klasy" przygotowanej po to, aby tworzyć obiekty tej klasy.
// Na takie klasy mówi się "klasa modelu".
public
class
Employee
{
private
int
employeeId
;
private
String
firstName
;
private
String
lastName
;
private
String
jobTitle
;
private
int
salary
;
private
LocalDate
hireDate
;
private
String
departmentName
;
private
String
streetAddress
;
private
String
postalCode
;
private
String
city
;
private
String
country
;
public
Employee
(
int
employeeId
,
String
firstName
,
String
lastName
,
String
jobTitle
,
int
salary
,
LocalDate
hireDate
,
String
departmentName
,
String
streetAddress
,
String
postalCode
,
String
city
,
String
country
)
{
this
.
employeeId
=
employeeId
;
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
getEmployeeId
()
{
return
employeeId
;
}
public
void
setEmployeeId
(
int
employeeId
)
{
this
.
employeeId
=
employeeId
;
}
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
int
getSalary
()
{
return
salary
;
}
public
void
setSalary
(
int
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 [employeeId="
+
employeeId
+
", 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
,
employeeId
,
firstName
,
hireDate
,
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
)
&&
employeeId
==
other
.
employeeId
&&
Objects
.
equals
(
firstName
,
other
.
firstName
)
&&
Objects
.
equals
(
hireDate
,
other
.
hireDate
)
&&
Objects
.
equals
(
jobTitle
,
other
.
jobTitle
)
&&
Objects
.
equals
(
lastName
,
other
.
lastName
)
&&
Objects
.
equals
(
postalCode
,
other
.
postalCode
)
&&
salary
==
other
.
salary
&&
Objects
.
equals
(
streetAddress
,
other
.
streetAddress
);
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/Grupowanie1.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
public
class
Grupowanie1
{
// Ta wersja jest poprawna pod względem wyników, natomiast nie jest optymalna pod wzgl wydajności.
// Dla każdego joba przeglądamy całą listę pracowników i ifem sprawdzamy czy pasuje.
// Dla 19 stanowisk i 107 pracowników koszt wynosi (19+1)*(107)
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
// etap 1 - zebranie stanowisk bez powtórzeń
Set
<
String
>
jobs
=
new
HashSet
<>();
for
(
Employee
emp
:
emps
)
{
jobs
.
add
(
emp
.
getJobTitle
());
}
//System.out.println(jobs);
// etap 2 - dla każdego stanowiska z tego zbioru obliczymy średnią
// przeglądając za każdym razem całą listę pracowników
for
(
String
job
:
jobs
)
{
int
ile
=
0
;
int
suma
=
0
;
for
(
Employee
emp
:
emps
)
{
if
(
emp
.
getJobTitle
().
equals
(
job
))
{
suma
+=
emp
.
getSalary
();
ile
++;
}
}
double
srednia
=
(
double
)
suma
/
ile
;
System
.
out
.
printf
(
"| %-32s | %2d | %8.2f |\n"
,
job
,
ile
,
srednia
);
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/Grupowanie2.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
Grupowanie2
{
// W tej wersji widzimy klasyczny schemat grupowania za pomocą słowników - algorytm, który warto znać.
// Używamy wyłącznie podstawowych metod get, put i containsKey.
// W kolejnych wersjach zobaczymy skrócone zapisy z wykorzystaniem nowych metod od Javy 8.
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
Map
<
String
,
Integer
>
sumy
=
new
HashMap
<>();
Map
<
String
,
Integer
>
ilosci
=
new
HashMap
<>();
for
(
Employee
emp
:
emps
)
{
if
(
sumy
.
containsKey
(
emp
.
getJobTitle
()))
{
// kolejny pracownik z tego stanowiska → do starej sumy trzeba dodać pensję tego pracownika
int
suma
=
sumy
.
get
(
emp
.
getJobTitle
());
sumy
.
put
(
emp
.
getJobTitle
(),
suma
+
emp
.
getSalary
());
int
ilosc
=
ilosci
.
get
(
emp
.
getJobTitle
());
ilosci
.
put
(
emp
.
getJobTitle
(),
ilosc
+
1
);
}
else
{
// pierwszy pracownik z tego stanowiska → wpisujemy jego pensję jako pierwszą wartość
sumy
.
put
(
emp
.
getJobTitle
(),
emp
.
getSalary
());
ilosci
.
put
(
emp
.
getJobTitle
(),
1
);
}
}
// System.out.println(sumy);
for
(
String
job
:
sumy
.
keySet
())
{
int
suma
=
sumy
.
get
(
job
);
int
ile
=
ilosci
.
get
(
job
);
double
srednia
=
(
double
)
suma
/
ile
;
System
.
out
.
printf
(
"| %-32s | %2d | %8.2f |\n"
,
job
,
ile
,
srednia
);
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/Grupowanie2a.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
Grupowanie2a
{
// W tej wersji widzimy klasyczny schemat grupowania za pomocą słowników - algorytm, który warto znać.
// Używamy wyłącznie podstawowych metod get, put i containsKey.
// W kolejnych wersjach zobaczymy skrócone zapisy z wykorzystaniem nowych metod od Javy 8.
public
static
void
main
(
String
[]
args
)
{
final
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
final
Map
<
String
,
Integer
>
sumy
=
new
HashMap
<>();
final
Map
<
String
,
Integer
>
ilosci
=
new
HashMap
<>();
for
(
Employee
emp
:
emps
)
{
final
String
jobTitle
=
emp
.
getJobTitle
();
if
(
sumy
.
containsKey
(
jobTitle
))
{
sumy
.
put
(
jobTitle
,
sumy
.
get
(
jobTitle
)
+
emp
.
getSalary
());
ilosci
.
put
(
jobTitle
,
ilosci
.
get
(
jobTitle
)
+
1
);
}
else
{
sumy
.
put
(
jobTitle
,
emp
.
getSalary
());
ilosci
.
put
(
jobTitle
,
1
);
}
}
for
(
String
job
:
sumy
.
keySet
())
{
int
suma
=
sumy
.
get
(
job
);
int
ile
=
ilosci
.
get
(
job
);
double
srednia
=
(
double
)
suma
/
ile
;
System
.
out
.
printf
(
"| %-32s | %2d | %8.2f |\n"
,
job
,
ile
,
srednia
);
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/Grupowanie3.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
Grupowanie3
{
// Tutaj używamy getOrDefault i nie musimy pisać ifa
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
Map
<
String
,
Integer
>
sumy
=
new
HashMap
<>();
Map
<
String
,
Integer
>
ilosci
=
new
HashMap
<>();
for
(
Employee
emp
:
emps
)
{
int
suma
=
sumy
.
getOrDefault
(
emp
.
getJobTitle
(),
0
);
sumy
.
put
(
emp
.
getJobTitle
(),
suma
+
emp
.
getSalary
());
int
ilosc
=
ilosci
.
getOrDefault
(
emp
.
getJobTitle
(),
0
);
ilosci
.
put
(
emp
.
getJobTitle
(),
ilosc
+
1
);
}
for
(
String
job
:
sumy
.
keySet
())
{
int
suma
=
sumy
.
get
(
job
);
int
ile
=
ilosci
.
get
(
job
);
double
srednia
=
(
double
)
suma
/
ile
;
System
.
out
.
printf
(
"| %-32s | %2d | %8.2f |\n"
,
job
,
ile
,
srednia
);
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/Grupowanie4.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
Grupowanie4
{
// Tutaj używamy operacji z zaszytym ifem i nie musimy pisać ifa
// Do operacji typu compute przekauzje się funkcję, która ma za zadanie zaktualizować wartość w słowniku.
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
Map
<
String
,
Integer
>
sumy
=
new
HashMap
<>();
Map
<
String
,
Integer
>
ilosci
=
new
HashMap
<>();
for
(
Employee
emp
:
emps
)
{
sumy
.
putIfAbsent
(
emp
.
getJobTitle
(),
0
);
sumy
.
computeIfPresent
(
emp
.
getJobTitle
(),
(
k
,
v
)
->
v
+
emp
.
getSalary
());
ilosci
.
putIfAbsent
(
emp
.
getJobTitle
(),
0
);
ilosci
.
computeIfPresent
(
emp
.
getJobTitle
(),
(
k
,
v
)
->
v
+
1
);
}
for
(
String
job
:
sumy
.
keySet
())
{
int
suma
=
sumy
.
get
(
job
);
int
ile
=
ilosci
.
get
(
job
);
double
srednia
=
(
double
)
suma
/
ile
;
System
.
out
.
printf
(
"| %-32s | %2d | %8.2f |\n"
,
job
,
ile
,
srednia
);
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/Grupowanie5.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
Grupowanie5
{
// działanie merge na przykładzie sumy:
// zaglądamy pod klucz emp.getJobTitle():
// - jeśli tam jeszcze nic nie ma → wstawiana jest wartość getSalary()
// - jeśli już coś było, to do starej wartości dodawane jest getSalary
// za pomocą funkcji podanej w trzecim argumencie.
// Tutaj jest to funkcja sum (dodawanie), a więc liczby matematycznie sumowane.
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
Map
<
String
,
Integer
>
sumy
=
new
HashMap
<>();
Map
<
String
,
Integer
>
ilosci
=
new
HashMap
<>();
for
(
Employee
emp
:
emps
)
{
sumy
.
merge
(
emp
.
getJobTitle
(),
emp
.
getSalary
(),
Integer:
:
sum
);
ilosci
.
merge
(
emp
.
getJobTitle
(),
1
,
Integer:
:
sum
);
}
for
(
String
job
:
sumy
.
keySet
())
{
int
suma
=
sumy
.
get
(
job
);
int
ile
=
ilosci
.
get
(
job
);
double
srednia
=
(
double
)
suma
/
ile
;
System
.
out
.
printf
(
"| %-32s | %2d | %8.2f |\n"
,
job
,
ile
,
srednia
);
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/MergeDodatkowe.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map.Entry
;
public
class
MergeDodatkowe
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
Map
<
String
,
String
>
mapa
=
new
HashMap
<>();
// przykład pokazujący, że merge działa też dla innych typów niż liczbowe
// uwaga: łączenie tekstów w ten sposób (za pomocą concat lub + w pętli) jest mało wydajny - to nie jest wzór do naśladowania
// prawidłowo byłoby uzyć StringBuildera w pętli lub Collectors.join
for
(
Employee
emp
:
emps
)
{
mapa
.
merge
(
emp
.
getJobTitle
(),
emp
.
getLastName
(),
String:
:
concat
);
}
for
(
Entry
<
String
,
String
>
entry
:
mapa
.
entrySet
())
{
System
.
out
.
println
(
entry
.
getKey
()
+
": "
+
entry
.
getValue
());
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/ObslugaCSV.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.PrintWriter
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Scanner
;
// Klasa narzędziowa ("utility class") zawierająca operacje wczytywania i zapisywania danych pracowników z/do pliku CSV.
public
class
ObslugaCSV
{
private
static
final
String
NAGLOWEK
=
"employee_id;first_name;last_name;job_title;salary;hire_date;department_name;address;postal_code;city;country"
;
private
static
final
String
SEP
=
";"
;
// Aby uniemożliwić innym programistom tworzenie obiektów tej klasy (bo wszystko jest tu statyczne i tworzenie obiektów nie ma sensu)
// można umieścić tu prywatny konstruktor domyślny - wtedy nikt go nie wywoła.
private
ObslugaCSV
()
{
}
public
static
List
<
Employee
>
wczytaj
(
File
plik
)
{
List
<
Employee
>
emps
=
new
ArrayList
<>();
try
(
Scanner
scanner
=
new
Scanner
(
plik
))
{
scanner
.
nextLine
();
while
(
scanner
.
hasNextLine
())
{
String
linia
=
scanner
.
nextLine
();
String
[]
t
=
linia
.
split
(
SEP
,
11
);
Employee
emp
=
new
Employee
(
Integer
.
parseInt
(
t
[
0
]),
t
[
1
],
t
[
2
],
t
[
3
],
Integer
.
parseInt
(
t
[
4
]),
LocalDate
.
parse
(
t
[
5
]),
t
[
6
],
t
[
7
],
t
[
8
],
t
[
9
],
t
[
10
]);
emps
.
add
(
emp
);
}
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
return
emps
;
}
public
static
List
<
Employee
>
wczytaj
(
String
sciezka
)
{
return
wczytaj
(
new
File
(
sciezka
));
}
public
static
void
zapisz
(
List
<
Employee
>
lista
,
File
plik
)
{
try
(
PrintWriter
out
=
new
PrintWriter
(
plik
))
{
out
.
println
(
NAGLOWEK
);
for
(
Employee
emp
:
lista
)
{
out
.
print
(
emp
.
getEmployeeId
());
out
.
print
(
SEP
);
out
.
print
(
emp
.
getFirstName
());
out
.
print
(
SEP
);
out
.
print
(
emp
.
getLastName
());
out
.
print
(
SEP
);
out
.
print
(
emp
.
getJobTitle
());
out
.
print
(
SEP
);
out
.
print
(
emp
.
getSalary
());
out
.
print
(
SEP
);
out
.
print
(
emp
.
getHireDate
());
out
.
print
(
SEP
);
out
.
print
(
emp
.
getDepartmentName
());
out
.
print
(
SEP
);
out
.
print
(
emp
.
getStreetAddress
());
out
.
print
(
SEP
);
out
.
print
(
emp
.
getPostalCode
());
out
.
print
(
SEP
);
out
.
print
(
emp
.
getCity
());
out
.
print
(
SEP
);
out
.
print
(
emp
.
getCountry
());
out
.
println
();
}
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
}
public
static
void
zapisz
(
List
<
Employee
>
lista
,
String
sciezka
)
{
zapisz
(
lista
,
new
File
(
sciezka
));
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P0_WczytajDane.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
public
class
P0_WczytajDane
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
System
.
out
.
println
(
"Wczytano "
+
emps
.
size
()
+
" rekordów:"
);
for
(
Employee
emp
:
emps
)
{
System
.
out
.
println
(
emp
);
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P1_WypiszWybranePola.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
public
class
P1_WypiszWybranePola
{
public
static
void
main
(
String
[]
args
)
{
// Dla każdego pracownika z pliku wypisz jego imię, nazwisko, stanowisko i pensję.
// Pracownik Steven King (President) zarabia 24000.
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
for
(
Employee
emp
:
emps
)
{
// System.out.println( emp.getFirstName() + " " + emp.getLastName() + " " + emp.getSalary());
System
.
out
.
printf
(
"Pracownik nr %d: %s %s (%s) zarabia %d\n"
,
emp
.
getEmployeeId
(),
emp
.
getFirstName
(),
emp
.
getLastName
(),
emp
.
getJobTitle
(),
emp
.
getSalary
());
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P2_WypiszBogatych.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
public
class
P2_WypiszBogatych
{
public
static
void
main
(
String
[]
args
)
{
// Wypisz tylko tych pracowników, którzy zarabiają co najmniej 10 tys
// i policz ilu jest jest
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
int
ilu
=
0
;
for
(
Employee
emp
:
emps
)
{
if
(
emp
.
getSalary
()
>=
10_000
)
{
System
.
out
.
printf
(
"%s %s (%s) - %d\n"
,
emp
.
getFirstName
(),
emp
.
getLastName
(),
emp
.
getJobTitle
(),
emp
.
getSalary
());
ilu
++;
}
}
System
.
out
.
printf
(
"\nZnaleziono %d pracowników o pensji >= 10 tys.\n"
,
ilu
);
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P3_SredniaPensja.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
import
java.util.Locale
;
public
class
P3_SredniaPensja
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
double
suma
=
0
;
for
(
Employee
emp
:
emps
)
{
suma
+=
emp
.
getSalary
();
}
double
srednia
=
suma
/
emps
.
size
();
System
.
out
.
println
(
"Średnia pensja wszystkich: "
+
srednia
);
System
.
out
.
printf
(
"Średnia pensja wszystkich: %.2f\n"
,
srednia
);
System
.
out
.
printf
(
Locale
.
US
,
"Średnia pensja wszystkich: %.2f\n"
,
srednia
);
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P4_SredniaProgramistow.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
public
class
P4_SredniaProgramistow
{
// Program oblicza średnią pensję osób na stanowisku Programmer
// (jobTitle)
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
double
suma
=
0
;
int
ile
=
0
;
for
(
Employee
emp
:
emps
)
{
if
(
emp
.
getJobTitle
().
equals
(
"Programmer"
))
{
suma
+=
emp
.
getSalary
();
ile
++;
}
}
double
srednia
=
suma
/
ile
;
System
.
out
.
println
(
"Średnia pensja programistów "
+
srednia
);
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P4a_SredniaWybranych_Interaktywnie.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.io.File
;
import
java.util.List
;
import
javax.swing.JFileChooser
;
import
javax.swing.JOptionPane
;
import
javax.swing.filechooser.FileNameExtensionFilter
;
public
class
P4a_SredniaWybranych_Interaktywnie
{
// W tej wersji uzytkownik będzie mógł wybrać plik z dysku i wskazać stanowisko, dla którego ma być wylicozna średnia.
public
static
void
main
(
String
[]
args
)
{
JFileChooser
chooser
=
new
JFileChooser
(
"."
);
chooser
.
setDialogTitle
(
"Wybierz plik z danymi pracowników"
);
chooser
.
setFileFilter
(
new
FileNameExtensionFilter
(
"Plii CSV"
,
"csv"
,
"txt"
));
int
coSieStalo
=
chooser
.
showOpenDialog
(
null
);
if
(
coSieStalo
!=
JFileChooser
.
APPROVE_OPTION
)
{
return
;
}
File
wybranyPlik
=
chooser
.
getSelectedFile
();
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
wybranyPlik
);
String
wybranyJob
=
JOptionPane
.
showInputDialog
(
"Podaj nazwę stanowiska:"
);
double
suma
=
0
;
int
ile
=
0
;
for
(
Employee
emp
:
emps
)
{
if
(
emp
.
getJobTitle
().
equalsIgnoreCase
(
wybranyJob
))
{
suma
+=
emp
.
getSalary
();
ile
++;
}
}
double
srednia
=
suma
/
ile
;
JOptionPane
.
showMessageDialog
(
null
,
"Średnia pensja na stanowisku "
+
wybranyJob
+
" wynosi "
+
srednia
);
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P4b_SredniaWybranych_Interaktywnie.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.io.File
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.TreeSet
;
import
javax.swing.JFileChooser
;
import
javax.swing.JOptionPane
;
import
javax.swing.filechooser.FileNameExtensionFilter
;
public
class
P4b_SredniaWybranych_Interaktywnie
{
// W tej wersji użytkownik będzie mógł wybrać plik z dysku i wskazać stanowisko, dla którego ma być wylicozna średnia.
public
static
void
main
(
String
[]
args
)
{
JFileChooser
chooser
=
new
JFileChooser
(
"."
);
chooser
.
setDialogTitle
(
"Wybierz plik z danymi pracowników"
);
chooser
.
setFileFilter
(
new
FileNameExtensionFilter
(
"Pliki CSV"
,
"csv"
,
"txt"
));
int
coSieStalo
=
chooser
.
showOpenDialog
(
null
);
if
(
coSieStalo
!=
JFileChooser
.
APPROVE_OPTION
)
{
return
;
}
File
wybranyPlik
=
chooser
.
getSelectedFile
();
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
wybranyPlik
);
Set
<
String
>
jobs
=
new
TreeSet
<>();
for
(
Employee
emp
:
emps
)
{
jobs
.
add
(
emp
.
getJobTitle
());
}
String
wybranyJob
=
(
String
)
JOptionPane
.
showInputDialog
(
null
,
"Czyją średnią chcesz obliczyć?"
,
"Wybierz stanowsiko"
,
JOptionPane
.
QUESTION_MESSAGE
,
null
,
jobs
.
toArray
(),
null
);
double
suma
=
0
;
int
ile
=
0
;
for
(
Employee
emp
:
emps
)
{
if
(
emp
.
getJobTitle
().
equalsIgnoreCase
(
wybranyJob
))
{
suma
+=
emp
.
getSalary
();
ile
++;
}
}
double
srednia
=
suma
/
ile
;
JOptionPane
.
showMessageDialog
(
null
,
"Średnia pensja na stanowisku "
+
wybranyJob
+
" wynosi "
+
srednia
);
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P5_MinMax_v1.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
public
class
P5_MinMax_v1
{
// Program znajduje pracownika, który zarabia najwięcej, i pracownika, który zarabia najmniej,
// i wypisuje ich dane (co najmniej: imię, nazwisko, pensję).
// W tej wersji najpierw ustalę jaka jest minimalna i maksymalna pensja, a dopiero potem kto tyle zarabia
// Zaleta: gdyby kilka osób miało jednakową pensję (np. nie tylko King zarabiał 24 tys),
// to wszystkie takie osoby się wypiszą.
// Wada: dane z lisy przeglądamy kilka razy.
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
int
minSalary
=
Integer
.
MAX_VALUE
;
int
maxSalary
=
Integer
.
MIN_VALUE
;
for
(
Employee
emp
:
emps
)
{
if
(
emp
.
getSalary
()
>
maxSalary
)
{
maxSalary
=
emp
.
getSalary
();
}
if
(
emp
.
getSalary
()
<
minSalary
)
{
minSalary
=
emp
.
getSalary
();
}
}
System
.
out
.
println
(
"Maksymalna pensja wynosi "
+
maxSalary
);
System
.
out
.
println
(
"Kto tyle zarabia?"
);
for
(
Employee
emp
:
emps
)
{
if
(
emp
.
getSalary
()
==
maxSalary
)
{
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
());
}
}
System
.
out
.
println
();
System
.
out
.
println
(
"Minimalna pensja wynosi "
+
minSalary
);
System
.
out
.
println
(
"Kto tyle zarabia?"
);
for
(
Employee
emp
:
emps
)
{
if
(
emp
.
getSalary
()
==
minSalary
)
{
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
());
}
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P5_MinMax_v2.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
public
class
P5_MinMax_v2
{
// Program znajduje pracownika, który zarabia najwięcej, i pracownika, który zarabia najmniej,
// i wypisuje ich dane (co najmniej: imię, nazwisko, pensję).
// W tej wersji od razu zapamiętujemy kto to jest.
// Program jest krótszy, a dane przeglądamy tylko raz.
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
int
minSalary
=
Integer
.
MAX_VALUE
;
int
maxSalary
=
Integer
.
MIN_VALUE
;
String
ktoBogaty
=
""
;
String
ktoBiedny
=
""
;
for
(
Employee
emp
:
emps
)
{
if
(
emp
.
getSalary
()
>
maxSalary
)
{
maxSalary
=
emp
.
getSalary
();
ktoBogaty
=
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
();
}
if
(
emp
.
getSalary
()
<
minSalary
)
{
minSalary
=
emp
.
getSalary
();
ktoBiedny
=
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
();
}
}
System
.
out
.
println
(
"Najbogatszy: "
+
ktoBogaty
+
", zarabia "
+
maxSalary
);
System
.
out
.
println
(
"Najbiedniejszy: "
+
ktoBiedny
+
", zarabia "
+
minSalary
);
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P5_MinMax_v3.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
public
class
P5_MinMax_v3
{
// Program znajduje pracownika, który zarabia najwięcej, i pracownika, który zarabia najmniej,
// i wypisuje ich dane (co najmniej: imię, nazwisko, pensję).
// W tej wersji od razu zapamiętujemy kto to jest.
// Program jest krótszy, a dane przeglądamy tylko raz.
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
Employee
bogaty
=
null
;
Employee
biedny
=
null
;
for
(
Employee
emp
:
emps
)
{
if
(
bogaty
==
null
||
emp
.
getSalary
()
>
bogaty
.
getSalary
())
{
bogaty
=
emp
;
}
if
(
biedny
==
null
||
emp
.
getSalary
()
<
biedny
.
getSalary
())
{
biedny
=
emp
;
}
}
System
.
out
.
println
(
"Najbogatszy: "
+
bogaty
.
getFirstName
()
+
" "
+
bogaty
.
getLastName
()
+
", zarabia "
+
bogaty
.
getSalary
());
System
.
out
.
println
(
"Najbiedniejszy: "
+
biedny
.
getFirstName
()
+
" "
+
biedny
.
getLastName
()
+
", zarabia "
+
biedny
.
getSalary
());
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P5_MinMax_v4.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
public
class
P5_MinMax_v4
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
if
(
emps
.
isEmpty
())
{
System
.
out
.
println
(
"Brak danych"
);
return
;
}
Employee
bogaty
=
emps
.
get
(
0
);
Employee
biedny
=
emps
.
get
(
0
);
for
(
Employee
emp
:
emps
)
{
if
(
emp
.
getSalary
()
>
bogaty
.
getSalary
())
{
bogaty
=
emp
;
}
if
(
emp
.
getSalary
()
<
biedny
.
getSalary
())
{
biedny
=
emp
;
}
}
System
.
out
.
println
(
"Najbogatszy: "
+
bogaty
.
getFirstName
()
+
" "
+
bogaty
.
getLastName
()
+
", zarabia "
+
bogaty
.
getSalary
());
System
.
out
.
println
(
"Najbiedniejszy: "
+
biedny
.
getFirstName
()
+
" "
+
biedny
.
getLastName
()
+
", zarabia "
+
biedny
.
getSalary
());
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P6_WypiszUnikalneMiasta.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.TreeSet
;
public
class
P6_WypiszUnikalneMiasta
{
//TODO Wypisz nazwy miast, w których pracują pracownicy, bez powtórzeń.
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
Set
<
String
>
miasta
=
new
TreeSet
<>();
for
(
Employee
emp
:
emps
)
{
miasta
.
add
(
emp
.
getCity
());
}
// System.out.println(miasta);
for
(
String
miasto
:
miasta
)
{
System
.
out
.
println
(
"* "
+
miasto
);
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P6a_WypiszUnikalneMiasta_Lista.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
P6a_WypiszUnikalneMiasta_Lista
{
// UWAGA! Ta wersja jest mniej wydajna od wersji z Set.
// do listy dodajemy miasto, jeśli go nie było wcześniej.
// Ale sprawdzenie czy element należy do listy wymaga (wewnętrznie) przejrzenia całej listy w pętli.
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
List
<
String
>
miasta
=
new
ArrayList
<>();
for
(
Employee
emp
:
emps
)
{
if
(!
miasta
.
contains
(
emp
.
getCity
()))
{
miasta
.
add
(
emp
.
getCity
());
}
}
// System.out.println(miasta);
for
(
String
miasto
:
miasta
)
{
System
.
out
.
println
(
"* "
+
miasto
);
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P7a_Sortowanie.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
// Program wypsiuje pracowników w kolejności posortowanej zwn pensje.
public
class
P7a_Sortowanie
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
// Listę można posortować, ale:
// 1) albo elementy tej listy muszą implementować interfejs Comparable, czyli "same potrafią się porównać"
// (tak jest dla String, Integer, BigDecimal, LocalDate)
// 2) albo musimy przekazać parametr Comparator, który określa kolejność elementów
// Tutaj sposób 2)
// W tej wersji Comparator zdefiniujemy za pomocą wyrażenia lambda.
// Tutaj używam zwykłego odejmowania int-ów, co jest poprawne gdy oba inty są dodatnie.
emps
.
sort
((
emp1
,
emp2
)
->
emp1
.
getSalary
()
-
emp2
.
getSalary
());
for
(
Employee
emp
:
emps
)
{
System
.
out
.
println
(
emp
);
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P7b_Sortowanie.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.List
;
// Program wypsiuje pracowników w kolejności posortowanej zwn pensje.
public
class
P7b_Sortowanie
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
// W tej wersji Comparator zdefiniujemy za pomocą wyrażenia lambda.
// Zalecane jest porównywanie liczb za pomocą gotowych metod compare,
// gdyż zapobiega to problemom z overflowami.
emps
.
sort
((
emp1
,
emp2
)
->
Integer
.
compare
(
emp1
.
getSalary
(),
emp2
.
getSalary
()));
for
(
Employee
emp
:
emps
)
{
System
.
out
.
println
(
emp
);
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/P7c_Sortowanie.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.util.Comparator
;
import
java.util.List
;
// Program wypsiuje pracowników w kolejności posortowanej zwn pensje.
public
class
P7c_Sortowanie
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytaj
(
"emps.csv"
);
// W tej wersji comparator tworzymy za pomocą metody statycznej comparingInt,
// jako parametr podaje się "wskazanie metody" (method reference), za pomocą której jest odczytywana pensja pracownika.
// Nieformalnie:
// sortujemy listę stosując sposób porównywania operaty o porównywanie intów,
// a te inty są pobierane z obiektów Employee za pomocą metody getSalary
// emps.sort(Comparator.comparingInt(Employee::getSalary));
emps
.
sort
(
Comparator
.
comparingInt
(
Employee:
:
getSalary
).
reversed
());
for
(
Employee
emp
:
emps
)
{
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
()
+
", zarobki: "
+
emp
.
getSalary
());
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/emps/obiektowo/PodwyzkaOkno.java
0 → 100644
View file @
8b7188e6
package
emps
.
obiektowo
;
import
java.awt.EventQueue
;
import
java.awt.Font
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
java.io.File
;
import
java.util.IntSummaryStatistics
;
import
java.util.List
;
import
java.util.Locale
;
import
javax.swing.DefaultComboBoxModel
;
import
javax.swing.JButton
;
import
javax.swing.JComboBox
;
import
javax.swing.JFileChooser
;
import
javax.swing.JFrame
;
import
javax.swing.JLabel
;
import
javax.swing.JOptionPane
;
import
javax.swing.JSpinner
;
import
javax.swing.SpringLayout
;
import
javax.swing.filechooser.FileNameExtensionFilter
;
import
javax.swing.SpinnerNumberModel
;
public
class
PodwyzkaOkno
{
private
JFrame
frame
;
private
List
<
Employee
>
emps
=
List
.
of
();
private
JLabel
lblLiczba
;
private
JLabel
lblSrednia
;
private
JLabel
lblLiczbaJob
;
private
JLabel
lblSredniaJob
;
private
JComboBox
<
String
>
comboBox
;
private
JSpinner
spinner
;
/**
* Launch the application.
*/
public
static
void
main
(
String
[]
args
)
{
EventQueue
.
invokeLater
(
new
Runnable
()
{
public
void
run
()
{
try
{
PodwyzkaOkno
window
=
new
PodwyzkaOkno
();
window
.
frame
.
setVisible
(
true
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
});
}
/**
* Create the application.
*/
public
PodwyzkaOkno
()
{
initialize
();
}
/**
* Initialize the contents of the frame.
*/
private
void
initialize
()
{
frame
=
new
JFrame
();
frame
.
setBounds
(
100
,
100
,
783
,
640
);
frame
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
SpringLayout
springLayout
=
new
SpringLayout
();
frame
.
getContentPane
().
setLayout
(
springLayout
);
JButton
btnWczytajPlik
=
new
JButton
(
"Wczytaj plik"
);
btnWczytajPlik
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
e
)
{
wczytajDane
();
}
});
btnWczytajPlik
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
btnWczytajPlik
,
10
,
SpringLayout
.
NORTH
,
frame
.
getContentPane
());
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
btnWczytajPlik
,
10
,
SpringLayout
.
WEST
,
frame
.
getContentPane
());
springLayout
.
putConstraint
(
SpringLayout
.
SOUTH
,
btnWczytajPlik
,
103
,
SpringLayout
.
NORTH
,
frame
.
getContentPane
());
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
btnWczytajPlik
,
378
,
SpringLayout
.
WEST
,
frame
.
getContentPane
());
frame
.
getContentPane
().
add
(
btnWczytajPlik
);
JButton
btnZapiszPlik
=
new
JButton
(
"Zapisz plik"
);
btnZapiszPlik
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
e
)
{
zapiszDane
();
}
});
btnZapiszPlik
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
btnZapiszPlik
,
10
,
SpringLayout
.
NORTH
,
frame
.
getContentPane
());
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
btnZapiszPlik
,
-
363
,
SpringLayout
.
EAST
,
frame
.
getContentPane
());
springLayout
.
putConstraint
(
SpringLayout
.
SOUTH
,
btnZapiszPlik
,
0
,
SpringLayout
.
SOUTH
,
btnWczytajPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
btnZapiszPlik
,
-
10
,
SpringLayout
.
EAST
,
frame
.
getContentPane
());
frame
.
getContentPane
().
add
(
btnZapiszPlik
);
JLabel
lblLiczbaPracownikw
=
new
JLabel
(
"Liczba pracowników:"
);
lblLiczbaPracownikw
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
lblLiczbaPracownikw
,
38
,
SpringLayout
.
SOUTH
,
btnWczytajPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
lblLiczbaPracownikw
,
0
,
SpringLayout
.
WEST
,
btnWczytajPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
SOUTH
,
lblLiczbaPracownikw
,
81
,
SpringLayout
.
SOUTH
,
btnWczytajPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
lblLiczbaPracownikw
,
0
,
SpringLayout
.
EAST
,
btnWczytajPlik
);
frame
.
getContentPane
().
add
(
lblLiczbaPracownikw
);
lblLiczba
=
new
JLabel
(
"liczba"
);
lblLiczba
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
lblLiczba
,
38
,
SpringLayout
.
SOUTH
,
btnZapiszPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
lblLiczba
,
0
,
SpringLayout
.
WEST
,
btnZapiszPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
SOUTH
,
lblLiczba
,
0
,
SpringLayout
.
SOUTH
,
lblLiczbaPracownikw
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
lblLiczba
,
0
,
SpringLayout
.
EAST
,
btnZapiszPlik
);
frame
.
getContentPane
().
add
(
lblLiczba
);
JLabel
lblredniaPensja
=
new
JLabel
(
"Średnia pensja:"
);
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
lblredniaPensja
,
21
,
SpringLayout
.
SOUTH
,
lblLiczbaPracownikw
);
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
lblredniaPensja
,
0
,
SpringLayout
.
WEST
,
btnWczytajPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
lblredniaPensja
,
0
,
SpringLayout
.
EAST
,
btnWczytajPlik
);
lblredniaPensja
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
frame
.
getContentPane
().
add
(
lblredniaPensja
);
lblSrednia
=
new
JLabel
(
"srednia"
);
lblSrednia
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
lblSrednia
,
21
,
SpringLayout
.
SOUTH
,
lblLiczba
);
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
lblSrednia
,
0
,
SpringLayout
.
WEST
,
btnZapiszPlik
);
frame
.
getContentPane
().
add
(
lblSrednia
);
JLabel
lblWybierzStanowisko
=
new
JLabel
(
"Wybierz stanowisko:"
);
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
lblWybierzStanowisko
,
33
,
SpringLayout
.
SOUTH
,
lblredniaPensja
);
lblWybierzStanowisko
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
lblWybierzStanowisko
,
0
,
SpringLayout
.
WEST
,
btnWczytajPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
lblWybierzStanowisko
,
0
,
SpringLayout
.
EAST
,
btnWczytajPlik
);
frame
.
getContentPane
().
add
(
lblWybierzStanowisko
);
comboBox
=
new
JComboBox
<>();
comboBox
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
e
)
{
zaktualizujDaneStanowiska
();
}
});
springLayout
.
putConstraint
(
SpringLayout
.
SOUTH
,
lblWybierzStanowisko
,
-
26
,
SpringLayout
.
NORTH
,
comboBox
);
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
comboBox
,
308
,
SpringLayout
.
NORTH
,
frame
.
getContentPane
());
comboBox
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
comboBox
,
0
,
SpringLayout
.
WEST
,
btnWczytajPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
comboBox
,
0
,
SpringLayout
.
EAST
,
btnWczytajPlik
);
frame
.
getContentPane
().
add
(
comboBox
);
JLabel
lblLiczbaTakichPracownikw
=
new
JLabel
(
"Liczba takich pracowników:"
);
lblLiczbaTakichPracownikw
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
lblLiczbaTakichPracownikw
,
36
,
SpringLayout
.
SOUTH
,
comboBox
);
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
lblLiczbaTakichPracownikw
,
0
,
SpringLayout
.
WEST
,
btnWczytajPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
lblLiczbaTakichPracownikw
,
0
,
SpringLayout
.
EAST
,
btnWczytajPlik
);
frame
.
getContentPane
().
add
(
lblLiczbaTakichPracownikw
);
lblLiczbaJob
=
new
JLabel
(
"liczba"
);
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
lblLiczbaJob
,
9
,
SpringLayout
.
NORTH
,
lblLiczbaTakichPracownikw
);
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
lblLiczbaJob
,
0
,
SpringLayout
.
WEST
,
btnZapiszPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
lblLiczbaJob
,
-
10
,
SpringLayout
.
EAST
,
frame
.
getContentPane
());
lblLiczbaJob
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
frame
.
getContentPane
().
add
(
lblLiczbaJob
);
JLabel
lblredniaPensja_1
=
new
JLabel
(
"Średnia pensja:"
);
lblredniaPensja_1
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
lblredniaPensja_1
,
22
,
SpringLayout
.
SOUTH
,
lblLiczbaTakichPracownikw
);
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
lblredniaPensja_1
,
10
,
SpringLayout
.
WEST
,
frame
.
getContentPane
());
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
lblredniaPensja_1
,
0
,
SpringLayout
.
EAST
,
btnWczytajPlik
);
frame
.
getContentPane
().
add
(
lblredniaPensja_1
);
lblSredniaJob
=
new
JLabel
(
"srednia"
);
lblSredniaJob
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
lblSredniaJob
,
0
,
SpringLayout
.
NORTH
,
lblredniaPensja_1
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
lblSredniaJob
,
0
,
SpringLayout
.
EAST
,
lblSrednia
);
frame
.
getContentPane
().
add
(
lblSredniaJob
);
JLabel
lblKwotaPodwyki
=
new
JLabel
(
"Kwota podwyżki"
);
lblKwotaPodwyki
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
lblKwotaPodwyki
,
26
,
SpringLayout
.
SOUTH
,
lblredniaPensja_1
);
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
lblKwotaPodwyki
,
10
,
SpringLayout
.
WEST
,
frame
.
getContentPane
());
springLayout
.
putConstraint
(
SpringLayout
.
SOUTH
,
lblKwotaPodwyki
,
41
,
SpringLayout
.
SOUTH
,
lblredniaPensja_1
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
lblKwotaPodwyki
,
204
,
SpringLayout
.
WEST
,
frame
.
getContentPane
());
frame
.
getContentPane
().
add
(
lblKwotaPodwyki
);
spinner
=
new
JSpinner
();
spinner
.
setModel
(
new
SpinnerNumberModel
(
Integer
.
valueOf
(
0
),
null
,
null
,
Integer
.
valueOf
(
1
)));
spinner
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
spinner
,
21
,
SpringLayout
.
SOUTH
,
lblSredniaJob
);
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
spinner
,
0
,
SpringLayout
.
WEST
,
btnZapiszPlik
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
spinner
,
0
,
SpringLayout
.
EAST
,
btnZapiszPlik
);
frame
.
getContentPane
().
add
(
spinner
);
JButton
btnZastosujPodwyk
=
new
JButton
(
"Zastosuj podwyżkę"
);
btnZastosujPodwyk
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
e
)
{
zastosujPodwyzke
();
}
});
btnZastosujPodwyk
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
springLayout
.
putConstraint
(
SpringLayout
.
NORTH
,
btnZastosujPodwyk
,
19
,
SpringLayout
.
SOUTH
,
lblKwotaPodwyki
);
springLayout
.
putConstraint
(
SpringLayout
.
WEST
,
btnZastosujPodwyk
,
10
,
SpringLayout
.
WEST
,
frame
.
getContentPane
());
springLayout
.
putConstraint
(
SpringLayout
.
SOUTH
,
btnZastosujPodwyk
,
72
,
SpringLayout
.
SOUTH
,
lblKwotaPodwyki
);
springLayout
.
putConstraint
(
SpringLayout
.
EAST
,
btnZastosujPodwyk
,
0
,
SpringLayout
.
EAST
,
btnWczytajPlik
);
frame
.
getContentPane
().
add
(
btnZastosujPodwyk
);
}
private
void
wczytajDane
()
{
JFileChooser
chooser
=
new
JFileChooser
(
"."
);
chooser
.
setDialogTitle
(
"Wybierz plik"
);
chooser
.
setFileFilter
(
new
FileNameExtensionFilter
(
"Pliki CSV"
,
"csv"
,
"txt"
));
int
coSieStalo
=
chooser
.
showOpenDialog
(
null
);
if
(
coSieStalo
!=
JFileChooser
.
APPROVE_OPTION
)
{
return
;
}
File
wybranyPlik
=
chooser
.
getSelectedFile
();
emps
=
ObslugaCSV
.
wczytaj
(
wybranyPlik
);
zaktualizujDaneGlobalne
();
zaktualizujListeJobow
();
}
private
void
zapiszDane
()
{
JFileChooser
chooser
=
new
JFileChooser
(
"."
);
chooser
.
setDialogTitle
(
"Wybierz plik"
);
chooser
.
setFileFilter
(
new
FileNameExtensionFilter
(
"Pliki CSV"
,
"csv"
,
"txt"
));
int
coSieStalo
=
chooser
.
showSaveDialog
(
null
);
if
(
coSieStalo
!=
JFileChooser
.
APPROVE_OPTION
)
{
return
;
}
File
wybranyPlik
=
chooser
.
getSelectedFile
();
if
(
wybranyPlik
.
exists
())
{
int
wybor
=
JOptionPane
.
showConfirmDialog
(
frame
,
"Plik istnieje.\nCzy chcesz go nadpisać?"
,
"Uwaga"
,
JOptionPane
.
YES_NO_OPTION
,
JOptionPane
.
WARNING_MESSAGE
);
if
(
wybor
==
JOptionPane
.
NO_OPTION
)
{
return
;
// nie zapisujemy
}
}
ObslugaCSV
.
zapisz
(
emps
,
wybranyPlik
);
}
private
void
zaktualizujDaneGlobalne
()
{
// Do pola z liczbą pracowników wpiszemy rozmiar listy:
lblLiczba
.
setText
(
String
.
valueOf
(
emps
.
size
()));
double
srednia
=
emps
.
stream
().
mapToInt
(
Employee:
:
getSalary
).
average
().
orElse
(
0
);
lblSrednia
.
setText
(
String
.
format
(
Locale
.
US
,
"%.2f"
,
srednia
));
}
private
void
zaktualizujListeJobow
()
{
String
[]
jobs
=
emps
.
stream
().
map
(
Employee:
:
getJobTitle
).
distinct
().
sorted
().
toArray
(
String
[]::
new
);
comboBox
.
setModel
(
new
DefaultComboBoxModel
<>(
jobs
));
}
private
void
zaktualizujDaneStanowiska
()
{
String
job
=
(
String
)
comboBox
.
getSelectedItem
();
IntSummaryStatistics
stats
=
emps
.
stream
()
.
filter
(
emp
->
emp
.
getJobTitle
().
equals
(
job
))
.
mapToInt
(
Employee:
:
getSalary
)
.
summaryStatistics
();
lblLiczbaJob
.
setText
(
String
.
valueOf
(
stats
.
getCount
()));
lblSredniaJob
.
setText
(
String
.
format
(
Locale
.
US
,
"%.2f"
,
stats
.
getAverage
()));
}
protected
void
zastosujPodwyzke
()
{
String
job
=
(
String
)
comboBox
.
getSelectedItem
();
int
zmiana
=
(
Integer
)
spinner
.
getValue
();
for
(
Employee
emp
:
emps
)
{
if
(
emp
.
getJobTitle
().
equals
(
job
))
{
emp
.
setSalary
(
emp
.
getSalary
()
+
zmiana
);
}
}
zaktualizujDaneStanowiska
();
zaktualizujDaneGlobalne
();
}
}
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