Question

In: Computer Science

Java programming. ** Create Account class include: 1/ private long   accountNumber; 2/ private String firstName; 3/...

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}


Solutions

Expert Solution

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


Related Solutions

java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement deposit(double amount) and withdraw(double amount) methods. If the amount is less than zero,...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement deposit(double amount) and withdraw(double amount) methods. If the amount is less than zero,...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement these methods: void deposit(double amount) and void withdraw(double amount). For both these methods,...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT