0% found this document useful (0 votes)
4 views

Report

database report work

Uploaded by

Rizwan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Report

database report work

Uploaded by

Rizwan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Title: "Development of a Sales Commission Calculation System for Sharaf DG"

Student Name: [Your Name]

Module Name: Advanced Programming (COMP 20014.1)

Module Code: COMP 20014.1

Instructor Name: [Instructor’s Name]

Institution Name: [Your Institution]

Date: [Submission Date]


1. Introduction

The user requirement specification of this report is aimed at creating commission calculation
system for Sharaf DG Company, for the computation of the commission for sales
representatives depending on the sales of laptops. The system has been developed in based
on client-server model and has database for storing the charge rates. The project is divided
into four tasks:

 The first task entails, developing a database and entering charge rates.
 Hence, in task 2, the client-side program to interact with the server is emphasized.
 This task goes through the server-side program that computes for commissions.
 The utility functions for database operations are included in the Task 4..

2. Task 1 – Database

2.1 Creating the Database

The first task is to establish a MySQL database in order to store charge rates concerning sales
representatives. There is a table in the database SharafDG known with the name ChargeRates;
this table stores charge codes and commission rates. The charge code will be an integer value,
while the charge rate constitutes a decimal value for percentage commission.

First of all, we need to create the database that will contain our ChargeRates table. You can do
this either with the graphical front-end application MySQL Workbench or directly on the
command line interface, MySQL CLI.

Using MySQL CLI:

2. Create a Table with Appropriate Fields:

Now, let's create a table named ChargeRates with the fields ChargeCode and ChargeRate. We'll use an
appropriate data type for each field.

 ChargeCode will be an integer because it identifies each charge rate.


 ChargeRate will be a decimal because it represents the percentage, such as 10.00%.

 Column 1:
oName: ChargeCode
oData Type: INT
oAttributes: PRIMARY KEY
oEnsure this field is not nullable and is set as the primary key.
 Column 2:
o Name: ChargeRate
o Data Type: DECIMAL(5, 2) (This allows for values like 10.00 or 5.00)

3. Insert Records into the Table:

Now that the table is created, let's insert at least five records. The charge rates will be stored as
per the requirements:

 ChargeCode 1: 10% (for sales profit > 20000 OMR)


 ChargeCode 2: 5% (for sales profit between 10000 and 20000 OMR)
 ChargeCode 3: 0% (for sales profit < 10000 OMR)
 We will also insert some additional dummy records.

4. Verify the Records:

To verify that the records have been inserted correctly, you can use the SELECT statement.

SELECT * FROM ChargeRates;


This should display all the records in the ChargeRates table.

5. Create the Database Server Role (Optional):

In MySQL, roles are often associated with user management. If you need to assign a specific
database server role to a user (such as read-only or administrative privileges), you can create and
assign roles in MySQL.

This may not be necessary unless explicitly requested by your assignment.

Coding:

-- Create the database

CREATE DATABASE SharafDG;

USE SharafDG;
-- Create the table

CREATE TABLE ChargeRates (

ChargeCode INT PRIMARY KEY,

ChargeRate DECIMAL(5, 2) NOT NULL

);

-- Insert the records

INSERT INTO ChargeRates (ChargeCode, ChargeRate) VALUES

(1, 10.00),

(2, 5.00),

(3, 0.00),

(4, 7.50),

(5, 3.00);

-- Verify the records

SELECT * FROM ChargeRates;

Task 2 – Client-Side Program:

The client side program takes as input the Sales Representative ID and the number of laptops
sold. It connects to the server and passes the data for the computation of commission. On getting
the result from the server, the client shows the sales profit, the charge rate that has been applied
and the commissions gotten.

Code:

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client {


public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter Sales Representative ID:");
int repId = sc.nextInt();

System.out.println("Enter the number of laptops sold:");


int laptopsSold = sc.nextInt();

// Threaded client-server communication


ClientThread clientThread = new ClientThread(repId,
laptopsSold);
Thread thread = new Thread(clientThread);
thread.start();
}
}
}

class ClientThread implements Runnable {


private int repId;
private int laptopsSold;

public ClientThread(int repId, int laptopsSold) {


this.repId = repId;
this.laptopsSold = laptopsSold;
}

@Override
public void run() {
try {
// Connect to the server
Socket socket = new Socket("localhost", 8080);
DataOutputStream out = new
DataOutputStream(socket.getOutputStream());
out.writeInt(repId);
out.writeInt(laptopsSold);

// Receive commission details


DataInputStream in = new
DataInputStream(socket.getInputStream());
double salesProfit = in.readDouble();
double commissionRate = in.readDouble();
double commission = in.readDouble();

System.out.println("Sales Profit: " + salesProfit);


System.out.println("Commission Rate: " + commissionRate +
"%");
System.out.println("Commission: " + commission);

socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

This client program takes the input of the representative ID from the user as well as the number
of laptops sold by them. It creates a thread to deal with the communication with the server then
sends the data and shows the value of commission that get from the server.

Task 3 – Server-Side Program:

The client side sends the Sales Representative ID and the number of laptops sold to the server
side program. It uses the current number of laptops sold to calculate sales profit; it then access
the proper charge rate and compute the commission before sending the returns to the client.

Code:

import java.io.*;
import java.net.*;
import java.sql.*;

public class Server {


public static void main(String[] args) {
try {
try (ServerSocket serverSocket = new ServerSocket(8080)) {
System.out.println("Server is listening on port
8080...");
while (true) {
Socket clientSocket = serverSocket.accept();
new ClientHandler(clientSocket).start();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

class ClientHandler extends Thread {


private Socket socket;

public ClientHandler(Socket socket) {


this.socket = socket;
}

public void run() {


try {
DataInputStream in = new
DataInputStream(socket.getInputStream());
@SuppressWarnings("unused")
int repId = in.readInt();
int laptopsSold = in.readInt();

// Calculate sales profit


double salesProfit = laptopsSold * 90; // Unit price = 90 OMR

// Retrieve the charge rate from the database


double chargeRate = getChargeRate(salesProfit);

// Calculate commission
double commission = salesProfit * chargeRate / 100;

// Send the result to the client


DataOutputStream out = new
DataOutputStream(socket.getOutputStream());
out.writeDouble(salesProfit);
out.writeDouble(chargeRate);
out.writeDouble(commission);

socket.close();
} catch (IOException | SQLException e) {
e.printStackTrace();
}
}
private double getChargeRate(double salesProfit) throws SQLException
{
double rate = 0.0;

try (Connection conn =


DriverManager.getConnection("jdbc:mysql://localhost:3306/SharafDG",
"root", "");
PreparedStatement ps = conn
.prepareStatement("SELECT ChargeRate FROM
ChargeRates WHERE ChargeCode = ?")) {

System.out.println("Connected to the database.");


int chargeCode = (salesProfit > 20000) ? 1 : (salesProfit >
10000) ? 2 : 3;
ps.setInt(1, chargeCode);
// Determine the charge code based on sales profit
System.out.println("Charge Code: " + chargeCode);

ResultSet rs = ps.executeQuery();
if (rs.next()) {
rate = rs.getDouble("ChargeRate");
System.out.println("Found charge rate: " + rate);
} else {
System.out.println("No charge rate found for ChargeCode:
" + chargeCode);
}
}
return rate;
}
}

The server waits for a connection request from the clients and processes the request based on the
type of client request made. This is because it determines the sales profit concerning the number
of laptops sold, extracts the charge rate from the database, and finally determines commission. It
produces such results and brings them to the client.

Task 4 – Utility Class:

The utility class provides methods to manipulate a collection, such as adding elements, searching
for a specific element, and removing elements based on a given scenario.

The utility class DatabaseUtil provides a method to obtain a connection to the MySQL database,
centralizing database connection management and ensuring consistency.
Code:

SalesUtility

import java.util.*;

public class SalesUtility {


private List<String> salesList;

public SalesUtility() {
salesList = new ArrayList<>();
}

// Add element
public void addSale(String sale, int index) {
salesList.add(index, sale);
System.out.println("Sale added: " + sale);
}

// Search element
public void searchSale(String sale) {
if (salesList.contains(sale)) {
System.out.println("Sale found: " + sale);
} else {
System.out.println("Sale not found.");
}
}
}

Conclusion
In this report, the author presents an overview of the commission calculation system for Sharaf
DG. The system is comprised of two parts, the client and the server where the client is
responsible for the data collection, communication with the server and presentation of
commission results. The server program then calculates commissions according to the profit of
the sales and gets the correct charge rate from a MySQL database. The report also points out use
of a utility class stating that it can be used to reduce the number of times someone has to open
connections to a database.

8. References:

MySQL Documentation. (n.d.). *MySQL 8.0 Reference Manual*. https://dev.mysql.com/doc/

Java Platform, Standard Edition. (n.d.). *Java 8 Documentation*.


https://docs.oracle.com/javase/8/docs/

You might also like