Question

In: Computer Science

Create a class Ledger that will record the sales for a store. It will have the...

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.

Solutions

Expert Solution

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


Related Solutions

Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables-a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for...
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member variable are allowed Create accessor and mutator functions to set/return numerator denominator Create a function to set a fraction Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4 use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string Create...
The < and == operators for the class Record have already been implemented for you.
The < and == operators for the class Record have already been implemented for you. Write the code necessary to complete the >, <=,>= and != operators. (hint: you do not need to know anything about the Record class to complete)
with PHP Create a class called Invoice that a hardware store might use to represent an...
with PHP Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables — a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private...
Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private data members of a UsedFurnitureItem are: age (double) // age in years – default value for brandNewPrice (double) // the original price of the item when it was brand new description (string) // a string description of the item condition (char) // condition of the item could be A, B, or C. size (double) // the size of the item in cubic inches. weight...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT