In: Computer Science
Java programming.
** Create Account class include:
1/ private long accountNumber;
2/ private String firstName;
3/ private String lastName;
4/ private double balance;
5/ public Account(long accountNumber, String firstName, String
lastName, double balance);
6/ get/set methods for all attributes.
7/ public boolean deposit(double amount);
• deposit only if amount is greater than 10 but less
than 200, add the deposit amount to
the currentbalance.
• if deposit is successful return true,
otherwise return false;
8/ public boolean withdrawal(double amount);
• withdraw money only if amount is at least
10 less than the balance, in other words,
do not let the balance go below
10.
• return true if the withdrawal is
successful, return false if
withdrawal is not successful.
** Create Test class include:
1/ import java.io.File;
2/ import java.io.FileNotFoundException;
3/ import java.util.Scanner;
4/ public static void main(String [] args) throws
FileNotFoundException{}.
5/ public static void displayAccounts(Account[] acc){}.
6/ public static void applyRandomBonus(KhalafallaAccount[]
acc){}.
The main method should do the following:
* Create an array of accounts of size 7.
* Read the file line by line and:
* Store the elements of a line into variables
* Use the variables to instantiate an account
* Populate (or add) the account into the array of accounts (note:
do not use
collections or any .add method , add elements by index as we have
learned)
* Loop to read the next line…
* At the end of your processing loop you will have an array of
accounts
* Send the array to displayAccounts method
* Then send the array to applyRandomBonus method
* Then send the array once more to displayAccounts, the updated
balance should be
reflected.
* *The displayAccounts method should do the following:
* Use a for each loop and the toString method of the account to
display the accounts, the
toString should include all elements, including balance.
* *The applyRandomBonus method should to the following:
* Use a for each loop to iterate through the array and deposit a
random bonus amount
(remember the Java random number generator) between 100 and 300
dollars… Call the
account deposit method.. the random number should be in your Test
class, not the
deposit method in the account class.
* As you generate a random deposit amount, if the deposit was
successful, print it out to
the console.
** Create data.txt include:
1/ Johnson Nell 98845255 28.00
2/ Kendrick Farwell 67854321 100.00
3/ Peter Jackson 55325678 400.00
4/ Layla Kerns 56789332 350.00
5/ London Parker 23412378 250.00
**OUTPUT:::
Account{accountNumber=98845255, firsName='Johnson',
lastName='Nell', balance= 250.0}
Account{accountNumber=67854321, firsName='Kendrick',
lastName='Farwell', balance= 340.0}
Account{accountNumber=55325678, firsName='Peter',
lastName='Jackson', balance= 100.0}
Account{accountNumber=56789332, firsName='Layla', lastName='Kerns',
balance= 350.0}
Account{accountNumber=23412378, firsName='London ',
lastName='Parker', balance= 450.0}
bonus:317
bonus:110
bonus:122
bonus:157
bonus:341
Account{accountNumber=98845255, firsName='Johnson',
lastName='Nell', balance= 567.0}
Account{accountNumber=67854321, firsName='Kendrick',
lastName='Farwell', balance= 450.0}
Account{accountNumber=55325678, firsName='Peter',
lastName='Jackson', balance= 222.0}
Account{accountNumber=56789332, firsName='Layla', lastName='Kerns',
balance= 762.0}
Account{accountNumber=23412378, firsName='London ',
lastName='Parker', balance= 791.0}
If you have any problem with the code feel free to comment.
Program
class Account{
//instance variables
private long accountNumber;
private String firstName;
private String lastName;
private double balance;
//constructor
public Account(long accountNumber, String firstName,
String lastName, double balance) {
this.accountNumber =
accountNumber;
this.firstName = firstName;
this.lastName = lastName;
this.balance = balance;
}
//all getters and setters
public long getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(long accountNumber)
{
this.accountNumber =
accountNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
//deposit method
public boolean deposit(double amount) {
if(amount > 10 && amount
< 200) {
balance+=amount;
return
true;
}
return true;
}
//withdraw method
public boolean withdraw(double amount) {
if(balance-amount > 10) {
balance -=
amount;
return
true;
}
return false;
}
@Override
public String toString() {
return "Account [accountNumber=" +
accountNumber + ", firstName=" + firstName + ", lastName=" +
lastName
+ ", balance=" + balance + "]";
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Test{
public static void main(String[] args)throws
FileNotFoundException {
//change the size to 7 as there was
only 5
//accounts provided in the text
file
//so i put array size 5
Account[] accounts = new
Account[5];
int index = 0;
Scanner sc = new Scanner(new
File("data.txt"));
long accountNumber;
String firstName, lastName;
double balance;
String line;
//reading the file line by
line
while(sc.hasNext()) {
line =
sc.nextLine();
String[] ar =
line.split(" ");
accountNumber =
Long.parseLong(ar[2]);
firstName =
ar[0];
lastName =
ar[1];
balance =
Double.parseDouble(ar[3]);
accounts[index++] = new Account(accountNumber, firstName, lastName,
balance);
}
sc.close();
displayAccount(accounts);
applyRandomBonus(accounts);
displayAccount(accounts);
}
//for displaying account
public static void displayAccount(Account[] acc)
{
for(Account i: acc) {
System.out.println(i);
}
}
//for giving random bonus to the accounts
public static void applyRandomBonus(Account[] acc)
{
for(Account i: acc) {
double bonus =
(int)(Math.random()*(300-100)+100);
if(i.deposit(bonus)) {
System.out.println("bonus:"+bonus);
}
}
}
}
Output