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...
Create a class that will store all the information needed for a song. Your class will...
Create a class that will store all the information needed for a song. Your class will store the following data: A string for the song title. A string for the Artist/Band title A string for the Album/Compilation Name A string for the Genre A boolean called liked that represents whether you like the song The class will have the following methods: A constructor that will make a Song object from a song title, artist and album name A constructor that...
IN C++ Create a class called TextInt. The purpose of a TextInt is to store an...
IN C++ Create a class called TextInt. The purpose of a TextInt is to store an integer and convert it to the English text form of the integer when needed, such as ‘zero’ for 0, ‘one’ for 1, and so on, up to ‘nine thousand nine hundred ninety nine’ for 9999. You do NOT need punctuation (commas, hyphens, ‘and’, etc.) The TextInt class should have its own .h and .cpp files. At least the translate function should be implemented in...
java programming Create a class named Money. It should have member variables for Member Variables Store...
java programming Create a class named Money. It should have member variables for Member Variables Store dollars and cents as members (both should be int). They should be accessible from only inside the class. Methods  Write a default constructor that sets members to 0.  Write a two-parameter constructor that sets members to the parameter values.  Write get/set methods for the member variables.  Write an override method for toString. The returned string should be formatted as a...
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...
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
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 a SavingsAccount class to store data of savers (account holders). Your class should match the...
Create a SavingsAccount class to store data of savers (account holders). Your class should match the following specifications. 1. Each instance of the class contains a private data member savingsBalance indicating the amount the saver currently has on deposit, saver’s name, saver’s CNIC, account number (this has to be unique) and a member to store saver status (if savingsBalance > 10000 then status changes to gold otherwise silver). 2. Class also has a data member annualInterestRate to store the annual...
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)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT