Also ich hatte mir einfach mal zur Übung selbst eine Aufgabe gestellt 🙂 Ich wollte einen Bankautomaten coden 🙂
Meine Frage wäre, findet jemand vielleicht noch eine Sicherheitslücke in meinem Code 🙂
Bank Klasse:
Konto Klasse:
Test Klasse:
Meine Frage wäre, findet jemand vielleicht noch eine Sicherheitslücke in meinem Code 🙂
Bank Klasse:
Java:
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Scanner;
public class Bank {
@SuppressWarnings("resource")
int BankID;
String Bankname;
ArrayList <Konto> Database = new ArrayList <Konto>();
Bank(String name){
this.BankID = 123456789;
this.Bankname = name;
}
void createAccount() {
Scanner sc = new Scanner(System.in);
Konto acc = new Konto();
System.out.println("Geben sie den Kontoinhaber ein: ");
acc.Kontoinhaber = sc.nextLine();
System.out.println("Geben sie ihre gewünschte Kontonummer ein: ");
acc.Kontonummer = Integer.parseInt(sc.nextLine());
acc.Kontostand = 0;
Database.add(acc);
System.out.println("Ihr Konto wurde erfolgreich erstellt.");
}
void closeAccount(int id) {
ListIterator<Konto> run = Database.listIterator();
Konto temp;
while(run.hasNext()) {
temp = run.next();
if(temp.Kontonummer == id) {
run.remove();
break;
}
}
System.out.println("Ihr Konto wurde erfolgreich aufgelöst.");
}
int checkBalance(int id) {
ListIterator<Konto> run = Database.listIterator();
Konto temp;
while(run.hasNext()) {
temp = run.next();
if(temp.Kontonummer == id) {
return temp.Kontostand;
}
}
return -1;
}
void withdraw(int id) {
Scanner sc = new Scanner(System.in);
System.out.println("Geben sie den Betrag ein, welchen sie abheben wollen: ");
int money = Integer.parseInt(sc.nextLine());
ListIterator<Konto> run = Database.listIterator();
Konto temp = new Konto();
while(run.hasNext()) {
temp = run.next();
if(temp.Kontonummer == id) {
temp.Kontostand = temp.Kontostand - money;
}else {
return;
}
}
System.out.println("Der Betrag wurde erfolgreich ausgezahlt. Ihr verbleibendes Guthaben beträgt " + temp.Kontostand + " $.");
}
void deposit(int id) {
Scanner sc = new Scanner(System.in);
System.out.println("Geben sie den Betrag ein, welchen sie einzahlen wollen: ");
int money = Integer.parseInt(sc.nextLine());
ListIterator<Konto> run = Database.listIterator();
Konto temp = new Konto();
while(run.hasNext()) {
temp = run.next();
if(temp.Kontonummer == id) {
temp.Kontostand = temp.Kontostand + money;
}
}
System.out.println("Der Betrag wurde erfolgreich eingezahlt. Ihr verbleibendes Guthaben beträgt " + temp.Kontostand + " $.");
}
}
Konto Klasse:
Java:
public class Konto {
public int Kontostand;
public int Kontonummer;
public String Kontoinhaber;
}
Test Klasse:
Java:
import java.util.Scanner;
public class Testing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Bank banking = new Bank("Postbank");
String check = "";
while(!(check.equals("Nein"))) {
System.out.println("Welche Aktion wollen sie ausführen? Geld einzahlen(1), Geld abheben(2), Kontostand abrufen(3), "
+ "Neues Konto eröffnen(4), Konto schließen(5)");
int mode = Integer.parseInt(sc.nextLine());
if(sc.hasNextInt()) {
switch(mode) {
case 1:
System.out.println("Geben sie ihre Kontonummer ein: ");
int num1 = Integer.parseInt(sc.nextLine());
if(sc.hasNextInt()) {
banking.deposit(num1);
}else {
System.out.println("Error! Das ist keine Zahl");
}
break;
case 2:
System.out.println("Geben sie ihre Kontonummer ein: ");
int num2 = Integer.parseInt(sc.nextLine());
if(sc.hasNextInt()) {
banking.withdraw(num2);
}else {
System.out.println("Error! Das ist keine Zahl");
}
break;
case 3:
System.out.println("Geben sie ihre Kontonummer ein: ");
int num3 = Integer.parseInt(sc.nextLine());
if(sc.hasNextInt()) {
System.out.println(banking.checkBalance(num3));
}else {
System.out.println("Error! Das ist keine Zahl");
}
break;
case 4:
banking.createAccount();
break;
case 5:
System.out.println("Geben sie ihre Kontonummer ein: ");
int num4 = Integer.parseInt(sc.nextLine());
if(sc.hasNextInt()) {
banking.closeAccount(num4);
}else {
System.out.println("Error! Das ist keine Zahl");
}
break;
}
System.out.println("Wollen sie eine weiter Aktion durchführen? Geben sie Ja oder Nein ein.");
check = sc.nextLine();
}else {
System.out.println("Error! Das ist keine Zahl");
}
}
}
}