Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program with comments using the following instructions: Design a cl

ID: 3862231 • Letter: W

Question

Write a java program with comments using the following instructions:

Design a class called Stock to store information about a stock transaction :

company

number of shares

purchase price

current price.

Your class must include the following member functions:

Default constructor (to initialize all data in class)

Parameterized constructor (to initialize company, shares, purchase price, current price)

Set functions for company, shares, purchase price, current price

getValue function that calculates the value of the stock shares * (current price – purchase price)

get functions for company, shares, purchase price, current price

Create an array in main to represent all stocks in my portfolio. Give a maximum size of 10. You will need to keep a counter of how many stocks have been entered.

Create a menu and to process transactions as follows: Your loop that prompts for the user’s choice must be in main. You should try to write a static function (in the class with main) to process each option (instead of all code in main).

1 – Add stock Prompt for data and add to the array. (You only add one at a time).

2 – Display profit/loss Print each stock along with its value, and the total value of the portfolio

3 - Quit

SAMPLE OUTPUT:

Portfolio Calculator

1 - Add stock

2 - Show portfolio

3 - Quit

Enter your choice: 1

Enter company: Intel

Number of shares: 100

Purchase price: 29.50

Current price: 36.50

Portfolio Calculator

1 - Add stock

2 - Show portfolio

3 - Quit

Enter your choice: 1

Enter company: Apple

Number of shares: 50

Purchase price: 89.50

Current price: 116.25

Portfolio Calculator

1 - Add stock

2 - Show portfolio

3 - Quit

Enter your choice: 2

Portfolio Report

Company Value

Intel 700.00

Apple 1337.50

Total $ 2037.50

Portfolio Calculator

1 - Add stock

2 - Show portfolio

3 - Quit Enter your choice: 1

Enter company: Cisco

Number of shares: 50

Purchase price: 35.50

Current price: 26.75

Portfolio Calculator

1 - Add stock

2 - Show portfolio

3 - Quit

Enter your choice: 2

Portfolio Report

Company Value

Intel 700.00

Apple 1337.50

Cisco -437.50

Total $ 1600.00

Portfolio Calculator

1 - Add stock

2 - Show portfolio

3 - Quit

Enter your choice: 3

Explanation / Answer

Stock.java

public class Stock {
   //Declaring instance variables
   private String company;
   private int no_of_shares;
   private double purchase_price;
   private double current_price;
  
   //Parameterized constructor
   public Stock(String company, int no_of_shares, double purchase_price,
           double current_price) {
       super();
       this.company = company;
       this.no_of_shares = no_of_shares;
       this.purchase_price = purchase_price;
       this.current_price = current_price;
   }
  
   //getters and setters
   public String getCompany() {
       return company;
   }
   public void setCompany(String company) {
       this.company = company;
   }
   public int getNo_of_shares() {
       return no_of_shares;
   }
   public void setNo_of_shares(int no_of_shares) {
       this.no_of_shares = no_of_shares;
   }
   public double getPurchase_price() {
       return purchase_price;
   }
   public void setPurchase_price(double purchase_price) {
       this.purchase_price = purchase_price;
   }
   public double getCurrent_price() {
       return current_price;
   }
   public void setCurrent_price(double current_price) {
       this.current_price = current_price;
   }
  
  
   public double getValue()
   {
       return getNo_of_shares()*(getCurrent_price()-getPurchase_price());
   }

}

___________________

Test.java
import java.util.Scanner;

public class Test {
   //Scanner object is used to get the inputs entered by the user
   static Scanner sc = new Scanner(System.in);
   static int no_of_stocks = 0;
   public static void main(String[] args) {

       //Declaring variable
       int choice;
      
       //Creating an array of Stock class object array
       Stock[] stock = new Stock[10];
       // Scanner object is used to get the inputs entered by the user

       //This loop continues to execute until the user enters option as 3
       while (true) {
          
           //Displaying the menu
           System.out.println("Portfolio Calculator");
           System.out.println("1 - Add stock");
           System.out.println("2 - Show portfolio");
           System.out.println("3 - Quit");
           System.out.print("Enter your choice: ");
           choice = sc.nextInt();

           //Based on the user entered choice,the corresponding case will get executed
           switch (choice) {
           case 1:
               addStock(stock);
               continue;
           case 2:
               showPortfolio(stock);
               continue;
           case 3:
               break;
           default:
               System.out.println("** Invalid input **");
           }
           break;
       }

   }

   //This method will add the stocks to an Stock array
   private static void addStock(Stock[] stock) {
      
       //Declaring variables
       String company;
       int no_of_shares;
       double pur_price,curr_price;
      
       //Getting the inputs
       System.out.print("Enter company:");
       company=sc.next();

       System.out.print("Number of shares: ");
no_of_shares=sc.nextInt();
       System.out.print("Purchase price: ");
       pur_price=sc.nextDouble();
      
       System.out.print("Current price: ");
       curr_price=sc.nextDouble();
      
       /* Creating the Stock class object by passing inputs
       * as arguments and poputae that object into an array
       */
   stock[no_of_stocks]=new Stock(company, no_of_shares, pur_price, curr_price);
no_of_stocks++;
   }

   //This method will display the stocks
   private static void showPortfolio(Stock[] stock) {
       double total=0.0;
      
       System.out.println("Portfolio Report");
       System.out.println("Company Value");
       for(int i=0;i<no_of_stocks;i++)
       {
           System.out.printf("%s %.2f ",stock[i].getCompany(),stock[i].getValue());
           total+=stock[i].getValue();
       }
           System.out.printf(" Total $ %.2f ",total);

   }

}

_____________________

output:

Portfolio Calculator
1 - Add stock
2 - Show portfolio
3 - Quit
Enter your choice: 1
Enter company:Intel
Number of shares: 100
Purchase price: 29.50
Current price: 36.50
Portfolio Calculator
1 - Add stock
2 - Show portfolio
3 - Quit
Enter your choice: 1
Enter company:Apple
Number of shares: 50
Purchase price: 89.50
Current price: 116.25
Portfolio Calculator
1 - Add stock
2 - Show portfolio
3 - Quit
Enter your choice: 2
Portfolio Report
Company   Value
Intel   700.00

Apple   1337.50
Total $ 2037.50

Portfolio Calculator
1 - Add stock
2 - Show portfolio
3 - Quit
Enter your choice: 1
Enter company:Cisco
Number of shares: 50
Purchase price: 35.50
Current price: 26.75
Portfolio Calculator
1 - Add stock
2 - Show portfolio
3 - Quit
Enter your choice: 2
Portfolio Report
Company   Value
Intel   700.00

Apple   1337.50

Cisco   -437.50
Total $ 1600.00

Portfolio Calculator
1 - Add stock
2 - Show portfolio
3 - Quit
Enter your choice: 3

__________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote