In: Computer Science
write a java code to represent a sales class as follows:
1- The Sales class contains the names of sellers (strings) and the sales/seller/day (matrix of integers). Assume the number of sellers is set dynamically by the constructor and that the sellers work 6 days/week.
Example:
names/days | 0 | 1 | 2 | 3 | 4 | 5 |
Ali | 30 | 5 | 89 | 71 | 90 | 9 |
Ahmad | 15 | 81 | 51 | 69 | 78 | 25 |
Omar | 85 | 96 | 7 | 87 | 41 | 54 |
The class should contain the following methods:
- setSaleDetails, this method takes two arguments: the array of sellers' names and their detailed sales as a matrix of integers.
- printSalesDetails. This method prints the sellers' names and their sales (as in the example)
- printTotalSalesPerSeller. This method prints the list of total sales for each seller during the week
- getBestSeller . This method prints the name of the seller who achieved the highest sales during the whole week.
- getBestSellerAtDay . This method takes an integer Day as an argument (from 0 - 5). and prints the name of the seller who achieved the highest sales at this specific day.
----- You are asked to implement a tester class called SalesTester which has the main and do the following:
- creates an object from class Sales with a specific number of sales (example: 3)
- generates a random set of sales for each seller. Assume sales/day is integer and between 0 and 100.
- set the names of sellers and their sales (pass it to created object)
- calls all the methods of the class Sales for testing.
Hi,
Hope you are doing fine. I have coded the above question in java as per all the requirements mentioned above. The code has been clearly explained using comments that have been highlighted in bold. Since the sample out of all the method has not been mentioned, I have taken the liberty to print out in an understandable easy manner. You may change it as per your requirement. Also have a look at the code snippets and output below that would provide you with a clearer picture of the code.
Program:
Sales.java
//class sales
public class Sales {
//num is used to store the number of
sellers
int num;
//name is an array of strings used to store
the names of sellers
String[] name=new String[num];
//sales is an integer matrix that is used to
record sales. The rows of sales will be same as number of sellers
and the columns will be 6 as there are 6 working
days
int sales[][]= new int[num][6];
//constructor that initializes the value of
num
public Sales(int num) {
this.num = num;
}
//Method used to set the values of name and
sales using the input parameters
public void setSaleDetails(String [] names,int [][]
sale)
{
this.name=names;
this.sales=sale;
}
//method used to print details of sales of all
sellers
public void printSalesDetails()
{
System.out.println("names/days 0
1 2 3 4 5");
//loop to traverse through
the rows of sales
for(int i=0;i<num;i++)
{
System.out.print(name[i]+"\t\t");
//loop
to traverse through the columns of sales
for(int
j=0;j<6;j++)
{
//printing the sales element at
i,j
System.out.print(sales[i][j]+"\t");
}
System.out.println();
}
}
//method to print total sales per
seller
public void printTotalSalesPerSeller()
{
System.out.println("names total
sales");
//sum is an int array used
to store sum of sales of each seller
int sum[]=new int[num];
//loop to traverse through
the rows of sales
for(int i=0;i<num;i++)
{
System.out.print(name[i]+" ");
//loop
to traverse through the columns of sales
for(int
j=0;j<6;j++)
{
//adding sales[i][j] to sum[i] st every
iteration
sum[i]=sum[i]+sales[i][j];
}
System.out.print(sum[i]);
System.out.println();
}
}
//method to find the highest sales during the whole
week
public void getBestSeller()
{
//highest is used to record
the highest figure of sales and index is used to note the index i.e
the seller number who has got the sale
int highest=0,index=0;
//loop to traverse through
the rows of sales
for(int i=0;i<num;i++)
{
//loop
to traverse through the columns of sales
for(int
j=0;j<6;j++)
{
//if current element is greater than
highest then highest will be reassigned along with index at current
index
if(sales[i][j]>highest)
{
highest=sales[i][j];
index=i;
}
}
}
System.out.println("The highest
sales during the week were "+highest+" which were recorded for
"+name[index]);
}
//method to find the highest sales for a
particular day
public void getBestSellerAtDay(int day)
{
//highest is used to record
the highest figure of sales and index is used to note the index i.e
the seller number who has got the sale
int highest=0,index=0;
//loop to traverse through
the rows of sales
for(int i=0;i<num;i++)
{
//if
current element is greater than highest then highest will be
reassigned along with index at current index
if(sales[i][day]>highest)
{
highest=sales[i][day];
index=i;
}
}
System.out.println("The highest
sales for the day "+day+" were "+highest+" which were recorded for
"+name[index]);
}
}
SalesTester.java
//Tester class
import java.util.Random;
public class SalesTester {
public static void main(String[] args) {
// TODO Auto-generated method
stub
//creating object s with 3
sellers for Sales
Sales s=new Sales(3);
//creating new object to
generate random numbers
Random rand=new Random();
//declaring sales array of
type int to store sales. It is of size 3x6 as there are 3 sellers
and 6 days
int sales[][]=new int[3][6];
//traversing through rows
of sales
for(int i=0;i<3;i++)
{
//traversing through columns of sales
for(int
j=0;j<6;j++)
{
//setting sales element to a random
number generated between 0 to 100 (both inclusive)
sales[i][j]=rand.nextInt(101);
}
}
//names is used to store
names of sellers
String []names=
{"Ali","Ahmad","Omar"};
//Calling all the methods
of class Sales
s.setSaleDetails(names,
sales);
s.printSalesDetails();
s.printTotalSalesPerSeller();
s.getBestSeller();
s.getBestSellerAtDay(4);
}
}
Executable code snippets:
Sales.java
SalesTester.java
Output: