Curso POO Teoria #05a - Exemplo Prático com Objetos

Curso POO Teoria #05a - Exemplo Prático com Objetos

Welcome to Object-Oriented Programming

Introduction to the Lesson

  • The instructor welcomes students to another lesson in their object-oriented programming course, emphasizing the importance of this particular session.
  • The instructor introduces themselves as Professor Guanabara and mentions that this is the fifth lesson, which will include a practical example alongside theoretical concepts.

Practical Example Overview

  • A practical exercise will be implemented in both PHP and Java, allowing students to apply what they have learned theoretically.
  • Students are encouraged not to skip ahead; practicing with examples will make future lessons easier, particularly one focused on encapsulation.

Learning Approach

  • Two approaches for learning are suggested: an advanced method for experienced programmers and a simpler method for beginners.
  • Students should attempt coding exercises independently before watching the full implementation in class.

Bank Account Implementation

Scenario Setup

  • The instructor presents a scenario involving two characters, Jubileu and Creuza, who each need to open bank accounts.
  • Jubileu chooses a savings account while Creuza opts for a checking account. Each character deposits different amounts into their respective accounts.

Transaction Examples

  • Jubileu deposits R$300 into his savings account while Creuza deposits R$500 into her checking account.
  • Later, Creuza withdraws R$100 from her checking account for a purchase, leaving her with R$400.

Defining Bank Account Class Attributes

Class Structure Explanation

  • The goal is to create an object representing a bank account by defining its attributes and methods based on previous lessons about object structure.

Attributes of the Bank Account Class

  • Account Number: Unique identifier for each bank account.
  • Account Type: Specifies whether it’s a checking or savings account.
  • Owner's Name: Basic identification of the account holder (though ideally would involve another class).
  • Balance: Current amount of money in the account.
  • Status: Indicates if the account is open or closed.

Methods Associated with Bank Accounts

  • Open Account: Functionality to create new accounts.
  • Close Account: Ability to close existing accounts.
  • Deposit Funds: Method for adding money into an account.
  • Withdraw Funds: Method for taking money out of an account.
  • Monthly Fee Payment: Represents costs associated with maintaining an open bank account.

Class Diagram Representation

Visualizing Class Structure

Account Management and Methods Overview

Account Types and Initialization

  • The account system includes attributes such as type, owner, balance, and status. Methods include opening, closing accounts, depositing, withdrawing funds, and paying monthly fees.
  • Two account types are defined: savings (conta poupança) and checking (conta-corrente). For programming simplicity, abbreviations 'cc' for checking and 'cp' for savings will be used.
  • Upon opening an account, the status is set to true. A checking account starts with a balance of 50 reais while a savings account starts with 150 reais.

Closing Accounts

  • To close an account, it must have no remaining balance or debts. Verification of these conditions is necessary before closure.

Deposit and Withdrawal Procedures

  • Deposits can only occur if the account is open (status true). Withdrawals also require an open status and sufficient balance; one cannot withdraw more than what is available.
  • For example, if the balance is 1,000 reais, a withdrawal cannot exceed this amount.

Monthly Fees Structure

  • Monthly fees are automatically deducted from the balance when the method to pay monthly fee is called. Checking accounts incur a fee of 12 reais while savings accounts incur a fee of 20 reais.

Visibility and Method Definitions

  • Attributes have varying visibility: public for account number; protected for type; private for owner, balance, and status. Understanding these visibility levels is crucial.
  • All methods in this example are treated as public. Special methods related to attributes will also be implemented as needed.

Constructor Method Insights

  • A constructor method initializes new accounts with their status set to false (closed), and balances set to zero upon creation.

Practical Application Encouragement

  • Students are encouraged to take notes on these characteristics while working on practical exercises in various programming languages like PHP or Java.

Importance of Practice in Learning Programming

  • Watching tutorials alone isn't enough; hands-on practice with real coding exercises solidifies understanding of object-oriented programming concepts.

Course Sponsorship Acknowledgment

  • The instructor expresses gratitude towards sponsors who support high-quality course content compared to typical YouTube tutorials.

Hosting Recommendations

  • Hostnet is recommended as a reliable hosting service for students looking to deploy their websites securely with proper data management practices.

Cloud Data Management and Military Career Opportunities

Importance of Cloud Data Recovery

  • The significance of data recovery is emphasized, particularly for websites that rely on cloud storage. Having control over your data is crucial.
  • Hostnet's cloud system is highlighted as one of the most efficient in the market, ensuring data replication across multiple servers to enhance accessibility.
  • The geographical distribution of servers allows users to access their data from locations closer to them, improving load times and user experience.

Educational Opportunities in the Military

  • A transition into discussing educational opportunities for young individuals (ages 17-24), particularly those considering a military career.
  • It’s noted that joining the military doesn't necessarily mean starting as a soldier; roles such as technician positions in the Navy or Air Force are available.
  • The benefits of military service include competitive salaries and job stability, which are appealing factors for potential recruits.

Programming Education and Support

  • The speaker introduces an educational institution supporting programming courses, emphasizing hands-on learning experiences with instructors.
  • Students interested in technology can learn about Linux systems, hardware networking, algorithms, object-oriented programming, Java, PHP, and database creation through this program.

Community Support and Contributions

  • An appeal is made for community support where viewers can become sponsors of the course by contributing financially to help improve resources and equipment.
  • The speaker encourages small contributions from many supporters to collectively enhance project quality and educational offerings.

Course Structure and Learning Approach

  • Emphasis on practical learning: students are encouraged not just to copy code but to understand object-oriented programming concepts deeply.
  • A reminder that initial challenges in learning new paradigms like object-oriented programming should be expected; practice is essential for mastery.

Account Attributes and Methods in Object-Oriented Programming

Defining Account Attributes

  • The account number is declared as an integer and set to public visibility, allowing access from outside the class.
  • The account type is a protected attribute that accepts character values, specifically for checking or savings accounts (cc or cpi).
  • The owner's name is a private character attribute, ensuring it remains hidden from external access.
  • The balance is also private and defined as a real number, representing the bank's current balance.
  • Lastly, the status indicates whether the account is open or closed; it's a private logical type.

Creating Methods for Account Management

  • Initial methods include opening and closing accounts, along with depositing, withdrawing, and paying fees. Understanding these methods requires familiarity with class structure.
  • A constructor method initializes new instances of the class. It sets default values: a starting balance of 10 and an initial status of false (closed).
  • When creating an account instance (for any user), both the balance starts at zero and the status remains false until explicitly changed.

Implementing Getter and Setter Methods

  • Getter (get) and setter (set) methods are created for each attribute. For example, setAccountNumber takes an integer parameter to define the account number.
  • Each setter method must accept parameters matching their respective attributes' types; e.g., setAccountNumber receives an integer parameter named dn.
  • Getter methods return their corresponding attributes without needing parameters. For instance, getAccountNumber simply returns the value of the account number.

Consistency in Method Creation

  • Following a consistent pattern across all attributes simplifies coding: each setter receives a parameter while getters return their respective values.
  • This repetition reinforces understanding of object-oriented programming principles; practice leads to mastery over time.

Final Thoughts on Implementation

  • All getter/setter pairs follow similar structures: defining parameters matching attribute types for setters while returning attribute values for getters.
  • Emphasizes that mastering these concepts requires practice; copying code examples can aid learning across various programming languages focused on object orientation.

Opening a Bank Account

Understanding Account Types and Initial Setup

  • The process of opening an account begins with determining the type: checking (conta corrente) or savings (conta poupança). Each type has associated bonuses: R$50 for checking accounts and R$150 for savings accounts.
  • The method to open an account is public, requiring a character input that specifies the account type. This setup is crucial for defining how the account will function.
  • The constructor sets the initial status of the account as false. Upon opening, this status must change to true, indicating that the account is active.
  • The code allows flexibility in naming variables; however, clarity should be maintained. For instance, using 'type' instead of 'setType' can simplify understanding while still being correct.
  • If the account type is checking (cc), it automatically receives R$50; if it's savings (cp), it gets R$150. This logic ensures proper initialization based on user choice.

Closing an Account

  • To close an account, it must be verified that there are no funds remaining—neither positive nor negative balances are allowed.
  • If there’s a positive balance, a message indicates that closure isn’t possible due to existing funds. This reinforces responsible banking practices.
  • In cases where the balance is negative, another error message states that the account is in debt and cannot be closed until resolved.
  • A check confirms if the balance equals zero before allowing closure. If so, the status changes back to false to indicate closure successfully.

Depositing Money into an Account

Verifying Account Status Before Deposit

  • Before making a deposit, it's essential to confirm whether the account status is true (open). This prevents errors during transactions.
  • When depositing money, the current balance increases by adding the deposit amount to it. This reflects standard banking operations where deposits enhance available funds.
  • An alternative approach involves using getters and setters for modifying balances more securely and clearly within object-oriented programming principles.

Understanding Account Management in Programming

Methods for Managing Account Balance

  • The instructor discusses two methods for managing account balances: one is straightforward and easier for beginners, while the other is more complex but ultimately correct. Beginners can use either method initially.
  • When attempting to deposit money, an error occurs if the account is closed. The instructor emphasizes that it's impossible to deposit funds into a closed account.

Conditions for Withdrawing Funds

  • To withdraw funds, two conditions must be met: the account must be active, and there should be sufficient balance. The code checks if the account status is true (active).
  • The instructor explains how to verify if there are enough funds by comparing the balance with the withdrawal amount. If both conditions are satisfied, the withdrawal can proceed.

Handling Insufficient Funds

  • If there are insufficient funds or if the account isn't open, appropriate error messages will be generated. This ensures users understand why their transaction cannot proceed.
  • The instructor reassures learners that difficulty in understanding programming concepts often stems from a lack of algorithm knowledge rather than object-oriented programming itself.

Importance of Practice and Learning Resources

  • Emphasizing practice, the instructor suggests that without it, students may struggle with programming concepts. He recommends taking courses on algorithms before diving deeper into programming languages.
  • Monthly fees are discussed; depending on whether it's a checking or savings account, different fees apply ($12 for checking accounts and $20 for savings accounts).

Implementing Monthly Fees Logic

  • A local variable is declared to hold monthly fee values based on account type. This logic determines which fee applies when processing transactions.
  • Monthly fees can only be charged if the account is open and has sufficient balance to cover them. If not, an error message indicating payment impossibility will appear.

Final Thoughts on Learning Programming

  • The instructor encourages viewers to choose between PHP or Java courses based on their interests while stressing that interactivity enhances learning experiences across devices.
Video description

Nessa aula de POO, vamos fazer um exemplo prático com Programação Orientada a Objetos, usando tudo aquilo que aprendemos até aqui. Gostou da aula? Então torne-se um Gafanhoto APOIADOR do CursoemVídeo acessando o site apoie.me/cursoemvideo Nós do CursoemVideo sempre recomendamos assistir a aula completa, mas se quiser aprender diretamente uma parte específica, clique nos marcadores de tempo a seguir: 0:19 - Qual é o assunto da aula? Aula do Curso de Programação Orientada a Objetos POO criado pelo professor Gustavo Guanabara para o portal CursoemVideo.com. Curso em Vídeo Seja um apoiador: http://apoie.me/cursoemvideo Site: http://www.cursoemvideo.com YouTube: http://www.youtube.com/cursoemvideo Facebook: http://www.facebook.com/cursosemvideo Twitter: http://twitter.com/cursosemvideo Google+: http://plus.google.com/112666558837414979080 Patrocínio HOSTNET: http://www.hostnet.com.br EDUCANDUS: http://www.sistemaeducandus.com.br GAFANHOTOS: http://apoie.me/cursoemvideo Ask Rufus de Audionautix está licenciada sob uma licença Creative Commons Attribution (https://creativecommons.org/licenses/by/4.0/) Artista: http://audionautix.com/