Commit cd360f46 by Patryk Czarnik

Konto - początek

parent 82b01991
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;
}
}
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");
}
}
......@@ -25,6 +25,23 @@ public class Przyklad {
student.dodajOcene(2);
student.dodajOcene(5);
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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment