Class 1: Account.java
Variables:
1. accountNumber (public)
2. accountName (public)
3. balance (private)
In Account.java
Functions:
1. Fully parametrized Constructor (to initialize class variables)
2. Getter (getBalance()) and Setter (setBalance()) for the float balance, must be greater than zero.
In Account.java
Methods:
Class 2: AccountTest.java
Ask user to input values off accountNo, accountName and balance.
Below is your code:
Account.java
public class Account {
public int accountNumber;
public String accountName;
private float balance;
// 1. Fully parametrized Constructor (to initialize class variables)
public Account(int accountNumber, String accountName, float balance) {
this.accountNumber = accountNumber;
this.accountName = accountName;
this.balance = balance;
}
// 2. Getter (getBalance()) and Setter (setBalance()) for the float balance,
// must be greater than zero.
public float getBalance() {
return balance;
}
public void setBalance(float balance) {
if (balance > 0)
this.balance = balance;
}
// checkBalance( ) to print CURRENT BALANCE.
public void checkBalance() {
System.out.println("Current Balance: " + this.balance);
}
// deposit(int added_amount) should add add_amount to balance
public void deposit(int added_amount) {
this.balance = this.balance + added_amount;
}
// withdraw(int withdraw_amount) should deduct withdraw_amount balance
public void withdraw(int withdraw_amount) {
this.balance = this.balance - withdraw_amount;
}
// display( ) will print all the information (account number, name,
// balance).
public void display() {
System.out.println("Account number: " + accountNumber
+ ", Account Name: " + accountName + ", Balance: "
+ this.balance);
}
}
AccountTest.java
import java.util.Scanner;
public class AccountTest {
public static void main(String[] args) {
int accountNo;
String accountName;
float balance;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Account Number: ");
accountNo = Integer.parseInt(sc.nextLine());
System.out.print("Enter Account Name: ");
accountName = sc.nextLine();
System.out.print("Enter Balance: ");
balance = Float.parseFloat(sc.nextLine());
Account account1 = new Account(accountNo, accountName, balance);
account1.display();
account1.checkBalance();
account1.deposit(40000);
account1.checkBalance();
account1.withdraw(12000);
account1.checkBalance();
}
}
Output
Enter Account Number: 123465879
Enter Account Name: Mark's Inventories
Enter Balance: 100000
Account number: 123465879, Account Name: Mark's Inventories,
Balance: 100000.0
Current Balance: 100000.0
Current Balance: 140000.0
Current Balance: 128000.0
Get Answers For Free
Most questions answered within 1 hours.