RenzCutie

OOP 5 numbers

Oct 19th, 2021 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.83 KB | None | 0 0
  1. using System;
  2.  
  3. namespace numbersNs {
  4.     public class numbers{
  5.         private double _num1;
  6.         public double num1{
  7.             get {
  8.                 return _num1;
  9.             }
  10.             set {
  11.                 _num1 = value;
  12.             }
  13.         }
  14.  
  15.         private double _num2;
  16.         public double num2{
  17.             get {
  18.                 return _num2;
  19.             }
  20.             set {
  21.                 _num2 = value;
  22.             }
  23.         }
  24.  
  25.         private double _num3;
  26.         public double num3{
  27.             get {
  28.                 return _num3;
  29.             }
  30.             set {
  31.                 _num3 = value;
  32.             }
  33.         }
  34.  
  35.         private double _num4;
  36.         public double num4{
  37.             get {
  38.                 return _num4;
  39.             }
  40.             set {
  41.                 _num4 = value;
  42.             }
  43.         }
  44.  
  45.         private double _num5;
  46.         public double num5{
  47.             get {
  48.                 return _num5;
  49.             }
  50.             set {
  51.                 _num5 = value;
  52.             }
  53.         }
  54.  
  55.         public double numPositive{
  56.             get {
  57.                 double _numPositive = 0;
  58.  
  59.                 if( _num1 >= 0) _numPositive++;
  60.                 if( _num2 >= 0) _numPositive++;
  61.                 if( _num3 >= 0) _numPositive++;
  62.                 if( _num4 >= 0) _numPositive++;
  63.                 if( _num5 >= 0) _numPositive++;
  64.  
  65.                 return _numPositive;
  66.             }
  67.         }
  68.  
  69.         public double numNegative{
  70.             get {
  71.                 double _numNegative = 0;
  72.  
  73.                 if( _num1 < 0) _numNegative++;
  74.                 if( _num2 < 0) _numNegative++;
  75.                 if( _num3 < 0) _numNegative++;
  76.                 if( _num4 < 0) _numNegative++;
  77.                 if( _num5 < 0) _numNegative++;
  78.  
  79.                 return _numNegative;
  80.             }
  81.         }
  82.  
  83.         public double numSum{
  84.             get {
  85.                 return _num1 + _num2 + _num3 + _num4 + _num5;
  86.             }
  87.         }      
  88.  
  89.         public double numProduct{
  90.             get {
  91.                 return _num1 * _num2 * _num3 * _num4 * _num5;
  92.             }
  93.         }    
  94.  
  95.  
  96.     }
  97.  
  98.     public class Program: numbers{
  99.  
  100.         public static void Main(){
  101.             numbers nums = new numbers();
  102.             double OutVal;
  103.             bool looping = false;
  104.  
  105.             cls();
  106.  
  107.             print("Insert 5 numbers: \n");
  108.            
  109.             double.TryParse(input("First number: "), out OutVal);
  110.             nums.num1 = OutVal;
  111.             double.TryParse(input("Second number: "), out OutVal);
  112.             nums.num2 = OutVal;
  113.             double.TryParse(input("Third number: "), out OutVal);
  114.             nums.num3 = OutVal;
  115.             double.TryParse(input("Fourth number: "), out OutVal);
  116.             nums.num4 = OutVal;
  117.             double.TryParse(input("Fifth number: "), out OutVal);
  118.             nums.num5 = OutVal;
  119.  
  120.             looping = true;
  121.  
  122.             cls();
  123.  
  124.             while (looping){
  125.                 print(String.Format("Numbers you have: \n{0} {1} {2} {3} {4}\n",nums.num1, nums.num2, nums.num3, nums.num4, nums.num5));
  126.  
  127.                 print("\nWhat should I do now?");
  128.                 print("1 - Get the number of positive numbers");
  129.                 print("2 - Get the number of negative numbers");
  130.                 print("3 - Get sum of all the numbers");
  131.                 print("4 - Get product of all the numbers");
  132.                 print("5 - Exit\n");
  133.  
  134.                
  135.                 while (true){
  136.                     ConsoleKeyInfo Out = inputKey("Choose: ");
  137.                     if (Out.KeyChar.ToString() =="1")
  138.                         print(String.Format("\nAnswer: {0}",nums.numPositive));
  139.                     else if (Out.KeyChar.ToString() =="2")
  140.                         print(String.Format("\nAnswer: {0}",nums.numNegative));
  141.                     else if (Out.KeyChar.ToString() =="3")
  142.                         print(String.Format("\nAnswer: {0}",nums.numSum));
  143.                     else if (Out.KeyChar.ToString() =="4")
  144.                         print(String.Format("\nAnswer: {0}",nums.numProduct));
  145.                     else if (Out.KeyChar.ToString() =="5")
  146.                         looping = false;
  147.                     else {
  148.                         print("Error!");
  149.                         continue;
  150.                     }
  151.                     if (Out.KeyChar.ToString() !="5")
  152.                         inputKey("\nPress any key to continue...");
  153.  
  154.                     cls();
  155.                     break;
  156.                
  157.    
  158.                 }
  159.  
  160.             }
  161.  
  162.                    
  163.  
  164.         }
  165.  
  166.         static void setNumVal(object obj){
  167.             double Out;
  168.             while (true){
  169.                 double.TryParse(input(), out Out);
  170.  
  171.                 if (double.IsNaN(Out)){
  172.                     print("Error!");
  173.                     continue;
  174.                 } else {
  175.                     obj = Out;
  176.                     break;
  177.                 }
  178.             }
  179.         }
  180.  
  181.         //simplified function for printing
  182.         static void print(String str, bool newline = true)
  183.         {
  184.             if (newline)
  185.                 Console.WriteLine(str);
  186.             else
  187.                 Console.Write(str);
  188.         }
  189.        
  190.         //simplified function for getting the user input
  191.         static String input(String str = "")
  192.         {
  193.             if (str != "") print(str,false);
  194.  
  195.             return Console.ReadLine();
  196.         }
  197.  
  198.         //simplified function for getting the user input key
  199.         static ConsoleKeyInfo inputKey(String str = "")
  200.         {
  201.             if (str != "") print(str,false);
  202.  
  203.             return Console.ReadKey();
  204.         }
  205.  
  206.         //simplified function for clering the screen
  207.         static void cls()
  208.         {
  209.             Console.Clear();
  210.         }
  211.  
  212.     }
  213.  
  214.  
  215.  
  216. }
Add Comment
Please, Sign In to add comment