// Example of encapsulation in a Java class
public class Account {
private double balance;
public Account(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
public double getBalance() {
return balance;
}
}
// Testing the Account class
public class AccountTest {
private Account account;
@Before
public void setUp() {
account = new Account(100.0); // Initialize with balance of 100
}
@Test
public void testDeposit() {
account.deposit(50.0);
assertEquals(150.0, account.getBalance(), 0.001);
}
@Test
public void testWithdraw() {
account.withdraw(30.0);
assertEquals(70.0, account.getBalance(), 0.001);
}
@Test
public void testWithdrawTooMuch() {
account.withdraw(150.0);
assertEquals(100.0, account.getBalance(), 0.001);
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?