# Calculate Sales Commision

Lab Exercise 13 — Java Calculating Commission Sales

Details:

Calculating Sales Commissions

Use a one-dimensional array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive \$200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses \$5000 in sales in a week receives \$200 plus 9% of \$5000, or a total of \$650. Write an application (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):

1. a) \$200–299
2. b) \$300–399
3. c) \$400–499
4. d) \$500–599
5. e) \$600–699
6. f) \$700–799
7. g) \$800–899
8. h) \$900–999
9. i) \$1000 and over

Hints: There are 9 total ranges.

If a sales person sells \$1000.00 worth of merchandise, then the salary will be 1000.00 * 0.09 + 200 or 90 + 200 = 290. So this person falls into the first range. Divide the salary, in this case 290/100 = 2. So all salaries from 200 to 299 will fall into this range.

Requirements

Use array and not a case statement.

Test Cases

Enter sales amount (negative to end): 1000

Enter sales amount (negative to end): 2000

Enter sales amount (negative to end): 3000

Enter sales amount (negative to end): 4000

Enter sales amount (negative to end): 5000

Enter sales amount (negative to end): 6000

Enter sales amount (negative to end): 7000

Enter sales amount (negative to end): 8000

Enter sales amount (negative to end): 9000

Enter sales amount (negative to end): -1

Range              Number

\$200-\$299       1

\$300-\$399       1

\$400-\$499       1

\$500-\$599       1

\$600-\$699       1

\$700-\$799       1

\$800-\$899       1

\$900-\$999       1

\$1000 and over           1

Lab Exercise 14 — Java Calculating Sales 2D Array

Details:

Use a two-dimensional array to solve the following problem: A company has four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each type of product sold.

Each slip contains the following:

1. a) The salesperson number
2. b) The product number
3. c) The total dollar value of that product sold that day

Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all the slips for last month is available. Write an application that will read all this information for last month’s sales and summarize the total sales by salesperson and by product. All totals should be stored in the two dimensional array sales. After processing all the information for last month, display the results in tabular format, with each column representing a salesperson and each row representing a particular product. Cross-total each row to get the total sales of each product for last month.

Cross-total each column to get the total sales by salesperson for last month. Your output should include these cross-totals to the right of the totaled rows and to the bottom of the totaled columns.

Input

1,1,1000

1,2,500

1,3,500

1,4,2145

1,5,235

2,1,1500

2,2,2500

2,3,500

2,4,3200

2,5,4235

3,1,4000

3,2,4000

3,3,500

3,4,2500

3,5,1035

4,1,2500

4,2,2000

4,3,4500

4,4,3500

4,5,5000

Expected Output

Product     Salesperson 1     Salesperson 2     Salesperson 3     Salesperson 4     Total

1          1,000.00          1,500.00          4,000.00          2,500.00      9,000.00

2            500.00          2,500.00          4,000.00          2,000.00      9,000.00

3            500.00            500.00            500.00          4,500.00      6,000.00

4          2,145.00          3,200.00          2,500.00          3,500.00     11,345.00

5            235.00          4,235.00          1,050.00          5,000.00     10,520.00

Total          4,380.00         11,935.00         12,050.00         17,500.00

Read lines of text from a file, for each line, modify (Manchester to New Manchester). Once reading all the lines and placing them into an array or list, write them back to a different file.

Part A – Java

Part B – C#

Smith, Mike, 33 South St, Concord, NH, 03301

Smith, Adam, 16 North St, Concord, NH, 03301

Smith, Paul, 14 Manchester St, Manchester, NH, 03301

Smith, Jane, 22 Main St, Concord, NH, 03301

Smith, Mary, 13 Elm St, Concord, NH, 03301

Solution

Lab13.java

importjava.util.Scanner;

public class Lab13 {

public static void main(String[] args) {

int[] ranges = new int[9];

Scanner in = new Scanner(System.in);

while (true) {

System.out.print(“Enter sales amount (negative to end): “);

double amount = in.nextInt();

if (amount < 0) {

break;

}

// calculate the salary and increase the number of its range

double salary = 200 + amount * 0.09;

int index = (int)Math.floor(salary / 100 – 2);

// increase the counter of the given range.

if (index >= ranges.length) {

ranges[ranges.length – 1] ++;

} else {

ranges[index] ++;

}

}

// display the result

System.out.println(“\nRange\t\t\tNumber”);

for  (int i = 0; i <ranges.length; i++) {

int start = (i + 2) * 100;

int end = start + 99;

if (i <ranges.length – 1) {

System.out.printf(“\$%d-\$%d\t\t%d\n”, start, end, ranges[i]);

} else {

System.out.printf(“\$%d and over\t\t%d\n”, start, ranges[i]);

}

}

}

}

Lab14.java

importjava.util.Scanner;

public class Lab14 {

public static void main(String[] args) {

double[][] sales = new double[4][5];

Scanner in = new Scanner(System.in);

// prompt the user to enter the sales

// assume the user will always enter the correct data

while (true) {

System.out.print(“Enter a slip (empty line to end): “);

String line = in.nextLine().trim();

if (line == null || line.length() == 0) {

break;

}

// parse the line into (salesperson, product, dollar value)

String[] values = line.split(“,”);

if (values.length == 3) {

int person = Integer.parseInt(values[0]);

int product = Integer.parseInt(values[1]);

double value = Double.parseDouble(values[2]);

// add this new slip into the two-dimenional array

if (person > 0 && person <= sales.length&&

product> 0 && product <= sales[person – 1].length) {

sales[person – 1][product – 1] += value;

}

}

}

// display the result in a table format

System.out.print(“\nProduct”);

for (int i = 1; i <= sales.length; i++) {

System.out.printf(” %15s %1d”, “Salesperson”, i);

}

System.out.println(”        Total”);

// display sales for each product and the total of that product

for (int i = 0; i < sales[0].length; i++) {

double total = 0;

System.out.printf(“%7d”, i + 1);

for (int j = 0; j <sales.length; j++) {

System.out.printf(” %,17.2f”, sales[j][i]);

total += sales[j][i];

}

System.out.printf(“%,13.2f\n”, total);

}

// calculate and display the total of each sales person

System.out.print(”  Total”);

for (int i = 0; i <sales.length; i++) {

double total = 0;

for (int j = 0; j < sales[i].length; j++) {

total += sales[i][j];

}

System.out.printf(” %,17.2f”, total);

}

System.out.println();

}

}

Lab16_java.java

importjava.util.Scanner;

importjava.io.File;

importjava.io.IOException;

importjava.io.PrintStream;

importjava.io.FileOutputStream;

importjava.util.ArrayList;

public class Lab16_java {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

// ask the user to enter the name of input and output files

System.out.print(“Enter the name of the input file: “);

String input = in.nextLine().trim();

System.out.print(“Enter the name of the out file: “);

String output = in.nextLine().trim();

ArrayList<String> lines = new ArrayList<String>();

try {

// open the input file

in = new Scanner(new File(input));

// process line by line

while (in.hasNextLine()) {

String line = in.nextLine();

// replace Manchester by New Manchester in the line

line = line.replace(“Manchester”, “New Manchester”);

}

in.close();

} catch (IOException e) {

System.out.println(“could not read file ” + input);

return;

}

try {

// open the output file to save the result

PrintStream out = new PrintStream(new FileOutputStream(output));

for (String line : lines) {

out.println(line);

}

out.close();

} catch (IOException e) {

System.out.println(“could not write file ” + output);

}

System.out.println(“DONE!”);

}

}

Lab16_cs.cs

using System;

using System.IO;

usingSystem.Collections.Generic;

namespace Lab16

{

public class Program

{

public static void Main(string[] args)

{

// ask the user to enter the name of input and output files.

Console.Write(“Enter the name of the input file: “);

Console.Write(“Enter the name of the input file: “);

List<string> lines = new List<string>();

try

{

// read the content from the input file

foreach (string line in content)

{

string newline = line.Replace(“Manchester”, “New Manchester”);

}

}

catch (FileNotFoundException)

{

Console.WriteLine(“could not read file {0}”, input);

return;

}

try

{

// save the content to the output file

string[] content = lines.ToArray();

File.WriteAllLines(output, content);

}

catch (FileNotFoundException)

{

Console.WriteLine(“could not write file {0}”, output);

}

}

}

}

Lab17_cs.cs

using System;

namespace Lab17

{

class Person

{

// properties of the person

public string LastName { get; set; }

public string FirstName { get; set; }

publicint Age { get; set; }

// constructor

public Person(string last, string first, int age)

{

LastName = last;

FirstName = first;

Age = age;

}

public Person() : this(string.Empty, string.Empty, 0)

{

}

public void DisplayPerson()

{

Console.WriteLine(“{0, 12} {1, -12} Age: {2}”, FirstName, LastName, Age);

}

}

classDataArray

{

// to store the persons and the number of them

private Person[] arr;

privateintnElems;

// constructor

publicDataArray(intmaxSize)

{

this.arr = new Person[maxSize];

this.nElems = 0;

}

// use a last name as the search keyword to search person

public Person Find(String searchName)

{

for (int j = 0; j <nElems; j++)

{

if (arr[j].LastName.Equals(searchName))

returnarr[j];

}

return null;

}

// add a new person into the array

public void Insert(String last, String first, int age)

{

Person person = new Person(last, first, age);

if (nElems == arr.Length)

{

// the array is already full, ignore the new person

return;

}

arr[nElems] = person;

nElems ++;

}

// delete the person from the array when the last name

// matches the given search keyword.

publicbool Delete(String searchName)

{

int j;

for (j = 0; j <nElems; j++)

{

if (arr[j].LastName.Equals(searchName))

break;

}

if (j == nElems)

return false;

}

else

{

// move the person in the following locations left

for  (int k = j; k <nElems – 1; k++)

{

arr[k] = arr[k + 1];

}

nElems –;

return true;

}

}

public void DisplayAll()

{

for (int j = 0; j <nElems; j++)

{

Console.Write(” {0,2}”, j + 1);

arr[j].DisplayPerson();

}

}

// return the number of persons in the array

publicintGetCount()

{

returnthis.nElems;

}

}

public class PersonApp

{

public static void Main(string[] args)

{

DataArrayarr = new DataArray(100);

// now ask the user to pick one operation and operate accordingly

Console.WriteLine(”  1. Insert an element into the person array.”);

Console.WriteLine(”  2. Display all elements from the person array.”);

Console.WriteLine(”  3. Search a person. Search by last name only.”);

Console.WriteLine(”  4. Delete a person.”);

Console.WriteLine(”  5. Display a count of persons.”);

Console.WriteLine(”  0. Quit the program”);

while (true)

{

Console.Write(“Choose (0 – 5): “);

Console.WriteLine();

switch (value)

{

case 0:

return; // quit the program

Insert(arr);

break;

case 2:   // display all

arr.DisplayAll();

break;

case 3:   // search a person

Search(arr);

break;

case 4:   // delete a person

Delete(arr);

break;

case 5:   // display the count

Console.WriteLine(“Number of persons: {0}”, arr.GetCount());

break;

default:

Console.WriteLine(“Incorrect choice.”);

break;

}

Console.WriteLine();

}

}

// implement the function to operate on the menu choice

static void Insert(DataArrayarr)

{

Console.Write(“Enter last name: “);

Console.Write(“Enter first name: “);

Console.Write(“Enter age: “);

Console.WriteLine();

// add this person into the array

arr.Insert(firstName, lastName, age);

}

static void Search(DataArrayarr)

{

Console.Write(“Enter last name to search: “);

Console.WriteLine();

Person person = arr.Find(lastName);

// find and show the result

if (person != null)

{

Console.Write(“Found: “);

person.DisplayPerson();

}

else

{

}

}

static void Delete(DataArrayarr)

{

Console.Write(“Enter last name to search and delete: “);

Console.WriteLine();

bool result = arr.Delete(lastName);

// delete and show the result

if (result)

{

Console.WriteLine(“Deleted!”);

}

else

{

}

}

}

}

DataArray.java

public class DataArray {

// to store the persons and the number of them

private Person[] arr;

privateintnElems;

// constructor

publicDataArray(intmaxSize) {

this.arr = new Person[maxSize];

this.nElems = 0;

}

// use a last name as the search keyword to search person

public Person find(String searchName) {

for (int j = 0; j <nElems; j++) {

if (arr[j].getLastName().equals(searchName)) {

returnarr[j];

}

}

return null;

}

// add a new person into the array

public void insert(String last, String first, int age) {

Person person = new Person(last, first, age);

if (nElems == arr.length) {

// the array is already full, ignore the new person

return;

}

arr[nElems] = person;

nElems ++;

}

// delete the person from the array when the last name

// matches the given search keyword.

publicboolean delete(String searchName) {

int j;

for (j = 0; j <nElems; j++) {

if (arr[j].getLastName().equals(searchName)) {

break;

}

}

return false;

} else {

// move the person in the following locations left

for  (int k = j; k <nElems – 1; k++) {

arr[k] = arr[k + 1];

}

nElems –;

return true;

}

}

public void displayAll() {

for (int j = 0; j <nElems; j++) {

System.out.printf(” %2d”, j + 1);

arr[j].displayPerson();

}

}

// return the number of persons in the array

publicintgetCount() {

returnthis.nElems;

}

}

Person.java

public class Person {

private String lastName;

private String firstName;

privateint age;

// constructors

public Person(String lastName, String firstName, int age) {

this.lastName = lastName;

this.firstName = firstName;

this.age = age;

}

public Person() {

this(“”, “”, 0);

}

// getters and setters

public String getLastName() {

returnthis.lastName;

}

public String getFirstName() {

returnthis.firstName;

}

publicintgetAge() {

returnthis.age;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public void setAge(int age) {

this.age = age;

}

public void displayPerson() {

System.out.printf(“%12s %-12s Age: %d\n”, firstName, lastName, age);

}

}

PersonApp.java

importjava.util.Scanner;

public class PersonApp {

private static Scanner in = new Scanner(System.in);

public static void main(String[] args) {

DataArrayarr = new DataArray(100);

// now ask the user to pick one operation and operate accordingly

System.out.println(”  1. Insert an element into the person array.”);

System.out.println(”  2. Display all elements from the person array.”);

System.out.println(”  3. Search a person. Search by last name only.”);

System.out.println(”  4. Delete a person.”);

System.out.println(”  5. Display a count of persons.”);

System.out.println(”  0. Quit the program”);

while (true) {

System.out.print(“Choose (0 – 5): “);

int value = Integer.parseInt(in.nextLine().trim());

System.out.println();

switch (value) {

case 0:

return; // quit the program

insert(arr);

break;

case 2:   // display all

arr.displayAll();

break;

case 3:   // search a person

search(arr);

break;

case 4:   // delete a person

delete(arr);

break;

case 5:   // display the count

System.out.println(“Number of persons: ” + arr.getCount());

break;

default:

System.out.println(“Incorrect choice.”);

break;

}

System.out.println();

}

}

// implement the function to operate on the menu choice

public static void insert(DataArrayarr) {

System.out.print(“Enter last name: “);

String lastName = in.nextLine().trim();

System.out.print(“Enter first name: “);

String firstName = in.nextLine().trim();

System.out.print(“Enter age: “);

int age = Integer.parseInt(in.nextLine().trim());

System.out.println();

// add this person into the array

arr.insert(firstName, lastName, age);

}

public static void search(DataArrayarr) {

System.out.print(“Enter last name to search: “);

String lastName = in.nextLine().trim();

System.out.println();

Person person = arr.find(lastName);

// find and show the result

if (person != null) {

System.out.print(“Found: “);

person.displayPerson();

} else {

}

}

public static void delete(DataArrayarr) {

System.out.print(“Enter last name to search and delete: “);

String lastName = in.nextLine().trim();

System.out.println();

boolean result = arr.delete(lastName);

// delete and show the result

if (result) {

System.out.println(“Deleted!”);

} else {