#include <stdio.h>
/*Bir bankanın müşterilerine ait bilgiler, aşağıda verilen struct yapısı kullanılarak musteri.dat
isimli rastgele erişimli bir dosya da saklanmaktadır.
1-Banka müşterilerinden Hesabındaki para miktarı (Depozitoları) sıfırdan az
 olan kaç müşterinin olduğunu döndüren bir fonksiyon yazınız. Fonksiyonunuzu ana menüden çağırınız.
Function prototype: int listIndebtedAccounts(FILE *);
2-Müşteri bilgilerini, hesabında maksimum para tutarına sahip
 olacak şekilde döndüren bir fonksiyon yazınız. Hesabındaki para miktarı sıfırdan az olan dosyada kaç müşteri saklanır.
Function prototype: struct customer findMaxDeposit(FILE *);
3- Müşterilerinin tüm bilgilerini, hesabı 300'den büyük veya ona eşit olan “list.txt” adında
 sıralı bir erişim dosyasına aktaran fonksiyonu yazınız.
Function prototype: void transferRecords(FILE *);
4-Müşteri hesaplarını değerini 50 artıran bir fonksiyon yazın ve hesabı 300'den büyük veya ona eşit olan tüm müşterilerin
 kaydını güncelleyin.
Function prototype: void updateRecords(FILE *); */
void createFile();
void fileFill();
void fileList();
int listIndebtedAccounts();
void transferRecords();
void updateRecords();
struct customer{
 int account;
 char name[25];
 double deposit;
};
int main ()
{
 int secim,musteri;
 printf("1-Dosyadaki olustur\n2-Dosyayi Doldur\n3-Dosyayi Listele\n4-Depozitolari sifirdan azolan musteri sayisi\n5-Hesabi 300'den buyuk veya ona esit olan list.txt aktar\n6-Kayit guncelle\n");
while(1) {
 printf("Secim:");
 scanf("%d",&secim);
 if(secim==1)
 createFile();
 else if(secim==2)
 fileFill();
 else if(secim==3)
 fileList();
 else if (secim==4){
 musteri=listIndebtedAccounts();
 printf("musteri sayisi:%d\n",musteri);
 }
 else if (secim==5)
 transferRecords();
 else if (secim==6)
 updateRecords();
 else ;
}
 return 0;
}
void createFile(){
 int kontrol=1;
 FILE *filep=fopen("musteri.dat","w");
 struct customer musteri={0,"",0.0};
 if(filep=='\0')
 printf("Dosya Olusturulamadi!\n");
 else{
 for(kontrol;kontrol<=100;kontrol++)
 fwrite(&musteri,sizeof(struct customer),1,filep);
 }
 printf("Dosya Olusturuldu\n");
 fclose(filep);
}
void fileFill(){
 FILE *filep=fopen("musteri.dat","r+");
 struct customer musteri={0,"",0.0};
 if(filep=='\0')
 printf("Dosya Yolu Hatali!\n");
 else{
 printf("Musteri Bilgilerini Giriniz Yeterli Musteri Sayisina Ulastiginizda account=0 tuslayiniz\n");
 printf("Account:");
 scanf("%d",&musteri.account);
 while(musteri.account!=0){
 printf("Name:");
 scanf ("%s",musteri.name);
 printf("Depozit:");
 scanf("%lf",&musteri.deposit);
 fseek(filep,sizeof(struct customer)*(musteri.account-1),SEEK_SET);
 fwrite(&musteri,sizeof(struct customer),1,filep);
 printf("Account:");
 scanf("%d",&musteri.account);
 }
 }
 printf("Musteri Bilgileri Basarili Bir Sekilde Kaydedildi\n");
 fclose(filep);
}
void fileList(){
 FILE *filep=fopen("musteri.dat","r");
 struct customer musteri={0,"",0.0};
 if(filep=='\0')
 printf("Dosya Yolu Hatali!\n");
 else{
 fread(&musteri,sizeof(struct customer),1,filep);
 while(musteri.account!=0){
 printf("Account:%d Name:%s Deposit:%lf\n",musteri.account,musteri.name,musteri.deposit);
 fread(&musteri,sizeof(struct customer),1,filep);
 }
 }
 fclose (filep);
}
int listIndebtedAccounts(){
 FILE *filep=fopen("musteri.dat","r");
 struct customer musteri={0,"",0.0};
 int kontrol=0;
 if(filep=='\0')
 printf("Dosya Yolu Hatali!\n");
 else{
 while(!feof(filep)){
 fread(&musteri,sizeof(struct customer),1,filep);
 if(musteri.deposit<0)
 kontrol++;
 else ;
 }
 }
 fclose(filep);
 return kontrol;
}
void transferRecords(){
 FILE *filep1=fopen("list.txt","w");
 FILE *filep2=fopen("musteri.dat","r");
 struct customer musteri={0,"",0.0};
 if(filep2=='\0')
 printf("Dosya Yolu Hatali!");
 else{
 while(!feof(filep2)){
 fread(&musteri,sizeof(struct customer),1,filep2);
 if(musteri.deposit>=300)
 fwrite(&musteri,sizeof(struct customer),1,filep1);
 }
 }
 fclose(filep1);
 fclose(filep2);
 printf("Yeni Dosya Olusturuldu Ve Veriler Tasindi\n");
}
void updateRecords(){
 FILE *filep=fopen("musteri.dat","r+");
 struct customer musteri;
 if(filep=='\0')
 printf("Dosya Yolu Hatali!");
 else{
 int kontrol=1;
 fread(&musteri,sizeof(struct customer),1,filep);
 while(!feof(filep)){
 if(musteri.deposit>300){
 musteri.deposit+=50;
 fseek(filep,(kontrol-1)*sizeof(struct customer),SEEK_SET);
 fwrite(&musteri,sizeof(struct customer),1,filep);
 }
 kontrol++;
 fseek(filep,(kontrol-1)*sizeof(struct customer),SEEK_SET);
 fread(&musteri,sizeof(struct customer),1,filep);
 }
 fileList();
 }
 fclose(filep);
}