Yaph inilah tugas terkahir buat mata kuliah praktikumku Algoritma Pemrograman dengan Bahasa Pemrograman C++ menggunakan IDE Borland 5.02 yang menyebalkan. Program ini bisa Read, Edit, Delete, Add Data di File TXT. Oke langsung saja yah …
#include <iostream>
#include <fstream>
#include <string>
#include <stdio>
#include <windows>
#include <vector>
using namespace std;
void menuAwal();
int inpmenu;
char tanya;
string baris;
string bacaFile;
string inputBaru;
vector <string> explode(const char& chSparate, const string& strText){
string next = "";
vector <string> result;
for(string::const_iterator it = strText.begin(); it != strText.end(); it++){
if(*it == chSparate){
if(next.length() > 0){
result.push_back(next);
next = "";
}
}else{
next += *it;
}
}
return result;
}
string& str_replace(const string &search, const string &replace, string &subject){
string buffer;
int sealeng = search.length();
int strleng = subject.length();
if (sealeng==0)
return subject;
for(int i=0, j=0; i<strleng; j=0 ){
while (i+j<strleng && j<sealeng && subject[i+j]==search[j])
j++;
if (j==sealeng){
buffer.append(replace);
i+=sealeng;
}else{
buffer.append( &subject[i++], 1);
}
}
subject = buffer;
return subject;
}
void cariBuku(string cariKodeBuku){
string tampungHasilExplodes;
vector <string> detailDataBarang;
vector <string> detailKodeBarang;
bacaFile = "";
system("cls");
cariKodeBuku = "["+cariKodeBuku+"]";
ifstream db;
db.open("dataBarang.txt");
while(!db.eof()){
getline(db,baris,'\n');
bacaFile += baris+"\n";
}
db.close();
detailDataBarang = explode('\n',bacaFile);
for(size_t i = 0; i < detailDataBarang.size(); i++){
if(detailDataBarang[i].find(cariKodeBuku)!=string::npos){
tampungHasilExplodes += detailDataBarang[i]+"\n ";
}
}
tampungHasilExplodes = str_replace("|","\t",tampungHasilExplodes);
tampungHasilExplodes = str_replace("=","\t",tampungHasilExplodes);
cout << "\n -------------------------------------------------" <<endl;
cout << " " << tampungHasilExplodes;
cout << "-------------------------------------------------" <<endl;
fflush(stdin);
cout << " Kembali ke Menu Awal ? [y/t] : ";
cin >> tanya;
if(tanya=='y') menuAwal();
else exit(0);
}
void editBuku(string cariKodeBuku){
int penanda;
string tampungExp, tampungBaru, tambahanKarakter;
string namaBukuBaru, stokBukuBaru, hargaBukuBaru;
vector <string> detailDataBarang;
ifstream db;
db.open("dataBarang.txt");
while(!db.eof()){
getline(db,baris,'\n');
bacaFile += baris+"\n";
}
db.close();
cariKodeBuku = "["+cariKodeBuku+"]";
if(bacaFile.find(cariKodeBuku)==string::npos){
cout << "Maaf, buku dengan kode tersebut tidak ada";
Sleep(1000);
menuAwal();
}
detailDataBarang = explode('\n',bacaFile);
for(size_t i = 0; i < detailDataBarang.size(); i++){
if(detailDataBarang[i].find(cariKodeBuku)!=string::npos){
tampungExp += detailDataBarang[i]+"\n";
penanda = i;
}
}
if(penanda==0 || penanda==detailDataBarang.size()){
tambahanKarakter = "\n";
}
cout << "Nama Buku yang baru : ";
cin >> namaBukuBaru;
cout << "Stok barang yang baru : ";
cin >> stokBukuBaru;
cout << "Harga barang yang baru : Rp ";
cin >> hargaBukuBaru;
tampungBaru = cariKodeBuku+"="+namaBukuBaru+"|"+stokBukuBaru+"|"+
hargaBukuBaru+"|"+tambahanKarakter;
tampungExp = "";
for(size_t i = 0; i < detailDataBarang.size(); i++){
if(penanda==i){
tampungExp += tampungBaru;
continue;
}else{
tampungExp += detailDataBarang[i]+"\n";
}
}
tampungExp = tampungExp.substr(0,(tampungExp.length() - 2));
ofstream fl;
fl.open("dataBarang.txt",ios::trunc);
fl << tampungExp;
fl.close();
cout << "Buku berhasil dirubah. Terima Kasih";
Sleep(1000);
menuAwal();
}
void inputDataBarang(){
system("cls");
string kodeBarang, namaBarang, stokBarang, hargaBarang;
cout << "Masukkan Kode Barang : ";
cin >> kodeBarang;
bacaFile = "";
ifstream db;
db.open("dataBarang.txt");
while(!db.eof()){
getline(db,baris,'\n');
bacaFile += baris+"\n";
}
db.close();
if(bacaFile.find(kodeBarang)!=string::npos){
cout << "Maaf, barang dengan kode tersebut sudah ada sebelumnya";
Sleep(1000);
menuAwal();
}
cout << "\nMasukkan Nama Barang : ";
cin >> namaBarang;
cout << "Masukkan Jumlah Stok Barang : ";
cin >> stokBarang;
cout << "Masukkan Harga Barang : ";
cin >> hargaBarang;
inputBaru = "["+kodeBarang+"]="+namaBarang+"|"+stokBarang+"|"+hargaBarang+"|";
ofstream fl;
fl.open("dataBarang.txt");
fl << bacaFile;
fl << inputBaru;
fl.close();
cout << "Barang baru berhasil ditambahkan." << endl;
Sleep(1000);
menuAwal();
}
void hapusDataBarang(string cariKodeBuku){
string tampungExp;
vector <string> detailDataBarang;
bacaFile = "";
system("cls");
ifstream db;
db.open("dataBarang.txt");
while(!db.eof()){
getline(db,baris,'\n');
bacaFile += baris+"\n";
}
db.close();
detailDataBarang = explode('\n',bacaFile);
cariKodeBuku = "["+cariKodeBuku+"]";
for(size_t i = 0; i < detailDataBarang.size(); i++) {
if(detailDataBarang[i].substr(0,4)!=cariKodeBuku){
tampungExp += detailDataBarang[i]+"\n";
}
}
tampungExp = tampungExp.substr(0,(tampungExp.length() - 2));
ofstream fl;
fl.open("dataBarang.txt",ios::trunc);
fl << tampungExp;
fl.close();
cout << "Barang telah berhasil dihapus." << endl;
Sleep(1000);
menuAwal();
}
void laporan(){
bacaFile = "";
system("cls");
ifstream db;
db.open("dataBarang.txt");
while(!db.eof()){
getline(db,baris,'\n');
bacaFile += baris+"\n";
}
db.close();
cout << "Kode\tNama Barang\tStok\tHarga Barang" <<endl;
cout << "--------------------------------------------" <<endl;
bacaFile = str_replace("|","\t",bacaFile);
bacaFile = str_replace("=","\t",bacaFile);
cout << bacaFile;
cout << "--------------------------------------------\n" <<endl;
fflush(stdin);
cout << "Kembali ke Menu Awal ? [y/t] : ";
cin >> tanya;
if(tanya=='y') menuAwal();
else system("exit");
system("pause");
}
void menuAwal(){
system("cls");
cout << "+-----------------------------------+"<< endl;
cout << "| Silakan Pilih Menu |"<< endl;
cout << "+-----------------------------------+"<< endl;
cout << "| 1. Lihat Data Barang |"<< endl;
cout << "| 2. Cari Data Barang |"<< endl;
cout << "| 3. Input Data Barang |"<< endl;
cout << "| 4. Edit Data Barang |"<< endl;
cout << "| 5. Hapus Data Barang |"<< endl;
cout << "+-----------------------------------+"<< endl;
cout << "\nPilihan Anda : ";
fflush(stdin);
cin >> inpmenu;
cout << endl;
if(inpmenu==1){
laporan();
}else if(inpmenu==2){
string cariKodeBarang;
cout << "Masukkan kode barang : ";
cin >> cariKodeBarang;
cariBuku(cariKodeBarang);
}else if(inpmenu==3){
inputDataBarang();
}else if(inpmenu==4){
string cariKodeBarang;
cout << "Masukkan kode barang : ";
cin >> cariKodeBarang;
editBuku(cariKodeBarang);
}else if(inpmenu==5){
string cariKodeBarang;
cout << "Masukkan kode barang : ";
cin >> cariKodeBarang;
hapusDataBarang(cariKodeBarang);
}else{
cout << "Terima Kasih";
Sleep(1000);
system("exit");
}
system("cls");
}
int main(){
menuAwal();
return EXIT_SUCCESS;
}
Untuk contoh isi file dataBarang.txt, silahkan lihat dibawah ini :
[01]=Keyboard|100|5000|
[02]=Sikat Gigi|100|5000|
[03]=Sabun Mandi|100|15000|
[04]=Obat Nyamuk|100|25000|
[05]=Minyal Goreng|100|35000|
[06]=Gula Pasir|100|45000|
[07]=Garam Dapur|100|55000|
[08]=Susu Kental|100|65000|
[09]=Kopi Bubuk|100|75000|
[10]=Teh Celup|100|85000|
-7.338006112.690159
You must be logged in to post a comment.