In: Computer Science
PUT IN JAVA PROGRAMMING
The StockB class:
Design a class named StockB that contains the following
properties:
• Name of company
• Number of shares owned
• Value of each share
• Total value of all shares
and the following operations:
• Acquire stock in a company
• Buy more shares of the same stock
• Sell stock
• Update the per-share value of a stock
• Display information about the holdings
• The StockB class should have the proper constructor(s).
• Create/write a client class (program) to test and use your StockB
class. In the client program,
you need to create objects of the StockB type and use those objects
to perform some meaningful and valid stock transactions.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// StockB.java
public class StockB {
private String name;
private int sharesOwned;
private double perShareCost;
private double totalSharesCost;
/**
* @param name
* @param sharesOwned
* @param perShareCost
*/
public StockB(String name, int sharesOwned, double
perShareCost) {
this.name = name;
this.sharesOwned =
sharesOwned;
this.perShareCost =
perShareCost;
this.totalSharesCost =
sharesOwned*perShareCost;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the sharesOwned
*/
public int getSharesOwned() {
return sharesOwned;
}
/**
* @param sharesOwned
* the sharesOwned to set
*/
public void setSharesOwned(int sharesOwned) {
this.sharesOwned =
sharesOwned;
}
/**
* @return the perShareCost
*/
public double getPerShareCost() {
return perShareCost;
}
/**
* @param perShareCost
* the perShareCost to set
*/
public void setPerShareCost(double perShareCost)
{
this.perShareCost =
perShareCost;
}
/**
* @return the totalSharesCost
*/
public double getTotalSharesCost() {
return totalSharesCost;
}
public void buyMoreStock(int
shares)
{
this.sharesOwned+=shares;
}
public double sellStock(double sellPrice)
{
double totSellAmt=0.0;
totSellAmt=sellPrice*sharesOwned;
return totSellAmt;
}
public void updatePreShareCost(double newPrice)
{
this.perShareCost=newPrice;
}
public void displayInfo()
{
System.out.println("Stock Name
:"+name);
System.out.println("Shares Owned
:"+sharesOwned);
System.out.println("Per Share Price
:$"+perShareCost);
System.out.println("Total Stock
Cost :$"+totalSharesCost);
}
}
=======================================
=======================================
// Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
/*
* Declaring variables
*/
String name;
int noOfShares;
double perShareCost;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
// Getting the input entered by
the user
System.out.print("Enter the Stock
name :");
name = sc.next();
System.out.print("Enter no of
shares owned :");
noOfShares = sc.nextInt();
System.out.print("Enter per share
cost :$");
perShareCost =
sc.nextDouble();
// Creating an instance of StockB
class
StockB sb=new StockB(name,
noOfShares, perShareCost);
sb.displayInfo();
// Getting the input entered by the user
System.out.print("Enter how many no
of shares you want to buy again :");
int moreNoOfShares =
sc.nextInt();
System.out.print("Enter new per
share cost :$");
double newPerShareCost =
sc.nextDouble();
// calling the methods
sb.buyMoreStock(moreNoOfShares);
sb.updatePreShareCost(newPerShareCost);
sb.displayInfo();
System.out.print("Enter selling
price of per share:$");
double sellPrice =
sc.nextDouble();
double
totBuyingAmount=sb.getTotalSharesCost();
double
totSellingAmount=sb.sellStock(sellPrice);
if(totSellingAmount>totBuyingAmount)
{
System.out.println("You got a profit of
$"+(totSellingAmount-totBuyingAmount));
}
else
{
System.out.println("You got a loss of
$"+(-(totSellingAmount-totBuyingAmount)));
}
}
}
========================================
========================================
output:
=====================Could you plz rate me well.Thank You