Question

Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount....

Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount.
Implement four methods:
• public void deposit(double amount, String account)
• public void withdraw(double amount, String account)
• public void transfer(double amount, String account)
• public double getBalance(String account)
Here the account string is "S" or "C". For the deposit or withdrawal, it indicates which account is affected. For a transfer, it indicates the account from which the money is taken; the money is automatically transferred to the other account.
Change the test class to perform the actions using the Portfolio class.

I need a separate BankAccount class that will work with my Portfolio class.

public class Portfolio

{

private BankAccount checking;

private BankAccount savings;

public Portfolio(double aChecking, double aSavings)

{

checking = new BankAccount(aChecking);

savings = new BankAccount(aSavings);

}

public void deposit(double amount, String account)

{

if (account.equals("S"))

{

savings.deposit(amount);

}

else if (account.equals("C"))

{

checking.deposit(amount);

}

}

public void withdraw(double amount, String account)

{

if (account.equals("S"))

{

savings.withdraw(amount);

}

else if (account.equals("C"))

{

checking.withdraw(amount);

}

}

public void transfer(double amount, String account)

{

if (account.equals("S"))

{

savings.withdraw(amount);

checking.deposit(amount);

}

else if (account.equals("C"))

{

checking.withdraw(amount);

savings.deposit(amount);

}

}

public double getBalance(String account)

{

double result = -1;

if (account.equals("S"))

{

result = savings.getBalance();

}

else if (account.equals("C"))

{

result = checking.getBalance();

}

return result;

}

}

class Tester {

public static void main(String[] args)

{

double checking = 100;

double savings = 50;

Portfolio portfolio1 = new Portfolio(checking, savings);

portfolio1.deposit(100, "C");

portfolio1.withdraw(300, "C");

portfolio1.transfer(20, "S");

System.out.println("Savings: " + portfolio1.getBalance("S"));

System.out.println("Checking: " + portfolio1.getBalance("C"));

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*******************************Portfolio.java****************/


/**
* The Class Portfolio.
*/
public class Portfolio

{

   /** The checking. */
   private BankAccount checking;

   /** The savings. */
   private BankAccount savings;

   /**
   * Instantiates a new portfolio.
   *
   * @param aChecking the a checking
   * @param aSavings the a savings
   */
   public Portfolio(double aChecking, double aSavings)

   {

       checking = new BankAccount(aChecking);

       savings = new BankAccount(aSavings);

   }

   /**
   * Deposit.
   *
   * @param amount the amount
   * @param account the account
   */
   public void deposit(double amount, String account)

   {

       if (account.equals("S"))

       {

           savings.deposit(amount);

       }

       else if (account.equals("C"))

       {

           checking.deposit(amount);

       }

   }

   /**
   * Withdraw.
   *
   * @param amount the amount
   * @param account the account
   */
   public void withdraw(double amount, String account)

   {

       if (account.equals("S"))

       {

           savings.withdraw(amount);

       }

       else if (account.equals("C"))

       {

           checking.withdraw(amount);

       }

   }

   /**
   * Transfer.
   *
   * @param amount the amount
   * @param account the account
   */
   public void transfer(double amount, String account)

   {

       if (account.equals("S"))

       {

           savings.withdraw(amount);

           checking.deposit(amount);

       }

       else if (account.equals("C"))

       {

           checking.withdraw(amount);

           savings.deposit(amount);

       }

   }

   /**
   * Gets the balance.
   *
   * @param account the account
   * @return the balance
   */
   public double getBalance(String account)

   {

       double result = -1;

       if (account.equals("S"))

       {

           result = savings.getBalance();

       }

       else if (account.equals("C"))

       {

           result = checking.getBalance();

       }

       return result;

   }

}

/****************************BankAccount.java*******************/

/**
* The Class BankAccount.
*/
public class BankAccount {

   /** The balance. */
   private double balance;

   /**
   * Instantiates a new bank account.
   *
   * @param balance the balance
   */
   public BankAccount(double balance) {
       super();
       this.balance = balance;
   }

   /**
   * Deposit.
   *
   * @param amount the amount
   */
   public void deposit(double amount) {

       this.balance += amount;
   }

   /**
   * Withdraw.
   *
   * @param amount the amount
   */
   public void withdraw(double amount) {

       if (amount < balance)
           this.balance -= amount;
   }

   /**
   * Gets the balance.
   *
   * @return the balance
   */
   public double getBalance() {

       return balance;
   }

}
/*****************************TestPortfolio.java********************/


public class TestPortfolio {
   public static void main(String[] args)

   {

       double checking = 100;

       double savings = 50;

       Portfolio portfolio1 = new Portfolio(checking, savings);

       portfolio1.deposit(100, "C");

       portfolio1.withdraw(300, "C");

       portfolio1.transfer(20, "S");

       System.out.println("Savings: " + portfolio1.getBalance("S"));

       System.out.println("Checking: " + portfolio1.getBalance("C"));

   }
}
/*********************output*********************/

Savings: 30.0
Checking: 220.0
2 Console X <terminated> TestPortfolio [Java Application] C:\Pru Savings: 30.0 Checking: 220.0

Please let me know if you have any doubt or modify the answer, Thanks :)

Add a comment
Know the answer?
Add Answer to:
Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • C++ Please create the class named BankAccount with the following properties below: // The BankAccount class...

    C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (name & ID), keeps track of a user's available balance. // It also keeps track of how many transactions (deposits and/or withdrawals) are made. public class BankAccount { Private String name private String id; private double balance; private int numTransactions; // Please define method definitions for Accessors and Mutator functions below Private member variables string getName(); // No set...

  • The code is attached below has the Account class that was designed to model a bank account.

    IN JAVAThe code is attached below has the Account class that was designed to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. I need creat program using the 2 java programs.Create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.I need to write a test program that creates objects of Account,...

  • java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

  • [JAVA] < Instruction> You are given two classes, BankAccount and ManageAccounts ManageAccounts is called a Driver...

    [JAVA] < Instruction> You are given two classes, BankAccount and ManageAccounts ManageAccounts is called a Driver class that uses BankAccount. associate a member to BankAcount that shows the Date and Time that Accounts is created. You may use a class Date. To find API on class Date, search for Date.java. Make sure to include date information to the method toString(). you must follow the instruction and Upload two java files. BankAccount.java and ManageAccounts.java. -------------------------------------------------------------------------- A Bank Account Class File Account.java...

  • java code ========= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ========= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

  • Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems....

    Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems. You have to do both of your java codes based on the two provided Java codes. Create one method for depositing and one for withdrawing. The deposit method should have one parameter to indicate the amount to be deposited. You must make sure the amount to be deposited is positive. The withdraw method should have one parameter to indicate the amount to be withdrawn...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT