Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Chp9Ex22
- {
- class Program
- {
- static void Main(string[] args)
- {
- // We don't need to input anything for these exercises, just instantiate the class with
- // your own data.
- /*
- char userInput = ' ';
- while (userInput == ' ')
- {
- userInput = getUserInput();
- if (userInput == 'n')
- {
- Console.WriteLine("Please press <enter> to exit.");
- Console.ReadLine();
- }
- }
- */
- // This is correct.
- ClassifiedAd yourAd = new ClassifiedAd(30, "House Rentals");
- // Now, create a method to display the ad information:
- DisplayAd(yourAd);
- }
- static void DisplayAd(ClassifiedAd ad)
- {
- // Now you can access ad.GetCost();
- // You will also need to access the words and category properties of the class, and in
- // order to do this you need to write the Get methods in the class--just like the
- // GetCost() method.
- }
- static char getUserInput()
- {
- Console.WriteLine("Would you like to publish a classified ad? [y/n]");
- char userInput = (char)Console.Read();
- Console.ReadLine();
- if (userInput != 'y' && userInput != 'n')
- {
- Console.WriteLine("Invalid Input");
- }
- return userInput;
- }
- static int getNumWords()
- {
- Console.WriteLine("How many words would you like the ad t be: ");
- int numWords = (int)Console.Read();
- return numWords;
- }
- }
- class ClassifiedAd
- {
- // These should not be defined as being readonly.
- /*
- private readonly int numWords;
- private readonly string catString;
- */
- private int numWords;
- private string catString;
- // Cost should not be defined as being public. It also must be private.
- //public readonly double cost;
- private double cost;
- public ClassifiedAd(int words, string category)
- {
- catString = category;
- cost = words * .09;
- numWords = words;
- Console.WriteLine($"{words} {category} {cost}");
- }
- // Now define a method to return the cost to the calling program
- public double GetCost()
- {
- return cost;
- // Or you could have done this:
- // return numWords * .09;
- }
- // Now define the other methods you need to return the data from the
- // numWords and catString properties.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement