Question

WRITE A JAVA PROGRAM 1. Assignment Description Write a stock transaction program that calculates a customer's...

WRITE A JAVA PROGRAM

1. Assignment Description

Write a stock transaction program that calculates a customer's profit (or loss) within a certain period of time.

1) First, the program need to get user's name, the stock's code, number of shares and the price he/she purchased the stock, it also asks the user for the price he/she sold the stock later on. The program then compute the following:

  • The amount of money the customer paid for the stock.

  • The amount of commission the customer paid when he/she bought the stock

  • The amount that the cusotmer sold the stock for.

  • The amount of commission the customer paid his broker when he sold the stock

  • The amount of profit or loss that the customer had after selling his stock and paying both buy & sell commissions.

See below for the sample run of the program.

2) Assume for both buying or selling the stock, the agent charges 2% of the total amount from customer. (Hint: this should be declared as a constant)

3) Your program also need to meet the specifications stated below:

  • For the input and output, the exact spacing is not necessary, but what's important is that your program's output looks good and the columns align, i.e. your program's output should be very similar to our sample output. You should consider using escape sequence such as \t, \n to achieve this.

  • Pick and use identifiers which are self-descriptive. One-letter variable names should not be used. As an example, if you were referring to the stock's purchasing price, the variable needed might be declared as double purchasePrice; instead of double x;

  • All ouput price and amounts should be formatted as currency by using DecimalFormat class.

  • Depends on whether the profit is postivie or negative, your program should display different messages on screen, see our two output test cases for this. To achieve this, you will need to use simple if..else statement. Check Lab #2 if you don't know how to do this.  

Note: User input is in bold.

Enter Your Name: John Smith

Enter the stock code you bought: APPL

At what price did you buy it: 176.42

How many shares did you buy: 100

At what price did you sell it: 208.8

******** Stock Transaction Report *********
Customer Name:         John Smith
Stock Code:            APPL

Number of Shares:      100
Purchase Price:        $176.42
Purchase Amt:          $17642.00
Purchase Commission:   $352.84

Sell Price:            $208.80
Sell Amt:              $20880.00
Sell Commission:       $417.60

Profit is:             $2467.56

Test Case 1 Input:

Joe Dow
UBER
45.0
3500
33.06

Test Case 1 Output:

******** Stock Transaction Report *********
Customer Name:          Joe Dow
Stock Code:             UBER

Number of Shares:       3500
Purchase Price:         $45.00
Purchase Amt:           $157500.00
Purchase Commission:    $3150.00

Sell Price:             $33.06
Sell Amt:               $115710.00
Sell Commission:        $2314.20

You lose:               $47254.20

Homework Answers

Answer #1
Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !!

===========================================================================


import java.util.Scanner;

public class StockTrader {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter Your Name: ");
        String name = scanner.nextLine();
        System.out.print("Enter the stock code you bought: ");
        String code = scanner.nextLine();
        System.out.print("At what price did you buy it: ");
        double buyingPrice = scanner.nextDouble();
        System.out.print("How many shares did you buy: ");
        int shares = scanner.nextInt();
        System.out.print("At what price did you sell it: ");
        double sellingPrice = scanner.nextDouble();

        System.out.println("******** Stock Transaction Report *********");
        System.out.println("Customer Name:   " + name);
        System.out.println("Stock Code:      " + code);
        System.out.println();
        System.out.println("Number of Shares:      " + shares);
        System.out.printf("Purchase Price:       $%.2f\n", buyingPrice);
        System.out.printf("Purchase Amt:         $%.2f\n", buyingPrice * shares);
        System.out.printf("Purchase Commission:  $%.2f\n", buyingPrice * shares * 0.02);
        System.out.println();
        System.out.println("Sell Price:           $" + sellingPrice);
        System.out.printf("Sell Amt:             $%.2f\n", sellingPrice * shares);
        System.out.printf("Sell Commission:      $%.2f\n", sellingPrice * shares * 0.02);

        double netCost = buyingPrice * shares * (1.02);
        double netSelling = sellingPrice * shares * (1.02);

        if (netCost < netSelling) {
            System.out.printf("\nProfit is:       $%.2f\n", (netSelling - netCost));
        } else {
            double totalLoss = (buyingPrice - sellingPrice) * shares +
                    sellingPrice * shares * 0.02 + buyingPrice * shares * 0.02;
            System.out.printf("\nLoss is:       $%.2f\n", (totalLoss));
        }

    }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
c++ class problem Topics if/else if Description Write a program that calculates a sales person’s monthly...
c++ class problem Topics if/else if Description Write a program that calculates a sales person’s monthly commission. The program asks the user to enter the total sales amount for the month. It calculates the commission on the basis of the sales amount. Then, it displays a report including the sales amount and the commission earned. The commission is computed using the following: 15% commission for the first $2,000.00 sales 20% commission for the next $1,000.00 sales 25% commission for the...
in C++ Write a program. The program should prompt the user for the number of shares...
in C++ Write a program. The program should prompt the user for the number of shares purchased, purchase price per share, the commission for the purchase, sale commission paid, and the price the shares were sold at. The program should validate each of these values to ensure they are acceptable (none can be less than zero) and should then pass them to a function called numberfunction.The function should use the following formula to determine the profit: Profit = ((number of...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
Write a complete Java program which prompts the user of the program to input his/her first...
Write a complete Java program which prompts the user of the program to input his/her first name, then prompts the user for the middle initial and finally prompts the user for the last name. As indicated, there are three prompts to the user. The program should output the user’s name with the first name first, followed by the middle initial, and the last name last, all on one line (with appropriate spacing). If you could also pinpoint exactly where to...
This is a Java program Program Description You work for a local cell phone company and...
This is a Java program Program Description You work for a local cell phone company and have been asked to write a program to calculate the price of a cell phone data plan being purchased by a customer. The program should do the following tasks: Display a menu of the data plans that are available to be purchased. Read in the user’s selection from the menu.  Input Validation: If the user enters an invalid option, the program should display an error...
6.3 Lab Lesson 3 (Part 3 of 3) Part of lab lesson 3 There are three...
6.3 Lab Lesson 3 (Part 3 of 3) Part of lab lesson 3 There are three parts to lab lesson 3. The entire lab will be worth 100 points. Bonus points for lab lesson 3 There are also 10 bonus points. To earn the bonus points you have to complete the Participation Activities and Challenge Activities for zyBooks/zyLabs unit 5 (Gaddis Chapter 3). These have to be completed by the due date for lab lesson 3. For example, if you...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year. All fields are required to be non-blank. The Date fields should be reasonably valid values (ex. month 1-12, day...
QUESTION ONE Mr D. Hamabwe places K100 000 into a margin account to take a long...
QUESTION ONE Mr D. Hamabwe places K100 000 into a margin account to take a long position in a stock with a price of K200. If the initial margin requirement is 80%, how many shares can he purchase on margin?                                            What amount in Kwacha will he borrow in this margin transaction?                                                                       He will receive a margin call if the margin rate drops below 35%. Below what price will you receive a margin call?                                            ...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
Problem 1 (Defining products and Creating a Menu) Write a program called a2_p1.py that asks a...
Problem 1 (Defining products and Creating a Menu) Write a program called a2_p1.py that asks a shop owner for product information and creates a menu system that allows customers to purchase multiple products of varying quantities. The program should start by asking the shop owner’s name (string) and company name (string). Next it should ask for three (3) products (strings), their prices (int – not ideal, but for now we will keep it this way), and the number of products...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT