diff options
Diffstat (limited to 'Bachelor/Prog2/Codebeispiele/4_ch14')
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_04.cpp | 61 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_07.cpp | 78 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_08.cpp | 163 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_12.cpp | 53 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_13.cpp | 94 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_14.cpp | 91 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_15.cpp | 331 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/4_ch14/clientData.cpp | 105 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/4_ch14/clientData.h | 56 |
9 files changed, 1032 insertions, 0 deletions
diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_04.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_04.cpp new file mode 100644 index 0000000..f132e11 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_04.cpp @@ -0,0 +1,61 @@ +// Fig. 14.4: fig14_04.cpp
+// Create a sequential file.
+#include <iostream>
+
+using std::cout;
+using std::cin;
+using std::ios;
+using std::cerr;
+using std::endl;
+
+#include <fstream>
+
+using std::ofstream;
+
+#include <cstdlib> // exit prototype
+
+int main()
+{
+ // ofstream constructor opens file
+ ofstream outClientFile( "clients.dat", ios::out );
+
+ // exit program if unable to create file
+ if ( !outClientFile ) { // overloaded ! operator
+ cerr << "File could not be opened" << endl;
+ exit( 1 );
+
+ } // end if
+
+ cout << "Enter the account, name, and balance." << endl
+ << "Enter end-of-file to end input.\n? ";
+
+ int account;
+ char name[ 30 ];
+ double balance;
+
+ // read account, name and balance from cin, then place in file
+ while ( cin >> account >> name >> balance ) {
+ outClientFile << account << ' ' << name << ' ' << balance
+ << endl;
+ cout << "? ";
+
+ } // end while
+
+ return 0; // ofstream destructor closes file
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_07.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_07.cpp new file mode 100644 index 0000000..7067985 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_07.cpp @@ -0,0 +1,78 @@ +// Fig. 14.7: fig14_07.cpp
+// Reading and printing a sequential file.
+#include <iostream>
+
+using std::cout;
+using std::cin;
+using std::ios;
+using std::cerr;
+using std::endl;
+using std::left;
+using std::right;
+using std::fixed;
+using std::showpoint;
+
+#include <fstream>
+
+using std::ifstream;
+
+#include <iomanip>
+
+using std::setw;
+using std::setprecision;
+
+#include <cstdlib> // exit prototype
+
+void outputLine( int, const char * const, double );
+
+int main()
+{
+ // ifstream constructor opens the file
+ ifstream inClientFile( "clients.dat", ios::in );
+
+ // exit program if ifstream could not open file
+ if ( !inClientFile ) {
+ cerr << "File could not be opened" << endl;
+ exit( 1 );
+
+ } // end if
+
+ int account;
+ char name[ 30 ];
+ double balance;
+
+ cout << left << setw( 10 ) << "Account" << setw( 13 )
+ << "Name" << "Balance" << endl << fixed << showpoint;
+
+ // display each record in file
+ while ( inClientFile >> account >> name >> balance )
+ outputLine( account, name, balance );
+
+ return 0; // ifstream destructor closes the file
+
+} // end main
+
+// display single record from file
+void outputLine( int account, const char * const name,
+ double balance )
+{
+ cout << left << setw( 10 ) << account << setw( 13 ) << name
+ << setw( 7 ) << setprecision( 2 ) << right << balance
+ << endl;
+
+} // end function outputLine
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_08.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_08.cpp new file mode 100644 index 0000000..ec49dee --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_08.cpp @@ -0,0 +1,163 @@ +// Fig. 14.8: fig14_08.cpp
+// Credit inquiry program.
+#include <iostream>
+
+using std::cout;
+using std::cin;
+using std::ios;
+using std::cerr;
+using std::endl;
+using std::fixed;
+using std::showpoint;
+using std::left;
+using std::right;
+
+#include <fstream>
+
+using std::ifstream;
+
+#include <iomanip>
+
+using std::setw;
+using std::setprecision;
+
+#include <cstdlib>
+
+enum RequestType { ZERO_BALANCE = 1, CREDIT_BALANCE,
+ DEBIT_BALANCE, END };
+int getRequest();
+bool shouldDisplay( int, double );
+void outputLine( int, const char * const, double );
+
+int main()
+{
+ // ifstream constructor opens the file
+ ifstream inClientFile( "clients.dat", ios::in );
+
+ // exit program if ifstream could not open file
+ if ( !inClientFile ) {
+ cerr << "File could not be opened" << endl;
+ exit( 1 );
+
+ } // end if
+
+ int request;
+ int account;
+ char name[ 30 ];
+ double balance;
+
+ // get user's request (e.g., zero, credit or debit balance)
+ request = getRequest();
+
+ // process user's request
+ while ( request != END ) {
+
+ switch ( request ) {
+
+ case ZERO_BALANCE:
+ cout << "\nAccounts with zero balances:\n";
+ break;
+
+ case CREDIT_BALANCE:
+ cout << "\nAccounts with credit balances:\n";
+ break;
+
+ case DEBIT_BALANCE:
+ cout << "\nAccounts with debit balances:\n";
+ break;
+
+ } // end switch
+
+ // read account, name and balance from file
+ inClientFile >> account >> name >> balance;
+
+ // display file contents (until eof)
+ while ( !inClientFile.eof() ) {
+
+ // display record
+ if ( shouldDisplay( request, balance ) )
+ outputLine( account, name, balance );
+
+ // read account, name and balance from file
+ inClientFile >> account >> name >> balance;
+
+ } // end inner while
+
+ inClientFile.clear(); // reset eof for next input
+ inClientFile.seekg( 0 ); // move to beginning of file
+ request = getRequest(); // get additional request from user
+
+ } // end outer while
+
+ cout << "End of run." << endl;
+
+ return 0; // ifstream destructor closes the file
+
+} // end main
+
+// obtain request from user
+int getRequest()
+{
+ int request;
+
+ // display request options
+ cout << "Enter request" << endl
+ << " 1 - List accounts with zero balances" << endl
+ << " 2 - List accounts with credit balances" << endl
+ << " 3 - List accounts with debit balances" << endl
+ << " 4 - End of run" << fixed << showpoint;
+
+ // input user request
+ do {
+ cout << "\n? ";
+ cin >> request;
+
+ } while( request < ZERO_BALANCE && request > END );
+
+ return request;
+
+} // end function getRequest
+
+// determine whether to display given record
+bool shouldDisplay( int type, double balance )
+{
+ // determine whether to display credit balances
+ if ( type == CREDIT_BALANCE && balance < 0 )
+ return true;
+
+ // determine whether to display debit balances
+ if ( type == DEBIT_BALANCE && balance > 0 )
+ return true;
+
+ // determine whether to display zero balances
+ if ( type == ZERO_BALANCE && balance == 0 )
+ return true;
+
+ return false;
+
+} // end function shouldDisplay
+
+// display single record from file
+void outputLine( int account, const char * const name,
+ double balance )
+{
+ cout << left << setw( 10 ) << account << setw( 13 ) << name
+ << setw( 7 ) << setprecision( 2 ) << right << balance
+ << endl;
+
+} // end function outputLine
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_12.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_12.cpp new file mode 100644 index 0000000..48ba334 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_12.cpp @@ -0,0 +1,53 @@ +// Fig. 14.12: fig14_12.cpp
+// Creating a randomly accessed file.
+#include <iostream>
+
+using std::cerr;
+using std::endl;
+using std::ios;
+
+#include <fstream>
+
+using std::ofstream;
+
+#include <cstdlib>
+#include "clientData.h" // ClientData class definition
+
+int main()
+{
+ ofstream outCredit( "credit.dat", ios::binary );
+
+ // exit program if ofstream could not open file
+ if ( !outCredit ) {
+ cerr << "File could not be opened." << endl;
+ exit( 1 );
+
+ } // end if
+
+ // create ClientData with no information
+ ClientData blankClient;
+
+ // output 100 blank records to file
+ for ( int i = 0; i < 100; i++ )
+ outCredit.write(
+ reinterpret_cast< const char * >( &blankClient ),
+ sizeof( ClientData ) );
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_13.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_13.cpp new file mode 100644 index 0000000..fefa264 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_13.cpp @@ -0,0 +1,94 @@ +// Fig. 14.13: fig14_13.cpp
+// Writing to a random access file.
+#include <iostream>
+
+using std::cerr;
+using std::endl;
+using std::cout;
+using std::cin;
+using std::ios;
+
+#include <iomanip>
+
+using std::setw;
+
+#include <fstream>
+
+using std::ofstream;
+
+#include <cstdlib>
+#include "clientData.h" // ClientData class definition
+
+int main()
+{
+ int accountNumber;
+ char lastName[ 15 ];
+ char firstName[ 10 ];
+ double balance;
+
+ ofstream outCredit( "credit.dat", ios::binary );
+
+ // exit program if ofstream cannot open file
+ if ( !outCredit ) {
+ cerr << "File could not be opened." << endl;
+ exit( 1 );
+
+ } // end if
+
+ cout << "Enter account number "
+ << "(1 to 100, 0 to end input)\n? ";
+
+ // require user to specify account number
+ ClientData client;
+ cin >> accountNumber;
+ client.setAccountNumber( accountNumber );
+
+ // user enters information, which is copied into file
+ while ( client.getAccountNumber() > 0 &&
+ client.getAccountNumber() <= 100 ) {
+
+ // user enters last name, first name and balance
+ cout << "Enter lastname, firstname, balance\n? ";
+ cin >> setw( 15 ) >> lastName;
+ cin >> setw( 10 ) >> firstName;
+ cin >> balance;
+
+ // set record lastName, firstName and balance values
+ client.setLastName( lastName );
+ client.setFirstName( firstName );
+ client.setBalance( balance );
+
+ // seek position in file of user-specified record
+ outCredit.seekp( ( client.getAccountNumber() - 1 ) *
+ sizeof( ClientData ) );
+
+ // write user-specified information in file
+ outCredit.write(
+ reinterpret_cast< const char * >( &client ),
+ sizeof( ClientData ) );
+
+ // enable user to specify another account number
+ cout << "Enter account number\n? ";
+ cin >> accountNumber;
+ client.setAccountNumber( accountNumber );
+
+ } // end while
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_14.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_14.cpp new file mode 100644 index 0000000..6a9728e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_14.cpp @@ -0,0 +1,91 @@ +// Fig. 14.14: fig14_14.cpp
+// Reading a random access file.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+using std::ios;
+using std::cerr;
+using std::left;
+using std::right;
+using std::fixed;
+using std::showpoint;
+
+#include <iomanip>
+
+using std::setprecision;
+using std::setw;
+
+#include <fstream>
+
+using std::ifstream;
+using std::ostream;
+
+#include <cstdlib>
+#include "clientData.h" // ClientData class definition
+
+void outputLine( ostream&, const ClientData & );
+
+int main()
+{
+ ifstream inCredit( "credit.dat", ios::in );
+
+ // exit program if ifstream cannot open file
+ if ( !inCredit ) {
+ cerr << "File could not be opened." << endl;
+ exit( 1 );
+
+ } // end if
+
+ cout << left << setw( 10 ) << "Account" << setw( 16 )
+ << "Last Name" << setw( 11 ) << "First Name" << left
+ << setw( 10 ) << right << "Balance" << endl;
+
+ ClientData client; // create record
+
+ // read first record from file
+ inCredit.read( reinterpret_cast< char * >( &client ),
+ sizeof( ClientData ) );
+
+ // read all records from file
+ while ( inCredit && !inCredit.eof() ) {
+
+ // display record
+ if ( client.getAccountNumber() != 0 )
+ outputLine( cout, client );
+
+ // read next from file
+ inCredit.read( reinterpret_cast< char * >( &client ),
+ sizeof( ClientData ) );
+
+ } // end while
+
+ return 0;
+
+} // end main
+
+// display single record
+void outputLine( ostream &output, const ClientData &record )
+{
+ output << left << setw( 10 ) << record.getAccountNumber()
+ << setw( 16 ) << record.getLastName().data()
+ << setw( 11 ) << record.getFirstName().data()
+ << setw( 10 ) << setprecision( 2 ) << right << fixed
+ << showpoint << record.getBalance() << endl;
+
+} // end outputLine
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_15.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_15.cpp new file mode 100644 index 0000000..da2ef2e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_15.cpp @@ -0,0 +1,331 @@ +// Fig. 14.15: fig14_15.cpp
+// This program reads a random access file sequentially, updates
+// data previously written to the file, creates data to be placed
+// in the file, and deletes data previously in the file.
+#include <iostream>
+
+using std::cout;
+using std::cerr;
+using std::cin;
+using std::endl;
+using std::ios;
+using std::left;
+using std::right;
+using std::fixed;
+using std::showpoint;
+
+#include <fstream>
+
+using std::ofstream;
+using std::ostream;
+using std::fstream;
+
+#include <iomanip>
+
+using std::setw;
+using std::setprecision;
+
+#include <cstdlib>
+#include "clientData.h" // ClientData class definition
+
+int enterChoice();
+void printRecord( fstream& );
+void updateRecord( fstream& );
+void newRecord( fstream& );
+void deleteRecord( fstream& );
+void outputLine( ostream&, const ClientData & );
+int getAccount( const char * const );
+
+enum Choices { PRINT = 1, UPDATE, NEW, DELETE, END };
+
+int main()
+{
+ // open file for reading and writing
+ fstream inOutCredit( "credit.dat", ios::in | ios::out );
+
+ // exit program if fstream cannot open file
+ if ( !inOutCredit ) {
+ cerr << "File could not be opened." << endl;
+ exit ( 1 );
+
+ } // end if
+
+ int choice;
+
+ // enable user to specify action
+ while ( ( choice = enterChoice() ) != END ) {
+
+ switch ( choice ) {
+
+ // create text file from record file
+ case PRINT:
+ printRecord( inOutCredit );
+ break;
+
+ // update record
+ case UPDATE:
+ updateRecord( inOutCredit );
+ break;
+
+ // create record
+ case NEW:
+ newRecord( inOutCredit );
+ break;
+
+ // delete existing record
+ case DELETE:
+ deleteRecord( inOutCredit );
+ break;
+
+ // display error if user does not select valid choice
+ default:
+ cerr << "Incorrect choice" << endl;
+ break;
+
+ } // end switch
+
+ inOutCredit.clear(); // reset end-of-file indicator
+
+ } // end while
+
+ return 0;
+
+} // end main
+
+// enable user to input menu choice
+int enterChoice()
+{
+ // display available options
+ cout << "\nEnter your choice" << endl
+ << "1 - store a formatted text file of accounts" << endl
+ << " called \"print.txt\" for printing" << endl
+ << "2 - update an account" << endl
+ << "3 - add a new account" << endl
+ << "4 - delete an account" << endl
+ << "5 - end program\n? ";
+
+ int menuChoice;
+ cin >> menuChoice; // receive choice from user
+
+ return menuChoice;
+
+} // end function enterChoice
+
+// create formatted text file for printing
+void printRecord( fstream &readFromFile )
+{
+ // create text file
+ ofstream outPrintFile( "print.txt", ios::out );
+
+ // exit program if ofstream cannot create file
+ if ( !outPrintFile ) {
+ cerr << "File could not be created." << endl;
+ exit( 1 );
+
+ } // end if
+
+ outPrintFile << left << setw( 10 ) << "Account" << setw( 16 )
+ << "Last Name" << setw( 11 ) << "First Name" << right
+ << setw( 10 ) << "Balance" << endl;
+
+ // set file-position pointer to beginning of record file
+ readFromFile.seekg( 0 );
+
+ // read first record from record file
+ ClientData client;
+ readFromFile.read( reinterpret_cast< char * >( &client ),
+ sizeof( ClientData ) );
+
+ // copy all records from record file into text file
+ while ( !readFromFile.eof() ) {
+
+ // write single record to text file
+ if ( client.getAccountNumber() != 0 )
+ outputLine( outPrintFile, client );
+
+ // read next record from record file
+ readFromFile.read( reinterpret_cast< char * >( &client ),
+ sizeof( ClientData ) );
+
+ } // end while
+
+} // end function printRecord
+
+// update balance in record
+void updateRecord( fstream &updateFile )
+{
+ // obtain number of account to update
+ int accountNumber = getAccount( "Enter account to update" );
+
+ // move file-position pointer to correct record in file
+ updateFile.seekg(
+ ( accountNumber - 1 ) * sizeof( ClientData ) );
+
+ // read first record from file
+ ClientData client;
+ updateFile.read( reinterpret_cast< char * >( &client ),
+ sizeof( ClientData ) );
+
+ // update record
+ if ( client.getAccountNumber() != 0 ) {
+ outputLine( cout, client );
+
+ // request user to specify transaction
+ cout << "\nEnter charge (+) or payment (-): ";
+ double transaction; // charge or payment
+ cin >> transaction;
+
+ // update record balance
+ double oldBalance = client.getBalance();
+ client.setBalance( oldBalance + transaction );
+ outputLine( cout, client );
+
+ // move file-position pointer to correct record in file
+ updateFile.seekp(
+ ( accountNumber - 1 ) * sizeof( ClientData ) );
+
+ // write updated record over old record in file
+ updateFile.write(
+ reinterpret_cast< const char * >( &client ),
+ sizeof( ClientData ) );
+
+ } // end if
+
+ // display error if account does not exist
+ else
+ cerr << "Account #" << accountNumber
+ << " has no information." << endl;
+
+} // end function updateRecord
+
+// create and insert record
+void newRecord( fstream &insertInFile )
+{
+ // obtain number of account to create
+ int accountNumber = getAccount( "Enter new account number" );
+
+ // move file-position pointer to correct record in file
+ insertInFile.seekg(
+ ( accountNumber - 1 ) * sizeof( ClientData ) );
+
+ // read record from file
+ ClientData client;
+ insertInFile.read( reinterpret_cast< char * >( &client ),
+ sizeof( ClientData ) );
+
+ // create record, if record does not previously exist
+ if ( client.getAccountNumber() == 0 ) {
+
+ char lastName[ 15 ];
+ char firstName[ 10 ];
+ double balance;
+
+ // user enters last name, first name and balance
+ cout << "Enter lastname, firstname, balance\n? ";
+ cin >> setw( 15 ) >> lastName;
+ cin >> setw( 10 ) >> firstName;
+ cin >> balance;
+
+ // use values to populate account values
+ client.setLastName( lastName );
+ client.setFirstName( firstName );
+ client.setBalance( balance );
+ client.setAccountNumber( accountNumber );
+
+ // move file-position pointer to correct record in file
+ insertInFile.seekp( ( accountNumber - 1 ) *
+ sizeof( ClientData ) );
+
+ // insert record in file
+ insertInFile.write(
+ reinterpret_cast< const char * >( &client ),
+ sizeof( ClientData ) );
+
+ } // end if
+
+ // display error if account previously exists
+ else
+ cerr << "Account #" << accountNumber
+ << " already contains information." << endl;
+
+} // end function newRecord
+
+// delete an existing record
+void deleteRecord( fstream &deleteFromFile )
+{
+ // obtain number of account to delete
+ int accountNumber = getAccount( "Enter account to delete" );
+
+ // move file-position pointer to correct record in file
+ deleteFromFile.seekg(
+ ( accountNumber - 1 ) * sizeof( ClientData ) );
+
+ // read record from file
+ ClientData client;
+ deleteFromFile.read( reinterpret_cast< char * >( &client ),
+ sizeof( ClientData ) );
+
+ // delete record, if record exists in file
+ if ( client.getAccountNumber() != 0 ) {
+ ClientData blankClient;
+
+ // move file-position pointer to correct record in file
+ deleteFromFile.seekp( ( accountNumber - 1 ) *
+ sizeof( ClientData ) );
+
+ // replace existing record with blank record
+ deleteFromFile.write(
+ reinterpret_cast< const char * >( &blankClient ),
+ sizeof( ClientData ) );
+
+ cout << "Account #" << accountNumber << " deleted.\n";
+
+ } // end if
+
+ // display error if record does not exist
+ else
+ cerr << "Account #" << accountNumber << " is empty.\n";
+
+} // end deleteRecord
+
+// display single record
+void outputLine( ostream &output, const ClientData &record )
+{
+ output << left << setw( 10 ) << record.getAccountNumber()
+ << setw( 16 ) << record.getLastName().data()
+ << setw( 11 ) << record.getFirstName().data()
+ << setw( 10 ) << setprecision( 2 ) << right << fixed
+ << showpoint << record.getBalance() << endl;
+
+} // end function outputLine
+
+// obtain account-number value from user
+int getAccount( const char * const prompt )
+{
+ int accountNumber;
+
+ // obtain account-number value
+ do {
+ cout << prompt << " (1 - 100): ";
+ cin >> accountNumber;
+
+ } while ( accountNumber < 1 || accountNumber > 100 );
+
+ return accountNumber;
+
+} // end function getAccount
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.cpp new file mode 100644 index 0000000..3885357 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.cpp @@ -0,0 +1,105 @@ +// Fig. 14.11: ClientData.cpp
+// Class ClientData stores customer's credit information.
+#include <iostream>
+
+using std::string;
+
+#include <cstring>
+#include "clientData.h"
+
+// default ClientData constructor
+ClientData::ClientData( int accountNumberValue,
+ string lastNameValue, string firstNameValue,
+ double balanceValue )
+{
+ setAccountNumber( accountNumberValue );
+ setLastName( lastNameValue );
+ setFirstName( firstNameValue );
+ setBalance( balanceValue );
+
+} // end ClientData constructor
+
+// get account-number value
+int ClientData::getAccountNumber() const
+{
+ return accountNumber;
+
+} // end function getAccountNumber
+
+// set account-number value
+void ClientData::setAccountNumber( int accountNumberValue )
+{
+ accountNumber = accountNumberValue;
+
+} // end function setAccountNumber
+
+// get last-name value
+string ClientData::getLastName() const
+{
+ return lastName;
+
+} // end function getLastName
+
+// set last-name value
+void ClientData::setLastName( string lastNameString )
+{
+ // copy at most 15 characters from string to lastName
+ const char *lastNameValue = lastNameString.data();
+ int length = strlen( lastNameValue );
+ length = ( length < 15 ? length : 14 );
+ strncpy( lastName, lastNameValue, length );
+
+ // append null character to lastName
+ lastName[ length ] = '\0';
+
+} // end function setLastName
+
+// get first-name value
+string ClientData::getFirstName() const
+{
+ return firstName;
+
+} // end function getFirstName
+
+// set first-name value
+void ClientData::setFirstName( string firstNameString )
+{
+ // copy at most 10 characters from string to firstName
+ const char *firstNameValue = firstNameString.data();
+ int length = strlen( firstNameValue );
+ length = ( length < 10 ? length : 9 );
+ strncpy( firstName, firstNameValue, length );
+
+ // append new-line character to firstName
+ firstName[ length ] = '\0';
+
+} // end function setFirstName
+
+// get balance value
+double ClientData::getBalance() const
+{
+ return balance;
+
+} // end function getBalance
+
+// set balance value
+void ClientData::setBalance( double balanceValue )
+{
+ balance = balanceValue;
+
+} // end function setBalance
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.h b/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.h new file mode 100644 index 0000000..37afb23 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.h @@ -0,0 +1,56 @@ +// Fig. 14.10: clientData.h
+// Class ClientData definition used in Fig. 14.12–Fig. 14.15.
+#ifndef CLIENTDATA_H
+#define CLIENTDATA_H
+
+#include <iostream>
+
+using std::string;
+
+class ClientData {
+
+public:
+
+ // default ClientData constructor
+ ClientData( int = 0, string = "", string = "", double = 0.0 );
+
+ // accessor functions for accountNumber
+ void setAccountNumber( int );
+ int getAccountNumber() const;
+
+ // accessor functions for lastName
+ void setLastName( string );
+ string getLastName() const;
+
+ // accessor functions for firstName
+ void setFirstName( string );
+ string getFirstName() const;
+
+ // accessor functions for balance
+ void setBalance( double );
+ double getBalance() const;
+
+private:
+ int accountNumber;
+ char lastName[ 15 ];
+ char firstName[ 10 ];
+ double balance;
+
+}; // end class ClientData
+
+#endif
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file |
