Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create Account class Implement private fields with public setters and getters in

ID: 3747730 • Letter: C

Question

Create Account class

Implement private fields with public setters and getters in the Account class

AccountNumber - should allow only for 20 digits, including leading zeros, but NO LETTERS - example value: "00045678493857683928".   Your setter should set empty if validation fails.  - "Numbers" allowing for leading zeros are really just strings. If less than 20 digits or more than 20 digits set to empty string

AccountType - "Savings" or "Checking" - no other types or string should be accepted in this setter. If I pass "MoneyMarket" the setter should default this property to empty. Casing can be ignored - for example, "SAVINGS" and "savings" can be accepted. I will accept strings but can also be enum.

AccountBalance - How much money is in the account. Balance should never be less than zero. If your setter allows for setting a value less than zero you will lose points. You should check the value first, and if it is less than zero, just set the balance to zero.

Implement the following public methods (behaviors) in the Account class:

Default constructor - no arguments but the properties are initialized to non-null values.

Parameterized constructor - arguments for all properties can be passed (AccountNumber, AccountType, AccountBalance)

ToString - return a formatted string with labels for each of the following: account number, account type, and balance. AccountBalance should be formatted with a $ and decimals. This should be in a single string on 1 line.

                                - ex: "Account number: 11111111111111111111 Account type: Checking Account Balance: $500.00"

Equals - accept an object as a parameter, cast it to an Account type, and compare the following

                                - Whether the Account number, Account type, and Account balance match. If they match return true, else return false

- Ignore mixed casing on Account type - Use ToUpper or ToLower to ignore casing

                                "CHECKING" should match "checking", etc.

Explanation / Answer

Please find the below Account Class and have also written main method if u dont want you can remove that

//Account.java

public class Account {

//private attributes

private String accountNo;

private String accountType;

private double accountBalance;

//default constructor

public Account() {

this.accountBalance=0d;

this.accountNo="";

this.accountType="";

}

//parameterized constructor

public Account(String ac_no,String ac_type,double bal) {

setAccountNo(ac_no);

setAccountBalance(bal);

setAccountType(ac_type);

}

//setting account type

public void setAccountNo(String ac_no) {

//chcking if string contains only num dig and have length equal to 20

if(ac_no.matches("[0-9]+") && ac_no.length()==20)

this.accountNo=ac_no;

else {

this.accountNo="";

}

}

public void setAccountType(String ac_type) {

if(ac_type.equalsIgnoreCase("savings")||ac_type.equalsIgnoreCase("checking")) {

this.accountType=ac_type;

}

else {

this.accountType="";

}

}

public void setAccountBalance(double bal) {

if(bal<0) {

this.accountBalance=0d;

}

else {

this.accountBalance=bal;

}

}

//getter

public String getAccountNo() {

return accountNo;

}

public String getAccountType() {

return accountType;

}

public double getAccountBalance() {

return accountBalance;

}

@Override

public boolean equals(Object obj) {

if(obj==null)return false;

Account other = (Account) obj;

if (accountBalance != other.accountBalance)

return false;

else if (!accountNo.equals(other.accountNo))

return false;

else if (!accountType.toLowerCase().equals(other.accountType.toLowerCase()))

return false;

return true;

}

//tostring method for printing object

@Override

public String toString() {

return String.format("Account No: %s "

+ "Account Type: %s "

+ "Balance: $%s ", this.accountNo,this.accountType,this.accountBalance);

}

//main method for testing

//main method start

public static void main(String[] args) {

Account a1 = new Account("00002378912349086890","savings",456.23);

System.out.println(a1);

Account a2 = new Account("00002378912349086890","Savings",456.23);

System.out.println(a2);

System.out.println("Accout 1 equal Account 2?: "+a1.equals(a2));

}

//main method end

}//end of account class

//OUT

Account No: 00002378912349086890
Account Type: savings
Balance: $456.23

Account No: 00002378912349086890
Account Type: Savings
Balance: $456.23

Accout 1 equal Account 2?: true