Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question two Program: Salary import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class Salary implements Raiseable { @Override public void

image text in transcribedimage text in transcribedimage text in transcribed

Question two

Program:

Salary

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class Salary implements Raiseable {

@Override

public void create(String fileName) {

BufferedWriter bw = null;

try {

// creating File Writer to write text file

FileWriter fw = new FileWriter(fileName);

// creating Buffered Writer to write text file

bw = new BufferedWriter(fw);

bw.flush();// Flushing File Writer

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException ioe) {

ioe.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (bw != null) {

bw.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

@Override

public void display(String fileName) {

Scanner sacnnerReader = null;

try {

File file = new File(fileName);

if (file.exists()) {

sacnnerReader = new Scanner(file);

while (sacnnerReader.hasNextLine()) {

String line = sacnnerReader.nextLine();

if (line != null && !line.equalsIgnoreCase("")) {

String lineArray[] = line.split(":");

int id = Integer.parseInt(lineArray[0]);

double salary = Double.parseDouble(lineArray[1]);

int serviceYear = Integer.parseInt(lineArray[2]);

System.out.println("ID: " + id + " Salary: " + salary+ " Year of Service: " + serviceYear);

}

}

} else {

System.out.println("Input File not found");

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

if (sacnnerReader != null) {

try {

sacnnerReader.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

@Override

public boolean addTo(String inFileName, String outFileName, int id,double salary, int yearsOfService) {

Scanner sacnnerReader = null;

boolean flag = true;

try {

copyFile(inFileName, outFileName);

File file = new File(inFileName);

if (file.exists()) {

sacnnerReader = new Scanner(file);

while (sacnnerReader.hasNextLine()) {

String line = sacnnerReader.nextLine();

if (line != null && !line.equalsIgnoreCase("")) {

String lineArray[] = line.split(":");

int employeeId = Integer.parseInt(lineArray[0]);

if (employeeId == id) {

System.out.println(id + " already exists. Not Created!");

flag = false;

}

}

}

} else {

System.out.println("Input File not found");

}

if (flag) {

String line = id +":" + salary + ":" + yearsOfService;

creatOuputFile(outFileName, line);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (sacnnerReader != null) {

sacnnerReader.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

return flag;

}

public void createSortedEntries (String outFileName,int[] id, double[] salary, int[] yearOfServices) {

for (int i = 0; i

int index = i;

for (int j = i + 1; j

if (id[j]

index = j;// searching for lowest index

}

}

int smallerNumber = id[index];

id[index] = id[i];

id[i] = smallerNumber;

salary[i] = salary[index];

yearOfServices[i] = yearOfServices[index];

}

for (int i = 0; i

String line = id[i] + ":" + salary[i] + ":" + yearOfServices[i];

creatOuputFile(outFileName, line);

}

}

@Override

public boolean removeFrom(String inFileName, String outFileName, int id,double salary, int yearsOfService) {

Scanner sacnnerReader = null;

BufferedWriter bw = null;

boolean flag = false;

try {

File file = new File(inFileName);

if (file.exists()) {

sacnnerReader = new Scanner(file);

while (sacnnerReader.hasNextLine()) {

String line = sacnnerReader.nextLine();

if (line != null && !line.equalsIgnoreCase("")) {

String lineArray[] = line.split(":");

int employeeId = Integer.parseInt(lineArray[0]);

if (employeeId == id) {

System.out.println(id + " exists!");

flag = true;

} else {

creatOuputFile(outFileName, line);

}

}

}

} else {

System.out.println("Input File not found");

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (sacnnerReader != null) {

sacnnerReader.close();

}

if (bw != null) {

bw.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

return flag;

}

@Override

public int raise(String inFileName, String outFileName, int yearsOfService,double salaryIncPercent) {

Scanner sacnnerReader = null;

BufferedWriter bw = null;

int count = 0;

try {

File file = new File(inFileName);

if (file.exists()) {

sacnnerReader = new Scanner(file);

while (sacnnerReader.hasNextLine()) {

String line = sacnnerReader.nextLine();

if (line != null && !line.equalsIgnoreCase("")) {

String lineArray[] = line.split(":");

int employeeId = Integer.parseInt(lineArray[0]);

double salary = Double.parseDouble(lineArray[1]);

int serviceYear = Integer.parseInt(lineArray[2]);

if (serviceYear >= yearsOfService) {

salary = salary + ((salary * salaryIncPercent) / 100);

line = employeeId + ":" + salary + ":"+ serviceYear;

creatOuputFile(outFileName, line);

count++;

}

}

}

} else {

System.out.println("Input File not found");

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (sacnnerReader != null) {

sacnnerReader.close();

}

if (bw != null) {

bw.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

return count;

}

@Override

public void mergeFiles(String inFileName1, String inFileName2,

String outFileName) {

Scanner sacnnerReader = null;

try {

File file = new File(inFileName1);

if (file.exists()) {

sacnnerReader = new Scanner(file);

while (sacnnerReader.hasNextLine()) {

String line = sacnnerReader.nextLine();

if (line != null && !line.equalsIgnoreCase("")) {

creatOuputFile(outFileName, line);

}

}

sacnnerReader.close();

} else {

System.out.println("Input File not found");

}

file = new File(inFileName2);

if (file.exists()) {

sacnnerReader = new Scanner(file);

while (sacnnerReader.hasNextLine()) {

String line = sacnnerReader.nextLine();

if (line != null && !line.equalsIgnoreCase("") && !checkIfEntryExits(inFileName1, line)) {

creatOuputFile(outFileName, line);

}

}

sacnnerReader.close();

} else {

System.out.println("Input File not found");

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

if (sacnnerReader != null) {

try {

sacnnerReader.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

public boolean checkIfEntryExits(String fileName, String line) {

Scanner sacnnerReader = null;

try {

File file = new File(fileName);

if (file.exists()) {

sacnnerReader = new Scanner(file);

while (sacnnerReader.hasNextLine()) {

String newLine = sacnnerReader.nextLine();

if (newLine != null && newLine.equalsIgnoreCase(line)) {

sacnnerReader.close();

return true;

}

}

} else {

System.out.println("Input File not found");

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

if (sacnnerReader != null) {

try {

sacnnerReader.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

return false;

}

public static void creatOuputFile(String outputFileName, String line) {

BufferedWriter bw = null;

try {

// creating File Writer to write text file

FileWriter fw = new FileWriter(outputFileName, true);

// creating Buffered Writer to write text file

bw = new BufferedWriter(fw);

bw.write(line);

bw.newLine();

bw.flush();// Flushing File Writer

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

if (bw != null) {

try {

bw.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

public static void copyFile(String inputFileName,String outputFileName) {

BufferedWriter bw = null;

Scanner sacnnerReader = null;

try {

File infile =new File(inputFileName);

File outfile =new File(outputFileName);

sacnnerReader = new Scanner(infile);

bw = new BufferedWriter(new FileWriter(outfile));

while (sacnnerReader.hasNextLine()) {

String line = sacnnerReader.nextLine();

bw.write(line);

bw.newLine();

}

bw.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

sacnnerReader.close();

bw.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

Driver

public class Driver {

public static void main(String[] args) {

Salary salary = new Salary();

String inputFileName1 = "input_salary_data_1.txt";

String inputFileName2 = "input_salary_data_2.txt";

String outputFileName = "output_salary_data.txt";

String increasedSalaryFileName = "increased_salary_data.txt";

System.out.println("Empty input_salary_data_1.txt created!");

salary.create(inputFileName1);

System.out.println("");

System.out.println("Empty input_salary_data_2.txt created!");

salary.create(inputFileName2);

System.out.println("");

boolean addFlag = salary.addTo(inputFileName1, outputFileName, 11111, 28000.00, 12);

System.out.println("11111 ID Added? "+addFlag);

addFlag = salary.addTo(inputFileName1, outputFileName, 77777, 29000.00, 17);

System.out.println("77777 ID Added? "+addFlag);

System.out.println("");

System.out.println("Current Employee Salary Info After Addion:");

salary.display(outputFileName);

System.out.println("");

boolean isRemoved = salary.removeFrom(inputFileName1, outputFileName, 11111, 28000.00, 12);

System.out.println("Is Entry Removed? "+isRemoved);

System.out.println("");

System.out.println("Current Employee Salary Info after Remove:");

salary.display(outputFileName);

System.out.println("");

int slarayIncreased = salary.raise(inputFileName1, increasedSalaryFileName, 15, 20);

System.out.println("Year of Experiance: "+15);

System.out.println("Percentage Increased: "+20);

System.out.println("Number Of Employee for Salary Increased: "+slarayIncreased);

System.out.println("Increased Employee Salary Info:");

salary.display(increasedSalaryFileName);

System.out.println("");

salary.mergeFiles (inputFileName1,inputFileName2,outputFileName);

System.out.println("Current Employee Salary Info after Merging:");

salary.display(outputFileName);

}

}

Files For this project, you will create an "Online Stock Market" using what you have learned about classes and objects. This online stock market should offer stocks for at least three companies. As an example, I created an online stock market that sells Apple, Google and Microsoft stocks. I created three different classes in my project, one for each type of stock, as can be seen in the image below from the NetBeans Project window: Projects x Services Stock MarketProject Source Packages stockmarketproject Apple.java Google.java Microsoft.java Stock MarketProject.java Test Packages Libraries Test Libraries You can use Investopedia to look up stocks To use Investopedia, type in the name of a company in the search box, for example: Austel AAPL APPLE INC APLE APPLE HOSPITALITY REN INC If there is more than one option, choose the company you wish to include in your program from the menu. This will bring you to a page with detailed information about the company's stocks. On the left hand side of the web page you will see the information included in the image below: The price given on the left is the cost of one "share" of MARKETS TODAY the stock. A share is the term used for one unit of a company's stock. In this case, one share of Apple stock costs $174.92 in U.S. dollars. So, if you spent $349.84 on this stock, you would own two shares APPLE INC of Apple stock. The stock symbol for Apple is AAPL" which can be seen in the image above, and the image AAPL NASDAQ to the right. Stock symbols are used to identify companies on the stock market. 174.92 is -4,781-2 Part 1 - The Classes: Create (at least three classes, where each class represents one company's stock. Each class should have the same three fields: A String stock symbol field to store the symbol for the company on the stock market. For example, Apple's stock symbol is AAPL" and Microsoft's stock symbol is MSFT" A double cost field to store the price of one share of the stock An integer field to store the number of shares the user currently owns of that stock Create a no-arg constructor and set the initial number of shares owned to 0, and the cost and stock symbol to the correct values from the Investopedia website. Also, create an argument-based constructor to allow the three fields to be initialized in the main method when the object is created. Create getter and setter methods for each of the three fields. Also, create the following methods: An addShares void method that takes an integer and adds it to the field that currently stores the total number of shares the user owns of that stock A removeShares void method that takes an integer and subtracts it from the field that currently stores the total number of shares the user owns of that stock A calcStockValue method that calculates and returns the total value of all the shares owned of that stock (e.g. if two shares of Apple stock are owned, this method should return 349.84) Include a toString method that prints out a user-friendly message displaying the three fields. Please submit a UML Diagram for one of your three classes when you hand in your project. Part 2 The Main Program: Create one object for each class at the beginning of your main method. You can use either the no-arg constructor or the argument-based constructor. Welcome the user, then begin a do-while loop with a switch, displaying the following menu: 1. Buy stock 2. Sell stock 3. View your stock portfolio 4. Exit program If the user chooses option 1, ask them how many shares of each stock they would like buy and add their input to the field that stores the total number of shares for that company's stock. If the user chooses option 2. ask them how many of each stock they wish to sell, and subtract each amount from each shares field. If the user chooses option 3, display a well-aligned chart summarizing the value of their stocks (please refer to the example on Page 4). If the user chooses option 4, the program should end, print a goodbye message, and call the toString() method for each object to summarize the number of stocks the user owns for each company. If the user chooses an option not in the menu, print an error message. Finally, for a thinking mark, you will need to include one extra feature for your program that is not mentioned above (for example, error traps for the user input, an option to convert the value of the stocks to Canadian dollars, an extra method that performs a function not mentioned above, and so on). Please include a program header for each file, I/comments, and method headers in each class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions