2nd push
This commit is contained in:
		
							parent
							
								
									57af84310a
								
							
						
					
					
						commit
						fcaa65a875
					
				
							
								
								
									
										136
									
								
								main.py
								
								
								
								
							
							
						
						
									
										136
									
								
								main.py
								
								
								
								
							| 
						 | 
					@ -0,0 +1,136 @@
 | 
				
			||||||
 | 
					import sqlite3
 | 
				
			||||||
 | 
					import matplotlib.pyplot as plt
 | 
				
			||||||
 | 
					from datetime import datetime
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Inicjalizacja bazy danych
 | 
				
			||||||
 | 
					def init_db():
 | 
				
			||||||
 | 
					    conn = sqlite3.connect("finance.db")
 | 
				
			||||||
 | 
					    cursor = conn.cursor()
 | 
				
			||||||
 | 
					    cursor.execute("""
 | 
				
			||||||
 | 
					        CREATE TABLE IF NOT EXISTS transactions (
 | 
				
			||||||
 | 
					            id INTEGER PRIMARY KEY AUTOINCREMENT,
 | 
				
			||||||
 | 
					            type TEXT NOT NULL, -- 'income' lub 'expense'
 | 
				
			||||||
 | 
					            amount REAL NOT NULL,
 | 
				
			||||||
 | 
					            category TEXT,
 | 
				
			||||||
 | 
					            date TEXT NOT NULL
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					    """)
 | 
				
			||||||
 | 
					    conn.commit()
 | 
				
			||||||
 | 
					    conn.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Dodawanie transakcji
 | 
				
			||||||
 | 
					def add_transaction(transaction_type, amount, category):
 | 
				
			||||||
 | 
					    conn = sqlite3.connect("finance.db")
 | 
				
			||||||
 | 
					    cursor = conn.cursor()
 | 
				
			||||||
 | 
					    date = datetime.now().strftime("%Y-%m-%d")
 | 
				
			||||||
 | 
					    cursor.execute("""
 | 
				
			||||||
 | 
					        INSERT INTO transactions (type, amount, category, date)
 | 
				
			||||||
 | 
					        VALUES (?, ?, ?, ?)
 | 
				
			||||||
 | 
					    """, (transaction_type, amount, category, date))
 | 
				
			||||||
 | 
					    conn.commit()
 | 
				
			||||||
 | 
					    conn.close()
 | 
				
			||||||
 | 
					    print("Transakcja dodana!")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Przeglądanie historii transakcji
 | 
				
			||||||
 | 
					def view_transactions():
 | 
				
			||||||
 | 
					    conn = sqlite3.connect("finance.db")
 | 
				
			||||||
 | 
					    cursor = conn.cursor()
 | 
				
			||||||
 | 
					    cursor.execute("SELECT * FROM transactions ORDER BY date DESC")
 | 
				
			||||||
 | 
					    transactions = cursor.fetchall()
 | 
				
			||||||
 | 
					    conn.close()
 | 
				
			||||||
 | 
					    print("\nHistoria transakcji:")
 | 
				
			||||||
 | 
					    for t in transactions:
 | 
				
			||||||
 | 
					        print(f"{t[0]} | {t[1]} | {t[2]:.2f} | {t[3]} | {t[4]}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Filtrowanie transakcji
 | 
				
			||||||
 | 
					def filter_transactions(category=None, start_date=None, end_date=None):
 | 
				
			||||||
 | 
					    conn = sqlite3.connect("finance.db")
 | 
				
			||||||
 | 
					    cursor = conn.cursor()
 | 
				
			||||||
 | 
					    query = "SELECT * FROM transactions WHERE 1=1"
 | 
				
			||||||
 | 
					    params = []
 | 
				
			||||||
 | 
					    if category:
 | 
				
			||||||
 | 
					        query += " AND category = ?"
 | 
				
			||||||
 | 
					        params.append(category)
 | 
				
			||||||
 | 
					    if start_date:
 | 
				
			||||||
 | 
					        query += " AND date >= ?"
 | 
				
			||||||
 | 
					        params.append(start_date)
 | 
				
			||||||
 | 
					    if end_date:
 | 
				
			||||||
 | 
					        query += " AND date <= ?"
 | 
				
			||||||
 | 
					        params.append(end_date)
 | 
				
			||||||
 | 
					    cursor.execute(query, params)
 | 
				
			||||||
 | 
					    transactions = cursor.fetchall()
 | 
				
			||||||
 | 
					    conn.close()
 | 
				
			||||||
 | 
					    print("\nFiltrowane transakcje:")
 | 
				
			||||||
 | 
					    for t in transactions:
 | 
				
			||||||
 | 
					        print(f"{t[0]} | {t[1]} | {t[2]:.2f} | {t[3]} | {t[4]}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Wyświetlanie salda
 | 
				
			||||||
 | 
					def calculate_balance():
 | 
				
			||||||
 | 
					    conn = sqlite3.connect("finance.db")
 | 
				
			||||||
 | 
					    cursor = conn.cursor()
 | 
				
			||||||
 | 
					    cursor.execute("SELECT SUM(amount) FROM transactions WHERE type = 'income'")
 | 
				
			||||||
 | 
					    income = cursor.fetchone()[0] or 0
 | 
				
			||||||
 | 
					    cursor.execute("SELECT SUM(amount) FROM transactions WHERE type = 'expense'")
 | 
				
			||||||
 | 
					    expense = cursor.fetchone()[0] or 0
 | 
				
			||||||
 | 
					    conn.close()
 | 
				
			||||||
 | 
					    balance = income - expense
 | 
				
			||||||
 | 
					    print(f"Saldo konta: {balance:.2f} zł")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Wyświetlanie statystyk
 | 
				
			||||||
 | 
					def show_statistics():
 | 
				
			||||||
 | 
					    conn = sqlite3.connect("finance.db")
 | 
				
			||||||
 | 
					    cursor = conn.cursor()
 | 
				
			||||||
 | 
					    cursor.execute("SELECT category, SUM(amount) FROM transactions WHERE type = 'expense' GROUP BY category")
 | 
				
			||||||
 | 
					    data = cursor.fetchall()
 | 
				
			||||||
 | 
					    conn.close()
 | 
				
			||||||
 | 
					    if not data:
 | 
				
			||||||
 | 
					        print("Brak danych do wyświetlenia statystyk.")
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    categories = [d[0] for d in data]
 | 
				
			||||||
 | 
					    amounts = [d[1] for d in data]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    plt.bar(categories, amounts)
 | 
				
			||||||
 | 
					    plt.title("Wydatki według kategorii")
 | 
				
			||||||
 | 
					    plt.xlabel("Kategoria")
 | 
				
			||||||
 | 
					    plt.ylabel("Kwota (zł)")
 | 
				
			||||||
 | 
					    plt.show()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Główna pętla programu
 | 
				
			||||||
 | 
					def main():
 | 
				
			||||||
 | 
					    init_db()
 | 
				
			||||||
 | 
					    while True:
 | 
				
			||||||
 | 
					        print("\n--- MENU ---")
 | 
				
			||||||
 | 
					        print("1. Dodaj transakcję")
 | 
				
			||||||
 | 
					        print("2. Wyświetl historię transakcji")
 | 
				
			||||||
 | 
					        print("3. Filtruj transakcje")
 | 
				
			||||||
 | 
					        print("4. Pokaż saldo konta")
 | 
				
			||||||
 | 
					        print("5. Wyświetl statystyki")
 | 
				
			||||||
 | 
					        print("6. Wyjście")
 | 
				
			||||||
 | 
					        choice = input("Wybierz opcję: ")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if choice == "1":
 | 
				
			||||||
 | 
					            t_type = input("Typ (income/expense): ").strip().lower()
 | 
				
			||||||
 | 
					            amount = float(input("Kwota: "))
 | 
				
			||||||
 | 
					            category = input("Kategoria: ").strip()
 | 
				
			||||||
 | 
					            add_transaction(t_type, amount, category)
 | 
				
			||||||
 | 
					        elif choice == "2":
 | 
				
			||||||
 | 
					            view_transactions()
 | 
				
			||||||
 | 
					        elif choice == "3":
 | 
				
			||||||
 | 
					            category = input("Kategoria (lub ENTER aby pominąć): ").strip() or None
 | 
				
			||||||
 | 
					            start_date = input("Data początkowa (YYYY-MM-DD, lub ENTER aby pominąć): ").strip() or None
 | 
				
			||||||
 | 
					            end_date = input("Data końcowa (YYYY-MM-DD, lub ENTER aby pominąć): ").strip() or None
 | 
				
			||||||
 | 
					            filter_transactions(category, start_date, end_date)
 | 
				
			||||||
 | 
					        elif choice == "4":
 | 
				
			||||||
 | 
					            calculate_balance()
 | 
				
			||||||
 | 
					        elif choice == "5":
 | 
				
			||||||
 | 
					            show_statistics()
 | 
				
			||||||
 | 
					        elif choice == "6":
 | 
				
			||||||
 | 
					            print("Do widzenia!")
 | 
				
			||||||
 | 
					            break
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            print("Nieprawidłowa opcja. Spróbuj ponownie.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == "__main__":
 | 
				
			||||||
 | 
					    main()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue