Created
December 8, 2017 03:45
-
-
Save merlinnusr/7bf098362ef357e02c230b8a4db06dc5 to your computer and use it in GitHub Desktop.
Sistema de cuenta con archivos binarios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Cliente.h" | |
Cliente::Cliente(int valorNumeroCuenta, const string &valorApellido, const string &valorPrimerNombre, double valorSaldo) | |
{ | |
establecerNumeroCuenta(valorNumeroCuenta); | |
establecerApellido(valorApellido); | |
establecerPrimerNombre(valorPrimerNombre); | |
establecerSaldo(valorSaldo); | |
}//fin del constructor | |
void Cliente::establecerNumeroCuenta(int valorNumeroCuenta) | |
{ | |
numeroCuenta = valorNumeroCuenta; | |
}//fin funcion establecerNumeroCuenta | |
int Cliente::obtenerNumeroCuenta()const | |
{ | |
return numeroCuenta; | |
}//retorna numeroCuenta | |
void Cliente::establecerApellido(const string &valorApellido) | |
{ | |
int longitud = valorApellido.size(); | |
longitud = (longitud < 15 ? longitud : 14); | |
valorApellido.copy(apellido, longitud); | |
apellido[longitud] = '\0'; | |
}//fin funcion establecerApellido | |
string Cliente::obtenerApellido()const | |
{ | |
return apellido; | |
}//retorna apellido | |
void Cliente::establecerPrimerNombre(const string &valorPrimerNombre) | |
{ | |
int longitud = valorPrimerNombre.size(); | |
longitud = (longitud < 10 ? longitud : 9); | |
valorPrimerNombre.copy(primerNombre, longitud); | |
primerNombre[longitud]= '\0'; | |
}//fin funcion establecerPrimerNombre | |
string Cliente::obtenerPrimerNombre()const | |
{ | |
return primerNombre; | |
}//retorna primerNombre | |
void Cliente::establecerSaldo(double valorSaldo) | |
{ | |
saldo = valorSaldo; | |
}//fin funcion establecerSaldo | |
double Cliente::obtenerSaldo()const | |
{ | |
return saldo; | |
}//retorna saldo | |
//muestra las cabeceras | |
void Cliente::cabeceras() | |
{ | |
cout << left << setw(10) << "CUENTA" << setw(16) << "APELLIDO" << setw(14) << "PRIMER NOMBRE" | |
<< left << setw(10) << right << "SALDO" << endl; | |
} //fin cabeceras | |
/////////////////////////////////////////////////////////////////////////////// | |
//ingresa numero de cuenta,nombre, apellido y saldo a un archivo binario | |
void Cliente::capturar(fstream &credito){ | |
int numeroCuenta=0; | |
while(numeroCuenta < 1 || numeroCuenta > 100){ | |
cout << " Escriba el numero de cuenta (de 1 a 100)\n"; | |
cin >> numeroCuenta; | |
} | |
credito.seekg((numeroCuenta - 1) *sizeof(Cliente)); | |
Cliente c; | |
credito.read(reinterpret_cast<char *>(&c), sizeof(Cliente)); | |
//el usuario proporciona la informacion que se guarda en el archivo | |
if(c.obtenerNumeroCuenta() == 0){ | |
char apellido[15]; | |
char primerNombre[10]; | |
double saldo; | |
cout << " Teclea apellido, primer nombre, saldo \n?"; | |
cin >> setw(15) >> apellido; | |
cin >> setw(10) >> primerNombre; | |
cin >> saldo; | |
// establecer los valores apellido, primerNombre, saldo del registro | |
c.establecerApellido(apellido); | |
c.establecerPrimerNombre(primerNombre); | |
c.establecerSaldo(saldo); | |
c.establecerNumeroCuenta(numeroCuenta); | |
//buscar la posicion en el archivo del registro especificado por el usuario | |
credito.seekp((numeroCuenta -1) *sizeof(Cliente)); | |
//seekp(n,ios::beg); | |
//escribir la informacion especificada por el usuario en el archivo | |
credito.write(reinterpret_cast<const char *>(&c), sizeof(Cliente)); | |
}//if | |
else | |
cerr << " El numero de cuenta " << numeroCuenta << " ya existe, favor de ingresar otro!\n"; | |
} //fin de ingresar | |
//sobrescribe un registro con un cliente en blanco | |
void Cliente::eliminar(fstream &credito){ | |
int numeroCuenta; | |
cout << " Dame el numero de cuenta que deseas eliminar\n"; | |
cin >> numeroCuenta; | |
credito.seekg((numeroCuenta - 1) * sizeof(Cliente)); | |
Cliente c; | |
credito.read(reinterpret_cast<char *>(&c), sizeof(Cliente)); | |
if(c.obtenerNumeroCuenta()!= 0){ | |
Cliente clienteEnBlanco; | |
credito.seekp((numeroCuenta - 1) * sizeof(Cliente)); | |
credito.write(reinterpret_cast<const char *>(&clienteEnBlanco), sizeof(Cliente)); | |
cout<<"El cliente se ha eliminado "<<endl<<endl; | |
}//fin de if | |
else | |
cerr << " El numero de cuenta no existe!\n"; | |
}//fin eliminar | |
// cambia el saldo actual del cliente | |
void Cliente::actualizar(fstream &credito) | |
{ | |
int nCuenta, transaccion, saldoAnterior; | |
cout << " Dame el numero de cuenta que deseas Actualizar\n"; | |
cin >> nCuenta; | |
credito.seekg((nCuenta - 1) * sizeof(Cliente)); // posiciona en el lugar indicado | |
Cliente c; | |
credito.read(reinterpret_cast<char *>(&c), sizeof(Cliente)); // lee el archivo | |
//actualizar el registro, es decir, actualizar el saldo unicamente | |
if(c.obtenerNumeroCuenta() != 0){ | |
c.cabeceras(); | |
credito.seekp((nCuenta - 1) * sizeof(Cliente)); | |
cout << left << setw(10) << c.obtenerNumeroCuenta() | |
<< setw(16) << c.obtenerApellido() | |
<< setw(14) << c.obtenerPrimerNombre() | |
<< setw(10) << setprecision (2) << right << fixed << showpoint << c.obtenerSaldo() | |
<< endl; | |
//solicitar al usuario que especifique la transacción | |
cout<<"\nEscriba cargo (+) o abono (-): "; | |
cin>>transaccion; | |
cout<<endl<<endl; | |
//actualizar el saldo del registro | |
saldoAnterior=c.obtenerSaldo(); | |
c.establecerSaldo(saldoAnterior + transaccion); | |
//mostrarRegistro | |
c.cabeceras(); | |
if(c.obtenerNumeroCuenta() != 0 ){ | |
credito.seekp((nCuenta - 1) * sizeof(Cliente)); | |
cout << left << setw(10) << c.obtenerNumeroCuenta() | |
<< setw(16) << c.obtenerApellido() | |
<< setw(14) << c.obtenerPrimerNombre() | |
<< setw(10) << setprecision (2) << right << fixed << showpoint << c.obtenerSaldo() | |
<< endl; | |
credito.seekg((nCuenta - 1) * sizeof(Cliente)); // posiciona en el lugar indicado | |
credito.write(reinterpret_cast<const char *>(&c), sizeof(Cliente)); | |
} //fin del if | |
else | |
{ | |
cerr<< "El numero de cuenta no existe "<<endl<<endl; | |
} | |
}//fin if | |
else | |
cerr<<"El numero de cuenta no existe"<<endl; | |
} //actualizar | |
//posiciona el puntero segun el numero de cuenta del cliente , muestra sus elementos y la diferencia | |
void Cliente::consultasI(fstream &credito) | |
{ | |
int nCuenta; | |
cout<<"Dame el numero de cuenta a mostrar: "; | |
cin>>nCuenta; | |
Cliente c;//un objeto o registro cliente | |
credito.seekg((nCuenta - 1) * sizeof(Cliente)); // nos movemos a la posicion en el archivo fisico | |
credito.read(reinterpret_cast< char *>(&c), sizeof(Cliente)); // lee archivo fisico | |
if(c.obtenerNumeroCuenta() != 0 ){ | |
c.cabeceras(); | |
credito.seekp((numeroCuenta - 1) * sizeof(Cliente)); | |
cout << left << setw(10) << c.obtenerNumeroCuenta() | |
<< setw(16) << c.obtenerApellido() | |
<< setw(14) << c.obtenerPrimerNombre() | |
<< setw(10) << setprecision (2) << right << fixed << showpoint << c.obtenerSaldo() | |
<< endl; | |
}//fin de if | |
else { | |
cerr<<"El numero de cuenta no existe"<<endl<<endl; | |
}// fin del ese | |
}// fin consultas individuales | |
// genera un barrido en el archivo para mostrar todos los registros siempre que el numero de cuenta sea diferente de 0 | |
void Cliente::consultasG(fstream &creditoEntrada) | |
{ | |
Cliente c;//un objeto o registro cliente | |
creditoEntrada.read(reinterpret_cast< char *>(&c), sizeof(Cliente)); // lee archivo fisico | |
c.cabeceras(); | |
while(creditoEntrada && !creditoEntrada.eof()){ | |
if(c.obtenerNumeroCuenta() != 0){ | |
cout << left << setw(10) << c.obtenerNumeroCuenta() | |
<< setw(16) << c.obtenerApellido() | |
<< setw(14) << c.obtenerPrimerNombre() | |
<< setw(10) << setprecision (2) << right << fixed << showpoint << c.obtenerSaldo() | |
<< endl; | |
}//fin de if | |
creditoEntrada.read(reinterpret_cast< char *>(&c), sizeof(Cliente)); | |
}//fin de while | |
}//consultas generales | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef CLIENTE_H | |
#define CLIENTE_H | |
#include <iostream> | |
#include <iomanip> | |
#include <string.h> | |
#include <fstream> | |
using namespace std; | |
class Cliente | |
{ | |
private: | |
int numeroCuenta; | |
char apellido[15]; | |
char primerNombre[10]; | |
double saldo; | |
public: | |
Cliente(int = 0, const string & = " ", const string & = " ", double = 0.0); | |
void establecerNumeroCuenta(int); | |
int obtenerNumeroCuenta()const; | |
void establecerApellido(const string &); | |
string obtenerApellido()const; | |
void establecerPrimerNombre(const string &); | |
string obtenerPrimerNombre()const; | |
void establecerSaldo(double); | |
double obtenerSaldo()const; | |
void capturar(fstream&); | |
void eliminar(fstream&); | |
void actualizar(fstream& ); | |
void consultasI(fstream&); | |
void consultasG(fstream&); | |
void cabeceras(); | |
};//fin de la clase | |
#endif // CLIENTE_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <cstdlib>//exit | |
#include "Cliente.h" | |
using namespace std; | |
int main() | |
{ | |
fstream creditoEntradaSalida("credito.dat", ios::in | ios::out); | |
if(!creditoEntradaSalida){ | |
cerr << " No se pudo abrir el archivo. " << endl; | |
exit(1); | |
}///if | |
char opcion; | |
Cliente c; | |
bool salir=false; | |
while(!salir){ | |
cout << " Menu - Sistema de cuentas\n"; | |
cout << " 1. Altas\n"; | |
cout << " 2. Bajas(Eliminar un registro)\n"; | |
cout << " 3. Cambios(Actualizar el saldo de la cuenta)\n"; | |
cout << " 4. Consultas individuales\n"; | |
cout << " 5. Consultas generales\n"; | |
cout << " 6. Salir\n\n"; | |
cout << " Elige tu opcion:\t\n\n"; | |
cin >> opcion; | |
cout<<endl<<endl; | |
switch(opcion){ | |
case '1': | |
c.capturar(creditoEntradaSalida); | |
break; | |
case '2': | |
c.eliminar(creditoEntradaSalida); | |
break; | |
case '3': | |
c.actualizar(creditoEntradaSalida); | |
break; | |
case '4': | |
c.consultasI(creditoEntradaSalida); | |
break; | |
case '5': | |
c.consultasG(creditoEntradaSalida); | |
break; | |
case '6': | |
salir=true; | |
break; | |
default: | |
cout << " Opcion invalida elige una opcion valida!\n"; | |
}//fin del switch | |
cout<<endl<<endl; | |
creditoEntradaSalida.clear(); | |
creditoEntradaSalida.seekg(0); | |
}//fin de while | |
return 0; | |
}//fin del main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment