Haushaufgabe Bankkonto Probleme mit Objekt löschen

Debty

Mitglied
Hi Leute ich bin neu hier und hab ein paar Probleme mit meiner Laboraufgabe für die FH.

Und zwar soll ich eine Klasse "Bankkonto" erstellen und dort Methoden benutzen und implementieren ich gebe euch kurz den Aufgabentext:
"Das Programm soll eine Klasse Bankonto haben mit den Attributen Kontonummer, Inhabername, PIN, Betrag und VerfallDatum.Das Datum soll Tag, Monat und Jahr als Attribute haben.

Es soll möglich sein: -Bankkonto anzulegen
- Bankkonto löschen
und die Kontonummer anzeigen zu lassen. Außerdem soll es möglich sein Geld auf sein Konto zu legen und abzuheben.
Die ersten 5 Einträge werden am Anfang aus einem File "input.txt" ausgelesen:

so nun mein Code für die Klasse Bankkonto(alles was auskommentiert ist, ist nur ein Vorläufer und war nur ein Gedanke also nicht beachten und meckern :)
Java:
package bankkonto;

import util.In;
import util.Out;

public class Bankkonto {
	int kontonummer;
	String inhabername;
	int pin;
	int betrag;
	Datum verfalldatum;
	
	/**
	 * Konstruktor zum anlegen eines neuen Kontos mit Parametern:
	 * Kontonummer,Inhaber,Pin, Betrag auf Kontaund das Verfalldatum
	 */
	public Bankkonto(int kontonummer, String inhabername, int pin, int betrag,
			Datum verfalldatum) {
		this.kontonummer = kontonummer;
		this.inhabername = inhabername;
		this.pin = pin;
		this.betrag = betrag;
		this.verfalldatum = verfalldatum;
	}
	/**
	 * Methode zum Einlesen der ersten 5(!) Konten aus einem File,das vorher erstellt wurde
	 */
	/**void ReadFile(){
		In.open("input.txt");
		this.kontonummer = In.readInt();
		this.inhabername = In.readWord();
		this.pin = In.readInt();
		this.betrag = In.readInt();	
	}*/
	/**
	 *Rückgabe der Kontonummer
	 */
	public void showkontonummer() {
		Out.print(kontonummer);
	}
	
	void deleteKonto(){
		//Out.print("Bitte Kontonummer des zu löschenden Kontos angeben: ");
		this.betrag = null;
		this.inhabername =null;				
	}
}
Wie man sieht funktionier (noch) gar nichts sitz aber auch erst ein paar Stunden dran hänge aber grad etwas...deswegen nicht meckern wegen Unvollständigkeit bitte :)

So nun zur Frage: Wie kann ich ein Objekt löschen? Muss ich mit einem Array arbeiten wie z.b. kontolist um löschen zu können? und wie lese ich die ersten 5 Files aus einer File ein wenn mein Attribut Verfalldatum den Datentyp Datum hat?

Sry hab erst seit einem halben Jahr Java und bin einfach noch nicht so fit bitte nicht böse sein.

PS: Meine Klasse Datum enthält nur Attribute und Konstruktor.

Mfg Debty
 

eRaaaa

Top Contributor
So nun zur Frage: Wie kann ich ein Objekt löschen? Muss ich mit einem Array arbeiten wie z.b. kontolist um löschen zu können?

So wird es wohl gemeint sein ja, oder eine Liste wie ArrayList oder so, aber falls ihr das noch ncith hattet ist bestimmt Array gemeint (jedenfalls war es bestimmt nicht so gemeint wie du in deleteKonto() implementiert hast :D)

und wie lese ich die ersten 5 Files aus einer File ein wenn mein Attribut Verfalldatum den Datentyp Datum hat?

Ziemlich schwer zu beantworten, wir alle kennen
a) den Inhalt
b) die komischen IO-Hilsklassen (In+Out) deiner FH

nicht
 

Debty

Mitglied
Alles klar dann probier ich das mit Arraylist :) schon mal gut zu hören dass ich es so machen muss
Java codes von in Out

IN:
Java:
public class In {

/** End of file indicator returned by read() or peek() when no more
characters can be read.
*/
public  static final char eof   = '\uffff';

private static final int empty = '\ufffe';

private static final char eofChar = '\u0005';  // ctrl E
private static InputStream in;
private static LinkedList inputStack, bufferStack;
private static boolean done; // true if recent operation was successful
private static char buf;     // last read character
private static char[] LS;    // line separator (eol)

private static char charAfterWhiteSpace() {
  char c;
  do c = read(); while (done && c <= ' ');
  return c;
}

private static String readDigits() {
  StringBuffer b = new StringBuffer();
  char c = charAfterWhiteSpace();
  if (done && c == '-') {
    b.append(c);
    c = read();
  }
  while (done && Character.isDigit(c)) {
    b.append(c);
    c = read();
  }
  buf = c;
  return b.toString();
}

private static String readFloatDigits() {
  StringBuffer b = new StringBuffer();
  char c = charAfterWhiteSpace();
  if (done && (c == '+' || c == '-')) {
    b.append(c);
    c = read();
  }
  while (done && Character.isDigit(c)) {
    b.append(c);
    c = read();
  }
  if (done && (c == '.')) {
    b.append(c);
    c = read();
    while (done && Character.isDigit(c)) {
      b.append(c);
      c = read();
    }
  }
  if (done && (c == 'e' || c == 'E')) {
    b.append(c);
    c = read();
    if (done && (c == '+' || c == '-')) {
      b.append(c);
      c = read();
    }
    while (done && Character.isDigit(c)) {
      b.append(c);
      c = read();
    }
  }
  buf = c;
  return b.toString();
}


/** Read a raw character (byte).
If an attempt is made to read beyond the end of the file,
eof is returned and done() yields false. Otherwise the read byte
is in the range 0..255.
*/
public static char read() {
  char c;
  if (buf != empty) {
    c = buf;
    if (buf != eof) buf = empty;
  } else {
    try {
      c = (char)in.read();
    } catch (IOException e) {
      done = false;
      c = eof; buf = eof;
    }
  }
  if (c == eofChar && inputStack.size() == 0) { c = eof; buf = eof; }
  done = c != eof;
  return c;
}

/** Current available raw characters.
In case of an error 0 is returned and done() yields false.
*/
public static int available() {
  int avail;

  try {
    avail = in.available();
  } catch(IOException exc) {
    avail = 0;
    done = false;
  }

  return avail;
}

/** Read a character, but skip white spaces (byte).
If an attempt is made to read beyond the end of the file,
eof is returned and done() yields false. Otherwise the read byte
is in the range 0..255.
*/
public static char readChar() {
  return charAfterWhiteSpace();
}

/** Read a boolean value.
This method skips white space and tries to read an identifier. If its value
is "true" the method returns true otherwise false. If the identifier is neither
"true" nor "false" done() yields false.
*/
public static boolean readBoolean() {
  String s = readIdentifier();
  done = true;
  if (s.equals("true")) return true;
  else { done = s.equals("false"); return false; }
}

/** Read an identifier.
This method skips white space and tries to read an identifier starting
with a letter and continuing with letters or digits. If a token of this
structure could be read, it is returned otherwise the empty string is
returned and done() yields false.
*/
public static String readIdentifier() {
  StringBuffer b = new StringBuffer();
  char c = charAfterWhiteSpace();
  if (done && Character.isLetter(c)) {
    b.append(c);
    c = read();
    while (done && (Character.isLetter(c) || Character.isDigit(c))) {
      b.append(c);
      c = read();
    }
  }
  buf = c;
  done = b.length() > 0;
  return b.toString();
}

/** Read a word.
This method skips white space and tries to read a word consisting of
all characters up to the next white space or to the end of the file.
If a token of this structure could be read, it is returned otherwise
an empty string is returned and done() yields false.
*/
public static String readWord() {
  StringBuffer b = new StringBuffer();
  char c = charAfterWhiteSpace();
  while (done && c > ' ') {
    b.append(c);
    c = read();
  }
  buf = c;
  done = b.length() > 0;
  return b.toString();
}

/** Read a line of text.
This method reads the rest of the current line (including eol) and
returns it (excluding eol). A line may be empty.
*/
public static String readLine() {
  StringBuffer b = new StringBuffer();
  char c = read();
  while (done && c != LS[0]) {
    b.append(c);
    c = read();
  }

  int i = 0;
  while (c == LS[i]) {
    ++i;
    if (i >= LS.length) { break; }
    c = read();
  }

  if (i < LS.length) {
    buf = c;
  } else {
    buf = empty;
  }
  if (b.length() > 0) done = true;
  return b.toString();
}

/** Read the whole file.
This method reads from the current position to the end of the
file and returns its text in a single large string. done() yields
always true.
*/
public static String readFile() {
  StringBuffer b = new StringBuffer();
  char c = charAfterWhiteSpace();
  while (done) {
    b.append(c);
    c = read();
  }
  buf = eof;
  done = true;
  return b.toString();
}

/** Read a quote-delimited string.
This method skips white space and tries to read a string in the form "...".
It can be used to read pieces of text that contain white space.
*/
public static String readString() {
  StringBuffer b = new StringBuffer();
  char c = charAfterWhiteSpace();
  if (done && c == '"') {
    c = read();
    while (done && c != '"') {
      b.append(c);
      c = read();
    }
    if (c == '"') { c = read(); done = true; } else done = false;
  } else done = false;
  buf = c;
  return b.toString();
}

/** Read an integer.
This method skips white space and tries to read an integer. If the
text does not contain an integer or if the number is too big, the
value 0 is returned and the subsequent call of done() yields false.
An integer is a sequence of digits, possibly preceded by '-'.
*/
public static int readInt() {
  String s = readDigits();
  try {
    done = true;
    return Integer.parseInt(s);
  } catch (Exception e) {
    done = false; return 0;
  }
}

/** Read a long integer.
This method skips white space and tries to read a long integer. If the
text does not contain a number or if the number is too big, the
value 0 is returned and the subsequent call of done() yields false.
A long integer is a sequence of digits, possibly preceded by '-'.
*/
public static long readLong() {
  String s = readDigits();
  try {
    done = true;
    return Long.parseLong(s);
  } catch (Exception e) {
    done = false; return 0;
  }
}

/** Read a float value.
This method skips white space and tries to read a float value. If the
text does not contain a float value or if the number is not well-formed,
the value 0f is returned and the subsequent call of done() yields false.
An float value is as specified in the Java language description. It may
be preceded by a '+' or a '-'.
*/
public static float readFloat() {
  String s = readFloatDigits();
  try {
    done = true;
    return Float.parseFloat(s);
  } catch (Exception e) {
    done = false; return 0f;
  }
}

/** Read a double value.
This method skips white space and tries to read a double value. If the
text does not contain a double value or if the number is not well-formed,
the value 0.0 is returned and the subsequent call of done() yields false.
An double value is as specified in the Java language description. It may
be preceded by a '+' or a '-'.
*/
public static double readDouble() {
  String s = readFloatDigits();
  try {
    done = true;
    return Double.parseDouble(s);
  } catch (Exception e) {
    done = false; return 0.0;
  }
}

/** Peek at the next character.
This method skips white space and returns the next character without removing
it from the input stream. It can be used to find out, what token comes next
in the input stream.
*/
public static char peek() {
  char c = charAfterWhiteSpace();
  buf = c;
  return c;
}

/** Open a text file for reading
The text file with the name fn is opened as the new current input
file. When it is closed again, the previous input file is restored.
*/
public static void open(String fn) {
  try {
    InputStream s = new FileInputStream(fn);
    bufferStack.add(new Character(buf));
    inputStack.add(in);
    in = s;
    done = true;
  } catch (FileNotFoundException e) {
    done = false;
  }
  buf = empty;
}

/** Close the current input file.
The current input file is closed and the previous input file is
restored. Closing the keyboard input has no effect but causes
done() to yield false.
*/
public static void close() {
  try {
    if (inputStack.size() > 0) {
      in.close();
      in = (InputStream) inputStack.removeLast();
      buf = ((Character) bufferStack.removeLast()).charValue();
      done = true;
    } else {
      done = false; buf = empty;
    }
  } catch (IOException e) {
    done = false; buf = empty;
  }
}

/** Check if the previous operation was successful.
This method returns true if the previous read operation was able
to read a token of the requested structure. It can also be called
after open() and close() to check if these operations were successful.
If done() is called before any other operation it yields true.
*/
public static boolean done() {
  return done;
}

static { // initializer
  done = true;
  in = System.in;
  buf = empty;
  inputStack = new LinkedList();
  bufferStack = new LinkedList();
  LS = System.getProperty("line.separator").toCharArray();
  if (LS == null || LS.length == 0) {
    LS = new char[] { '\n' };
  }
}

}

OUT:
Java:
public class Out {

private static PrintStream out;
private static PrintStream[] stack;
private static int sp;
private static boolean done;

/** Return true if the previous Out operation was
successful, otherwise return false. */
public static boolean done() {
	return done && ! out.checkError();
}

/** Print the boolean value b either as "true" or "false". */
public static void print(boolean b) { out.print(b); }

/** Print the character value c. */
public static void print(char s) { out.print(s); }

/** Print the integer value i. */
public static void print(int i) { out.print(i); }

/** Print the long value l. */
public static void print(long l) { out.print(l); }

/** Print the float value f. */
public static void print(float f) { out.print(f); }

/** Print the double value d. */
public static void print(double d) { out.print(d); }

/** Print the character array a. */
public static void print(char[] a) { out.print(a); }

/** Print the String s. */
public static void print(String s) { out.print(s); }

/** Print the Object o as resulting from String.valueOf(o). */
public static void print(Object o) { out.print(o); }

/** Terminate the current line by writing a line separator string.
On windows this is the character sequence '\r' and '\n' */
public static void println() { out.println(); }

/** Print the boolean value b and terminate the line. */
public static void println(boolean b) { out.println(b); }

/** Print the character value c and terminate the line. */
public static void println(char s) { out.println(s); }

/** Print the integer value i and terminate the line. */
public static void println(int i) { out.println(i); }

/** Print the long value l and terminate the line. */
public static void println(long l) { out.println(l); }

/** Print the float value f and terminate the line. */
public static void println(float f) { out.println(f); }

/** Print the double value d and terminate the line. */
public static void println(double d) { out.println(d); }

/** Print the character array a and terminate the line. */
public static void println(char[] a) { out.println(a); }

/** Print the String s and terminate the line. */
public static void println(String s) { out.println(s); }

/** Print the Object o as resulting from String.valueOf(o)
and terminate the line. */
public static void println(Object o) { out.println(o); }

/** Open the file with the name fn as the current output file.
All subsequent output goes to this file until it is closed.
The old output file will be restored when the new output file is closed. */
public static void open(String fn) {
	try {
		PrintStream s = new PrintStream(new FileOutputStream(fn));
		stack[sp++] = out;
		out = s;
	} catch (Exception e) {
		done = false;
	}
}

/** Close the current output file.
The previous output file is restored and becomes the current output file. */
public static void close() {
	out.flush();
	out.close();
	if (sp > 0) out = stack[--sp];
}

static { // initializer
	done = true;
	out = System.out;
	stack = new PrintStream[8];
	sp = 0;
}

}
 

Debty

Mitglied
Ach huch ganz vergessen die input.txt

Code:
895652 Mustermann 5895 2100 5 Spetember 2011
185697 Müller 9865 10200 18 September 2013
123456 Schmied 5566 5600 10 September 2015
987654 Schmitt 4321 25300 10 Januar 2014
741852 Schäfer 9822 53000 12 Februar 2012

sieht so aus.
 

eRaaaa

Top Contributor
In der ersten Zeile der Datei wird denke ich mal ein Fehler sein, ist der von dir? :) (Spetember)

Aber generell war dein Versuch zum Einlesen doch gar nicht so verkehrt, fehlt halt jetzt nur noch das Datum.
Du hast nun mehrere Möglichkeiten.Entweder du benutzt weiterhin deine Klasse Datum, dann musst du eben jetzt eben noch ein int,String,int einlesen. Je nachdme wie deine Datumsklasse aussieht und was du erreichen willst, musst du dann eben den String (September) dann evtl. noch zu dem entsprechenden Monat als Zahl umwandeln (8 bzw. 9). Aber das geht hier irgendwie nicht ganz hervor. Sollt ihr denn überhaupt eine eigene Klasse Datum schreiben? Du könntes tja auch einfach Date + SimpleDateFormat benutzen
Java:
        SimpleDateFormat sdf = new SimpleDateFormat(" d MMMMM yyyy", Locale.GERMAN);
//dann könntest du nach deinem  this.betrag = In.readInt(); folgendes einfügen:
        Date d = sdf.parse(In.readLine());
 

Debty

Mitglied
So hab jez nochmal angefangen:

Bis auf einlesen von datei und auf Konto abheben und einzahlen denke ich könnte es so passen oder?
Java:
package bankkonto;

import util.In;
import util.Out;

public class Bankkonto {
	Bankkonto[] kontolist;
	int max_konto_entry;
	int act_konto_entry;
	
	int kontonummer;
	String inhabername;
	int pin;
	int betrag;
	Datum verfalldatum;
	
	/**Konstruktor zum anlegen einer neuen Kontoliste mit Länge des Arrays*/
	public Bankkonto(int max) {
		Out.print("Bitte die maximale Anzahl Ihrer Konten eingeben: ");
		this.max_konto_entry = max;
		this.kontolist = new Bankkonto[max];
	}
	/**Methode um einen Object werte zuzuweisen(wird nicht als Konstruktorbenutz*/
	void setParameters(int ktnr, String name, int pin, int betrag,Datum datum){
		Out.print("Bitte die Kontonummer eingeben: ");
		ktnr = this.kontonummer;
		Out.print("Bitte Name des Kontoinhabers eingeben: ");
		name = this.inhabername;
		Out.print("Bitte gewünschten PIN eingeben: ");
		pin = this.pin;
		Out.print("Bitte den Betrag des auf den Konto vorhanden Geldes eingeben: ");
		betrag = this.betrag;
		Out.print("Bitte das Verfallsdatum eingeben: ");
		datum = this.verfalldatum;
	}
	/**Methode um das Object im Array kontolist zu speicher*/
	void placeobject (Bankkonto b){
		if(this.act_konto_entry<this.max_konto_entry){
			this.kontolist[this.act_konto_entry]=b;
			this.act_konto_entry++;
			}
			else Out.print("Kontoliste hat keine Plätze mehr frei.");
		}
	/**
	 *Rückgabe der Kontonummer
	 */
	public void showkontonummer() {
		Out.print(kontonummer);
	}
	/**
	 * Methode zum Einlesen der ersten 5(!) Konten aus einem File,das vorher erstellt wurde
	 */
	void ReadFile(){
		In.open("input.txt");
		this.kontonummer = In.readInt();
		this.inhabername = In.readWord();
		this.pin = In.readInt();
		this.betrag = In.readInt();	
	}
	/**Eine "Hilfsmethode um nach einem bestimmtem Kontozu suchen und zu sehen wo das im Array liegt
	 * die methode wird gebraucht um danach das Konto löschen zu können.
	 * Ich vergleiche nur die Kontonummer, da wie man weiß es nur jeweils immer eine Kontonummer
	 * gibt und nie zwei die genau die gleichen Zahlen sind.
	 * Also ist die Kontonummer ein eindeutiger Indentifikator
	 * @param b
	 * @return
	 */
	int searchKonto(Bankkonto b){
		int place = -1;
		for(int i =0; i<this.act_konto_entry;i++){
			if(kontolist[i].kontonummer==this.kontonummer)
				place = i;
		}
		return place;
	}
	void deleteKonto(Bankkonto b){
		int place=searchKonto(b);
		if(place >= 0){
			for (int i =place;i<this.act_konto_entry;i++ ){
				kontolist[i]=kontolist[i+1];
			}
			this.act_konto_entry--;
		}
	}
	
}
Zu deinem Vorschlag mit dem Datum
Java:
 SimpleDateFormat sdf = new SimpleDateFormat(" d MMMMM yyyy", Locale.GERMAN);
//dann könntest du nach deinem  this.betrag = In.readInt(); folgendes einfügen:
        Date d = sdf.parse(In.readLine());]
sowas hatten wir noch nicht also denke ich mal darf ich das nicht verwenden:)

meine Datumklasse sieht übrigens so aus (brauche ich den "Parameter Konstruktor" überhaupt?
Java:
package bankkonto;

public class Datum {
	int tag;
	String Monat;
	int Jahr;
	
	public Datum(int tag, String monat, int jahr) {
		this.tag = tag;
		this.Monat = monat;
		this.Jahr = jahr;
	}	
}

Danke für deine große Hilfe echt tolles FORUM!
PS: JA der Schreibfehler ist von mir :)
 

Debty

Mitglied
Java:
/**
	 * Methode zum Einlesen der ersten 5(!) Konten aus einem File,das vorher erstellt wurde
	 */
	void ReadFile(){
		In.open("input.txt");
		for (int i =0;i<=5;i++){
			this.kontolist[i].kontonummer = In.readInt();
			this.kontolist[i].inhabername = In.readWord();
			this.kontolist[i].pin = In.readInt();
			this.kontolist[i].betrag = In.readInt();	
			this.kontolist[i].verfalldatum = In.readInt();In.readWord();In.readInt();
			this.act_konto_entry ++;
		}
	}

So funktionierts net :(
 

Debty

Mitglied
Wie ich gerade festegellst habe passt der Javacode von oben (vor 2 Posts) nicht.
Und zwar wollte ich mal im groben testen ob es so funktionieren würde also habe ich mir ein Object erstellt:
Java:
package bankkonto;

import util.Out;

public class StartBankapp {

	public static void main(String[] args) {
		Datum C;
		C = new Datum(13,"September",2015);
		Bankkonto A;
		A = new Bankkonto(100);
		A.setParameters(111111, "test", 0000, 3000,C);
		A.placeobject(A);
	}
}
Jedoch sieht man nun, dass die Methode "placeobject"
Java:
void placeobject (Bankkonto b){
		if(this.act_konto_entry<this.max_konto_entry){
			this.kontolist[this.act_konto_entry]=b;
			this.act_konto_entry++;
			}
			else Out.print("Kontoliste hat keine Plätze mehr frei.");
		}
völliger Nonsense ist, da ich fürjedes Bankkonte ein eigenes Array erstelle und nur ein Konto in jedem Array speichern kann. Ich könnte das Problem Lösen indem ich einfach eine neue Klasse namens Konto mache und die Klasse Bankkonto dann nur meine Arrays "verwaltet". Ich weiß aber nicht ob ich das darf weil in meiner Aufgabenstellung ja nur die Rede von der Klasse Bankkonto ist. Gibt es noch eine andere Möglichkeit das so zu implementieren wie ich es gern hätte? (wenn ihr verstanden habt wie ich is gern möchte:))?
Mfg Andy
 

Neue Themen


Oben