MobileAccount.java

← Back to Course
Java Source File Download
public class MobileAccount {
private String ownerName;
private String phoneNumber;
private double balance;
private int pin;

public String getOwnerName() {
    return ownerName;
}

public void setOwnerName(String ownerName) {
    this.ownerName = ownerName;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}

public int getPin() {
    return pin;
}

public void setPin(int pin) {
    this.pin = pin;
}

public MobileAccount(String ownerName, String phoneNumber, double balance, int pin) {
    this.ownerName = ownerName;
    this.phoneNumber = phoneNumber;
    this.balance = balance;
    this.pin = pin;
}

//deposit(double amount) adds the amount to the balance.
public double deposit(double amount) {
    balance += amount;
    return balance;
}

//withdraw(double amount, int enteredPin) deducts from the balance only if the pin matches and sufficient funds exist; otherwise prints an appropriate error.

public double withdraw(double amount, int enteredPin) {
    if (enteredPin != pin) {
        System.out.println("Error: Incorrect PIN.");
        return balance;
    }
    if (amount > balance) {
        System.out.println("Error: Insufficient funds.");
        return balance;
    }
    balance -= amount;
    return balance;
}

    public static void main(String[] args) {
        MobileAccount account = new MobileAccount("John Doe", "123-456-7890", 100.0, 1234);
        System.out.println("Initial Balance: " + account.getBalance());
        
        account.deposit(50.0);
        System.out.println("Balance after deposit: " + account.getBalance());
        
        account.withdraw(30.0, 1234);
        System.out.println("Balance after withdrawal: " + account.getBalance());
        
        account.withdraw(150.0, 1234); // Should print insufficient funds error
        account.withdraw(30.0, 1111); // Should print incorrect PIN error
    }
    
}