- #1
duydaniel
- 6
- 0
Hello,
I am working on a program about credit card that basically, generate 16 ramdom digits, a starting balance and write it to a file. The file works as an database and can be updated later.
Here is my credit card class
Here is the method to create a random access file which can hold 1000 record
Here is the method of writing new record to the file (does not work somehow)
Any input would be appreciated.
Regard,
I am working on a program about credit card that basically, generate 16 ramdom digits, a starting balance and write it to a file. The file works as an database and can be updated later.
Here is my credit card class
Code:
class CCard
{
// variables section
private:
//sixteen digits of a credit card
int group1[4];
int group2[4];
int group3[4];
int group4[4];
double balance;
double cardNumber;
int ID;
// methods sections
public:
CCard(int=0); // constructor
double getAccount() const; // return account number
double getBalance() const; // return balance number
void setBalance(double);
void resetGroups(); // initialize 16 digits to 0
private: //should not be accessible by outside objects.
int randomNumber(); // return random number from 0->9
void setRandNumberToCard(); // set random number for card
int calculateCheckDigit(); // find the last digit of the card
double arrayToNumber(int [], double); // convert array of int to a double number
void convertGroupsToCardNumber(); //convert 16 digits to cardNumber (account)
};
Here is the method to create a random access file which can hold 1000 record
Code:
void createFile()
{
ofstream file("data.dat",ios::out|ios::binary);
if (!file){
cerr << "File could not be opended" << endl;
exit(1);
}
// empty object, all the value is 0
CCard card;
card.resetGroups();
//create 1000 accounts
for (int i = 0; i < 1000; i++)
{
file.write(reinterpret_cast<const char *>(&card),sizeof(CCard));
}
file.close();
}
Here is the method of writing new record to the file (does not work somehow)
Code:
void newRecord(fstream &insertFile)
{
double acc = 0.0;
cout << "\nEnter 3-7 :"; //possible starting digits of a credit card
cin >> acc;
//create an object which include 15 random digits of a card and balance
CCard card(acc);
// get the 16 digits account number (generated randomly)
double accountNumber = card.getAccount();
//move the pointer to the place
insertFile.seekp((accountNumber -1)*sizeof(CCard));
//write to the place (assume the place is empty)
insertFile.write(reinterpret_cast<char *>(&card),sizeof(CCard));
}
Regard,
Last edited: