Cannot find symbol mit Konstruktoren

arhzz

Bekanntes Mitglied
Hallo!

In dieser Klasse Banking :

Java:
import java.lang.IllegalArgumentException;
import io.In;
import io.Out;

class Banking {
  static final int CAPACITY = 100;

  final Account[] accounts = new Account[CAPACITY];
  int nOfAccounts = 0;
 
  int createAccount(String firstName, String lastName, String phone, double overdraft) throws BankServiceException {
    if (nOfAccounts == CAPACITY) {
        throw new BankServiceException("The maximum capacity has been reached");
    }
    Customer owner = new Customer(firstName, lastName, phone);
    Account account = new Account(nOfAccounts, owner, overdraft);
    accounts[nOfAccounts] = account;
    nOfAccounts++;

    return account.accountNo;
  }

  private Account getAccount(int accountNo) {
  if (accountNo >= accounts.length || accounts[accountNo] == null) {
   throw new NoSuchAccountException(accountNo);
  }
  return  accounts[nOfAccounts];
}


  boolean deposit(int accountNo, double amount) throws IllegalArgumentException  {
    Account account = getAccount(accountNo);
      if(amount < 0.0 ) {
        throw new IllegalArgumentException("Cannot deposit negative amounts");
      }else {
    return account.deposit(amount);
      }
  }
  public boolean withdraw(int accountNo, double amount) throws OverdraftLimitReachedException {
    if (amount > 0.0) {
        throw new OverdraftLimitReachedException(accountNo,amount,overdraft);
    }
    Account account = accounts[accountNo];
    return account.withdraw(amount);
    
  }
  public boolean transfer(int fromNo, int toNo, double amount) {
    if (fromNo < 0 || toNo < 0 ||
        fromNo >= nOfAccounts || toNo >= nOfAccounts) return false;

    Account from = accounts[fromNo];
    Account to = accounts[toNo];
    return from.transfer(to, amount);
  }

  public double getBalance(int accountNo) {
    if (accountNo < 0 || accountNo >= nOfAccounts) return 0;

    Account account = accounts[accountNo];
    return account.balance;
  }

  double getBalance() {
    double sum = 0;

    for (int i = 0; i < nOfAccounts; i++) {
      sum += accounts[i].balance;
    }
    return sum;
  }

  void print() {
    Out.println("---------- Bankauszug ----------");

    for (int i = 0; i < nOfAccounts; i++) {
      accounts[i].print();
      Out.println("--------------------------------");
    }
    Out.format("Bilanzsumme: %.2f%n", getBalance());
    Out.println("--------------------------------");
  }

  // --------------------- Optionaler Teil ---------------------

  public static void main(String[] args) {
    Banking banking = new Banking();
    char op;

    do {
      printMenu();
      op = readOperation();

      switch (op) {
        case 'a': {
          printTitle("Konto anlegen");
          String firstName = getString("Vorname");
          String lastName = getString("Nachname");
          String phone = getString("Telefonnummer");
          double overdraft = getDouble("Überziehungsrahmen");

          int accountNo = banking.createAccount(
            firstName, lastName, phone, overdraft);
          printMessage("Anlegen von Konto " + accountNo, accountNo != -1);
          break;
        }
        case 'e': {
          printTitle("Einzahlen");
          int accountNo = getInt("Kontonummer");
          double amount = getDouble("Einzahlungsbetrag");

          boolean success = banking.deposit(accountNo, amount);
          printMessage("Einzahlen", success);
          break;
        }
        case 'b': {
          printTitle("Abheben");
          int accountNo = getInt("Kontonummer");
          double amount = getDouble("Abhebungsbetrag");

          boolean success = banking.withdraw(accountNo, amount);
          printMessage("Abheben", success);
          break;
        }
        case 't': {
          printTitle("Überweisen");
          int fromNo = getInt("Von Kontonummer");
          int toNo = getInt("Auf Kontonummer");
          double amount = getDouble("Betrag");

          boolean success = banking.transfer(fromNo, toNo, amount);
          printMessage("Überweisen", success);
          break;
        }
        case 'd':
          banking.print();
          break;
        case 'q':
          Out.println("Beenden");
          break;
        default:
          Out.println("Ungültige Operation");
          break;
      }
    } while(op != 'q');
  }

  static void printMenu() {
    Out.println();
    Out.println("*********** Bankverwaltung ********");
    Out.println("Konto anlegen ................... a");
    Out.println("Einzahlen ....................... e");
    Out.println("Beheben ......................... b");
    Out.println("Überweisen ...................... t");
    Out.println("Übersicht drucken ............... d");
    Out.println("Beenden ......................... q");
    Out.print("Welche Menuoption? [a|e|b|t|d|q]: ");
  }

  static char readOperation() {
    char ch = Character.toLowerCase(In.readChar());
    In.readLine();
    return ch;
  }

  static void printTitle(String text) {
    Out.println("*** " + text + " ***");
  }

  static String getString(String text) {
    Out.print(text + ": ");
    return In.readLine();
  }

  static double getDouble(String text) {
    Out.print(text + ": ");
    return In.readDouble();
  }

  static int getInt(String text) {
    Out.print(text + ": ");
    return In.readInt();
  }

  static void printMessage(String operation, boolean success) {
    String message = success ?
      operation + " erfolgreich durchgeführt" :
      "Ungültige Operation";
    Out.println(message);
  }
}

Bekomme ich ein "cannot find symbol" in der withdraw methode. Was mache ich falsch?
 

arhzz

Bekanntes Mitglied
Was ist overdraft? Die variable musst du schon definieren

Die Variable habe ich in diese Klasse definiert

Java:
Account(int accountNo, Customer owner, double overdraft) {
    this.accountNo = accountNo;
    this.owner = owner;
    this.overdraft = overdraft;
  }

  void print() {
    Out.println("Kontonummer: " + accountNo);
    owner.print();
    Out.format("Kontostand: %.2f%nÜberziehungsrahmen: %.2f%n",
      balance, overdraft);
  }

  boolean deposit(double amount) {
    if (amount <= 0) return false;

    balance += amount;
    return true;
  }

  boolean withdraw(double amount) {
    if (amount <= 0 || !isCovered(amount)) return false;

    balance -= amount;
    return true;
  }

  boolean isCovered(double amount) {
    return amount - overdraft <= balance;
  }

  boolean transfer(Account target, double amount) {
    boolean success = withdraw(amount);
    if (!success) return false;

    target.deposit(amount);
    return true;
  }
}
 

LimDul

Top Contributor
Dann musst da auch drauf zugreifen - du hast die in der Klasse Account definiert - aber du bist in der Klasse Banking. Du musst dann schon auf die Variable overdraft vom entsprechenden Account zugreifen. Woher soll java wissen, welchen Wert die Variable overdraft in der Klasse banking hat.
 

arhzz

Bekanntes Mitglied
Dann musst da auch drauf zugreifen - du hast die in der Klasse Account definiert - aber du bist in der Klasse Banking. Du musst dann schon auf die Variable overdraft vom entsprechenden Account zugreifen. Woher soll java wissen, welchen Wert die Variable overdraft in der Klasse banking hat.
Ok ich muss auf dass Feld zugreifen, wie genau mache ich dass?
 

athkougr

Mitglied
Du kannst die Signatur der withdraw Methode ändern um ein Account-Objekt als Parameter zu haben. So kannst du auf die overdraft und accountNo Variablen zugreifen.

Also,
Java:
//public boolean withdraw(int accountNo, double amount) throws OverdraftLimitReachedException {
public boolean withdraw(final Account account, double amount) throws OverdraftLimitReachedException {

Dann noch get-Methoden für die Variablen der Account-Klasse definieren um auf die Variablen zuzugreifen.
 

mrBrown

Super-Moderator
Mitarbeiter
Du kannst die Signatur der withdraw Methode ändern um ein Account-Objekt als Parameter zu haben. So kannst du auf die overdraft und accountNo Variablen zugreifen.

Also,
Java:
//public boolean withdraw(int accountNo, double amount) throws OverdraftLimitReachedException {
public boolean withdraw(final Account account, double amount) throws OverdraftLimitReachedException {
Dann funktioniert die Main-Methode nicht mehr, die Signatur kann ruhig so bleiben. Es reicht in der Methode was zu ändern. Extra Getter braucht man dafür auch nicht :)
 

mrBrown

Super-Moderator
Mitarbeiter
Du brauchst auf die Variable overdraft gar nicht zugreifen, guck dir die Klasse Account mal genauer an...
 

athkougr

Mitglied
Dann funktioniert die Main-Methode nicht mehr, die Signatur kann ruhig so bleiben. Es reicht in der Methode was zu ändern. Extra Getter braucht man dafür auch nicht :)
Ich habe grade gesehen dass die Banking-Klasse auch eine private getAccount-Methode hat. Durch diese Methode kann er auch auf overdraft zugreifen wenn er sie in der throw-Klause benutzen will.
 

mrBrown

Super-Moderator
Mitarbeiter
Ich habe grade gesehen dass die Banking-Klasse auch eine private getAccount-Methode hat. Durch diese Methode kann er auch auf overdraft zugreifen wenn er sie in der throw-Klause benutzen will.
Er muss überhaupt nicht auf overdraft zugreifen, das ist reines Account-interna, was von außen nicht sichtbar sein muss.
 

temi

Top Contributor
Zunächst mal: Bist du sicher, dass die withdraw() Methode tut was sie soll?

Du wirft eine Exception, sobald jemand einen Betrag größer als Null abheben möchte. Solltest du nicht eher schauen, ob das Konto gedeckt ist?
 

arhzz

Bekanntes Mitglied
Zunächst mal: Bist du sicher, dass die withdraw() Methode tut was sie soll?

Du wirft eine Exception, sobald jemand einen Betrag größer als Null abheben möchte. Solltest du nicht eher schauen, ob das Konto gedeckt ist?
Ja eigentlich soll ich eine Exception werfen sobald jemand einen Betrag größer als Overdraft abheben möchte.Aber dass ich die variable nicht implementieren kann wird das nicht gehen.Und dein Punkt steht,aber ich denke dass ich zuerst diesen Problem lösen soll
 

temi

Top Contributor
Ja eigentlich soll ich eine Exception werfen sobald jemand einen Betrag größer als Overdraft abheben möchte.Aber dass ich die variable nicht implementieren kann wird das nicht gehen.Und dein Punkt steht,aber ich denke dass ich zuerst diesen Problem lösen soll
Account hat doch eine passende Methode, um herauszufinden ob das Konto gedeckt ist, also sollte das ja kein Problem sein...
 

temi

Top Contributor
Würde ich auf ein Konto die Methode isCovered() aufrufen, dann würde ich die Information über die Kontodeckung erwarten, aber nicht, dass eine Exception ausgelöst wird.

Allerdings gibt es bei Konto auch eine Methode, um Geld abzuheben, von dieser würde ich das schon erwarten.
 

temi

Top Contributor
Fragt sich nur, wozu die Methode withdraw() überhaupt einen Rückgabewert hat? Kann ja eigentlich nie false werden.
 

arhzz

Bekanntes Mitglied
Würde ich auf ein Konto die Methode isCovered() aufrufen, dann würde ich die Information über die Kontodeckung erwarten, aber nicht, dass eine Exception ausgelöst wird.

Allerdings gibt es bei Konto auch eine Methode, um Geld abzuheben, von dieser würde ich das schon erwarten.
Ja,dass denke ich auch,aber man soll die Exception in der Banking Klasse werfen nicht in der Account.
 

arhzz

Bekanntes Mitglied
Dann mach das so.
Ja ich versuche,aber die overdraft variable kann nicht gefunden werden.Die exception nimmt sie als argument,sie befindet sich nicht in der klass Banking und Java kann sie nicht erkennen.Egal wo ich die Exception in klasse Banking werfe wird es nicht klappen,ich muss einen weg finden diese Exception zu implementieren,wo sie geworfen werden soll,dass kann ich auch später ausprobieren und sehen was funktioniert was macht sinn und was nicht,aber im momement kann ich die Exception überhaupt nicht werfen.
 

tommysenf

Top Contributor
Java:
public boolean withdraw(int accountNo, double amount) throws OverdraftLimitReachedException {
    Account account = accounts[accountNo];
    if (amount > 0.0) {
        throw new OverdraftLimitReachedException(accountNo,amount,account.overdraft);
    }
    return account.withdraw(amount);
}
Wobei ich nicht denke das die Methode so implementiert werden soll...
 

arhzz

Bekanntes Mitglied
Java:
public boolean withdraw(int accountNo, double amount) throws OverdraftLimitReachedException {
    Account account = accounts[accountNo];
    if (amount > 0.0) {
        throw new OverdraftLimitReachedException(accountNo,amount,account.overdraft);
    }
    return account.withdraw(amount);
}
Wobei ich nicht denke das die Methode so implementiert werden soll...
Warum denkst du dass? Danke!
 

temi

Top Contributor
Ja ich habe überprüft wo die methode geworfen werden soll, und es steht "wenn beim Abheben eines Betrags der Überziehungsrahmen erreicht wird" dass sollte die withdraw methode sein.
Es gibt die withdraw() Methode ja zweimal, einmal in Banking und einmal in Account.

Ich würde die Exception ja an der Stelle auslösen, an der die "Arbeit" getan werden soll.

Warum denkst du dass?

Eine Instanzvariable, wie "overdraft", sollte normalerweise und auch sinnvollerweise als privat deklariert werden. Damit wäre ein Zugriff account.overdraft aber nicht möglich und die Exception ist womöglich an der falschen Stelle.

Ist die Signatur der OverdraftLimitReachedException vorgegeben?

Außerdem passt die Methode immer noch nicht, was den Vergleich angeht...
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
W Cannot find Symbol Java Basics - Anfänger-Themen 5
A Cannot find symbol bei exceptions Java Basics - Anfänger-Themen 2
L cannot find symbol variable Kon Java Basics - Anfänger-Themen 8
F Erste Schritte error: cannot find symbol Java Basics - Anfänger-Themen 5
R return: cannot find symbol Java Basics - Anfänger-Themen 2
L Bluej Error: Cannot find Symbol Java Basics - Anfänger-Themen 13
S Cannot find symbol (symbol ist eine Variable) Java Basics - Anfänger-Themen 13
N Cannot find symbol Java Basics - Anfänger-Themen 18
P Cannot find symbol, wieso? Java Basics - Anfänger-Themen 5
M Erste Schritte cannot find symbol - Probleme mit Klassen Java Basics - Anfänger-Themen 6
J Error: cannot find symbol - variable Java Basics - Anfänger-Themen 3
F Erste Schritte parseint: cannot find symbol Java Basics - Anfänger-Themen 6
M Vererbung - Cannot Find Symbol constructor... Java Basics - Anfänger-Themen 11
D error: cannot find symbol Java Basics - Anfänger-Themen 3
B Frage zu Beispielprogramm: "error: cannot find symbol" Java Basics - Anfänger-Themen 2
BlueMountain Erste Schritte error: cannot find symbol Java Basics - Anfänger-Themen 2
L Error: Cannot find symbol Java Basics - Anfänger-Themen 1
P Cannot find Symbol Java Basics - Anfänger-Themen 3
L Cannot Find Symbol - Was soll denn das bedeuten?!? Java Basics - Anfänger-Themen 7
P StdIn.readDouble: cannot find symbol Java Basics - Anfänger-Themen 7
B Fehler "Cannot find symbol - variable number1" Java Basics - Anfänger-Themen 13
B Compiler-Fehler cannot find symbol Java Basics - Anfänger-Themen 6
K Cannot find symbol Java Basics - Anfänger-Themen 3
H cannot find symbol Java Basics - Anfänger-Themen 4
S cannot find symbol, symbol: constructor Java Basics - Anfänger-Themen 2
3 Compiler-Fehler Fehlerbehebung cannot find Symbol Java Basics - Anfänger-Themen 4
R Compiler-Fehler Cannot find symbol (Method printIn) Java Basics - Anfänger-Themen 3
B Polymorphie A obj = new B; "cannot find symbol app()" Java Basics - Anfänger-Themen 5
S wieso Fehlermeldung cannot find symbol hier Java Basics - Anfänger-Themen 10
T Cannot find Symbol(String) Java Basics - Anfänger-Themen 9
2 Compiler-Fehler cannot find symbol Java Basics - Anfänger-Themen 13
B Erste Schritte cannot find symbol - problem Java Basics - Anfänger-Themen 9
D Cannot find symbol variable Java Basics - Anfänger-Themen 9
A Compiler-Fehler Cannot find Symbol Java Basics - Anfänger-Themen 6
V Packages: Cannot find symbol Java Basics - Anfänger-Themen 12
J Cannot find Symbol Variable mit JPanels Java Basics - Anfänger-Themen 2
L Fehlermeldung: RealMirror.java cannot find symbol Java Basics - Anfänger-Themen 2
D Cannot Find Symbol Java Basics - Anfänger-Themen 6
M Compile Time Error - cannot find symbol (Objekt!) Java Basics - Anfänger-Themen 2
L Problem mit Vererbung (extends) cannot find symbol Java Basics - Anfänger-Themen 3
S Problem beim Kompilieren - cannot find symbol - constructor() Java Basics - Anfänger-Themen 12
N cannot find symbol URL Java Basics - Anfänger-Themen 2
S Problem mit Javaeditor: Cannot find Symbol Java Basics - Anfänger-Themen 13
P cannot find Symbol - WTF? Java Basics - Anfänger-Themen 4
StupidAttack cannot find symbol Java Basics - Anfänger-Themen 16
B Cannot find symbol-Fehler Java Basics - Anfänger-Themen 3
Luk10 Cannot find Symbol Daten Java Basics - Anfänger-Themen 10
E cannot find symbol??? Java Basics - Anfänger-Themen 8
B cannot find symbol Java Basics - Anfänger-Themen 11
N cannot find symbol Java Basics - Anfänger-Themen 11
T OOP cannot find symbol Java Basics - Anfänger-Themen 4
A cannot find symbol - symbol : method Java Basics - Anfänger-Themen 5
T cannot find symbol Java Basics - Anfänger-Themen 5
I Cannot find Symbol & NullPointerException Java Basics - Anfänger-Themen 8
A cannot find symbol. Java Basics - Anfänger-Themen 5
G cannot find symbol! Java Basics - Anfänger-Themen 22
K Ständige Fehlermeldung "Cannot find symbol" Java Basics - Anfänger-Themen 2
L cannot find symbol-method Java Basics - Anfänger-Themen 3
P Error: Cannot find Symbol Java Basics - Anfänger-Themen 4
J JOptionDialog "cannot find symbol" Java Basics - Anfänger-Themen 3
P JLayer 1.0 - cannot find symbol Java Basics - Anfänger-Themen 4
M cannot find symbol. Java Basics - Anfänger-Themen 7
T ChangeListener cannot find Symbol? Java Basics - Anfänger-Themen 2
D "identifier expected" und "cannot find symbol Java Basics - Anfänger-Themen 4
B cannot find symbol method equalsIgnoreCase? Java Basics - Anfänger-Themen 23
S Fehlermeldung cannot find symbol Java Basics - Anfänger-Themen 8
G Cannot find symbol, Suchfunktion benutzt Java Basics - Anfänger-Themen 3
A cannot find symbol :-( Java Basics - Anfänger-Themen 2
D Cannot find JUnit.framework Java Basics - Anfänger-Themen 1
G Collections.binarySearch(LinkedList): cannot find method Java Basics - Anfänger-Themen 6
R Cannot find a free socket for the debugger Java Basics - Anfänger-Themen 6
W Cannot find symbole variable Java Basics - Anfänger-Themen 4
M NullPointerException: Cannot read the array length because "this.Kinder" is null Java Basics - Anfänger-Themen 1
W Cannot resolve symbol 'HttpServlet' Java Basics - Anfänger-Themen 2
I JSON - cannot deserialize from Object value Java Basics - Anfänger-Themen 16
J Scanner cannot be resolved to a type Java Basics - Anfänger-Themen 3
N Fehler "Cannot instantiate the type" Java Basics - Anfänger-Themen 3
jakobfritzz Array- cannot invoke "" because "" is null Java Basics - Anfänger-Themen 4
Flo :3 Variablen Type dismatch: cannot convert from string to int Java Basics - Anfänger-Themen 9
C system cannot be resolved Fehler in Eclipse Java Basics - Anfänger-Themen 18
V ClientProtocolException cannot be resolved Java Basics - Anfänger-Themen 6
J The import org.bukkit cannot be resolved Java Basics - Anfänger-Themen 3
J Fehlermeldung unklar. non-static variable player0 cannot be referenced from a static context Java Basics - Anfänger-Themen 4
P non-static variable cannot be referenced from a static context Java Basics - Anfänger-Themen 6
L constructor cannot be applied... Java Basics - Anfänger-Themen 22
M Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 10
P a cannot be resolved bei einer do while Schleife Java Basics - Anfänger-Themen 1
Aprendiendo Interpreter-Fehler "non-static variable this cannot be referenced from a static context" Java Basics - Anfänger-Themen 2
M Iterator cannot refer to a non final... Java Basics - Anfänger-Themen 20
T Error: int cannot be dereferenced Java Basics - Anfänger-Themen 10
J JLabel cannot be resolved Java Basics - Anfänger-Themen 8
H Variablen error: non-static variable cannot be referenced from a static context Java Basics - Anfänger-Themen 4
UnityFriday method getPrevious in class List<ContentType> cannot be applied to given types Java Basics - Anfänger-Themen 29
B OOP next cannot be resolved or is not a field Java Basics - Anfänger-Themen 6
B OOP Cannot instantiate the type AuDList<Integer> Java Basics - Anfänger-Themen 18
U Erste Schritte cannot be referenced from a static context Java Basics - Anfänger-Themen 1
D Java Eclipse cannot be cast to java.awt.event.ItemListener Java Basics - Anfänger-Themen 3
J Fehlermeldung : cannot invoke char(at) int on the primitive type int --- Anfänger Java Basics - Anfänger-Themen 5
M Erste Schritte [Variable] cannot be resolved to a variable Java Basics - Anfänger-Themen 4
M The Selection cannot be launched... Java Basics - Anfänger-Themen 4

Ähnliche Java Themen

Neue Themen


Oben