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

JAVA CS210 Introduction to Programming Topic 14: Files and File Input/Output Req

ID: 3719019 • Letter: J

Question

JAVA

CS210 Introduction to Programming

Topic 14: Files and File Input/Output

Requirements for zyLab 14.5: File I/O

Use the template files provided in zyBooks 13.12 to complete the following exercises (template files are included at the end of this requirements file, in case you want to develop your code in NetBeans).

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 file. 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.00D 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.

Test 1 (10 pts):

Within the EventManager class:

Define variables to hold an amount type and an amount value.

Using the already defined File and Scanner variables from the template, add code to instantiate a File object and a Scanner object that can be used to read the file whose name was input by the user.

Read data from the first line of the file only.

After reading the line, display a message containing the data from the line read, with the amount formatted to 2 decimal places, as follows:

Line read: T 111.11

Close the file.

Test 2 (15 pts):

Within the EventManager class:

Add loop around the code that reads the data from the file and displays what was read, so that it reads and displays all lines in the data file (should work no matter how many lines are in the file).

After all data has been read and displayed, use the constant to display a message including the filename, as follows:

Done reading file event1.txt

Test 3 (10 pts):

Within the Event class:

Add a addAmount() method that:

Has 2 parameters: an amount type letter and an amount value

Increments the total for the amount type specified by the first parameter by the amount specified in the second parameter

Test 4 (15 pts):

Within the Event class:

Add a displayTotals() method that will:

Display one blank line o Display the data fields, as follows (with decimals aligned and final digit in column 30):

Total ticket sales: Total donations:         2000 500.00.00  

                    Total expenses:         133.33

Within the EventManager class:

Above the file reading loop, create an Event object.

Within the loop, as each line of data is read, use the Event object to call the addAmount() method, with the data read from the file line as the arguments.

After reading all data in the file, add code to use the Event object to call the displayTotals() method.

           

Test 5 (10 pts):

Within the Event class:

Add a calcEventProfit() method that:

Will alculate and return the event profit (tickets + donations – expenses)

(NOTE: The profit will be negative, if the event lost money)

Tests 6 & 7 (10 pts each):

Within the EventManager class: ?       Display a blank line.

Use the Event object to call the calcEventProfit() method

If the returned event profit is positive (including 0), display

Event generated a profit of $ 111.11

Otherwise display

Event generated a loss of $ -111.11

Test 8 (10 pts):  

Within the EventManager class:

Remove the following clause from the main method header.

throws IOException

Implement file exception handling:

Add a try block to catch any errors when opening the data file. o Add a catch block to display the following message that includes the filename:

Could not open input file data.txt. Program exiting.

Test 9 (10 pts):

Blind test

Submission

You can develop and test all of your code directly in the zyLab area or start with the templates on the next page in NetBeans and copy your code over when done.

After you finish coding your solution, submit in zyLab for testing.

(see next page for template files)

           

Event.java template:

/**

Class description

@author   */

public class Event {

   private double ticketSales;   // total profit from event ticket sales    private double donations;     // total profit from event donations    private double expenses;      // total expenses to put on the event

    public Event () {       ticketSales = 0;       donations = 0;       expenses = 0;

   }       

   public double getTicketSales () {       return ticketSales;

   }

   public double getDonations () {       return donations;

   }     

   public double getExpenses () {       return expenses;

   }

   

   // Add your code here (and do not delete any of the above code)

   

}

EventManager.java template:

/**

Program description

@author   */

import java.util.Scanner; import java.io.*;

public class EventManager {

   public static void main(String[] args) throws IOException {

      Scanner keyboard = new Scanner(System.in);

      String filename;

      File inFile = null;

      Scanner eventFile = null;

      // Add additional variable declarations here

      System.out.println("Enter filename:");       filename = keyboard.next();

      

      // Insert executatble code here      

      

   }

   

}    

Explanation / Answer

//EventManager.java

/**

Program description

@author */

import java.util.Scanner;

import java.io.*;

public class EventManager {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

String filename;

File inFile = null;

Scanner eventFile = null;

  

String amountType = null;

long amountValue = 0;

  

String line = "";

  

// Add additional variable declarations here

System.out.println("Enter filename:"); filename = keyboard.next();

inFile = new File(filename);

try {

keyboard = new Scanner(inFile);

} catch (FileNotFoundException e) {

System.out.println("Could not open input file "+filename+". Program exiting.");

e.printStackTrace();

System.exit(1);

}

  

Event event = new Event();

  

while(keyboard.hasNext()) {

line = keyboard.nextLine();

System.out.println("Line read: "+line);

event.addAmount(line.substring(0, 1),Double.parseDouble(line.substring(2)));

}

event.displayTotals();

System.out.println("Done reading file "+filename);

System.out.println("");

  

double profitLoss = event.calcEventProfit();

  

if(profitLoss>=0)

System.out.println("Event generated a profit of $ "+profitLoss);

else

System.out.println("Event generated a loss of $ "+profitLoss);

  

}

}   

//Event.java

/**

*

* Class description

*

* @author

*/

public class Event {

private double ticketSales; // total profit from event ticket sales

private double donations; // total profit from event donations

private double expenses; // total expenses

// to put on the event

public Event() {

ticketSales = 0;

donations = 0;

expenses = 0;

}

public double getTicketSales() {

return ticketSales;

}

public double getDonations() {

return donations;

}

public double getExpenses() {

return expenses;

}

void addAmount(String amoutType,double amountValue){

if("T".equals(amoutType))

ticketSales = ticketSales +amountValue;

if("D".equals(amoutType))

donations = donations +amountValue;

if("E".equals(amoutType))

expenses = expenses +amountValue;

}

void displayTotals(){

System.out.printf("%-30s $%.2f%n","Total ticket sales:", ticketSales);

System.out.printf("%-30s $%.2f%n","Total donations:", donations);

System.out.printf("%-30s $%.2f%n","Total expenses:", expenses);

}

double calcEventProfit(){

return ticketSales+donations-expenses;

}

}

Few points to add:
Make sure file is in same folder

Make sure you type filename with extension eg: "demo.txt"

Make sure each line has only one entry in file