Advertisement
ivandrofly

Shop Project (Beta)

Nov 21st, 2019
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.02 KB | None | 0 0
  1. using System;
  2.  
  3. namespace SchoolProject
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int tamMax = 100;
  10.  
  11.             // list/arrays that will old the data
  12.             String[] nome = new String[tamMax];
  13.             double[] quanto = new double[tamMax];
  14.             double[] preco = new double[tamMax];
  15.             Boolean[] feito = new Boolean[tamMax];
  16.  
  17.             // TODO: available item names. (review)
  18.             string[] names = { "leite", "ovos", "macas", "azeite" };
  19.             double[] prices = { .57, 1.99, 1.20, 4.29 };
  20.  
  21.             // tracker (this variable will track how many item is already in the list)
  22.             int itemCount = 0;
  23.  
  24.             do
  25.             {
  26.                 Console.Clear();
  27.                 // display available items:
  28.                 // print the header
  29.                 Console.WriteLine($"{"Item",-10}{"Name",-10}{"price",-10}{"bought",-10}");
  30.  
  31.                 for (int i = 0; i < names.Length; i++)
  32.                 {
  33.                     Console.WriteLine($"{i,-10}{names[i],-10}{prices[i],-10}{(feito[i] ? "x" : ""),-10}");
  34.                 }
  35.  
  36.                 Console.WriteLine();
  37.  
  38.                 Console.WriteLine("(E)ditar lista.");
  39.                 Console.WriteLine("(F)azer compras.");
  40.                 Console.WriteLine("Fazer(c)ontas.");
  41.                 Console.WriteLine("(S)air");
  42.                 Console.WriteLine();
  43.  
  44.                 // this varaible can be reused with other operations too!
  45.                 char ch = Console.ReadKey().KeyChar;
  46.  
  47.                 switch (ch)
  48.                 {
  49.                     case 'e': // editar list
  50.  
  51.                         // print list edit operations
  52.                         Console.WriteLine("(I)nserir item no fim da lista.)");
  53.                         Console.WriteLine("Inserir item na(p)osição n da lista.");
  54.                         Console.WriteLine("(A)pagar último item inserido na lista.");
  55.                         Console.WriteLine("Apagar item na posição(n) da lista.");
  56.                         Console.WriteLine("(A)pagar itens da posição m à n da lista.");
  57.                         Console.WriteLine("(L)istar todos os itens.");
  58.                         Console.WriteLine("(V)oltar.");
  59.                         Console.WriteLine();
  60.  
  61.                         // todo: remove remaining char (user input)
  62.                         ch = Console.ReadKey().KeyChar;
  63.                         Console.WriteLine();
  64.  
  65.                         // control edit list operations
  66.                         switch (ch)
  67.                         {
  68.                             case 'i':
  69.  
  70.                                 // e.g: 0 for leite, 1 for ovos...
  71.                                 Console.WriteLine("Enter item index:");
  72.  
  73.                                 // this will be the index/position of the item in printed list
  74.                                 var index = int.Parse(Console.ReadLine());
  75.  
  76.                                 // get the name of the item from the list
  77.                                 string name = names[index];
  78.  
  79.                                 // check if item doesn't already exits in list, if it does, only update the quantity
  80.  
  81.                                 // todo: validation, in case user enter something other than number
  82.                                 Console.WriteLine("Quanto:");
  83.                                 double quantity = double.Parse(Console.ReadLine());
  84.  
  85.                                 // update name, quantity, price,
  86.                                 double price = quantity * prices[index];
  87.  
  88.                                 // update items
  89.                                 names[index] = name;
  90.                                 quanto[index] = quantity;
  91.                                 preco[index] = price;
  92.                                 feito[index] = false;
  93.  
  94.                                 // print info of the item that was just addedi in the list
  95.  
  96.                                 Console.WriteLine($"{names[index],-10}{quanto[index],-10}{preco[index] * quanto[index],-10}");
  97.                                 Console.WriteLine("Added to the list!");
  98.                                 Console.ReadLine();
  99.  
  100.                                 break;
  101.  
  102.                             case 'l':
  103.                                 // this is not clear enough, which items? that one that is being sold or items in bag-list?
  104.                                 Console.WriteLine("Listar(t)odos os itens.");
  105.                                 Console.WriteLine("Listar itens(c)omprados.");
  106.                                 Console.WriteLine("Listar itens(p)or comprar.");
  107.                                 ch = Console.ReadKey().KeyChar;
  108.  
  109.                                 switch (ch)
  110.                                 {
  111.                                     case 't':
  112.                                         for (int i = 0; i < itemCount; i++)
  113.                                         {
  114.                                             Console.WriteLine($"{names[i],-10}{quanto[i],-10}{(feito[i] ? "x" : ""),-10}");
  115.                                         }
  116.                                         break;
  117.  
  118.                                     case 'c':
  119.                                         for (int i = 0; i < itemCount; i++)
  120.                                         {
  121.                                             // item bought!
  122.                                             if (feito[i] == true)
  123.                                             {
  124.                                                 continue;
  125.                                             }
  126.                                             Console.WriteLine($"{names[i],-10}{quanto[i],-10}{(feito[i] ? "x" : ""),-10}");
  127.                                         }
  128.                                         break;
  129.  
  130.                                     case 'p':
  131.                                         for (int i = 0; i < itemCount; i++)
  132.                                         {
  133.                                             // item not bought!
  134.                                             if (feito[i] == false)
  135.                                             {
  136.                                                 continue;
  137.                                             }
  138.                                             Console.WriteLine($"{names[i],-10}{quanto[i],-10}{(feito[i] ? "x" : ""),-10}");
  139.                                         }
  140.                                         break;
  141.                                 }
  142.                                 break;
  143.  
  144.                             default:
  145.                                 Console.WriteLine("you entered invalid input!");
  146.                                 break;
  147.                         }
  148.  
  149.                         break;
  150.  
  151.                     // fazer compras
  152.                     case 'f':
  153.                         Console.WriteLine("(M)arcar primeiro item por comprar.");
  154.                         Console.WriteLine("(D)esmarcar primeiro item comprado.");
  155.                         Console.WriteLine("Trocar estado por(n)ome.");
  156.                         Console.WriteLine("Trocar estado por(p)osição.");
  157.                         Console.WriteLine("(L)istar.");
  158.                         Console.WriteLine("(V)oltar.");
  159.  
  160.                         ch = Console.ReadKey().KeyChar;
  161.                         switch (ch)
  162.                         {
  163.                             case 'm': // undone
  164.                                 feito[0] = true;
  165.                                 break;
  166.  
  167.                             case 'd': // undone
  168.                                 feito[0] = false;
  169.                                 break;
  170.  
  171.                             case 'n':
  172.  
  173.                                 // print out the message for user to enter the item name
  174.                                 Console.WriteLine("Enter the name of the item: ");
  175.  
  176.                                 // read the name of the item
  177.                                 string itemName = Console.ReadLine();
  178.  
  179.                                 // get item by name and chnage the state
  180.                                 // - find the item index in list
  181.                                 // - use the index to update the state of the item
  182.                                 for (int i = 0; i < itemCount; i++)
  183.                                 {
  184.                                     // find the name that matched the name of the item
  185.                                     if (names[i] == itemName)
  186.                                     {
  187.                                         // invert the state from true/false vice-versa
  188.                                         feito[i] = !feito[i];
  189.                                     }
  190.                                 }
  191.                                 break;
  192.  
  193.                             // change item state by position/index
  194.                             case 'p':
  195.                                 Console.WriteLine("Enter item position: ");
  196.  
  197.                                 // note: handle the validation / parsing correctly
  198.                                 int position = int.Parse(Console.ReadLine());
  199.  
  200.                                 // invalid position
  201.                                 if (position < 0 || position > itemCount)
  202.                                 {
  203.                                     Console.WriteLine("Position out of boundary");
  204.                                 }
  205.  
  206.                                 // change item status
  207.                                 feito[position] = !feito[position];
  208.  
  209.                                 // TODO: Mabye display a message of state changed
  210.  
  211.                                 break;
  212.  
  213.                             case 'l': // print items
  214.  
  215.                                 // display items available for sale
  216.                                 Console.WriteLine($"{"Item",-10}{"Name",-10}{"price",-10}{"bought",-10}");
  217.                                 for (int i = 0; i < names.Length; i++)
  218.                                 {
  219.                                     Console.WriteLine($"{i,-10}{names[i],-10}{prices[i],-10}{(feito[i] ? "x" : ""),-10}");
  220.                                 }
  221.                                 break;
  222.  
  223.                             case 'v': // done
  224.                                 break;
  225.                         }
  226.  
  227.                         break;
  228.  
  229.                     // control fazer contas
  230.                     case 'c':
  231.                         Console.WriteLine("Quanto custa a (l)ista?"); // these are all the item inthe list
  232.                         Console.WriteLine("Quanto já (g)astei?"); // these are the item marked as "feito=true"
  233.                         Console.WriteLine("Quanto custa o que (f)alta comprar?");
  234.                         Console.WriteLine("Qual o preço (m)édio por item?"); // this item[i...n].price (added)  /  i...n
  235.                         ch = Console.ReadKey().KeyChar;
  236.  
  237.                         double totalPrice = 0;
  238.  
  239.                         switch (ch)
  240.                         {
  241.                             // total amount the whole list is worth, (note: this includes bought and not bought item in the list)
  242.                             case 'a':
  243.  
  244.                                 for (int i = 0; i < itemCount; i++)
  245.                                 {
  246.                                     // don't process, it's not yet bought!
  247.                                     if (feito[i] == false)
  248.                                     {
  249.                                         continue;
  250.                                     }
  251.  
  252.                                     // get total price for each item
  253.                                     double itemPrice = 0; // this is the quantity * price
  254.  
  255.                                     // update total price
  256.                                     totalPrice += itemPrice;
  257.                                 }
  258.  
  259.                                 Console.WriteLine("The list is worth: " + totalPrice);
  260.                                 break;
  261.  
  262.                             // total amount wasted in shop
  263.                             case 'g':
  264.                                 for (int i = 0; i < itemCount; i++)
  265.                                 {
  266.                                     // filter only item with feito = true
  267.  
  268.                                     // get total price for each item
  269.                                     double itemPrice = 0; // this is the quantity * price
  270.  
  271.                                     // update total price
  272.                                     totalPrice += itemPrice;
  273.                                 }
  274.                                 Console.WriteLine("The list is worth: " + totalPrice);
  275.                                 break;
  276.  
  277.                             case 'm':
  278.                                 // TODO: add implementation
  279.                                 break;
  280.                         }
  281.  
  282.                         break;
  283.  
  284.                     case 's': // exit application
  285.                         return;
  286.                 }
  287.  
  288.             } while (true);
  289.  
  290.             // TODO: add control statement which will exit the loop
  291.         }
  292.     }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement