Account. Java
To prepare an Account for employees using Java, you can create a simple Java program with classes that represent:
-
Employee
-
Account
(each employee has one)
This example includes:
-
Defining an
Employee
class. -
Defining an
Account
class. -
Creating and linking employee accounts in the
main()
method.
β 1. Account Class
public class Account {
private double balance;
public Account(double balance) {
if (balance > 0.0) {
this.balance = balance;
}
}
public void deposit(double amount) {
if (amount > 0.0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
β 2. Employee Class
public class Employee {
private String name;
private int employeeId;
private Account account;
public Employee(String name, int employeeId, double startingBalance) {
this.name = name;
this.employeeId = employeeId;
this.account = new Account(startingBalance); // Each employee has an account
}
public void depositToAccount(double amount) {
account.deposit(amount);
}
public void displayInfo() {
System.out.println(“Employee: ” + name);
System.out.println(“ID: ” + employeeId);
System.out.println(“Account Balance: $” + account.getBalance());
System.out.println();
}
}
β 3. Main Method β Test Employees and Accounts
public class EmployeeAccountTest {
public static void main(String[] args) {
// Create employees with initial balances
Employee emp1 = new Employee(“Alice”, 1001, 500.0);
Employee emp2 = new Employee(“Bob”, 1002, 300.0);
// Deposit money to their accounts
emp1.depositToAccount(200.0);
emp2.depositToAccount(150.0);
// Display employee and account info
emp1.displayInfo();
emp2.displayInfo();
}
}
π’ Sample Output
π§ Summary of Steps
Step | Action |
---|---|
1 | Create Account class for balance |
2 | Create Employee class with ID & name |
3 | Assign an Account to each employee |
4 | Create objects and use them in main() |
Other subjects
https://t.co/wQyQbLvwXk