Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Designing User and Account Classes | Advanced Class Features
JavaScript Classes and OOP Foundations

bookDesigning User and Account Classes

Designing robust classes is a cornerstone of object-oriented programming. When you set out to create a User and an Account class, you need to consider which properties and behaviors belong to each, how to keep sensitive data safe, and how to leverage inheritance to avoid code duplication. Begin by identifying the shared and unique attributes of users and accounts. For instance, every user might have a name and email, while an account may have a balance and account number. To protect sensitive data, use encapsulation: mark fields as private and expose them only through controlled getters and setters. Inheritance allows you to extend the User class with an Account class, so that all account holders are also users, but with additional account-specific features. Static methods can provide utility functions, such as validating email formats or generating unique account numbers, which do not depend on individual instances. This planning ensures your classes are both reusable and secure.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
class User { // Private fields for encapsulation #name; #email; constructor(name, email) { this.#name = name; this.#email = email; } // Getter for name get name() { return this.#name; } // Setter for name set name(newName) { if (typeof newName === "string" && newName.trim() !== "") { this.#name = newName; } else { throw new Error("Name must be a non-empty string."); } } // Getter for email get email() { return this.#email; } // Setter for email with validation set email(newEmail) { if (User.isValidEmail(newEmail)) { this.#email = newEmail; } else { throw new Error("Invalid email format."); } } // Static method for email validation static isValidEmail(email) { // Simple regex for demonstration return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); } } class Account extends User { // Private fields for encapsulation #accountNumber; #balance; constructor(name, email, accountNumber, balance = 0) { super(name, email); this.#accountNumber = accountNumber; this.#balance = balance; } // Getter for accountNumber get accountNumber() { return this.#accountNumber; } // Getter for balance get balance() { return this.#balance; } // Method to deposit money deposit(amount) { if (amount > 0) { this.#balance += amount; } else { throw new Error("Deposit amount must be positive."); } } // Method to withdraw money withdraw(amount) { if (amount > 0 && amount <= this.#balance) { this.#balance -= amount; } else { throw new Error("Invalid withdrawal amount."); } } // Static method to generate a random account number static generateAccountNumber() { return "ACCT-" + Math.floor(Math.random() * 1000000); } } // Example usage: const user = new User("Alice", "alice@example.com"); console.log(user.name); // Alice const account = new Account( "Bob", "bob@example.com", Account.generateAccountNumber(), 100 ); console.log(account.name); // Bob console.log(account.accountNumber); // e.g., ACCT-123456 account.deposit(50); console.log(account.balance); // 150 account.withdraw(20); console.log(account.balance); // 130
copy
question mark

Which OOP principles are demonstrated in the User and Account class design above, and how do they contribute to the maintainability and security of your code?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 6.25

bookDesigning User and Account Classes

Scorri per mostrare il menu

Designing robust classes is a cornerstone of object-oriented programming. When you set out to create a User and an Account class, you need to consider which properties and behaviors belong to each, how to keep sensitive data safe, and how to leverage inheritance to avoid code duplication. Begin by identifying the shared and unique attributes of users and accounts. For instance, every user might have a name and email, while an account may have a balance and account number. To protect sensitive data, use encapsulation: mark fields as private and expose them only through controlled getters and setters. Inheritance allows you to extend the User class with an Account class, so that all account holders are also users, but with additional account-specific features. Static methods can provide utility functions, such as validating email formats or generating unique account numbers, which do not depend on individual instances. This planning ensures your classes are both reusable and secure.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
class User { // Private fields for encapsulation #name; #email; constructor(name, email) { this.#name = name; this.#email = email; } // Getter for name get name() { return this.#name; } // Setter for name set name(newName) { if (typeof newName === "string" && newName.trim() !== "") { this.#name = newName; } else { throw new Error("Name must be a non-empty string."); } } // Getter for email get email() { return this.#email; } // Setter for email with validation set email(newEmail) { if (User.isValidEmail(newEmail)) { this.#email = newEmail; } else { throw new Error("Invalid email format."); } } // Static method for email validation static isValidEmail(email) { // Simple regex for demonstration return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); } } class Account extends User { // Private fields for encapsulation #accountNumber; #balance; constructor(name, email, accountNumber, balance = 0) { super(name, email); this.#accountNumber = accountNumber; this.#balance = balance; } // Getter for accountNumber get accountNumber() { return this.#accountNumber; } // Getter for balance get balance() { return this.#balance; } // Method to deposit money deposit(amount) { if (amount > 0) { this.#balance += amount; } else { throw new Error("Deposit amount must be positive."); } } // Method to withdraw money withdraw(amount) { if (amount > 0 && amount <= this.#balance) { this.#balance -= amount; } else { throw new Error("Invalid withdrawal amount."); } } // Static method to generate a random account number static generateAccountNumber() { return "ACCT-" + Math.floor(Math.random() * 1000000); } } // Example usage: const user = new User("Alice", "alice@example.com"); console.log(user.name); // Alice const account = new Account( "Bob", "bob@example.com", Account.generateAccountNumber(), 100 ); console.log(account.name); // Bob console.log(account.accountNumber); // e.g., ACCT-123456 account.deposit(50); console.log(account.balance); // 150 account.withdraw(20); console.log(account.balance); // 130
copy
question mark

Which OOP principles are demonstrated in the User and Account class design above, and how do they contribute to the maintainability and security of your code?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 3
some-alt