Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 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 Chp9Ex22
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // We don't need to input anything for these exercises, just instantiate the class with
  14.             // your own data.
  15.  
  16.             /*
  17.             char userInput = ' ';
  18.             while (userInput == ' ')
  19.             {
  20.                 userInput = getUserInput();
  21.                 if (userInput == 'n')
  22.                 {
  23.                     Console.WriteLine("Please press <enter> to exit.");
  24.                     Console.ReadLine();
  25.                    
  26.                 }
  27.             }
  28.             */
  29.  
  30.             // This is correct.
  31.             ClassifiedAd yourAd = new ClassifiedAd(30, "House Rentals");
  32.  
  33.             // Now, create a method to display the ad information:
  34.  
  35.             DisplayAd(yourAd);
  36.         }
  37.  
  38.         static void DisplayAd(ClassifiedAd ad)
  39.         {
  40.             // Now you can access ad.GetCost();
  41.            
  42.             // You will also need to access the words and category properties of the class, and in
  43.             // order to do this you need to write the Get methods in the class--just like the
  44.             // GetCost() method.
  45.  
  46.         }
  47.  
  48.         static char getUserInput()
  49.         {
  50.             Console.WriteLine("Would you like to publish a classified ad? [y/n]");
  51.             char userInput = (char)Console.Read();
  52.             Console.ReadLine();
  53.             if (userInput != 'y' && userInput != 'n')
  54.             {
  55.                 Console.WriteLine("Invalid Input");
  56.             }
  57.  
  58.             return userInput;
  59.         }
  60.         static int getNumWords()
  61.         {
  62.             Console.WriteLine("How many words would you like the ad t be: ");
  63.             int numWords = (int)Console.Read();
  64.             return numWords;
  65.         }
  66.  
  67.     }
  68.     class ClassifiedAd
  69.     {
  70.         // These should not be defined as being readonly.
  71.  
  72.         /*
  73.         private readonly int numWords;
  74.         private readonly string catString;
  75.         */
  76.  
  77.         private int numWords;
  78.         private string catString;
  79.  
  80.         // Cost should not be defined as being public. It also must be private.
  81.         //public readonly double cost;
  82.  
  83.         private double cost;
  84.  
  85.         public ClassifiedAd(int words, string category)
  86.         {
  87.             catString = category;
  88.             cost = words * .09;
  89.             numWords = words;
  90.             Console.WriteLine($"{words} {category} {cost}");
  91.         }
  92.  
  93.         // Now define a method to return the cost to the calling program
  94.         public double GetCost()
  95.         {
  96.             return cost;
  97.  
  98.             // Or you could have done this:
  99.             // return numWords * .09;
  100.         }
  101.  
  102.         // Now define the other methods you need to return the data from the
  103.         // numWords and catString properties.
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement