Auf Thema antworten

Möchte auch gerne direkt eine nächste Frage stellen. Nun habe ich die Kategorien erstellt und als nächstes soll ein BudgetZiel für eine Kategorie gesetzt werden. Ziel soll es, eine kleine Haushaltsapp zu schreiben, wo man Einzahlungen und Auszahlungen tätigen kann. Bei jeder Zahlung (Payment) wird ein Konto, ein Betrag und eine Kategorie übergeben. Wenn man ein Datum übergibt, wird der Zahlung ebenfalls ein Datum übergeben. Übergibt man kein Datum, wird das heutige Datum übergeben.


Jetzt möchte ich quasi für eine Kategorie ein BudgetZiel hinterlegen. Im nächsten Schritt soll mir über eine Methode angezeigt werden, wie viel Budget ich für diese Kategorie übrig habe. Bspw. setze ich für die Kategorie "Essen" ein Budget von 1000€ fest. Anschließend mache ich drei Zahlungen in Höhe von jeweils 100€. Dann soll mir über eine Methode angezeigt werden, dass ich für die Kategorie "Essen" noch 700€ zur Verfügung habe.


Ich stelle mal einfach etwas Code rein. By the way, dass ist nicht alles mein Code, sondern eine Gemeinschaftsprojekt.


[CODE=java]import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;


abstract class Account {


    String name;

    Double amount;

    ArrayList<Payment> accPaymentList = new ArrayList<Payment>();

   

   

   

    //Konstruktor.

    public Account (String name) {

        this.name = name;

        this.amount = null;

    }

    /*

     * Gibt alle Buchungen des aufrufenden Kontos in der Form:

     * "xx.xx.xxxx: -xx.xx € (Kategorie)"

     * aus.

     * Mithilfe der static inner class ListSortedByDate wird die accPaymentList zunächst nach Datum sortiert.

     * Die Datumsanzeige erfolgt über getDate(), eine Methode der Klasse Payment.

     */

    public void showAccPaymentList() {


        Collections.sort(accPaymentList, new ListSortedByDate());


        for( Payment p: this.accPaymentList) {

            String s = null;

            if (p instanceof Deposit)

                s = "+";

            if (p instanceof Withdrawal)

                s = "-";

            System.out.println(p.getDate()+": "+s+p.amount+" € ("+p.cat.name+")");

        }

        //gibt zusätzlich den amount an

        System.out.println(this.name+": "+this.getAmount()+" €");

    }

    /*

     * static inner class.

     * Dient der Sortierung von verschiedenen Zahlungen nach Datum.

     */

    static class ListSortedByDate implements Comparator<Payment> {

        @Override

        public int compare(Payment a, Payment b) {

            return a.cal.compareTo(b.cal);

        }

    }

   

    // Getter & Setter

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public Double getAmount() {

        return amount;

    }

    public void setAmount(Double amount) {

        this.amount = amount;

    }  

    public ArrayList<Payment> getAccPaymentList() {

        return accPaymentList;

    }

    public void setAccPaymentList(ArrayList<Payment> accPaymentList) {

        this.accPaymentList = accPaymentList;

    }

}[/CODE]

[CODE=java]

import java.util.Calendar;

import java.util.GregorianCalendar;


abstract class Payment {

   

    Account account;

    Account transferAccount;

    double amount;

    Category cat;

    GregorianCalendar cal;

   

   

    //Kontruktor für sofortige Ausführung. SinglePayment.

    public Payment (Account account, double amount, Category cat) {

        this.account = account;

        this.transferAccount = null;

        this.amount = amount;

        this.cat = cat;

        this.cal = new GregorianCalendar();

        this.account.accPaymentList.add(this);

        this.cat.catPaymentList.add(this);

       

    }

    // Kontruktor mit variablem Ausführungszeitpunkt. SinglePayment.

    public Payment (Account account, double amount, Category cat, int date, int month, int year) {

        this.account = account;

        this.transferAccount = null;

        this.amount = amount;

        this.cat = cat;

        this.cal = new GregorianCalendar();

        this.cal.set(Calendar.DATE, date);

        this.cal.set(Calendar.MONTH, month-1);

        this.cal.set(Calendar.YEAR, year);

        this.account.accPaymentList.add(this);

        this.cat.catPaymentList.add(this);

   

    }

    // Konstruktor mit 2 Accounts für Transfer-Buchungen. Sofortige Ausführung.

    public Payment (Account account, Account transferAccount, double amount, Category cat) {

        this.account = account;

        this.transferAccount = transferAccount;

        this.amount = amount;

        this.cat = cat;

        this.cal = new GregorianCalendar();

        Payment p = new Withdrawal(account, amount, cat);                                                                    //!! Müssen noch abgespeichert werden      

        Payment q = new Deposit(transferAccount, amount, cat);

    }

    // Konstruktor mit 2 Accounts für Transfer-Buchungen. Variabler Ausführungszeitpunkt.

    public Payment (Account account, Account transferAccount, double amount, Category cat, int date, int month, int year) {

        this.account = account;

        this.transferAccount = transferAccount;

        this.amount = amount;

        this.cat = cat;

        this.cal = new GregorianCalendar();

        this.cal.set(Calendar.DATE, date);

        this.cal.set(Calendar.MONTH, month-1);

        this.cal.set(Calendar.YEAR, year);

        Payment p = new Withdrawal(account, amount, cat, date, month, year);                                                //!! Müssen noch abgespeichert werden

        Payment q = new Deposit(transferAccount, amount, cat, date, month, year);

    }

    // Erhöht das Guthaben des übergebenen Kontos um den übergebenen Betrag

    public void increaseAccountAmount(Account account, double amount) {

        if (this.account.amount == null)

            this.account.amount = amount;

        else

            this.account.amount = this.account.amount+amount;

    }

    // Verringert das Guthaben des übergebenen Kontos um den übergebenen Betrag

    public void decreaseAccountAmount(Account account, double amount) {

        if (this.account.amount == null)

            this.account.amount = -amount;

        else

            this.account.amount = this.account.amount-amount;

    }

    // getDate im Format "Datum: D.M.Y"

    public String getDate () {

        return this.cal.get(Calendar.DATE)+"."+(this.cal.get(Calendar.MONTH)+1)+"."+this.cal.get(Calendar.YEAR);

    }

    /*

     * Methode, die von Payments aufgerufen werden muss, die wiederholt werden sollen.

     * Zahlung wird so oft wie übergeben monatsweise wiederholt.

     */

    public void repPayment (int times) {                                                                    //!!Müssen noch abgespeichert werden

        if (this  instanceof Deposit) {

            for (int i = 1; i < times; i++){

                int a = this.cal.get(Calendar.DATE);

                int b = ((this.cal.get(Calendar.MONTH)+1));

                int c = this.cal.get(Calendar.YEAR);

                b = b+i;

                Payment x = new Deposit(this.getAccount(), this.getAmount(), this.getCat(), a, b, c);

            }

        }

        if (this instanceof Withdrawal) {

            for (int i = 1; i<= times; i++){

                int a = this.cal.get(Calendar.DATE);

                int b = ((this.cal.get(Calendar.MONTH)+1));

                int c = this.cal.get(Calendar.YEAR);

                b = b+i;

                Payment x = new Withdrawal(this.getAccount(), this.getAmount(), this.getCat(), a, b, c);

            }

        }

        if (this instanceof Transfer) {

            for (int i = 1; i<= times; i++){

                int a = this.cal.get(Calendar.DATE);

                int b = ((this.cal.get(Calendar.MONTH)+1));

                int c = this.cal.get(Calendar.YEAR);

                b = b+i;

                Payment x = new Withdrawal(this.getAccount(), this.getAmount(), this.getCat(), a, b, c);

                Payment y = new Deposit(this.getTransferAccount(), this.getAmount(), this.getCat(), a, b, c);

            }

        }

    }



   

    //Getter & Setter

    public Account getAccount() {

        return account;

    }

    public void setAccount(Account account) {

        this.account = account;

    }

    public Double getAmount() {

        return amount;

    }

    public void setAmount(Double amount) {

        this.amount = amount;

    }

    public Category getCat() {

        return cat;

    }

    public void setCat(Category cat) {

        this.cat = cat;

    }

    public GregorianCalendar getCal() {

        return cal;

    }

    public void setCal(GregorianCalendar cal) {

        this.cal = cal;

    }

    public Account getTransferAccount() {

        return transferAccount;

    }

    public void setTransferAccount(Account transferAccount) {

        this.transferAccount = transferAccount;

    }

}[/CODE]

[CODE=java]import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;


public class Category {

   

    public String name;

    public ArrayList<Payment> catPaymentList = new ArrayList<Payment>();

   

    public Category (String name) {

        this.name = name;

    }

   

   

    public void showCatPaymentList() {


        Collections.sort(catPaymentList, new ListSortedByDate());

       

        System.out.println("Kategorie: " +this.getName());

       

        //zusätzlich

        double sum = 0;

        for( Payment p: catPaymentList) {

            String s = null;

            if (p instanceof Deposit)

                s = "+";

            if (p instanceof Withdrawal)

                s = "-";

            System.out.println("Datum: " + p.getDate()+": "+s+p.amount + "€");

            sum = sum + p.amount;

        }

        //Zusätzliche Ausgabe

//        System.out.println(this.name+": "+this.getAmount()+" €");

        System.out.println(this.name + " Summe an Ausgaben: " + sum + "€\n");

    }

   

    /*

     * static inner class.

     * Dient der Sortierung von verschiedenen Zahlungen nach Datum.

     */

    static class ListSortedByDate implements Comparator<Payment> {

        @Override

        public int compare(Payment a, Payment b) {

            return a.cal.compareTo(b.cal);

        }

    }


    // Getter und Setter

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public ArrayList<Payment> getCatPaymentList() {

        return catPaymentList;

    }

    public void setCatPaymentList(ArrayList<Payment> catPaymentList) {

        this.catPaymentList = catPaymentList;

    }

}[/CODE]


[CODE=java]Payment x = new Withdrawal(cash, 100.00,food);

Payment y = new Withdrawal(cash,100.00,food);

Payment c = new Withdrawal(cash, 100.00, food);[/CODE]


In der Main Methode erstelle ich drei Zahlungen unter der Kategorie "food".


Habe eine neue Klasse BudgetTarget angelegt, doch weiß nicht so recht weiter.

[CODE=java]import java.util.GregorianCalendar;


public class BudgetTarget {


    //Erste Ideen

    double budget;

    double targetbudget;

    GregorianCalendar startDate;

    GregorianCalendar targetDate;

   

   

}[/CODE]



Oben