Προς το περιεχόμενο

Προτεινόμενες αναρτήσεις

Δημοσ.

Αν χτες μπήκατε στις κλάσεις και μάλιστα έλλειπες μάλλον δεν έχεις καταλάβει ευρύτερα τον αντικειμενοστραφή προγραμματισμό. Διάβασε τα links που σου έδωσα και αν έχεις απορίες, εδώ είμαστε.

Καλησπέρα, διαβασα τα Links, ξανα εριξα μια ματια στον κωδικα μου και καταλαβα τα λαθη μου...

Διορθωσα τον κωδικα μου, (σκεφτηκα πιο απλα), δεν μου αποθηκευει την λιστα ομως δεν βλεπω το προβλημα... Εχει δει 4-5 φορες για τα tab αλλα ειναι σωστα... Σας παραθετω τον κωδικα : 

#Classes
    
class User:
    def __init__(self, name, surname, address, telephone, email):
        self.name = name
        self.surname = surname
        self.address = address
        self.telephone = telephone
        self.email = email
    def print_user_info(self):
        print("Name:",self.name,
         "\nSurname:",self.surname,
          "\nAddress:",self.address,
          "\nTelephone:",self.telephone,
          "\nEmail:",self.email)
        
class Account:
    def __init__(self, id, user, balance):
        self.id= id
        self.user= user
        self.balance= balance
    def print_account_info(self):
      print ("Owner:",  self.user.name,self.user.surname,
           "Account ID:",self.id,
           "Balance:", self.balance)
    def print_balance(self):
        print("Balance:",self.balance)

class AccountManager:
    def __init__(self):
        self.accounts=[]
    def create_account(self):
        if len(self.accounts) == 0:
              id = 1
        else:
            id = self.accounts[-1].id + 1
        print("Choose Action :1 \n\n")
        print(".::New Account::.")
        name=str(input("Insert Name:"))
        surname=str(input("Insert Surname:"))
        address=str(input("Insert Address:"))
        telephone=str(input("Insert Telephone:"))
        email=str(input("Insert EMail:"))
        balance=float(input("Insert Account current Balance:"))
        u= User(name, surname, address, telephone, email)
        a= Account(id, u, balance)
        self.accounts.append(a)
        print("Account with ID", id,"successfully created!")
    def print_accounts_info(self):
        print("Choose Action:4 \n\n"
            ".::Accounts Info::.")
        for k in  self.accounts:
            print( "ID:", k.id,", Balance: ", k.balance, " Euro, Owner:",k.User.name, k.User.surname)
    def deposit(self, account_id, amount):
        print("Choose Action: 3")
        for i in self.accounts:
            if (i.id == account_id):
                i.balance+=amount
                return i.balance
        return None
    def withdraw(self, account_id, amount):
        print("Choose Action: 2")
        for k in self.accounts:
            if (k.id ==account_id):
                if(amount>k.balance):
                    print("No funds available")
                else:
                    k.balance-= amount
                return k.balance
        return None
    
#Start
x=10
while (x!=5):
  print(".::MENU::.\n"
        "1: Create Account\n"
        "2: Withdraw\n"
        "3: Deposit\n"
        "4: Print Accounts Info\n"
        "5: Exit\n")
  x=int(input("Choose: "))
  execute=AccountManager()
  if (x==1):
    execute.create_account()
  elif (x==2):
    a_id = int(input("Insert Account ID: "))
    a_am= int(input("Insert Amount: "))
    WITHDRAW=execute.withdraw(a_id, a_am)
    if (WITHDRAW!=None):
     print(".::Withdraw::.")
     print(a_am, "Euro withdraw @ Account ID " ,a_id," Current Balance:",WITHDRAW," Euro")
    else:
     print("\nAccount ID wasn't found!")
  elif (x==3):
    a_id = int(input("Insert Account ID: "))
    a_am= int(input("Insert Amount: "))
    DEPOSIT=execute.deposit(a_id, a_am)
    if (DEPOSIT!=None):
     print(".::Deposit::.")
     print(a_am, "Euro deposit @ Account ID ", a_id," Current Balance:",WITHDRAW," Euro")
    else:
     print("\nAccount ID wasn't found!")
  elif (x==4):
    execute.print_accounts_info()
  elif (x==5):
    print(" Thank you, GoodBye!!")
  else:
    print("\nError - Type from 1-5\n")

Δημοσ. (επεξεργασμένο)

βγάλε το execute=AccountManager() έξω από το while για να σου βλέπει τα accounts που χρησιμοποιείς. Φτιάξε ένα function στην AccountManager που να του δίνεις id κ να σου επιστρέφει το αντίστοιχο account object. Αφού το κάνεις αυτό φτιάξε τα functions deposit και withdraw (στο αφήνω να βρείς τα λάθη τους). Γενικά έχεις πρόβλημα με την έννοια του scope, διάβασε εδώ για να την καταλάβεις καλύτερα.

Άλλαξε το όνομα της μεταβλητής "id" παντού (είναι στα builtins και μπερδεύεσαι μόνο να τη βλέπεις.)

Επεξ/σία από iceblade
Δημοσ.

βγάλε το execute=AccountManager() έξω από το while για να σου βλέπει τα accounts που χρησιμοποιείς. Φτιάξε ένα function στην AccountManager που να του δίνεις id κ να σου επιστρέφει το αντίστοιχο account object. Αφού το κάνεις αυτό φτιάξε τα functions deposit και withdraw (στο αφήνω να βρείς τα λάθη τους). Γενικά έχεις πρόβλημα με την έννοια του scope, διάβασε εδώ για να την καταλάβεις καλύτερα.

Άλλαξε το όνομα της μεταβλητής "id" παντού (είναι στα builtins και μπερδεύεσαι μόνο να τη βλέπεις.)

Το εβγαλα και τρεχει σωστα!!!! Σε ευχαριστω πολυ!!!!!!!!!!!!

Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε

Πρέπει να είστε μέλος για να αφήσετε σχόλιο

Δημιουργία λογαριασμού

Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!

Δημιουργία νέου λογαριασμού

Σύνδεση

Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.

Συνδεθείτε τώρα
  • Δημιουργία νέου...