Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jvstd1
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
jvstd1
Commits
cd360f46
Commit
cd360f46
authored
Sep 30, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Konto - początek
parent
82b01991
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
132 additions
and
0 deletions
+132
-0
Konto.java
src/main/java/p06_enkapsulacja/Konto.java
+50
-0
ProgramBankowy.java
src/main/java/p06_enkapsulacja/ProgramBankowy.java
+65
-0
Przyklad.java
src/main/java/p06_enkapsulacja/Przyklad.java
+17
-0
No files found.
src/main/java/p06_enkapsulacja/Konto.java
0 → 100644
View file @
cd360f46
package
p06_enkapsulacja
;
public
class
Konto
{
private
final
int
numer
;
private
Osoba
wlasciciel
;
private
int
saldo
;
public
Konto
(
int
numer
,
Osoba
wlasciciel
,
int
saldo
)
{
this
.
numer
=
numer
;
this
.
wlasciciel
=
wlasciciel
;
this
.
saldo
=
saldo
;
}
@Override
public
String
toString
()
{
return
"Konto nr "
+
numer
+
", wł: "
+
wlasciciel
+
", saldo: "
+
saldo
;
}
// Nie tworzymy setterów do pól:
// - numer - ponieważ numer nigdy się nie zmienia
// - saldo - ponieważ zmiany salda muszą być wykonane za pomocą "operacji biznesowych" wplata / wyplata / przelew
public
int
getNumer
()
{
return
numer
;
}
public
Osoba
getWlasciciel
()
{
return
wlasciciel
;
}
public
void
setWlasciciel
(
Osoba
wlasciciel
)
{
this
.
wlasciciel
=
wlasciciel
;
}
public
int
getSaldo
()
{
return
saldo
;
}
public
void
wplata
(
int
kwota
)
{
saldo
+=
kwota
;
}
public
void
wyplata
(
int
kwota
)
{
saldo
-=
kwota
;
}
public
void
przelew
(
Konto
cel
,
int
kwota
)
{
this
.
saldo
-=
kwota
;
cel
.
saldo
+=
kwota
;
}
}
src/main/java/p06_enkapsulacja/ProgramBankowy.java
0 → 100644
View file @
cd360f46
package
p06_enkapsulacja
;
import
java.util.Scanner
;
public
class
ProgramBankowy
{
public
static
void
main
(
String
[]
args
)
{
@SuppressWarnings
(
"resource"
)
Scanner
sc
=
new
Scanner
(
System
.
in
);
Osoba
ala
=
new
Osoba
(
"Ala"
,
"Kowalska"
,
"1985-05-05"
);
System
.
out
.
print
(
"Podaj początkową kwotę: "
);
int
kwota
=
sc
.
nextInt
();
sc
.
nextLine
();
// wymuszenie przejścia do nowej linii
Konto
konto
=
new
Konto
(
1
,
ala
,
kwota
);
System
.
out
.
println
(
konto
);
petla:
while
(
true
)
{
try
{
System
.
out
.
println
(
"Co chcesz zrobić? W - wpłata, Y - wypłata, K - koniec"
);
String
wybor
=
sc
.
nextLine
().
toUpperCase
();
switch
(
wybor
)
{
case
"K"
,
"Q"
->
{
break
petla
;
// przejście do Koniec programu
}
case
"W"
->
{
System
.
out
.
print
(
"Podaj kwotę wpłaty: "
);
kwota
=
sc
.
nextInt
();
sc
.
nextLine
();
konto
.
wplata
(
kwota
);
System
.
out
.
println
(
"Pieniądze zostały wpłacone"
);
}
case
"Y"
->
{
System
.
out
.
print
(
"Podaj kwotę wypłaty: "
);
kwota
=
sc
.
nextInt
();
sc
.
nextLine
();
konto
.
wyplata
(
kwota
);
// ta linia wykona się tylko jeśli nie było wyjątku:
System
.
out
.
println
(
"Pieniądze zostały wypłacone"
);
}
default
->
{
System
.
out
.
println
(
"Nieznane polecenie"
);
continue
petla
;
// Przejście do Co chcesz zrobić
}
}
// Jeśli o różnych sytuacjach błędnych będą informować wyjątki różnych klas,
// to w programie możemy te sytuacje rozróżnić i obsłużyć w róznych catchach
}
catch
(
IllegalArgumentException
e
)
{
System
.
out
.
println
(
"Niepoprawny argument: "
+
e
.
getMessage
());
// } catch(BrakSrodkow e) {
// System.out.println(e.getMessage());
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Inny błąd: "
+
e
);
}
System
.
out
.
println
();
System
.
out
.
println
(
konto
);
System
.
out
.
println
();
}
System
.
out
.
println
(
"Koniec programu"
);
}
}
src/main/java/p06_enkapsulacja/Przyklad.java
View file @
cd360f46
...
@@ -25,6 +25,23 @@ public class Przyklad {
...
@@ -25,6 +25,23 @@ public class Przyklad {
student
.
dodajOcene
(
2
);
student
.
dodajOcene
(
2
);
student
.
dodajOcene
(
5
);
student
.
dodajOcene
(
5
);
System
.
out
.
println
(
student
.
getImie
()
+
" ma średnią ocen "
+
student
.
getSredniaOcen
());
System
.
out
.
println
(
student
.
getImie
()
+
" ma średnią ocen "
+
student
.
getSredniaOcen
());
System
.
out
.
println
();
Konto
a
=
new
Konto
(
1
,
ala
,
5000
);
Konto
b
=
new
Konto
(
2
,
ola
,
5000
);
System
.
out
.
println
(
a
);
System
.
out
.
println
(
b
);
a
.
wplata
(
500
);
b
.
wyplata
(
300
);
System
.
out
.
println
(
a
);
System
.
out
.
println
(
b
);
a
.
przelew
(
b
,
1000
);
System
.
out
.
println
(
a
);
System
.
out
.
println
(
b
);
System
.
out
.
println
();
a
.
wplata
(-
400
);
System
.
out
.
println
(
a
);
}
}
}
}
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