Created
April 23, 2020 20:49
-
-
Save abdullahmujahidali/e3f6143da713e85efe2ab217d4eacb66 to your computer and use it in GitHub Desktop.
Console App serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
************************************************** | |
using System; | |
namespace project05 | |
{ | |
public class precip | |
{ | |
private string precipDate; | |
private string precipType; | |
private decimal precipAmount; | |
//default constructor | |
public precip() | |
{ | |
} | |
//parametised constructor | |
public precip(string date, string type, decimal amount) | |
{ | |
this.precipDate = date; | |
this.precipType = type; | |
this.precipAmount = amount; | |
} | |
//seters | |
public void setDate(string date) | |
{ | |
this.precipDate = date; | |
} | |
public void setType(string type) | |
{ | |
this.precipType = type; | |
} | |
public void setAmount(decimal amount) | |
{ | |
this.precipAmount = amount; | |
} | |
//geters | |
public string getDate() | |
{ | |
return this.precipDate; | |
} | |
public string getType() | |
{ | |
return this.precipType; | |
} | |
public decimal getAmount() | |
{ | |
return this.precipAmount; | |
} | |
//the display method | |
public string displayPrecip() | |
{ | |
return getDate() + ", " + getType() + ", " + getAmount().ToString(); | |
} | |
} | |
} | |
program.cs | |
************************************************** | |
using System; | |
using System.Collections.Generic; | |
using System.Xml.Serialization; | |
using System.IO; | |
namespace project05 | |
{ | |
class MainClass | |
{ | |
//prints menu | |
public static int printMenu() | |
{ | |
while(true) | |
{ | |
Console.WriteLine("1. Add a precip event"); | |
Console.WriteLine("2. View all precip events"); | |
Console.WriteLine("3. Exit"); | |
Console.Write(": "); | |
//selected option | |
int option = Convert.ToInt32(Console.ReadLine()); | |
//validate selection | |
switch (option) | |
{ | |
case 1: | |
case 2: | |
case 3: | |
Console.Clear(); | |
return option; | |
} | |
Console.Clear(); | |
Console.WriteLine("Invalid option!"); | |
} | |
} | |
//ads new prcip | |
public static precip addPrecip() | |
{ | |
while (true) | |
{ | |
try | |
{ | |
string date, type; | |
decimal amount; | |
Console.Write("Enter Date: "); | |
date = Console.ReadLine(); | |
Console.Write("Enter type: "); | |
type = Console.ReadLine(); | |
Console.Write("Enter Amount: "); | |
amount = decimal.Parse(Console.ReadLine()); | |
precip p = new precip(date, type, amount); | |
return p; | |
} | |
catch | |
{ | |
Console.Clear(); | |
Console.WriteLine("Invalid input!"); | |
} | |
} | |
} | |
public static void Main(string[] args) | |
{ | |
List<precip> P = new List<precip>(); | |
while(true) | |
{ | |
int option = printMenu(); | |
switch (option) | |
{ | |
case 1: | |
Console.Clear(); | |
P.Add(addPrecip()); | |
Console.Clear(); | |
break; | |
case 2: | |
Console.Clear(); | |
Console.WriteLine("***********************************************"); | |
for (int i = 0; i < P.Count; i++) | |
Console.WriteLine(P[i].displayPrecip()); | |
Console.WriteLine("***********************************************"); | |
break; | |
case 3: | |
//serialize | |
for (int i = 0; i < P.Count; i++) | |
{ | |
XmlSerializer x = new XmlSerializer(P[i].GetType()); | |
x.Serialize(Console.Out, P[i]); | |
Console.WriteLine(); | |
} | |
Console.ReadLine(); | |
System.Environment.Exit(1); | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment