In: Computer Science
Design a class named Account (put it in a package named accountspackages) with the following UML diagram:
Account |
-customerID: int -customerName: String -balance: double |
+setCustomerID(int): void +setCustomerName(String): void +setBalance(double):void +getCustomerID(): int +getCustomerName(): String +getBalance(): double +deposit(double): void +withdraw(double): void +printInformation():void |
The method withdraw(double) withdraws a specified amount from the account if the amount is less than or equal the balance, otherwise the method prints the message:
Sorry! The account does not have sufficient funds.
The method printInformation() prints:
the customerName, the customerID, and the account balance.
Test the Account class by running the following code
(You are not allowed to modify this code):
package accountsdemocom;
public class AccountDemo{
public static void main(String[] args) {
Account accountOne = new Account();
Account accountTwo = new Account();
accountOne.setCustomerID(78654);
accountOne.setCustomerName("John Smith");
accountOne.deposit(10000.0);
accountOne.withdraw(25000.0);
accountTwo.setCustomerID(76234);
accountTwo.setCustomerName("Jennifer Harris");
accountOne.deposit(2000.0);
accountOne.withdraw(150.0);
accountOne.printInformation();
accountTwo.printInformation();
accountOne.withdraw(250.0);
accountOne.getBalance();
}
}
Account.java
package accountspackages;
public class Account {
private int customerID;
private String customerName;
private double balance;
public Account() {
}
public void setCustomerID(int id) {
customerID = id;
}
public void setCustomerName(String name)
{
customerName =
name;
}
public void setBalance(double balance)
{
this.balance =
balance;
}
public int getCustomerID() {
return customerID;
}
public String getCustomerName() {
return
customerName;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <=
balance) {
balance -= amount;
} else {
System.err.println("Sorry! The account does not have sufficient
funds.(ID: " + customerID + ")");
}
}
public void printInformation() {
System.out.println("ID:
" + customerID + " Name: " + customerName + " Balance: " +
balance);
}
}
AccountDemo.java
import accountspackages.Account;
public class AccountDemo {
public static void main(String[] args) {
Account accountOne = new
Account();
Account accountTwo = new
Account();
accountOne.setCustomerID(78654);
accountOne.setCustomerName("John Smith");
accountOne.deposit(10000.0);
accountOne.withdraw(25000.0);
accountTwo.setCustomerID(76234);
accountTwo.setCustomerName("Jennifer Harris");
accountOne.deposit(2000.0);
accountOne.withdraw(150.0);
accountOne.printInformation();
accountTwo.printInformation();
accountOne.withdraw(250.0);
accountOne.getBalance();
}
}