Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace TokoCNS
  5. {
  6. class Program
  7. {
  8. static string username;
  9. static string password;
  10. static char command;
  11. static int stock;
  12. static void Main(string[] args)
  13. {
  14. Register();
  15. }
  16. static void Register()
  17. {
  18. Console.Clear();
  19. Console.WriteLine("Halaman registrasi\n");
  20. Console.WriteLine("Masukkan username");
  21. username = Console.ReadLine();
  22. if(!string.IsNullOrEmpty(username))
  23. {
  24. Console.WriteLine("Masukkan password");
  25. password = Console.ReadLine();
  26. if(string.IsNullOrEmpty(password))
  27. Register();
  28. else if(!string.IsNullOrEmpty(password))
  29. if(!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
  30. Login();
  31. }
  32. else
  33. Register();
  34. }
  35. static void Login()
  36. {
  37. Console.Clear();
  38. Console.WriteLine("Halaman login\n");
  39. Console.WriteLine("Masukkan username");
  40. string newUsername = Console.ReadLine();
  41. if(newUsername == username)
  42. {
  43. Console.WriteLine("Masukkan password");
  44. string newPassword = Console.ReadLine();
  45. if(newPassword == password)
  46. HalamanUtama();
  47. else
  48. Login();
  49. }
  50. else
  51. Login();
  52. }
  53. static void HalamanUtama()
  54. {
  55. Console.Clear();
  56. Console.WriteLine("Selamat datang di Toko Buku\n");
  57. Console.WriteLine("1. Beli buku");
  58. Console.WriteLine("2. Beli alat tulis");
  59. Console.WriteLine("3. Beli tas");
  60. Console.Write("Pilihan: ");
  61. try
  62. {
  63. command = Char.Parse(Console.ReadLine());
  64. }
  65. catch(Exception)
  66. {
  67. Console.WriteLine("Input tidak valid!");
  68. HalamanUtama();
  69. }
  70. finally
  71. {
  72. Transaksi(command);
  73. }
  74. }
  75. static void Transaksi(char command)
  76. {
  77. int jumlah;
  78.  
  79. switch(command)
  80. {
  81. case '1':
  82. stock = 50;
  83. Console.WriteLine($"Stock buku: {stock}");
  84. Console.Write("Jumlah buku: ");
  85. jumlah = int.Parse(Console.ReadLine());
  86. Hitung(command, jumlah);
  87. break;
  88. case '2':
  89. stock = 200;
  90. Console.WriteLine($"Stock alat tulis: {stock}");
  91. Console.Write("Jumlah alat tulis: ");
  92. jumlah = int.Parse(Console.ReadLine());
  93. Hitung(command, jumlah);
  94. break;
  95. case '3':
  96. stock = 10;
  97. Console.WriteLine($"Stock tas: {stock}");
  98. Console.Write("Jumlah tas: ");
  99. jumlah = int.Parse(Console.ReadLine());
  100. Hitung(command, jumlah);
  101. break;
  102. default:
  103. Console.WriteLine("Input tidak valid!");
  104. HalamanUtama();
  105. break;
  106. }
  107. }
  108. static void Hitung(char command, int jumlah)
  109. {
  110. double totalHarga;
  111. double jumlahUang;
  112. switch(command)
  113. {
  114. case '1':
  115. totalHarga = 3000 * jumlah;
  116. Console.WriteLine($"Total harga: {totalHarga}");
  117. Console.WriteLine("Masukkan jumlah uang anda: ");
  118. jumlahUang = double.Parse(Console.ReadLine());
  119. if(jumlahUang < totalHarga)
  120. {
  121. Console.WriteLine("Jumlah uang tidak cukup");
  122. Hitung(command, jumlah);
  123. }
  124. else
  125. {
  126. stock -= jumlah;
  127. CetakStruk(totalHarga, jumlahUang, jumlah, command);
  128. }
  129. break;
  130. case '2':
  131. totalHarga = 2000 * jumlah;
  132. Console.WriteLine($"Total harga: {totalHarga}");
  133. Console.WriteLine("Masukkan jumlah uang anda: ");
  134. jumlahUang = double.Parse(Console.ReadLine());
  135. if(jumlahUang < totalHarga)
  136. {
  137. Console.WriteLine("Jumlah uang tidak cukup");
  138. Hitung(command, jumlah);
  139. }
  140. else
  141. {
  142. stock -= jumlah;
  143. CetakStruk(totalHarga, jumlahUang, jumlah, command);
  144. }
  145. break;
  146. case '3':
  147. totalHarga = 600000 * jumlah;
  148. Console.WriteLine($"Total harga: {totalHarga}");
  149. Console.WriteLine("Masukkan jumlah uang anda: ");
  150. jumlahUang = double.Parse(Console.ReadLine());
  151. if(jumlahUang < totalHarga)
  152. {
  153. Console.WriteLine("Jumlah uang tidak cukup");
  154. Hitung(command, jumlah);
  155. }
  156. else
  157. {
  158. stock -= jumlah;
  159. CetakStruk(totalHarga, jumlahUang, jumlah, command);
  160. }
  161. break;
  162. }
  163. }
  164. static void CetakStruk(double totalHarga, double jumlahUang, int jumlahBarang, char command)
  165. {
  166. string namaBarang = "";
  167. double kembalian = jumlahUang - totalHarga;
  168. switch(command)
  169. {
  170. case '1':
  171. namaBarang = "Buku";
  172. break;
  173. case '2':
  174. namaBarang = "Alat tulis";
  175. break;
  176. case '3':
  177. namaBarang = "Tas";
  178. break;
  179. }
  180. string formatStruk = $@"
  181. Toko Buku
  182. =============================
  183. Nama pembeli: {username}
  184. Tanggal pembelian: {DateTime.Now}
  185. Nama barang: {namaBarang}
  186. Jumlah barang: {jumlahBarang}
  187. Total harga: {totalHarga}
  188. Jumlah uang: {jumlahUang}
  189. Kembalian: {kembalian}
  190. =============================
  191. Terima kasih sudah berbelanja";
  192. string path = @"D:\Struk.txt";
  193. if(File.Exists(path))
  194. {
  195. var stream = File.Create(path);
  196. stream.Close();
  197. }
  198. else
  199. {
  200. var stream = File.Create(path);
  201. stream.Close();
  202. }
  203. File.WriteAllText(path, formatStruk);
  204. return;
  205. }
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement