In: Computer Science
Look at Stock and StockDriver. Get these programs to run. We will be keeping track of both our cost and the fair market value of our shares of stock.
import java.text.*;
public class Stock {
// this line is used to output money but this is
NOT considered a field
NumberFormat money =
NumberFormat.getCurrencyInstance();
// create five fields - a String called stockName,
a integer called custID
// a integer called numShares, a double named
costPaid,and a double called currentValue
// Do not forget the access modifier
public String stockName;
public int custID;
public int numShares;
public double costPaid;
public double currentValue;
// create an empty and full
constructor
public Stock()
{
}
public Stock(String s, int c, int n, double co, double
cv)
{
stockName = s;
custID = c;
numShares = n;
costPaid = co;
currentValue = cv;
}
public String toString() {
return custID + " owns " +
numShares + " shares of " + stockName
+ " worth " + money.format(currentValue) + " per
share. You paid "+
money.format(costPaid) + " per share.";
}
// write a calcCost method that takes no parameters
and returns a double
// Have it return the total cost you paid for the
stock
// write a method named calcCurrentValue that takes no
parameters and returns a double
// that is the current value of the stock.
// write your getters and setters. You should at
least once try to create
// these by hand so you understand them
// (even though Eclipse will write these for
you)
// (DO NOT create one for money - it really is not a
field)
}
import java.text.*;
import java.util.*;
public class StockDriver {
public static void main(String[] args) {
boolean more = true;
Scanner scan = new
Scanner(System.in);
NumberFormat money =
NumberFormat.getCurrencyInstance();
while (more)
{
/* NOTE : in the
next four comments, you will need two lines per comment
one for the
prompt and one for the answer. Make certain your choice
of data types
for each is the same as the associated field in Stock*/
// ask for the
Customer ID and store the answer in the variable named
custNum
// ask for the
name of the stock and store the answer in the variable named
type
// ask for the
number of shares and store the answer in the variable named
num
// ask for how
much was paid per share and store the answer in the variable named
paid
// ask for the
current value per share and store the answer in the variable named
current
// create a
Stock instance named stock by calling the full constructor
// call the
calcCost method on the stock instance (-- NOTE - this is in the
Stock class!!)
//
and store the answer in the double variable grossAmt
// call the
calcCurrentValue method on the stock instance and store the answer
in the double
// variable
named currentAmt
// print out the
toString() method on the stock instance
System.out.println("The total cost of the stock is " +
money.format(grossAmt) +
" and the current value is "
+ money.format(currentAmt));
System.out.println ("More stocks to calculate? (true or
false)");
more =
scan.nextBoolean();
}
}
}
Sample output:
Customer id: 1234
Name of stock: IBM
Number of shares: 200
Price paid per share: 60.00
Current value per share: 65.00
1234 owns 200 shares of IBM worth $65.00 per share. You paid $60.00 per share.
The total cost of the stock is $12,000.00 and the current value is $13,000.00
More stocks to calculate? (true or false)
true
Customer id: 3434
Name of stock: GOOG
Number of shares: 120
Price paid per share: 45.00
Current value per share: 120.00
3434 owns 120 shares of GOOG worth $120.00 per share. You paid $45.00 per share.
The total cost of the stock is $5,400.00 and the current value is $14,400.00
More stocks to calculate? (true or false)
false
Stock.java
import java.text.*;
public class Stock {
// this line is used to output money but this is NOT considered
a field
NumberFormat money = NumberFormat.getCurrencyInstance();
// create five fields - a String called stockName, a integer
called custID
// a integer called numShares, a double named costPaid,and a double
called currentValue
// Do not forget the access modifier
public String stockName;
public int custID;
public int numShares;
public double costPaid;
public double currentValue;
// create an empty and full constructor
public Stock()
{
}
public Stock(String s, int c, int n, double co, double cv)
{
stockName = s;
custID = c;
numShares = n;
costPaid = co;
currentValue = cv;
}
public String toString() {
return custID + " owns " + numShares + " shares of " +
stockName
+ " worth " + money.format(currentValue) + " per share. You paid
"+
money.format(costPaid) + " per share.";
}
// write a calcCost method that takes no parameters and returns
a double
// Have it return the total cost you paid for the stock
public double calcCost(){
return costPaid * numShares;
}
// write a method named calcCurrentValue that takes no
parameters and returns a double
// that is the current value of the stock.
public double calcCurrentValue(){
return currentValue * numShares;
}
// write your getters and setters. You should at least once try
to create
// these by hand so you understand them
// (even though Eclipse will write these for you)
// (DO NOT create one for money - it really is not a field)
public String getStockName(){
import java.text.*;
public class Stock {
// this line is used to output money but this is NOT considered
a field
NumberFormat money = NumberFormat.getCurrencyInstance();
// create five fields - a String called stockName, a integer
called custID
// a integer called numShares, a double named costPaid,and a double
called currentValue
// Do not forget the access modifier
public String stockName;
public int custID;
public int numShares;
public double costPaid;
public double currentValue;
// create an empty and full constructor
public Stock()
{
}
public Stock(String s, int c, int n, double co, double cv)
{
stockName = s;
custID = c;
numShares = n;
costPaid = co;
currentValue = cv;
}
public String toString() {
return custID + " owns " + numShares + " shares of " +
stockName
+ " worth " + money.format(currentValue) + " per share. You paid
"+
money.format(costPaid) + " per share.";
}
// write a calcCost method that takes no parameters and returns
a double
// Have it return the total cost you paid for the stock
public double calcCost(){
return costPaid * numShares;
}
// write a method named calcCurrentValue that takes no
parameters and returns a double
// that is the current value of the stock.
public double calcCurrentValue(){
return currentValue * numShares;
}
// write your getters and setters. You should at least once try
to create
// these by hand so you understand them
// (even though Eclipse will write these for you)
// (DO NOT create one for money - it really is not a field)
public String getStockName(){
return stockName;
}
public int getCustID(){
return custID;
}
public int getNumShares(){
return numShares;
}
public double getCostPaid(){
return costPaid;
}
public double getCurrentValue(){
return currentValue;
}
public void setStockName(String stockName){
this.stockName = stockName;
}
public void setCustID(int custID){
this.custID = custID;
}
public void setNumShares(int numShares){
this.numShares = numShares;
}
public void setCostPaid(double costPaid){
this.costPaid = costPaid;
}
public void setCurrentValue(double currentValue){
this.currentValue = currentValue;
}
}
return stockName;
}
public int getCustID(){
return custID;
}
public int getNumShares(){
return numShares;
}
public double getCostPaid(){
return costPaid;
}
public double getCurrentValue(){
return currentValue;
}
public void setStockName(String stockName){
this.stockName = stockName;
}
public void setCustID(int custID){
this.custID = custID;
}
public void setNumShares(int numShares){
this.numShares = numShares;
}
public void setCostPaid(double costPaid){
this.costPaid = costPaid;
}
public void setCurrentValue(double currentValue){
this.currentValue = currentValue;
}
}
StockDriver.java
import java.text.*;
import java.util.*;
public class StockDriver {
public static void main(String[] args) {
boolean more = true;
Scanner scan = new Scanner(System.in);
NumberFormat money = NumberFormat.getCurrencyInstance();
while (more)
{
/* NOTE : in the next four comments, you will need two lines per
comment
one for the prompt and one for the answer. Make certain your
choice
of data types for each is the same as the associated field in
Stock*/
// ask for the Customer ID and store the answer in the variable
named custNum
System.out.print("Customer id: ");
int custID = scan.nextInt();
//extra scan.nextLine() added to read the remaining line of the
cutomer ID.
scan.nextLine();
// ask for the name of the stock and store the answer in the
variable named type
System.out.print("Name of stock: ");
String stockName = scan.nextLine();
// ask for the number of shares and store the answer in the
variable named num
System.out.print("Number of shares: ");
int numShares = scan.nextInt();
// ask for how much was paid per share and store the answer in the
variable named paid
System.out.print("Price paid per share: ");
double costPaid = scan.nextDouble();
// ask for the current value per share and store the answer in the
variable named current
System.out.print("Current value per share: ");
double currentValue = scan.nextDouble();
// create a Stock instance named stock by calling the full
constructor
Stock stock = new Stock(stockName, custID, numShares, costPaid,
currentValue);
// call the calcCost method on the stock instance (-- NOTE - this
is in the Stock class!!)
// and store the answer in the double variable grossAmt
double grossAmt = stock.calcCost();
// call the calcCurrentValue method on the stock instance and store
the answer in the double
// variable named currentAmt
double currentAmt = stock.calcCurrentValue();
// print out the toString() method on the stock instance
System.out.println(stock);
System.out.println("The total cost of the stock is " +
money.format(grossAmt) +
" and the current value is " + money.format(currentAmt));
System.out.println ("More stocks to calculate? (true or
false)");
more = scan.nextBoolean();
}
}
}
-----------------------------------------------------------------------------------------------------------------------
The above is the complete code to the problem. It consists of two files Stock.java and StockDriver.java which should be kept int the same directory.
Output of the above code:
note: In my case it is showing currency as rupees because I am from India. However it will show the currency according to the region\country where you live.
-----------------------------------------------------------------------------------------------------
Hope this helps. If you like the answer, please upvote. Cheers!!