This is my assignment and below I have the code that I have written so far for the program. I am struggling to use all the get methods in the test class as well as getting the program to print the values, specifically the fractions correctly. Can someone please help it would greatly appreciated if someone could make sure my main method is correct and help finishing the test class thank you.
I. The StockPortfolio Class
Instance variables of the class will include
- The name of the company
- The number of shares held
- The dollar portion of the share price
- The eighths portion of the share price To receive credit for this assignment, all instance variables except for the company name must be type int
Naturally, your class will have a constructor that initializes the instance variables to arguments passed when the object is created
Other methods of the class will include
- “Get” methods for each of the instance variables
- A method to modify the stock price. This method will have 2 int parameters: • The change in the dollar portion of the price • The change in the eighths portion of the price
- A method to compute and return the portfolio value as a decimal
II. The Test Class (aka: “Client Code”)
Your test class will do the following:
- Have the userenter the data (company name, number of shares, dollars, eighths)
- Create a StockPortfolio object
- Call the “get” methods to access the object data, and then print it
Get and print the portfolio value as a decimal
Have the user enter the change in the share price - in dollars and eighths - and print it
- Get and print the updated share price
- Get and print the updated portfolio value
- Have the user enter another change in the share price - in dollars and eighths - and print it
- Get and print the updated share price
- Get and print the updated portfolio value
Here is some sample output (assuming the price changes were +7 2/8 and +4 4/8):
Company: The Ramjac Corporation
Shares Held: 100
Opening Price per Share: 37 5/8
Opening Portfolio Value: $3762.50
Change in Share Price: 7 2/8
Closing Price per Share: 44 7/8
Closing Portfolio Value: $4487.50
Change in Share Price: 4 4/8
Closing Price per Share: 49 3/8
Closing Portfolio Value: $4937.50
public class StockPortfolio
{
private String name;
private int shares;
private int DollarPortion;
private int EighthPortion;
public StockPortfolio(String name, int shares, int DollarPortion, int EighthPortion)
{
this.name = name;
this.shares = shares;
this.DollarPortion = DollarPortion;
this.EighthPortion = EighthPortion;
}
public String getName()
{
return name;
}
public int getShare()
{
return shares;
}
public int getDollar()
{
return DollarPortion;
}
public int getEighth()
{
return EighthPortion;
}
public void setStockPrice(int DollarPortion, int EighthPortion)
{
this.DollarPortion = DollarPortion;
this.EighthPortion = EighthPortion;
}
public double getPortfolio()
{
return (shares + DollarPortion + EighthPortion);
}
}
Test Class
import javax.swing.JOptionPane;
public class StockPortfolioTest
{
public static void main(String[] args)
{
String name = (JOptionPane.showInputDialog(null, "What is the name of the Company "));
int shares = Integer.parseInt(JOptionPane.showInputDialog(null, " What is the number of shares? "));
int DollarPortion = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the number of dollars? "));
int EighthPortion = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the number of eighths? "));
System.out.println(" Company Name = " + name);
System.out.println(" Shares Held = " + shares);
System.out.println("Opening Price per Share = " + DollarPortion +(EighthPortion));
}
}
Aucun commentaire:
Enregistrer un commentaire