Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace TokoCNS
- {
- class Program
- {
- static string username;
- static string password;
- static char command;
- static int stock;
- static void Main(string[] args)
- {
- Register();
- }
- static void Register()
- {
- Console.Clear();
- Console.WriteLine("Halaman registrasi\n");
- Console.WriteLine("Masukkan username");
- username = Console.ReadLine();
- if(!string.IsNullOrEmpty(username))
- {
- Console.WriteLine("Masukkan password");
- password = Console.ReadLine();
- if(string.IsNullOrEmpty(password))
- Register();
- else if(!string.IsNullOrEmpty(password))
- if(!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
- Login();
- }
- else
- Register();
- }
- static void Login()
- {
- Console.Clear();
- Console.WriteLine("Halaman login\n");
- Console.WriteLine("Masukkan username");
- string newUsername = Console.ReadLine();
- if(newUsername == username)
- {
- Console.WriteLine("Masukkan password");
- string newPassword = Console.ReadLine();
- if(newPassword == password)
- HalamanUtama();
- else
- Login();
- }
- else
- Login();
- }
- static void HalamanUtama()
- {
- Console.Clear();
- Console.WriteLine("Selamat datang di Toko Buku\n");
- Console.WriteLine("1. Beli buku");
- Console.WriteLine("2. Beli alat tulis");
- Console.WriteLine("3. Beli tas");
- Console.Write("Pilihan: ");
- try
- {
- command = Char.Parse(Console.ReadLine());
- }
- catch(Exception)
- {
- Console.WriteLine("Input tidak valid!");
- HalamanUtama();
- }
- finally
- {
- Transaksi(command);
- }
- }
- static void Transaksi(char command)
- {
- int jumlah;
- switch(command)
- {
- case '1':
- stock = 50;
- Console.WriteLine($"Stock buku: {stock}");
- Console.Write("Jumlah buku: ");
- jumlah = int.Parse(Console.ReadLine());
- Hitung(command, jumlah);
- break;
- case '2':
- stock = 200;
- Console.WriteLine($"Stock alat tulis: {stock}");
- Console.Write("Jumlah alat tulis: ");
- jumlah = int.Parse(Console.ReadLine());
- Hitung(command, jumlah);
- break;
- case '3':
- stock = 10;
- Console.WriteLine($"Stock tas: {stock}");
- Console.Write("Jumlah tas: ");
- jumlah = int.Parse(Console.ReadLine());
- Hitung(command, jumlah);
- break;
- default:
- Console.WriteLine("Input tidak valid!");
- HalamanUtama();
- break;
- }
- }
- static void Hitung(char command, int jumlah)
- {
- double totalHarga;
- double jumlahUang;
- switch(command)
- {
- case '1':
- totalHarga = 3000 * jumlah;
- Console.WriteLine($"Total harga: {totalHarga}");
- Console.WriteLine("Masukkan jumlah uang anda: ");
- jumlahUang = double.Parse(Console.ReadLine());
- if(jumlahUang < totalHarga)
- {
- Console.WriteLine("Jumlah uang tidak cukup");
- Hitung(command, jumlah);
- }
- else
- {
- stock -= jumlah;
- CetakStruk(totalHarga, jumlahUang, jumlah, command);
- }
- break;
- case '2':
- totalHarga = 2000 * jumlah;
- Console.WriteLine($"Total harga: {totalHarga}");
- Console.WriteLine("Masukkan jumlah uang anda: ");
- jumlahUang = double.Parse(Console.ReadLine());
- if(jumlahUang < totalHarga)
- {
- Console.WriteLine("Jumlah uang tidak cukup");
- Hitung(command, jumlah);
- }
- else
- {
- stock -= jumlah;
- CetakStruk(totalHarga, jumlahUang, jumlah, command);
- }
- break;
- case '3':
- totalHarga = 600000 * jumlah;
- Console.WriteLine($"Total harga: {totalHarga}");
- Console.WriteLine("Masukkan jumlah uang anda: ");
- jumlahUang = double.Parse(Console.ReadLine());
- if(jumlahUang < totalHarga)
- {
- Console.WriteLine("Jumlah uang tidak cukup");
- Hitung(command, jumlah);
- }
- else
- {
- stock -= jumlah;
- CetakStruk(totalHarga, jumlahUang, jumlah, command);
- }
- break;
- }
- }
- static void CetakStruk(double totalHarga, double jumlahUang, int jumlahBarang, char command)
- {
- string namaBarang = "";
- double kembalian = jumlahUang - totalHarga;
- switch(command)
- {
- case '1':
- namaBarang = "Buku";
- break;
- case '2':
- namaBarang = "Alat tulis";
- break;
- case '3':
- namaBarang = "Tas";
- break;
- }
- string formatStruk = $@"
- Toko Buku
- =============================
- Nama pembeli: {username}
- Tanggal pembelian: {DateTime.Now}
- Nama barang: {namaBarang}
- Jumlah barang: {jumlahBarang}
- Total harga: {totalHarga}
- Jumlah uang: {jumlahUang}
- Kembalian: {kembalian}
- =============================
- Terima kasih sudah berbelanja";
- string path = @"D:\Struk.txt";
- if(File.Exists(path))
- {
- var stream = File.Create(path);
- stream.Close();
- }
- else
- {
- var stream = File.Create(path);
- stream.Close();
- }
- File.WriteAllText(path, formatStruk);
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement