Question

In: Computer Science

write a java code to represent a sales class as follows: 1- The Sales class contains...

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.

Solutions

Expert Solution

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:


Related Solutions

Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
Write a Java application, and an additional class to represent some real-world entity such as a...
Write a Java application, and an additional class to represent some real-world entity such as a technology item, an animal, a person, a vehicle, etc. Keep in mind that a class is a model in code of something real or imagined, which has attributes (member variables) and behaviors (member methods). The class will: Create a total of 5 member variables for the class, selecting the appropriate data types for each field. For example, a class to represent a lamp might...
Java Language The int t contains an integer between 1 and 50 (inclusive). Write code that...
Java Language The int t contains an integer between 1 and 50 (inclusive). Write code that outputs the number in words and stores the result in the String inwords. For example, if t is 35 then inwords should contain "thirty five". Test Cases Test case #1 Expected result: When t is 2, your code sets inwords to "two" Test case #2 Expected result: When t is 50, your code sets inwords to "fifty" Test case #3 Expected result: When t...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3 integers which represent the sides of a triangle. Returns true if the triangle is isosceles and false otherwise. 2. perimeter - accepts 3 integers that represent the sides of a triangle and returns the perimeter of the triangle. 3. area -- accepts 3 integers, which represent the sides of a triangle and calculates and returns the area of the triangle. Hint: use Heron's formula....
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
Write the following methods in java class ARM that represent state information as well as functional...
Write the following methods in java class ARM that represent state information as well as functional blocks of the ARM platform. [Go through the following 5 classes then write methods for the instructions: mov, str, ldr, add in class ARM, finally, write a print method for ARM in Main.java that can display the registers and the memory locations that have been used. (make sure to make a visualization of the print method instead of just a console dump)] --- Please...
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT