Sistem Kasir - Tugas Individu (OOP dan Operasi File)
Sistem Kasir
main.cpp
#include "kasir.h"
using namespace std;
// Judul : Tugas Individu Algoritma Pemrograman
// Najma Amira Mumtaz
// A11.2022.14708
//
// Deskripsi
int main() {
// Kamus
char pil;
int kodetransaksi;
do {
cout << setw(45) << setfill('=') << "=" << endl;
cout << setw(42) << setfill(' ') << "- WELCOME TO AMEERA SWALAYAN SEMARANG -" << endl;
cout << setw(45) << setfill('=') << "=" << endl;
cout << setw(27) << setfill(' ') << "PILIH MENU" << endl;
cout << setw(45) << setfill('-') << "-" << endl;
cout << "1. Buat Transaksi" << endl;
cout << "2. Cari Struk" << endl;
cout << "0. Keluar" << endl;
cout << setw(45) << setfill('-') << "-" << endl;
cout << "Menu yang dipilih: ";
cin >> pil;
cout << setw(45) << setfill('-') << "-" << endl << endl << endl;
if(pil == '1'){
kasir transaksi;
transaksi.kopkasir();
transaksi.inputtransaksi();
cout << setw(45) << setfill('-') << "-" << endl;
transaksi.cetakstruk();
}else if (pil == '2'){
cout << "Masukkan kode transaksi yang dicari: ";
cin >> kodetransaksi;
kasir transaksi;
transaksi.caristruk(kodetransaksi);
}else if (pil != '0'){
cout << "Pilihan tidak valid." << endl;
}
cout << "Apakah Anda ingin kembali ke menu pilihan awal? (Y/N): ";
cin >> pil;
cout << endl;
}while(pil == 'Y' || pil == 'y');
return 0;
}
kasir.h
#ifndef KASIR_H
#define KASIR_H
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
class kasir {
public:
int kode_trans;
int tanggal;
int bulan;
int tahun;
string kode_prod[10];
int qty[10];
char pil;
int x;
int i;
int totalharga;
int harga;
int subtotal;
string namaproduk;
void kopkasir(){
cout << setw(45) << setfill('=') << "=" << endl;
cout << setw(29) << setfill(' ') << "TRANSAKSI BARU" << endl;
cout << setw(45) << setfill('=') << "=" << endl;
cout << "Hai, kasir!" << endl;
cout << "Masukkan data transaksi di bawah ini" << endl;
cout << setw(45) << setfill('-') << "-" << endl;
}
void inputtransaksi(){
cout << "Kode Transaksi: ";
cin >> kode_trans;
cout << "Tanggal: ";
cin >> tanggal;
cout << "Bulan: ";
cin >> bulan;
cout << "Tahun: ";
cin >> tahun;
cout << endl;
x = 0;
do{
cout << "Kode Produk: ";
cin >> kode_prod[x];
cout << "Jumlah produk: ";
cin >> qty[x];
cout << "Tambah produk?(Y/N): ";
cin >> pil;
x = x + 1;
}while(pil == 'Y' || pil == 'y');
}
string dataproduk(string kd_prod){
if(kd_prod == "A1"){
return "Mi lemonilo";
}else if(kd_prod == "A2"){
return "Sariroti kj";
}else if(kd_prod == "B1"){
return "Air mineral";
}else if (kd_prod == "B2"){
return "Pocarisweat";
}
return "Produk tidak ditemukan";
}
int hargaproduk(string kd_prod){
if(kd_prod == "A1"){
return 6000;
}else if(kd_prod == "A2"){
return 2500;
}else if(kd_prod == "B1"){
return 3000;
} else if(kd_prod == "B2"){
return 5000;
}
return 0;
}
void outputtransaksi(){
int harga;
int subtotal;
cout << endl << endl;
cout << setw(45) << setfill('-') << "-" << endl;
cout << setw(29) << setfill(' ') << right << "STRUK BELANJA" << endl;
cout << setw(34) << setfill(' ') << right << "AMEERA SWALAYAN SEMARANG" << endl;
cout << setw(45) << setfill('-') << "-" << endl;
cout << tanggal << "-" << bulan << "-" << tahun << endl;
cout << "Kode Transaksi: " << kode_trans << endl;
cout << setw(45) << setfill('-') << "-" << endl;
cout << setw(8) << setfill(' ') << right << "PRODUK" << setw(12) << setfill(' ') << right << "JUMLAH" << setw(10) << setfill(' ') << right << "HARGA" << setw(14) << setfill(' ') << right << "SUBTOTAL" << endl;
i = 0;
totalharga = 0;
do{
namaproduk = dataproduk(kode_prod[i]);
harga = hargaproduk(kode_prod[i]);
subtotal = harga * qty[i];
cout << namaproduk << setw(6) << setfill(' ') << right << qty[i] << setw(8) << setfill(' ') << right << "Rp" << harga << ",00" << setw(5) << setfill(' ') << right << "Rp" << subtotal << ",00" << endl;
totalharga = totalharga + subtotal;
i = i + 1;
}while(i < x);
cout << setw(45) << setfill('=') << "=" << endl;
cout << "Total Harga: Rp" << totalharga << ",00" << endl;
cout << setw(45) << setfill('=') << "=" << endl;
}
void cetakstruk(){
ofstream file("struk.txt", ios::app);
if(file.is_open()){
file << setw(45) << setfill('-') << "-" << endl;
file << setw(29) << setfill(' ') << right << "STRUK BELANJA" << endl;
file << setw(34) << setfill(' ') << right << "AMEERA SWALAYAN SEMARANG" << endl;
file << setw(45) << setfill('-') << "-" << endl;
file << tanggal << "-" << bulan << "-" << tahun << endl;
file << "Kode Transaksi: " << kode_trans << endl;
file << setw(45) << setfill('-') << "-" << endl;
file << setw(8) << setfill(' ') << right << "PRODUK" << setw(12) << setfill(' ') << right << "JUMLAH" << setw(10) << setfill(' ') << right << "HARGA" << setw(14) << setfill(' ') << right << "SUBTOTAL" << endl;
i = 0;
totalharga = 0;
do{
namaproduk = dataproduk(kode_prod[i]);
harga = hargaproduk(kode_prod[i]);
subtotal = harga * qty[i];
file << namaproduk << setw(6) << setfill(' ') << right << qty[i] << setw(8) << setfill(' ') << right << "Rp" << harga << ",00" << setw(5) << setfill(' ') << right << "Rp" << subtotal << ",00" << endl;
totalharga = totalharga + subtotal;
i = i + 1;
}while(i < x);
file << setw(45) << setfill('=') << "=" << endl;
file << "Total Harga: Rp" << totalharga << ",00" << endl;
file << setw(45) << setfill('=') << "=" << endl;
file.close();
cout << "Struk berhasil dicetak." << endl;
}else{
cout << "Gagal membuka file struk.txt" << endl;
}
}
void caristruk(int kodetransaksi){
ifstream file("struk.txt");
string cari;
bool found = false;
if(file.is_open()){
while(getline(file, cari)){
if(cari.find("Kode Transaksi: " + to_string(kodetransaksi)) != string::npos){
found = true;
cout << cari << endl;
while(getline(file, cari)){
cout << cari << endl;
}
break;
}
}
file.close();
if (!found) {
cout << "Struk dengan kode transaksi " << kodetransaksi << " tidak ditemukan." << endl;
}
}else{
cout << "Gagal membuka file struk.txt" << endl;
}
}
};
#endif
Komentar
Posting Komentar