sparbuch

Status
Nicht offen für weitere Antworten.

matthias77

Mitglied
Hallo Forum,
an diesen Programm hänge ich schon seit Stunden aber ich bekomme es nicht zu laufen.
Vielleicht kann sich das mal einer ansehen:

Code:
package javaapplication1;

public int add(int value);
public int withdraw(int value);
public void info(int value);

private int total;
public Sparbuch(int value)
    {
        this.value =value;
        this.total = value;
    }

public class Sparbuch
{
    private int total;
    private int value;
    public Sparbuch(int value)
    {
        this.value = value;
        this.total = value;
    }
    
public int withdraw(int amount)
    {
        //Geld abheben
    }
public int add(int amount)
    {
        //Geld einzahlen
    }
public void info(int value)
    {
        //Kontostand anzeigen
    }
public static void main(String[] args)
    {
        Sparbuch s = new Sparbuch(500);
    }
}


public int add(int amount)
{
    total = amount;
    return total++;
}

public int withdraw(int amount)
{
    amount = total;
    total--;
    if (total <= 0)
        System.out.println("Keine Auszahllung möglich! Ihr Kontostand ist: " +total);
    return total;
}

public void info(int value)
{
    if(value <= 0)
        System.out.println("Warnung! Kontostand: " -value);
    else
        {
            total = total + value;
                System.out.println("Kontostand: " +total);
        }   
}

int x = s.add(value);//Kontostand speichern
s.info(x);//Wert von Kontostand an info() übergeben


public static void main(String[[] args)
{
    BufferedReader in = new BufferedReader(new InputSreamReader(System.in));
    int x =0;
    Sparbuch s = new Sparbuch(500);
    System.out.println("Wilkommen bei der Sparkasse.");
    System.out.println("Wählen Sie eine Aktion aus!");
    System.out.println("1. Geld einzahlen");
    System.out.println("2. Geld abheben");
    System.out.println("3. Kontostand anzeigen");
    System.out.println("4. Beenden");
    
        try
        {
            int option = Integer.parseInt{int.readLine()};
                switch(option)
                {
                    case 1:
                    {
                        System.out.println("Betrag eingeben: ");
                        int value = Integer.parseInt(in.readLine());
                        x = s.add(value);
                        s.info(x);
                    }
                    break;
                    case 2:
                    {
                        System.out.println("Betrag eingeben: ");
                        int value = Integer.parseInt(in.readLine());
                        x = s.withdraw(value);
                        s.info(x);   
                    }
                    break;
                    case 3:
                    {
                        System.out.println("Ihr Kontostand: ");
                        s.showTotal();
                    }
                    break;
                    case 4:
                    {
                        System.exit(0);
                    }
                }
        }
    catch (Exception e)
    {
    
    }
}

Danke!
 

Der Müde Joe

Top Contributor
naja.....mal extremstens aufgeräumt etc....

Code:
import java.util.Scanner;

public class Sparbuch {

    private int total;
    private int value;
    
    public Sparbuch(int value){
        this.value = value;
        this.total = value;
    }
   
    public int add(int amount){
        total = amount;
        return total++;
    }

    public int withdraw(int amount){
        amount = total;
        total--;
        if (total <= 0)
            System.out.println("Keine Auszahllung möglich! Ihr Kontostand ist: " +total);
        return total;
    }

    public void info(int value){
        if(value <= 0){
            System.out.println("Warnung! Kontostand: " + value);
        }else {
        total = total + value;
         System.out.println("Kontostand: " +total);
        }   
    } 


	public static void main(String[] args){
		
		Scanner scanner = new Scanner(System.in);
	    int x =0;
	    Sparbuch s = new Sparbuch(500);
	    System.out.println("Wilkommen bei der Sparkasse.");
	    System.out.println("Wählen Sie eine Aktion aus!");
	    System.out.println("1. Geld einzahlen");
	    System.out.println("2. Geld abheben");
	    System.out.println("3. Kontostand anzeigen");
	    System.out.println("4. Beenden");
	   
	        try
	        {
	            int option = scanner.nextInt();
	                switch(option)
	                {
	                    case 1:
	                    {
	                        System.out.println("Betrag eingeben: ");
	                        int value = scanner.nextInt();
	                        x = s.add(value);
	                        s.info(x);
	                    }
	                    break;
	                    case 2:
	                    {
	                        System.out.println("Betrag eingeben: ");
	                        int value = scanner.nextInt();
	                        x = s.withdraw(value);
	                        s.info(x);   
	                    }
	                    break;
	                    case 3:
	                    {
	                        System.out.println("Ihr Kontostand: ");
	                     //   s.showTotal();
	                    }
	                    break;
	                    case 4:
	                    {
	                        System.exit(0);
	                    }
	                }
	        }
	    catch (Exception e)
	    {
	   
	    }
	}
}

EDIT:
also die Methoden hab ich nicht angepasst....aber wäre nötig

Stellt sich sicherlich die Frage, was ist der Unterschied zwischen total und value??
 

matthias77

Mitglied
Danke erst einmal für die Hilfe.
Jetzt läuft es schon ganz gut auser das ich keine ausgabe habe wen ich den Kontostand abfragen will.

Was ist denn der unterschied zwischen total und value?
 

Der Müde Joe

Top Contributor
matthias77 hat gesagt.:
Was ist denn der unterschied zwischen total und value?

na das Frage ich eben dich? Wieso hat dein Konto eine total und ein value?

Du hast ja die info Abfrage...einfach die benutzen für das Anzeigen.
also
s.info() dort wo case 3 ist....achja..wieso einen Wert übergeben bei einer Abfrage?
 

matthias77

Mitglied
Also, ich beschäftige mich seit ca. 1 Woch mit Java und versuche mir das ganze mit einen Buch beizubringen.
Java 6 Programmierung, von Data Becker und das Programm stand da als Beispiel drinn.
 

Der Müde Joe

Top Contributor
ich hab mal alles gefixed.

DataBecker kenn ich nicht. Finde diese noch gut:
http://www.galileocomputing.de/openbook/javainsel7/

Code:
import java.util.Scanner;

public class Sparbuch {

	//amount of money in this account
    private int value;
    
    public Sparbuch(int value){
        this.value = value;
    }
   
    /**
     * add cash to account
     * @param amount
     * @return
     */
    public int add(int amount){
    	this.value += amount;
    	return value;
    }

    /**
     * withdraw cash
     * 
     * @param amount
     * @return
     */
    public int withdraw(int amount){
    	//try to withdraw
    	int val = this.value - amount;
    	//if less than zero not ok
        if (val < 0) {
        	System.out.println("Keine Auszahllung möglich! Ihr Kontostand ist: " + this.value);        	
        } else {
        	//withdraw
        	this.value = val;
        	System.out.println("Sie erhalten : " + amount);
        }
        return value;
    }

    /**
     * print accounts info
     */
    public void info(){
    	//impossible smaller than zero
        if(value < 0){
            System.out.println("Warnung! Kontostand: " + value);
        }else {
        	System.out.println("Kontostand: " +value);
        }   
    } 


	public static void main(String[] args){
		
		Scanner scanner = new Scanner(System.in);
	    Sparbuch s = new Sparbuch(500);
	    
	    while(true) {    
		    System.out.println("Wilkommen bei der Sparkasse.");
			System.out.println("Wählen Sie eine Aktion aus!");
			System.out.println("1. Geld einzahlen");
			System.out.println("2. Geld abheben");
			System.out.println("3. Kontostand anzeigen");
			System.out.println("4. Beenden");

			try {
				int option = scanner.nextInt();
				switch (option) {
					case 1: {
						System.out.println("Betrag eingeben: ");
						int value = scanner.nextInt();
						s.add(value);
						s.info();
						break;
					}
					case 2: {
						System.out.println("Betrag eingeben: ");
						int value = scanner.nextInt();
						s.withdraw(value);
						s.info();
						break;
					}
					case 3: {
						s.info();
						break;
					}
					case 4: {
						System.out.println("Auf Wiedersehen");
						System.exit(0);
						break;
					}
					default: {
						System.out.println("Try again");
						break;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

EDIT:
zu sagen ist noch, dass case.. eigentlich keine {} braucht..also
Code:
switch (x) {
case 1:
	//do something
	break;
case 2:
	//do something
	break;
default:
	//do something
	break;
}
 

matthias77

Mitglied
Vielen Dank, es läuft jetzt perfekt.
Wie ich sehen kann ich jetzt auch mehrere Eingaben hintereinander machen ohne das Programm neu starten zu
müssen.
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
M Problem mit Sparbuch Aufgabe Java Basics - Anfänger-Themen 6

Ähnliche Java Themen

Neue Themen


Oben