I\'m having trouble finishing this program. First class:(Trip) Represents the th
ID: 3914204 • Letter: I
Question
I'm having trouble finishing this program.
First class:(Trip)
Represents the things you are planning on doing. Includes the following characteristics: name of the place, location, short description of things you have to do, arrival date, and departure date. Write at least three constructors (including default). Include properties, accessors, and modifiers according to your design. Make sure to include ToString() method.
Second class:
Displays instructions to the user of your application (a short description of your application’s functionalities and how to run it). Tests your trip(first class) by creating objects. Asking the user to input the information about the venues and writing it in the corresponding fields of each object.Displaying the data from all the objects to test the ToString() method. It should have at least three different objects for the venues trying out each of the three different constructors, as well as your properties/instance methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Trip
{
class Trip
{
static void Main(string[] args)
{
string location;
string nameofplace;
string thingstodo;
string arrival;
string departure;
string displayMessage;
location = GetInfo("Location");
nameofplace = GetInfo("Name of Place");
thingstodo = GetInfo("Things to do");
arrival = GetInfo("Arrival");
departure = GetInfo("Departure");
displayMessage = DisplayInfo(location, nameofplace, thingstodo, arrival, departure);
Console.WriteLine("********************************");
Console.WriteLine("INFO");
Console.WriteLine("********************************");
Console.WriteLine(displayMessage);
DateTime time = DateTime.Parse(Console.ReadLine());
Console.WriteLine("********************************");
Console.Read();
}
public static string GetInfo(string infoType)
{
string userInput;
Console.WriteLine("Enter {0}", infoType);
userInput = Console.ReadLine();
return userInput;
}
//public override string ToString()
public static string DisplayInfo(string location, string nameofplace, string thingstodo, string arrival, string departure)
{
return "Location: " + location + " Name of place:" + nameofplace +
" Things to do: " + thingstodo + " Arrival: " + arrival +
" Departure: " + departure;
}
}
}
Explanation / Answer
Assumptions : Its an console application.
Explanation :
There will be 2 classes, Program.cs & Trip.cs.
Program - Start point of application, It has method named "Main".
Trip - Contains properties & constructors to hold trip info and an overridden method ToSTring.
Find all code part in bold.
Code :
// Program.cs class
// In this class you have to update howToUse string contain to describe your application’s functionalities
// and how to run it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
string howToUse = "A short description of your application’s functionalities and how to run it";
Console.WriteLine(howToUse);
Console.WriteLine();
List<Trip> trips = new List<Trip>();
string result = GetInfo("Add a trip [Y/N]");
while (string.Compare(result, "Y", true) == 0)
{
Trip trip = AddTrip();
trips.Add(trip);
result = GetInfo("Add a trip [Y/N]");
}
foreach(Trip trip in trips)
{
Console.WriteLine(trip.ToString());
}
Console.ReadLine();
}
private static Trip AddTrip()
{
string name = GetInfo("Name of Place");
string location = GetInfo("Location");
string thingstodo = GetInfo("Things to do");
string arrival = GetInfo("Arrival");
string departure = GetInfo("Departure");
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(location) && !string.IsNullOrEmpty(arrival) && !string.IsNullOrEmpty(departure) && !string.IsNullOrEmpty(thingstodo))
{
Trip trip = new Trip(name, location, arrival, departure, thingstodo);
return trip;
}
else if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(arrival))
{
Trip trip = new Trip(name, arrival);
return trip;
}
else
{
Trip trip = new Trip();
trip.Location = location;
trip.NameOfPlace = name;
trip.Arrival = arrival;
trip.Departure = departure;
trip.ThingsToDo = thingstodo;
return trip;
}
}
static string GetInfo(string infoType)
{
string userInput;
Console.Write("Enter {0} : ", infoType);
userInput = Console.ReadLine();
return userInput;
}
}
}
// Trip.cs class
namespace ConsoleApp
{
public class Trip
{
public string NameOfPlace { get; set; }
public string Location { get; set; }
public string ThingsToDo { get; set; }
public string Arrival { get; set; }
public string Departure { get; set; }
public Trip()
{
}
public Trip(string nameOfPlace, string arrivalDate) : this()
{
this.NameOfPlace = nameOfPlace;
this.Arrival = arrivalDate;
}
public Trip(string nameOfPlace, string location, string arrivalDate, string departureDate, string thingsToDo) : this(nameOfPlace, arrivalDate)
{
this.Location = location;
this.Departure = departureDate;
this.ThingsToDo = thingsToDo;
}
public override string ToString()
{
string str = string.Empty;
if (!string.IsNullOrEmpty(this.NameOfPlace))
str += "Name Of Place : " + this.NameOfPlace + ";";
if (!string.IsNullOrEmpty(this.Location))
str += "Location : " + this.Location + ";";
if (!string.IsNullOrEmpty(this.ThingsToDo))
str += "Things To Do : " + this.ThingsToDo + ";";
if (!string.IsNullOrEmpty(this.Arrival))
str += "Arrival : " + this.Arrival + ";";
if (!string.IsNullOrEmpty(this.Departure))
str += "Departure : " + this.Departure + ";";
return str;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.