370 likes | 746 Views
Programming Logic and Design Seventh Edition. Chapter 7 File Handling and Applications Revised. Objectives. In this chapter, you will learn about: Computer files The data hierarchy Performing sequential file operations Operations on records Control break logic.
E N D
Programming Logic and DesignSeventh Edition Chapter 7 File Handling and Applications Revised
Objectives In this chapter, you will learn about: • Computer files • The data hierarchy • Performing sequential file operations • Operations on records • Control break logic
Understanding Computer Files • Random access memory (RAM) • aka Primary Storage • Temporary storage • Volatile storage (requires constant refreshing) • Example: store a value in a variable • Permanent storage • aka Secondary Storage • Non-volatile storage • Example: save a program to a USB drive
Understanding Computer Files • File Concepts • A file is a collection of related data stored on a storage device • Types • Computer Files • Text file (Notepad file) • Binary file (created by a user-written program) • Word document • Excel Workbook • Access Database • Music (MP3) file / Video (MP4) file • PDF files • Characteristics (attributes) of a file • Name • Time and date of creation and last modified • Size measured in bytes • Application used to open it
Organizing Files • Folders (aka directories) • Container for storing related files • My Music, My Pictures, My Documents, Java, RAPTOR, Assignments • Microsoft DOS used the term directory • directory / subdirectory • Windows uses the term folder • folder / subfolder • Path • Combination of the disk drive letter plus the complete hierarchy of directories in which a file resides • F:\CIS 103\Assignments\Assignment_7\PayrollData.dat
Understanding the Data Hierarchy • Data hierarchy • Describes the relationships between data components • Consists of: File Record Field Characters / Numbers, etc.
Performing FileOperations(pseudocode) • Use data files in your programs • Declare a file • InputFile: EmployeeData.txt • RAPTOR Redirect_Input("EmployeeData.txt") • OutputFile: EmployeeReport.txt • RAPTOR Redirect_Output("EmployeeReport.txt") • Open a file • open "EmployeeData.txt" as inputFile (file handle) • open "EmployeeReport.txt" as outputFile (file handle) • Readfrom a file (get) • get namefrom inputFile • get addressfrom inputFile • get payRatefrom inputFile
Performing FileOperations (continued) Figure 7-2 Reading three data items from a storage device into memory
Performing FileOperations(pseudocode) • Write (put) • Put name, address, payRate to outputReport • Close • close inputFile • close outputFile
Performing File Operations in Java • Declare • public Scanner inputFile; • public PrintWriter outputFile; • Open • inputFile = new Scanner( new File("EmployeeData.txt") ); • outputFile = new PrintWriter( “EmloyeeReport.txt" ); • Read • name = inputFile.next(); //get a string delimited by whitespace • name = inputFile.nextLine(); //get rest of line as a string • payRate = inputFile.nextDouble(); • hoursWorked = inputFile.nextDouble();
Performing FileOperations in Java • Write • outputFile.println(name); • formatted output • outputFile.printf("%-15s %-40s %,10.2f%n", name, address, netPay); • Close • inputFile.close(); • outputFile.close();
Primingread Don't usually need this in Java! Regular read Programming Logic & Design, Sixth Edition Figure 7-3 Flowchart for a program that uses a file
Remove priming read A priming read is not usually necessary in RAPTOR or Java Move regularread Programming Logic & Design, Sixth Edition Figure 7-3 Revised (no priming read) Flowchart for a program that uses a file
Figure 7-3 Pseudocode for a program that uses a file Programming Logic & Design, Sixth Edition
A Program that Performs File Operations • Backup file • Copy that is kept in case values need to be restored to their original state • Called a parent file • Newly revised copy is a child file • sequential file • Records are stored one after another in unsorted or sorted sequence
Understanding Control Break Logic • control break logic • Temporary detour in the logic of a program • to print a summary line • Create a report with summaries • summary report only • detail and summaryreport
Understanding Control Break Logic (continued) • Control break program • Typically a change in the value of a variable initiates special actions or causes special processing to occur • Single- or multi-level control breaks are possible • Control break field / hold field • Note:control break programs usually have a priming read; this is necessary to set up hold fields!
Understanding Control Break Logic (continued) Control Break logic requires a file be sorted on one or more fields Figure 7-4 A control break report with city totals after each state state is the control break field
Understanding Control Break Logic (continued) • Examples of control break reports • employee report • sorted by department number, with a new page started for each department • books for sale by category report • sorted by category (such as reference or self-help), with a count following each category of book • items sold report • sorted by date of sale, with a different ink color for each new month
Programming Logic & Design, Sixth Edition Figure 7-5 Mainline logic and getReady()module for a program that produces a report of clients within state report
hold field declaration - oldState this variable will hold the state we are accumulating information for priming read initialize hold variable Programming Logic & Design, Sixth Edition Figure 7-5 Mainline logic and getReady()module for a program that produces a report of clients within state report
normal processing for a detail and summary report Figure 7-6 The produceReport( ) and controlBreak( ) modules for a program that produces a report of clients within state Programming Logic & Design, Sixth Edition
check for a control break normal processing Programming Logic & Design, Sixth Edition Figure 7-6 The produceReport() and controlBreak( ) modules for a program that produces a report of clients within state
Figure 7-7 The finishUp()module for a program that produces a report of clients within state
Performing Multiple-Level Control BreaksNew Example • Summary report: • group totals, not detail records • city totals • state totals • grand total • contains summary information and optionally detail information • Multiple-level control break: • breaks occur for more than one change in condition [multiple hold fields] Book Sales by city withinstate state major city minor Report of book sales by city and state[summary information for city and state ]
Performing Multiple-LevelControl Breaks • In this example, a control break occurs when: • Value of city variable changes [ oldCity ] • Value of state variable changes [ oldState ] • Input file must be sorted by city within state • When you detect a new city record print total for city • When you detect a new state record printtotals for city and state • When you detect end-of-file print totals for city, state, and grand total • Use arrays to store book counts as well as control break fieldsIMHO:The use of arrays here is an additional unnecessary complication
Performing Multiple-Level Control Breaks • cityBreak( ) module performs standard tasks: • Performs processing for previous group • Rolls up the current-level totals to next higher level • Resets current level’s totals to 0 • Performs processing for new group • Updates the control break field • stateBreak( ) moduledoes the same, starting with processing cityBreak( ) module • you must check for a break in the statebefore checking for a break in the city! Programming Logic and Design, Fifth Edition, Comprehensive
Performing Multiple-Level Control Breaks • Main program checks for change in city and state variables [ checks for state change first… ] • When city changes, city’sname and total are printed • When state changes, state’sname and total are printed • All city totals within a state print before state total for same state • Seems logical to check for change in city before state, but that would be incorrect logic! • Must check for statechange first Programming Logic and Design, Fifth Edition, Comprehensive
Performing Multiple-LevelControl Breaks • If two cities with the same name (in the same state) follow each other: • Program will not detect new city name • Always check for a major-level control break first • if the records are sorted by city within state, a change in the state causes a major-level break • If the records are sorted by city within state, then a change in the city causes a minor-level break • Change in stateimplies a change in city • Even if the cities have the same name
Performing Multiple-Level Control Breaks • Within each control break module, check if you need to: • Perform control break processing for the previous group(s) (if any) • if the state changed, then you must perform a control break on the city • Roll up the current-level totals to next higher level • Reset the current-level totals to 0 • Perform any control break processing for current group • Update the control break field(s)
Summary • Control break: • a change in a variable’s value causes special actions to occur • Control break field: • holds data from a previous record to compare to the current record • Control break data can be used in a heading or footer • Control break report prints summary lines and optionally detail lines
Summary (continued) • For multiple-level control breaks, test for a major-level break before a minor-level break • In a control break, check if lower-level breaks need to be processed • Page breaks can be handled based on line counters
Random Access Files • Batch program • Involves performing the same tasks with many records, one after the other (not in real-time) • Uses sequential files • Real-time applications • Require that a record be accessed immediately while a client is waiting • Interactive program • Program in which the user makes direct requests