Bank 3
#include <iostream>
#include <fstream>
#include<cstdio>
#include<stdio.h>
#include <string>
#include "Header.h";
#include <stdlib.h>
#include <time.h>
using namespace std;
struct Balance
{
char type[10];
int amount;
char date[20];
};
class Bank {
int amount;
public:
void CreateAccount() {
char name[250];
string dob;
string mobile;
char address[300];
string account;
cout << "Please Input Name:\n";
cin.getline(name, 250);
cout << "Please Input DOB:\n";
cin >> dob;
cout << "Please Input Mobile:\n";
cin >> mobile;
cin.ignore();
cout << "Please Input address:\n";
cin >> address;
cout << "Please Input account No:\n";
cin >> account;
//==============P=======
ofstream file_p(account+"p.txt");
if (file_p.is_open()) {
file_p << "Name=" << name << endl;
file_p << "DOB=" << dob << endl;
file_p << "Mobile" << mobile << endl;
file_p << "Address=" << address << endl;
file_p.close();
}
//==============P=======
ofstream file_b(account + "b.txt");
file_b.close();
}
void ShowBalence() {
string account;
cout << "Please Input account No:\n";
cin >> account;
ifstream file_p(account + "p.txt");
if (file_p.is_open()) {
string tempdata;
while (getline(file_p,tempdata))
{
cout << tempdata <<endl;
}
}
file_p.close();
//=================Show Balance======
ifstream file_b(account + "b.txt",ios::binary);
if (file_b.is_open()) {
Balance balance;
int i = 1;
cout << "| SR | Action Type | Amount | Date " << endl;
// Read until the end of the file
while (file_b.read(reinterpret_cast<char*>(&balance), sizeof(Balance))) {
// Output the read data
cout << "| " << i++ << " | "
<< balance.type << " | "
<< balance.amount << " | "
<< balance.date << " |" << endl;
}
// Check for any read errors
if (file_b.eof()) {
cout << "Reached end of file." << endl;
}
else if (file_b.fail()) {
cout << "Error reading file." << endl;
}
file_b.close();
/*while (file_b.read(reinterpret_cast<char*>(&balance), sizeof(balance))) {
cout << "| " << i++ << " | " << balance.type << " | " << balance.amount << " | " << balance.date << " |" << endl;
}*/
}
}
//Jama
void CreditAmount() {
Balance balance;
string account;
cout << "Please Input account No:\n";
cin >> account;
cout << "Please Input credit amount:\n";
cin >>balance.amount;
cout << "Please Input current date:\n";
cin >> balance.date;
cout << "Please Input opration type:\n";
cin >> balance.type;
ofstream file_b(account + "b.txt", ios::binary | ios::out |ios::app);
if (file_b.is_open()) {
file_b.write(reinterpret_cast<char*>(&balance), sizeof(balance));
file_b.close();
}
}
int GenrateAccountNumber() {
// Seed the random number generator with the current time
srand(time(0));
// Generate a random number between 1 and 100
int random_number = rand() % 10000 + 1;
return random_number;
}
};
int main() {
Bank bank;
bank.ShowBalence();
//bank.CreditAmount();
//bank.CreateAccount();
//cout << bank.GenrateAccountNumber()<<endl;
/*
//============Create Binary file==
ofstream f_write("test.txt", ios::binary);
if (f_write.is_open()) {
for (int i = 0; i <= 10; i++) {
f_write.write(reinterpret_cast<char*>(&i), sizeof(i));
}
f_write.close();
}
//===================read data form ==========
int sum = 0;
fstream f_read("test.txt", ios::binary|ios::in|ios::out);
if (f_read.is_open()) {
for (int i = 0; i <= 10; i++) {
int j;
f_read.read(reinterpret_cast<char*>(&j), sizeof(j));
sum = sum + j;
}
f_read.close();
cout << "Sum=" << sum;
}
*/
return 0;
}
Comments
Post a Comment