In: Computer Science
Create a class Ledger that will record the sales for a store. It will have the attributes ■ sale—an array of double values that are the amounts of all sales ■ salesMade—the number of sales so far ■ maxSales—the maximum number of sales that can be recorded and the following methods: ■ Ledger(max)—a constructor that sets the maximum number of sales to max ■ addSale(d)—adds a sale whose value is d ■ getNumberOfSales—returns the number of sales made ■ getTotalSales—returns the total value of the sales, Lastly define the following methods for the class Ledger, as described in the previous exercise: ■ getAverageSale()—returns the average value of all the sales ■ getCountAbove(v)—returns the number of sales that exceeded v in value.
Please find below the program(written in JAVA):
////////////////////////Ledger.java//////////////////////////
/**
*Class: Ledger
*Purpose: Records sales for a store
*/
public class Ledger {
//private members
//array of double values that are amount of all
sales
private double[] sale;
private int salesMade;//the number of sales so
far
private int maxSales;//maximum number of sales that
can be recorded
//constructor: sets the maximum number of sales
public Ledger(int max){
this.maxSales = max;
//initialize the array
sale = new
double[this.maxSales];
//initialize the number of sales so
far to zero
this.salesMade =0;
}
/**
* adds a sale whose value is d
* @param d
*/
public void addSale(double d){
if(salesMade< maxSales){
//As long as
maxSales is not reached, add the sales
sale[salesMade]
= d;//store the sale amount
salesMade++;//increase the count of salesMade by 1
System.out.println("Sale added in ledger with value = "+d);
}else{
System.out.println("Max sales reached for the ledger!");
}
}
/**
* returns the number of sales made so far
* @return
*/
public int getNumberOFSales(){
return salesMade;
}
/**
* returns the total value of the sales
* @return
*/
public double getTotalSales(){
double total = 0;
for(int i = 0 ; i < salesMade;
i++){
total = total +
sale[i];//do running sum
}
return total;
}
/**
* get Average value of all the sales
* @return
*/
public double getAverageSale(){
double total =
getTotalSales();
double average = 0;
if(salesMade!=0){//to get rid of
division by zero error
average =
total/salesMade;
}
return average;
}
/**
* returns number of sales that exceeded v in
value
* @param v
* @return
*/
public int getCountAbove(double v){
int count = 0;
for(int i =
0;i<salesMade;i++){
//increment only
if current sale is greater than v
if(sale[i]>v){
count++;
}
}
return count;
}
}
//////////////////////////////////TestLedger.java//////////////////////////////////////////
/**
*TestLedger class test various features of Ledger class
*/
public class TestLedger {
//main program begins
public static void main(String[] args){
//A ledger instance is created with
maxSales 10
Ledger ledger = new
Ledger(10);
//initialize a double array with
salesAmount
double salesAmount[] =
{59,45,67.5,39.45,44.47,61.54,73.29};
//traverse the array salesAmount
and add the sale amount in ledger
for(double d: salesAmount){
ledger.addSale(d);
}
//prints total number of sales for
this ledger
System.out.println("Total number of
sales = "+ ledger.getNumberOFSales());
//prints total sales amount for
this ledger
System.out.println("Total sales
amount = "+ ledger.getTotalSales());
//prints average sales amount for
this ledger
System.out.println("Average sale
amount = "+ ledger.getAverageSale());
//give a check sale amount, it can
vary .
//here for test purpose it is given
as 50
double checkSaleAmount = 50;
//prints number of sales that are
greater than checkSaleAmount
System.out.println("Number of sales
exceeded "+checkSaleAmount
+ " in value =
"+ledger.getCountAbove(checkSaleAmount));
}//main ends
}
=======================================
OUTPUT
=======================================
Sale added in ledger with value = 59.0
Sale added in ledger with value = 45.0
Sale added in ledger with value = 67.5
Sale added in ledger with value = 39.45
Sale added in ledger with value = 44.47
Sale added in ledger with value = 61.54
Sale added in ledger with value = 73.29
Total number of sales = 7
Total sales amount = 390.25
Average sale amount = 55.75
Number of sales exceeded 50.0 in value = 4