Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace SchoolProject
- {
- class Program
- {
- static void Main(string[] args)
- {
- int tamMax = 100;
- // list/arrays that will old the data
- String[] nome = new String[tamMax];
- double[] quanto = new double[tamMax];
- double[] preco = new double[tamMax];
- Boolean[] feito = new Boolean[tamMax];
- // TODO: available item names. (review)
- string[] names = { "leite", "ovos", "macas", "azeite" };
- double[] prices = { .57, 1.99, 1.20, 4.29 };
- // tracker (this variable will track how many item is already in the list)
- int itemCount = 0;
- do
- {
- Console.Clear();
- // display available items:
- // print the header
- Console.WriteLine($"{"Item",-10}{"Name",-10}{"price",-10}{"bought",-10}");
- for (int i = 0; i < names.Length; i++)
- {
- Console.WriteLine($"{i,-10}{names[i],-10}{prices[i],-10}{(feito[i] ? "x" : ""),-10}");
- }
- Console.WriteLine();
- Console.WriteLine("(E)ditar lista.");
- Console.WriteLine("(F)azer compras.");
- Console.WriteLine("Fazer(c)ontas.");
- Console.WriteLine("(S)air");
- Console.WriteLine();
- // this varaible can be reused with other operations too!
- char ch = Console.ReadKey().KeyChar;
- switch (ch)
- {
- case 'e': // editar list
- // print list edit operations
- Console.WriteLine("(I)nserir item no fim da lista.)");
- Console.WriteLine("Inserir item na(p)osição n da lista.");
- Console.WriteLine("(A)pagar último item inserido na lista.");
- Console.WriteLine("Apagar item na posição(n) da lista.");
- Console.WriteLine("(A)pagar itens da posição m à n da lista.");
- Console.WriteLine("(L)istar todos os itens.");
- Console.WriteLine("(V)oltar.");
- Console.WriteLine();
- // todo: remove remaining char (user input)
- ch = Console.ReadKey().KeyChar;
- Console.WriteLine();
- // control edit list operations
- switch (ch)
- {
- case 'i':
- // e.g: 0 for leite, 1 for ovos...
- Console.WriteLine("Enter item index:");
- // this will be the index/position of the item in printed list
- var index = int.Parse(Console.ReadLine());
- // get the name of the item from the list
- string name = names[index];
- // check if item doesn't already exits in list, if it does, only update the quantity
- // todo: validation, in case user enter something other than number
- Console.WriteLine("Quanto:");
- double quantity = double.Parse(Console.ReadLine());
- // update name, quantity, price,
- double price = quantity * prices[index];
- // update items
- names[index] = name;
- quanto[index] = quantity;
- preco[index] = price;
- feito[index] = false;
- // print info of the item that was just addedi in the list
- Console.WriteLine($"{names[index],-10}{quanto[index],-10}{preco[index] * quanto[index],-10}");
- Console.WriteLine("Added to the list!");
- Console.ReadLine();
- break;
- case 'l':
- // this is not clear enough, which items? that one that is being sold or items in bag-list?
- Console.WriteLine("Listar(t)odos os itens.");
- Console.WriteLine("Listar itens(c)omprados.");
- Console.WriteLine("Listar itens(p)or comprar.");
- ch = Console.ReadKey().KeyChar;
- switch (ch)
- {
- case 't':
- for (int i = 0; i < itemCount; i++)
- {
- Console.WriteLine($"{names[i],-10}{quanto[i],-10}{(feito[i] ? "x" : ""),-10}");
- }
- break;
- case 'c':
- for (int i = 0; i < itemCount; i++)
- {
- // item bought!
- if (feito[i] == true)
- {
- continue;
- }
- Console.WriteLine($"{names[i],-10}{quanto[i],-10}{(feito[i] ? "x" : ""),-10}");
- }
- break;
- case 'p':
- for (int i = 0; i < itemCount; i++)
- {
- // item not bought!
- if (feito[i] == false)
- {
- continue;
- }
- Console.WriteLine($"{names[i],-10}{quanto[i],-10}{(feito[i] ? "x" : ""),-10}");
- }
- break;
- }
- break;
- default:
- Console.WriteLine("you entered invalid input!");
- break;
- }
- break;
- // fazer compras
- case 'f':
- Console.WriteLine("(M)arcar primeiro item por comprar.");
- Console.WriteLine("(D)esmarcar primeiro item comprado.");
- Console.WriteLine("Trocar estado por(n)ome.");
- Console.WriteLine("Trocar estado por(p)osição.");
- Console.WriteLine("(L)istar.");
- Console.WriteLine("(V)oltar.");
- ch = Console.ReadKey().KeyChar;
- switch (ch)
- {
- case 'm': // undone
- feito[0] = true;
- break;
- case 'd': // undone
- feito[0] = false;
- break;
- case 'n':
- // print out the message for user to enter the item name
- Console.WriteLine("Enter the name of the item: ");
- // read the name of the item
- string itemName = Console.ReadLine();
- // get item by name and chnage the state
- // - find the item index in list
- // - use the index to update the state of the item
- for (int i = 0; i < itemCount; i++)
- {
- // find the name that matched the name of the item
- if (names[i] == itemName)
- {
- // invert the state from true/false vice-versa
- feito[i] = !feito[i];
- }
- }
- break;
- // change item state by position/index
- case 'p':
- Console.WriteLine("Enter item position: ");
- // note: handle the validation / parsing correctly
- int position = int.Parse(Console.ReadLine());
- // invalid position
- if (position < 0 || position > itemCount)
- {
- Console.WriteLine("Position out of boundary");
- }
- // change item status
- feito[position] = !feito[position];
- // TODO: Mabye display a message of state changed
- break;
- case 'l': // print items
- // display items available for sale
- Console.WriteLine($"{"Item",-10}{"Name",-10}{"price",-10}{"bought",-10}");
- for (int i = 0; i < names.Length; i++)
- {
- Console.WriteLine($"{i,-10}{names[i],-10}{prices[i],-10}{(feito[i] ? "x" : ""),-10}");
- }
- break;
- case 'v': // done
- break;
- }
- break;
- // control fazer contas
- case 'c':
- Console.WriteLine("Quanto custa a (l)ista?"); // these are all the item inthe list
- Console.WriteLine("Quanto já (g)astei?"); // these are the item marked as "feito=true"
- Console.WriteLine("Quanto custa o que (f)alta comprar?");
- Console.WriteLine("Qual o preço (m)édio por item?"); // this item[i...n].price (added) / i...n
- ch = Console.ReadKey().KeyChar;
- double totalPrice = 0;
- switch (ch)
- {
- // total amount the whole list is worth, (note: this includes bought and not bought item in the list)
- case 'a':
- for (int i = 0; i < itemCount; i++)
- {
- // don't process, it's not yet bought!
- if (feito[i] == false)
- {
- continue;
- }
- // get total price for each item
- double itemPrice = 0; // this is the quantity * price
- // update total price
- totalPrice += itemPrice;
- }
- Console.WriteLine("The list is worth: " + totalPrice);
- break;
- // total amount wasted in shop
- case 'g':
- for (int i = 0; i < itemCount; i++)
- {
- // filter only item with feito = true
- // get total price for each item
- double itemPrice = 0; // this is the quantity * price
- // update total price
- totalPrice += itemPrice;
- }
- Console.WriteLine("The list is worth: " + totalPrice);
- break;
- case 'm':
- // TODO: add implementation
- break;
- }
- break;
- case 's': // exit application
- return;
- }
- } while (true);
- // TODO: add control statement which will exit the loop
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement