Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Problem Summary When a non-profit group holds charity events, the event man

ID: 3716575 • Letter: J

Question

Java

Problem Summary When a non-profit group holds charity events, the event manager saves data about the expenses, ticket sales, and donations in a data ile. The data is then used to determine how much money the event generated or lost Each line of the data file contains an amount type and an amount. The amount type is an uppercase letter: T- ticket sales D- donations E = expenses After the letter, an amount is listed Test Data Files (included in the zyLab) event1.txt T 1100.00 E 444.44 D 100.00 D 250.00 D 222.00 E 555.55 event2.txt T 100.00 E 800.00 D 200.00 D 250.00 You will write a program to: Read the data from the file and sum the amounts for each category within an object NOTE: You may assume all file data will be valid (no error checking necessary) » Provide a report on the event profits/losses Documentation You must fill in the descriptions and author tags at the top of each file, but you do NOT have to add the Javadoc comments above each method Class files will be EventManager.java and Event.java The Event class data fields will hold the totals for each amount type (ticket sales, donations, and expenses) for one particular event Each step of the instructions is associated with specific tests, so you can test your program as you go along

Explanation / Answer


//Code to copy
//EventManager.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class EventManager {
  
   public static void main(String[] args) {
      
       Scanner keyboard=
               new Scanner(System.in);
       String fileName;
       File inFile=null;
       Scanner eventFile=null;
       //declare additional variables
       String amountType;
       double amountValue;
      
       //Create an instance of Event class
       Event event=null;
      
       System.out.println("Enter filename:");
       fileName=keyboard.next();
      
       keyboard.nextLine();
      
       try
       {
           //instantiate the File object with fileNmae
           inFile=new File(fileName);
           //instantitate the Scanenr with file object,inFile
           eventFile=new Scanner(inFile);
           //create an instance of Event class
           event=new Event();
           //read end of file
           while(eventFile.hasNextLine())
           {
               amountType=eventFile.next();
               amountValue=eventFile.nextDouble();
              
               System.out.printf("Line read : %s %4.2f ",
                       amountType,
                       amountValue);
               //Calling addAmount method
               event.addAmount(amountType, amountValue);
           }
          
           System.out.println("Done reading file event1.txt");
           System.out.println();
          
           //calling displayTotal on event object
           event.displayTotals();
           double netsales=event.calcEventProfit();
          
           if(netsales>0)
               System.out.println("Event generated a profit of $ "+netsales);
           else
               System.out.println("Event generated a loss of $ "+netsales);
                      
       }
       //catch the exception if file not found
       catch (FileNotFoundException e) {
           System.out.println("Could not open input file data.txt Program exiting");
       }      
   }
}//end of the class

------------------------------------------------------------------------------------------------------------------------

//Event.java
public class Event
{
   private double ticketSales;
   private double donations;
   private double expenses;
  
   public Event() {
       ticketSales=0;
       donations=0;
       expenses=0;
   }
  
   /**Adding method addAmount that takes string tyoe
   * and amount and the amount to correcponding
   * sales amount*/
   public void addAmount(String type,double amount)
   {
       if(type.equals("T"))
           ticketSales+=amount;
       else if(type.equals("D"))
           donations+=amount;
       else if(type.equals("E"))
           expenses+=amount;      
   }
   /**Method that display the sales,donations and expenses*/
   public void displayTotals()
   {      
       System.out.printf("%-20s%5.2f ","Total ticket sales: ",ticketSales);
       System.out.printf("%-20s%5.2f ","Total donations: ",donations);
       System.out.printf("%-20s%5.2f ","Total expenses: ",expenses);
   }
  
   /**Returns the profit of the event*/
   public double calcEventProfit()
   {
       return (ticketSales+donations-expenses);
   }  
   public double getTicketSales(){
       return ticketSales;
   }
   public double getDonations(){
       return donations;
   }
   public double getExpenses(){
       return expenses;
   }
  
}//end of the class Event

------------------------------------------------------------------------------------------------------------------------

Input file ,event1.txt

T 1100.00
E 444.44
D 100.00
D 250.00
D 222.00
E 555.55

-----------------------------------------------------------------------------

Sample Output:

Enter filename:
event1.txt
Line read : T 1100.00
Line read : E 444.44
Line read : D 100.00
Line read : D 250.00
Line read : D 222.00
Line read : E 555.55
Done reading file event1.txt

Total ticket sales: 1100.00
Total donations: 572.00
Total expenses: 999.99
Event generated a profit of $ 672.01

------------------------------------------------------------------------------

Sample run2:

Enter filename:
input.txt
Could not open input file data.txt Program exiting

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote