Advertisement
MoNoLidThZ

StructurePractice7

Sep 21st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6.  
  7. namespace StructurePractice7 {
  8.     struct Product {
  9.         public string ID, Name;
  10.         public double Price;
  11.         public int Qty;
  12.         public double Total;
  13.     }
  14.     static class Program {
  15.         static void Main() {
  16.             line();
  17.             int productCount = readInputRange("\b\b\b\b\b\bHow many product to input",1,9999);
  18.             line();
  19.             Console.WriteLine("INPUT {0} DATA OF PRODUCT TO STRUCTURE",productCount);
  20.             line();
  21.             Product[] products = new Product[productCount];
  22.             double sum = 0;
  23.             for (int i = 0; i < products.Length; i++) {
  24.                 products[i].ID = readLine("product ID");
  25.                 products[i].Name = readLine("product Name");
  26.                 products[i].Price = readInputRange("product Price", 0.0, double.MaxValue);
  27.                 products[i].Qty = readInputRange("product Quant", 0, int.MaxValue);
  28.                 products[i].Total = products[i].Price * products[i].Qty;
  29.                 sum += products[i].Total;
  30.                 line();
  31.             }
  32.             Console.WriteLine();
  33.             Console.WriteLine("{0,-5}{1,7}{2,7}{3,7}{4,7}{5,7}","No.","ID","NAME","PRICE","QUANT","TOTAL");
  34.             line();
  35.             for (int i = 0; i < products.Length; i++) {
  36.                 Console.WriteLine("{0,-5}{1,7}{2,7}{3,7}{4,7}{5,7:n0}",i+1,products[i].ID, products[i].Name, products[i].Price, products[i].Qty, products[i].Total);
  37.             }
  38.             line();
  39.             Console.WriteLine();
  40.             Console.WriteLine("All Total price of {0} product = {1:n} Baht.",productCount,sum);
  41.             Console.ReadKey();
  42.         }
  43.         static void line() {
  44.             Console.WriteLine("---------------------------------------------------------------------");
  45.         }
  46.         static int readInputRange(string name, int min, int max) {
  47.             int num;
  48.             while (true) {
  49.                 try {
  50.                     Console.Write("Input {0} : ", name);
  51.                     num = int.Parse(Console.ReadLine());
  52.                 } catch (Exception) {
  53.                     num = min - 1; //Workaround
  54.                 }
  55.                 if ((num >= min && num <= max)) {
  56.                     return num;
  57.                 } else {
  58.                     Console.SetCursorPosition(40, Console.CursorTop - 1);
  59.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  60.                     Console.WriteLine("Invalid Input!!!");
  61.                     Console.ResetColor();
  62.                 }
  63.             }
  64.         }
  65.  
  66.         static double readInputRange(string name, double min, double max) {
  67.             double num;
  68.             while (true) {
  69.                 try {
  70.                     Console.Write("Input {0,-15} : ", name);
  71.                     num = double.Parse(Console.ReadLine());
  72.                 } catch (Exception) {
  73.                     num = min - 1;
  74.                 }
  75.                 if ((num >= min && num <= max)) {
  76.                     return num;
  77.                 } else {
  78.                     Console.SetCursorPosition(40, Console.CursorTop - 1);
  79.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  80.                     Console.WriteLine("Invalid Input!!!");
  81.                     Console.ResetColor();
  82.                 }
  83.             }
  84.         }
  85.         static string readLine(string name) {
  86.             Console.Write("Input {0,-15} : ", name);
  87.             return Console.ReadLine();
  88.         }
  89.  
  90.         static int readInputNumber(string name) {
  91.             while (true) {
  92.                 try {
  93.                     Console.Write("Input {0,-15} : ", name);
  94.                     return int.Parse(Console.ReadLine());
  95.                 } catch (Exception) {
  96.                     Console.SetCursorPosition(40, Console.CursorTop - 1);
  97.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  98.                     Console.WriteLine("Invalid Input!!!");
  99.                     Console.ResetColor();
  100.                 }
  101.             }
  102.         }
  103.     }
  104. }
  105. /*Input
  106. 3
  107. 125-89
  108. Pen
  109. 5.5
  110. 560
  111. 126-78
  112. Book
  113. 12.5
  114. 100
  115. 127-43
  116. Candy
  117. 3
  118. 20
  119. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement