In: Computer Science
protected String
name;
protected ArrayList<Stock>
stocks;
protected double
balance;
+ Account ( String name, double balance) //stocks = new
ArrayList<Stock>() ;
+ get and set property for name; get method for balance
+ void buyStock(String symbol, int shares, double unitPrice )
//update corresponding balance and stocks ( stocks.add(new
Stock(…..)); )
+ deposit(double amount) : double //returns new
balance
+ withdraw(double amount) : double //returns new balance
+ toString() :
String
@Override
public String toString(){
StringBuffer str = new
StringBuffer("Stocks: ");
for (Stock s :
stocks){
str.append(s.toString() + " ");
}
return "Name: " + name +
", balance: " + balance +"\t" + str.toString();
}
can anyone convert this to java?
I have implemented the given model to the Java class. First let me write the complete code here.
Code:
import java.util.ArrayList;
class Stock
{
protected String symbol;
protected int shares;
protected double unitPrice;
Stock(String symbol, int shares, double unitPrice)
{
this.symbol = symbol;
this.shares = shares;
this.unitPrice = unitPrice;
}
}
public class Account
{
protected String name;
protected ArrayList<Stock> stocks;
protected double balance;
Account(String name, double balance)
{
this.name = name;
stocks = new ArrayList<Stock>();
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
void buyStock(String symbol, int shares, double unitPrice
)
{
balance = balance - shares*unitPrice;
stocks.add(new Stock(symbol, shares, unitPrice));
}
public double deposit(double amount)
{
balance = balance + amount;
return balance;
}
public double withdraw(double amount)
{
balance = balance - amount;
return balance;
}
@Override
public String toString(){
StringBuffer str = new StringBuffer("Stocks: ");
for (Stock s : stocks){
str.append(s.toString() + " ");
}
return "Name: " + name + ", balance: " + balance +"\t" +
str.toString();
}
}
Explanation:
Here, + indicates they are public methods. Account is a public method that indicates it is a constructor. So, the class name must be Account. This class has 3 protected variables as given here. So I added them along with the required get and set methods.
Then comes buyStocks() method. Here, it is mentioned that stock is added in the arrayList of type Stock so 'Stock' must be a class. So, I created a Stock class definition as per the details i could extract from given data. It will have 3 protected variables and a parameterized constructor. This constructor is called in buyStocks() so I have written the same as mentioned in the question.
It has also mentioned to update the balance. So, I thought for the same and concluded that if I am buying the stock in my account, then the account will be added with that stock but the balance will get deducted that was used to purchase the stock. So, to find the balance used to buy the stocks: multiply unit share price with number of shares. This will give me amount I spent for that stock and I am subtracting it from balance.
deposit() method is having return type double. It gives amount which was credited so we need to add that amount in the balance so that we get new balance and after that the new balance is returned by this method.
withdraw() method is also having return type double. It gives amount to be deducted from balance. So, we subtracted it from balance and got the new balance which is returned by the method.
The toString() method implementation is already given here. You do not need to change single line in it. So, the complete code and the explanation is this.
Do comment if there is any query. I will solve it for sure. Thank you. :)