Introduction to Input and Output
Layers (or Tiers) of an Application Software in the real world normally takes the form of a number of independent classes, like lego blocks, that can be stuck together in whatever form the designer wishes. Programmers thus write sections or components of a solution and put them together as they go along.
Layers (or Tiers) of an Application The structure, where a solution is made out of connected classes, is called a layered or tiered approach. Distinct parts of a solution are organized into distinct groups and these are then put together to form layers. There are usually 3 distinct layers –The user interface –The logic layer –The data layer
Layer/ Tiers There are usually three distinct layers: –The user interface layer is intended to contain all the statements necessary to supply information to and obtain information from the user. The user interface layer may not necessarily process the information or store that information, for these functions it can pass the information to an application layer. –The application layer, sometimes called the business logic or logic layer, is usually responsible for managing the information and performing operations on it, for example retrieving information, computing results, applying rules. – The data layers responsibility is to store or retrieve the data on disk, in memory or in a database.
Tier Example - Bank For example imagine that you were asked to write an Information System for a bank. The structure for a bank : you meet the teller when you go in the door, they take your particular request and then defer to a manager. The manager then looks up your file to see if you can be granted your request, passes their decision to the teller who will notify you.
Tier Example - Bank To computerise this would require a User Interface to the system that the tellers could use to create new accounts and to lodge and withdraw money from accounts. This User Interface would use another part of the program which would manage accounts. The layer which manages accounts would effectively replace the manager and therefore carries out business logic. Account information could be stored as objects in the data layer. Therefore we have three distinct sections for our problem, a Graphical User Interface layer, a Business Logic layer and a Data layer.
Bank Layer nameFunction of this layer GUITo allow the user to input and view information. Specifically: - Create a new account - Lodge money - Withdraw money - Check balance Business Logic/ ApplicationTo receive requests from the GUI and to pass it back the result of those requests. Operations to handle: - Create a new account: Search for an existing account with the same name, if it exists deny request. If number of accounts created exceeds a preset limit deny creation request. - Lodge money: Search to see if the named account exists, if so add the amount supplied to the balance of account and return the new balance, return a message otherwise. - Withdraw money: Search to see if the named account exists, if it does deduct a supplied amount from the account provided there is sufficient funds to meet the withdrawal and return the new balance, refuse withdrawal otherwise. Data / DatabaseHold account name and balance of that account. Operations to handle: - Create new record - Get record information - Store information
Developing Layers Each layer must be debugged before use Layers should be as independent as possible
Layers Enter Data Login User Handle data input DataBaseAccount Manager GUI Logic Data Account Object
Scenario We want to write a file to maintain an array of person objects. The user can add to, delete from or modify Person objects in the array. To make this practical, we must save data to a file If data was saved to a file then the program can read them back from the file and rebuild the array
File I/O The most basic of Input/Output with Java is done with streams. Think of a stream as a flow of data, which can be an input stream or an output steam The stream commands enable you to write to a disk or to the output screen.
File I/O A common term in programming is persistence. How can you make data persist, or still be available, from one run of a program to the next? You have 2 main choices- –Write/Read from files –Write/Read from database
File I/O No matter where the data is coming from or going to and no matter what type its type, the mechanism for sequentially reading and writing data are basically the same. READINGWRITING open a stream while more information read information read information write information write information close the stream
File and FileDialog Objects Suppose we want to read the contents of a file sample.data. Before we begin the actual operation of reading data from this file, we must create a File object (from the java.io package) and associate it to the file. We call the File constructor –File inFile = new File (“sample.data”); –File inFile = new File (“c:\\JavaProjects”, “xyz.data”);
File and FileDialog A file MUST be opened before performing any file access operation. Since the user can select a file from a directory different from the current directory, we should use directoryPath in creating a file object File inFile = new File (directoryPath, filename);
Streams A Stream is simply a sequence of data items, usually eight-bit bytes. Java has 2 types of streams 1.An input stream 2.An output stream An input stream has a source from where the data items come and an output stream has a destination to where the data items are going. To read/write data items from/to a file, we attach one of the Java input/output stream objects to the file. The basic classes for Java I/0 are FileInputStream and FileOutputStream