Lirbo

C# Bills Management

Apr 19th, 2022 (edited)
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace College_Classes
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] billsNames = new string[0];
  14.             int[] billsPayments = new int[0];
  15.             int option = 0;
  16.             do
  17.             {
  18.                 Console.WriteLine("1. Print Bills\n2. Add Bill\n3. Get bill payment by bill name\n4. Average payment\n5. Print max bill payment\n6. Remove Bill\n7. Exit");
  19.                 option = int.Parse(Console.ReadLine());
  20.                 if(option >= 1 && option <= 7 && option != 2 && option != 7 && billsNames.Length == 0)
  21.                 {
  22.                     Console.WriteLine("You don't have any bills");
  23.                     Console.ReadKey();
  24.                     Console.Clear();
  25.                     continue;
  26.                 }
  27.                 if(option == 1) // Print Bills
  28.                 {
  29.                     PrintBills(billsNames, billsPayments);
  30.                 }
  31.                 else if (option == 2) // Add Bill
  32.                 {
  33.                     billsNames = ModifiedStringArray(billsNames, 1);
  34.                     billsPayments = ModifiedIntegerArray(billsPayments, 1);
  35.                     Console.Write("Please enter a new bill name: ");
  36.                     billsNames[billsNames.Length - 1] = Console.ReadLine();
  37.                     Console.Write($"Please enter {billsNames[billsNames.Length -1]}'(s) payment: ");
  38.                     billsPayments[billsPayments.Length - 1] = int.Parse(Console.ReadLine());
  39.                     PrintBills(billsNames, billsPayments);
  40.                 }
  41.                 else if (option == 3) // Find Bill by Name
  42.                 {
  43.                     Console.Write("Please enter the bill's name: ");
  44.                     string tmpstr = Console.ReadLine();
  45.                     bool found = false;
  46.                     for (int i = 0; i < billsNames.Length; i++)
  47.                     {
  48.                         if (tmpstr == billsNames[i])
  49.                         {
  50.                             PrintBills(billsNames, billsPayments, i);
  51.                             found = true;
  52.                         }
  53.                     }
  54.                     if (found == false) Console.WriteLine($"The bill \"{tmpstr}\" was not found!");
  55.                 }
  56.                 else if (option == 4) // Average Bill Payment
  57.                 {
  58.                     double sum = 0;
  59.                     for (int i = 0; i < billsPayments.Length; i++)
  60.                     {
  61.                         sum += billsPayments[i];
  62.                     }
  63.                     sum /= billsPayments.Length;
  64.                     Console.WriteLine($"Average bill payment: {sum}");
  65.                 }
  66.                 else if (option == 5) // Maximum Value Bill
  67.                 {
  68.                     int max = 0, maxSlot = 0;
  69.                     for (int i  = 0; i < billsPayments.Length; i++)
  70.                     {
  71.                         if(billsPayments[i] > max)
  72.                         {
  73.                             max = billsPayments[i];
  74.                             maxSlot = i;
  75.                         }
  76.                     }
  77.                     Console.WriteLine($"Max bill: {billsNames[maxSlot]} (${billsPayments[maxSlot]})");
  78.                 }
  79.                 else if (option == 6) // Delete Bill
  80.                 {
  81.                     Console.Write("Please enter the name of the bill you'd like to delete: ");
  82.                     string tmpstr = Console.ReadLine();
  83.                     bool found = false;
  84.                     for (int i = 0; i < billsNames.Length; i++)
  85.                     {
  86.                         if (tmpstr == billsNames[i])
  87.                         {
  88.                             Console.WriteLine($"The bill \"{billsNames[i]}\" (${billsPayments[i]}) has been deleted!");
  89.                             billsNames[i] = "";
  90.                             billsPayments[i] = 0;
  91.                             billsNames = ModifiedStringArray(billsNames, -1);
  92.                             billsPayments = ModifiedIntegerArray(billsPayments, -1);
  93.                             found = true;
  94.                         }
  95.                     }
  96.                     if (found == false) Console.WriteLine($"The bill \"{tmpstr}\" was not found!");
  97.                 }
  98.                 else if (option == 7) // Exit
  99.                 {
  100.                     Console.WriteLine("Exiting...");
  101.                 }
  102.                 else Console.WriteLine("Stop cuasing troubles, pick 1 to 7.");
  103.                 Console.ReadKey();
  104.                 Console.Clear();
  105.             }
  106.             while (option != 7);
  107.         }
  108.  
  109.         static void PrintBills(string[] bNames, int[] bPayments, int slotid = -1)
  110.         {
  111.             if (slotid != -1) Console.WriteLine($"Bill Name: {bNames[slotid]}, Amount: {bPayments[slotid]}");
  112.             else for(int i = 0; i < bNames.Length; i++) Console.WriteLine($"Bill Name: {bNames[i]}, Amount: {bPayments[i]}");
  113.         }
  114.  
  115.         static string[] ModifiedStringArray(string[] array, int size)
  116.         {
  117.             string[] tmpstr = new string[array.Length + size];
  118.             int emptyCount = 0;
  119.             for (int i = 0; i < array.Length; i++)
  120.             {
  121.                 if (array[i] == "")
  122.                 {
  123.                     emptyCount++;
  124.                     continue;
  125.                 }
  126.                 tmpstr[i-emptyCount] = array[i];
  127.             }
  128.             return tmpstr;
  129.         }
  130.  
  131.         static int[] ModifiedIntegerArray(int[] array, int size)
  132.         {
  133.             int[] tmpint = new int[array.Length + size];
  134.             int emptyCount = 0;
  135.             for (int i = 0; i < array.Length; i++)
  136.             {
  137.                 if(array[i] == 0)
  138.                 {
  139.                     emptyCount++;
  140.                     continue;
  141.                 }
  142.                 tmpint[i-emptyCount] = array[i];
  143.             }
  144.             return tmpint;
  145.         }
  146.  
  147.     }
  148. }
  149.  
Add Comment
Please, Sign In to add comment