This is assignment 2 investment class Create and submit java source code for tra
ID: 3813863 • Letter: T
Question
This is assignment 2 investment class
Create and submit java source code for transforming homework 2 to include arrays or arrays list. classes. One with the main method and the other as an investment class.
Problem description:
Revise homework 2 by using an array or Array List to track at least 10 different investments. Please use reasonable investment and commission prices. For example, recent of investment price for some of the technology companies are listed below:
Investment Stock Symbol
Price
AAPL
139.855
MSFT
64.88
AMZN
853.66
FB
136.911
GOOG
834.7137
GOOGL
856.8
CMCSA
37.63
INTC
35.77
CSCO
34.47
AMGN
179.51
For the commission rate, it is usually between $5 to $100 at the time of buying or selling:
Make sure that your project consists of at least two classes. One class for the main method (named InvestmentTest) and the other is an investment class. and an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below.
The Investment class has the following members:
At least six private fields (instance variables) to store an Investment name and or symbol, number of shares, buying price, selling price, and buying commission and sales commission.
Write set and get methods to access each of the private fields
“InvestmentSummary” method to print Investment Information including the result of
Create 10 different objects of the investment class and store the objects into an array or an array list.
Create an object of java.util.Scanner to input the information about each investment, one at a time form the keyboard, and track the information in the proper Investment objects by placing calls to the proper set methods for each of the two objects.
When invoking the InvestmentSummary method, use the get method found in the Investment class to access the private members.
Print all investments information including gain or loss using the string %s specifier.
Investment Stock Symbol
Price
AAPL
139.855
MSFT
64.88
AMZN
853.66
FB
136.911
GOOG
834.7137
GOOGL
856.8
CMCSA
37.63
INTC
35.77
CSCO
34.47
AMGN
179.51
public class Investment t private String investmentName; private int numShares private double buyPrice; private double sellPrice; private double buyComm; private double saleComm; protected void setName (String s investmentName s; protected String getName return investmentName; protected void setshares (int ns) num Shares 2- nS protected int getShares return numShares; protected void setBuyPrice(double bP) buy Price bP; protected double getBuyPrice return buy Price; protected void setSellPrice(double sP) sell Price SP protected double getSellPrice() return sellPrice; protected void setBuyComm(double bC) buy Comm J bC protected double getBuyComm return buy Comm; protected void setSalecomm (double sc) sale Comm sc;Explanation / Answer
public class Investment {
private String investmentName;
private int numOfShares;
private double buyPrice;
private double sellPrice;
private double buyCommission;
private double salesCommission;
public String getInvestmentName() {
return investmentName;
}
public void setInvestmentName(String investmentName) {
this.investmentName = investmentName;
}
public int getNumOfShares() {
return numOfShares;
}
public void setNumOfShares(int numOfShares) {
this.numOfShares = numOfShares;
}
public double getBuyPrice() {
return buyPrice;
}
public void setBuyPrice(double buyPrice) {
this.buyPrice = buyPrice;
}
public double getSellPrice() {
return sellPrice;
}
public void setSellPrice(double sellPrice) {
this.sellPrice = sellPrice;
}
public double getBuyCommission() {
return buyCommission;
}
public void setBuyCommission(double buyCommission) {
this.buyCommission = buyCommission;
}
public double getSalesCommission() {
return salesCommission;
}
public void setSalesCommission(double salesCommission) {
this.salesCommission = salesCommission;
}
public void investmentSummary() {
System.out.printf(
"The investment name is %s and profit is %2f ",
getInvestmentName(),
profit(getNumOfShares(), getBuyPrice(), getBuyCommission(),
getSellPrice(), getSalesCommission()));
}
public static double profit(int noOfShares, double buyPrice,
double buyCommPrice, double sellPrice, double sellCommPrice) {
return ((noOfShares * buyPrice) - sellCommPrice)
- ((noOfShares * sellPrice) + buyCommPrice);
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Investment i1 = new Investment();
Investment i2 = new Investment();
Investment i3 = new Investment();
Investment i4 = new Investment();
Investment i5 = new Investment();
Investment i6 = new Investment();
Investment i7 = new Investment();
Investment i8 = new Investment();
Investment i9 = new Investment();
Investment i10 = new Investment();
List<Investment> listOfInvestments = new ArrayList<Investment>();
listOfInvestments.add(i1);
listOfInvestments.add(i2);
listOfInvestments.add(i3);
listOfInvestments.add(i4);
listOfInvestments.add(i5);
listOfInvestments.add(i6);
listOfInvestments.add(i7);
listOfInvestments.add(i8);
listOfInvestments.add(i9);
listOfInvestments.add(i10);
for (Investment investment : listOfInvestments) {
System.out.println("Please enter the investment name");
investment.setInvestmentName(scanner.next());
System.out
.println("Please enter the number of stocks you want to buy or sell");
investment.setNumOfShares(scanner.nextInt());
System.out
.println("Please enter the price you paid for each stock");
investment.setBuyPrice(scanner.nextDouble());
System.out.println("Please enter the purchase commission");
investment.setBuyCommission(scanner.nextDouble());
System.out
.println("Please enter the price on which you sold each stock");
investment.setSellPrice(scanner.nextDouble());
System.out.println("Please enter the sales commission");
investment.setSalesCommission(scanner.nextDouble());
// Now print the investment summary
investment.investmentSummary();
}
// close the scanner
scanner.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.