guys I just want some help. Sorry, I'm a newbie in C# programming. And my problem is that when I enter a value let say I enter 2, I want to print out: (Inserted so far: 0 out of 23.) But what happens is it shows the same value of soda which is 30 instead of 23.
namespace VendingMachine
{
class Program
{
static void Main(string[] args)
{
int itemPrice = 0;
int moneyAvailable = 0;
int change = 0;
int soda = 30;
int juice = 23;
int water = 15;
string userChoice;
// Menu
Console.WriteLine();
Console.WriteLine("1. Soda = P30");
Console.WriteLine("2. Juice = P23");
Console.WriteLine("3. Water = P15");
Console.WriteLine("4. Exit");
Console.WriteLine();
// Values entered by the user
Console.Write("Your choice: ");
userChoice = Console.ReadLine();
Console.WriteLine("================================");
// THE MAIN PROBLEM
if (itemPrice < soda)
{
Console.WriteLine($"Inserted so far: P0 out of P{soda}");
}
Console.Write("Enter the amount of Money!: P");
moneyAvailable = int.Parse(Console.ReadLine());
switch (userChoice)
{
case "1":
itemPrice = itemPrice + soda;
break;
case "2":
itemPrice = itemPrice + juice;
break;
case "3":
itemPrice = itemPrice + water;
break;
default:
Console.WriteLine("Invalid. Choose 1, 2 or 3.");
break;
}
You are using string interpolation with wrong variable name
if (itemPrice < soda)
{
Console.WriteLine($"Inserted so far: P0 out of P{itemPrice}");
// ^^^^^^^^^^ Problem is here
}
Instead of printing value of soda everytime you should print value of itemPrice.
This print statement with if condition should goes to end of switch statement
Something like,
Console.Write("Enter the amount of Money!: P");
moneyAvailable = int.Parse(Console.ReadLine());
switch (userChoice)
{
case "1":
itemPrice = itemPrice + soda;
break;
case "2":
itemPrice = itemPrice + juice;
break;
case "3":
itemPrice = itemPrice + water;
break;
default:
Console.WriteLine("Invalid. Choose 1, 2 or 3.");
break;
}
if (itemPrice < soda)
{
Console.WriteLine($"Inserted so far: P0 out of P{itemPrice}");
}
Wrong variable in the string output: {soda} instead of {itemPrice}
In your case, you have to check if (itemPrice < soda) after switch (userChoice)
This is what you wanted to do:
class Program
{
static void Main(string[] args)
{
List<Item> items = new List<Item>()
{
new Item
{
Name = "Soda",
Price = 30
},
new Item
{
Name = "Juice",
Price = 23
},
new Item
{
Name = "Water",
Price = 15
}
};
while (true)
{
//MENU
Log("Available Items:");
for(int i = 0; i < items.Count; i++)
{
Log("{0}. {1} = P{2}", i, items[i].Name, items[i].Price);
}
Console.WriteLine();
bool isInputValid = false;
string userChoice = string.Empty;
int chosenIndex = 0;
while (isInputValid == false)
{
try
{
Log("Choose your Item:");
userChoice = Console.ReadLine();
chosenIndex = int.Parse(userChoice);
if(chosenIndex < 0 || chosenIndex >= items.Count)
{
throw new ArgumentOutOfRangeException();
}
isInputValid = true;
}
catch
{
Log("Invalid choice!");
}
}
Item chosenItem = items[chosenIndex];
bool isPriceReached = false;
string userInsertedMoney = string.Empty;
int insertedMoney = 0;
while (isPriceReached == false)
{
try
{
Log("P{0} of P{1} needed Money for {2} inserted, waiting for more...", insertedMoney, chosenItem.Price, chosenItem.Name);
userInsertedMoney = Console.ReadLine();
insertedMoney += int.Parse(userInsertedMoney);
isPriceReached = insertedMoney >= chosenItem.Price;
}
catch
{
Log("Invalid money!");
}
}
Log("Here is your {0} with a rest of {1} money.{2}", chosenItem.Name, insertedMoney - chosenItem.Price, Environment.NewLine);
}
}
private static void Log(string message, params object[] args)
{
Console.WriteLine(string.Format(message, args));
}
}
public class Item
{
public string Name { get; set; }
public int Price { get; set; }
}
Related
I'm new to C# and want to implement loops , right now I'm using goto statement and labels but I have read that it is not suggested to use goto statement
so i was thinking to implement loops instead of goto and labels , byt i dont know how can i replace goto with loops
and if possible please also give a small explanation of the answer
here is my code with goto and labels
using System;
/* A simple coffee ordering program
* used switch,if else and lable*/
class Program
{
public static void Main()
{
int TotalCoffeeCost = 0;
// Start is a lable to point to this location so i can use it in goto
Start:
Console.WriteLine("");
Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
Console.Write(" ");
int CoffeeChoice = int.Parse(Console.ReadLine());
switch (CoffeeChoice)
{
case 1:
TotalCoffeeCost += 2;
break;
case 2:
TotalCoffeeCost += 5;
break;
case 3:
TotalCoffeeCost += 7;
break;
default:
Console.WriteLine("");
Console.WriteLine(" Please enter a vaild choice");
goto Start;
}
// YesOrNo is a lable to point to this location so i can use it in goto
YesOrNo:
Console.WriteLine("");
Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
Console.Write(" ");
string UserChoice = Console.ReadLine();
string upperCaseChoice = UserChoice.ToUpper();
if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
{
goto Start;
}else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
{
goto LastConfirmation;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a vailed choice");
goto YesOrNo;
}
// LastConfirmation is a lable to point to this location so i can use it in goto
LastConfirmation:
Console.WriteLine(" ");
Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
Console.Write(" ");
string Anything = Console.ReadLine();
string UpperCaseAnything = Anything.ToUpper();
if(UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
{
goto Amount;
}
else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
{
goto Start;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a vaild choice");
goto LastConfirmation;
}
// Amount is a lable to point to this location so i can use it in goto
Amount:
Console.WriteLine("");
Console.WriteLine(" Your total bill amount is = {0}$", TotalCoffeeCost);
Console.WriteLine("");
Console.WriteLine(" Please pay the amount by entering the amount bellow");
Console.Write(" ");
int EnterdAmt = int.Parse(Console.ReadLine());
if(EnterdAmt != TotalCoffeeCost)
{
Console.WriteLine("");
Console.WriteLine(" Please pay the correct amount");
goto Amount;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
}
}
}
Just use while loops and methods to make this work.
Your Start method could look like this:
public void Start(int TotalCoffeCost)
{
Console.WriteLine("");
Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, - large");
Console.Write(" ");
int CoffeChoice = 0;
do
{
CoffeChoice = int.Parse(Console.ReadLine());//Asking for Choice until you put in 1,2 or 3
}while(CoffeChoice != 1 | CoffeChoice != 2 | CoffeChoice != 3)
switch (CoffeeChoice)
{
case 1:
TotalCoffeeCost += 2;
break;
case 2:
TotalCoffeeCost += 5;
break;
case 3:
TotalCoffeeCost += 7;
break;
}
}
And you can call the method like this:
public static void Main()
{
int TotalCoffeeCost = 0;
Start(TotalCoffeCost);
}
We can use methods and a simple bool "lock" that either keeps you inside the while loop, or breaks out of it when conditions are met:
using System;
/* A simple coffee ordering program
* used switch,if else and lable*/
class Program
{
public static void Main()
{
var exiting = false;
while (!exiting)
{
programLoop();
}
}
private int askForCoffee()
{
int totalCoffeeCost = 0;
bool invalidChoice = true;
while(invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
Console.Write(" ");
int CoffeeChoice = int.Parse(Console.ReadLine());
switch (CoffeeChoice)
{
case 1:
totalCoffeeCost += 2;
invalidChoice = false;
break;
case 2:
totalCoffeeCost += 5;
invalidChoice = false;
break;
case 3:
totalCoffeeCost += 7;
invalidChoice = false;
break;
default:
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
return totalCoffeeCost;
}
private void programLoop()
{
int TotalCoffeeCost = 0;
TotalCoffeeCost += askForCoffee();
var invalidChoice = true;
while(invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
Console.Write(" ");
string UserChoice = Console.ReadLine();
string upperCaseChoice = UserChoice.ToUpper();
if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
{
TotalCoffeeCost += askForCoffee(); //Note here that we did not set invalidChoice to false, meaning it will loop again
}
else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
{
invalidChoice = false;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while(invalidChoice)
{
Console.WriteLine(" ");
Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
Console.Write(" ");
string Anything = Console.ReadLine();
string UpperCaseAnything = Anything.ToUpper();
if(UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
{
invalidChoice = false;
}
else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
{
return;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while(invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Your total bill amount is = {0}$", TotalCoffeeCost);
Console.WriteLine("");
Console.WriteLine(" Please pay the amount by entering the amount bellow");
Console.Write(" ");
int EnterdAmt = int.Parse(Console.ReadLine());
if(EnterdAmt != TotalCoffeeCost)
{
Console.WriteLine("");
Console.WriteLine(" Please pay the correct amount");
}
else
{
invalidChoice = false;
}
}
Console.WriteLine("");
Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
}
}
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 1 year ago.
I'm working on a program in c# in which I've got an array of objects of a class. I've got a constructor in the class that should give the attached string a value of "" to make sure they're empty so information can be passed in easily. I then have the array created like so:
participants[8][8] grid = new participants[8][];
But I'm being thrown a NullReferenceExcetption error. Here is my code for your reference.
using System;
namespace TreasurehuntCsharp
{
class square
{
public string strX = "";
public int y = 0;
public int x = 0;
}
class participants
{
public string name = "";
public string contact = "";
public participants()
{
name = "";
contact = "";
}
}
class Program
{
//Method for user to choose a square
static void Input(square Coord)
{
//Variables
bool correct = false;
//Inputs
Console.WriteLine("Please enter the coordinates of the square you would like to select: \r\n");
do
{
//X coordinate
Console.WriteLine("X: ");
Coord.strX = Console.ReadLine().ToLower();
//Convert letter to array coordinate
switch (Coord.strX)
{
case "a":
Coord.x = 0;
correct = true;
break;
case "b":
Coord.x = 1;
correct = true;
break;
case "c":
Coord.x = 2;
correct = true;
break;
case "d":
Coord.x = 3;
correct = true;
break;
case "e":
Coord.x = 4;
correct = true;
break;
case "f":
Coord.x = 5;
correct = true;
break;
case "g":
Coord.x = 6;
correct = true;
break;
case "h":
Coord.x = 7;
correct = true;
break;
default:
Console.WriteLine("Please enter a letter from A to H");
correct = false;
break;
}
} while (correct != true);
correct = false;
do
{
//Y coordinate
Console.WriteLine("Y: ");
Coord.y = Convert.ToInt32(Console.ReadLine());
if (Coord.y >= 1 && Coord.y <= 7)
{
correct = true;
}
else
{
Console.WriteLine("Please input an integer value from 1 to 7.");
correct = false;
}
} while (correct != true);
}
static void ParticipantDetails(participants User)
{
Console.WriteLine("Please input your name and Contact number: ");
//User name input
Console.WriteLine("Name: ");
User.name = Console.ReadLine();
//User contact number input
Console.WriteLine("Number: ");
User.contact = Console.ReadLine();
}
static void Main(string[] args)
{
//Objects
square Coord = new square();
participants User = new participants();
//Initialise 2D array
participants[][] grid = new participants[8][];
//Variables
bool correct = false;
do
{
//Methods
Input(Coord);
ParticipantDetails(User);
//Input data to array
if (grid[Coord.x][Coord.y].name == "" && grid[Coord.x][Coord.y].contact == "")
{
grid[Coord.x][Coord.y].name = User.name;
grid[Coord.x][Coord.y].contact = User.contact;
Console.WriteLine(grid[Coord.x][Coord.y]);
correct = true;
}
else
{
Console.WriteLine("That square is already filled. Please try again.");
correct = false;
}
} while (correct == false);
}
}
You did not initialize all the dimensions of the array.
//Initialise 2D jagged array
participants[][] grid = new participants[8][];
// You can do that in a for loop
grid[0] = new participants[8];
grid[1] = new participants[8];
grid[2] = new participants[8];
...
grid[7] = new participants[8];
// For loop equivalent
for (int i = 0; i < grid.Length; i++)
{
grid[i] = new participant[8];
}
A better way for your case (since all the sub-arrays have the same number of elements), would be to use multidimensional-arrays
participants[,] grid = new participants[8,8];
// and access like this
grid[Coord.x, Coord.y]
You created an array of arrays.
participants[][] grid = new participants[8][];
It is an array with 8 entries. Each of the entries is supposed to be an array of participants, but it hasn't been created.
What you need to do is to create 8 arrays and assign them to the elements of the grid array.
participant[][] grid = new participant[8][];
for (int i = 0; i < grid.Length; i++)
grid[i] = new participant[8];
Here I assume that each element of the grid array is also going to contain 8 elements, but it's up to you.
I am working on a simple code that asks for the name, age, and gender of at most 5 patients. After each patient, it should ask to input another patient or return to main menu. Once 5 have been input into an array, there should be a prompt to the user that the array is full.
My problem is the code asks for name,age and gender 5 times upfront, and does not give any indication the array is full. How would I change the code to reflect that and still save the inputs? (Code below).
class MainClass
{
enum Gender { female, male }
struct Record
{
public string _Name;
public int _Age;
public Gender _Gender;
}
public static void Main(string[] args)
{
//title
Console.Write("\t\t\t\t\tPatient Records\n");
string selection = "";
Record[] patients = new Record[5];
GetRecords(patients);
Console.Write("a. Add\n d.Display\ns. Stats\nq. Quit");
Console.Write("Your selection: ");
selection = Console.ReadLine();
switch (selection)
{
case "a":
GetRecords(patients);
break;
case "d":
break;
case "s":
Stats(patients);
break;
case "q":
//CUtility.Pause();
break;
}
}
static void GetRecords(Record[] patient_rec)
{
for (int i = 0; i < patient_rec.Length; i++)
{
Console.Write("Enter your age: ");
int.TryParse(Console.ReadLine(), out patient_rec[i]._Age);
Console.Write("Enter your name: ");
patient_rec[i]._Name = Console.ReadLine();
Console.Write("Enter your gender (female or male): ");
Gender.TryParse(Console.ReadLine(), out patient_rec[i]._Gender);
}
}
static void Stats(Record[]patient_rec)
{
}
}
I would suggest trying to make your code a little easier to read - and more robust.
Try this:
static void GetRecords(Record[] patient_rec)
{
for (int i = 0; i < patient_rec.Length; i++)
{
Console.WriteLine("Record {0} of {1} entry", i + 1, patient_rec.Length);
patient_rec[i] = new Record()
{
_Age = AskInteger("Enter your age: "),
_Name = AskString("Enter your name: "),
_Gender = AskGender("Enter your gender (female or male): "),
};
string ask = "";
while (string.IsNullOrEmpty(ask) || (ask.ToLower()[0] != 'y' && ask.ToLower()[0] != 'n'))
{
Console.WriteLine("Continue? yes or no (then hit enter)");
ask = Console.ReadLine();
}
if (ask.ToLower()[0] == 'y')
{
continue;
}
break;
}
Console.WriteLine("Thank you. Input completed.");
}
To make this work you need these three input functions:
private static int AskInteger(string message)
{
int result;
Console.WriteLine(message);
string input = Console.ReadLine();
while (!int.TryParse(input, out result))
{
Console.WriteLine("Invalid input.");
Console.WriteLine(message);
input = Console.ReadLine();
}
return result;
}
private static string AskString(string message)
{
Console.WriteLine(message);
string input = Console.ReadLine();
while (string.IsNullOrWhiteSpace(input))
{
Console.WriteLine("Invalid input.");
Console.WriteLine(message);
input = Console.ReadLine();
}
return input;
}
private static Gender AskGender(string message)
{
Gender result;
Console.WriteLine(message);
string input = Console.ReadLine();
while (!Gender.TryParse(input, out result))
{
Console.WriteLine("Invalid input.");
Console.WriteLine(message);
input = Console.ReadLine();
}
return result;
}
Your loop is only set to go to the size of the array, so logically you could show a message after the loop (this will get hit when the loop finishes).
If you were controlling your array access in a while loop then just compare your indexer i to the length of the array (patient_rec.Length), if it equals or exceeds the length then show the message.
I would do it in a simpler way:
enum Gender { female, male }
struct Record
{
public string _Name;
public int _Age;
public Gender _Gender;
}
static void Main(string[] args)
{
//title
Console.Write("\t\t\t\t\tPatient Records\n");
IList<Record> patients = GetRecords(5);
SchedulePatients(patients);
}
static void SchedulePatients(IList<Record> patients)
{
Console.Write("a. Add\n d.Display\ns. Stats\nq. Quit");
Console.Write("Your selection: ");
string selection = Console.ReadLine();
switch (selection)
{
case "a":
patients.Add(GetRecord());
SchedulePatients(patients);
break;
case "d":
break;
case "s":
Stats(patients);
break;
case "q":
//CUtility.Pause();
break;
}
}
static IList<Record> GetRecords(int amount)
{
IList<Record> patients = new List<Record>();
for (int i = 0; i < amount; i++)
{
patients.Add(GetRecord());
}
return patients;
}
static Record GetRecord()
{
Record patient = new Record();
Console.Write("Enter your age: ");
int.TryParse(Console.ReadLine(), out patient._Age);
Console.Write("Enter your name: ");
patient._Name = Console.ReadLine();
Console.Write("Enter your gender (female or male): ");
Enum.TryParse(Console.ReadLine(), out patient._Gender);
return patient;
}
static void Stats(IList<Record> patients)
{
foreach (var patient in patients)
{
Console.WriteLine(string.Concat("Name: ", patient._Name, " Age: ", patient._Age, " Gender: ", patient._Gender));
}
Console.ReadLine();
}
}
If you would like to meet the requirements with the smallest change possible, you just need to add the bit about prompting the user.
static void GetRecords(Record[] patient_rec)
{
for (int i = 0; i < patient_rec.Length; i++)
{
Console.Write("Enter your age: ");
int.TryParse(Console.ReadLine(), out patient_rec[i]._Age);
Console.Write("Enter your name: ");
patient_rec[i]._Name = Console.ReadLine();
Console.Write("Enter your gender (female or male): ");
Gender.TryParse(Console.ReadLine(), out patient_rec[i]._Gender);
Console.Write("Enter another (Y/N)? ");
var s = Console.ReadLine();
if (s.ToUpper() != "Y") return;
}
Console.WriteLine("You've entered the maximum number of records.");
}
hey i am doing BIT(bachelor of information technology) and I have read from a text file that has 20 lines and it is split by ',' what I am trying to do is get the and I am trying to find the cow that has the most milk in the switch menu I have done the search by ID number but I just can't get my head around the cow that produces the most milk from that text file
class Program
{
static void Main(string[] args)
{
Livestock[] animals = new Livestock[20];
int counter = 0;
string myLine;
string[] words;
TextReader tr = new StreamReader("S:/BIT694/livestock.txt");
while ((myLine = tr.ReadLine()) != null)
{
words = myLine.Split(',');
int ID = int.Parse(words[0]);
string LivestockType = words[1];
int YearBorn = int.Parse(words[2]);
double CostPerMonth = double.Parse(words[3]);
double CostOfVaccination = double.Parse(words[4]);
double AmountMilk = double.Parse(words[5]);
if (LivestockType == "Cow")
{
Cow newCow = new Cow(ID, "Cow", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk);
animals[counter] = newCow;
}
else if (LivestockType == "Goat")
{
Goat newGoat = new Goat(ID, "Goat", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk);
animals[counter] = newGoat;
}
counter++;
}
int choice;
for (;;)
{
do
{
Console.WriteLine("--------Menu--------"); // The menue Title
Console.WriteLine();
Console.WriteLine("1) Display livestock information by ID"); // Display the livestock by ID number
Console.WriteLine("2) Display cow that produced the most milk"); // Displays the cow that porduces the most milk
Console.WriteLine("3) Display goat that produced the least amount of milk"); // Displays the goat that produces the least amount of milk
Console.WriteLine("4) Calculate farm profit"); // Calculates the farm profit
Console.WriteLine("5) Display unprofitable livestock"); // Displays the unprofitable livestock
Console.WriteLine("0) Exit"); // Exits the program
Console.WriteLine();
Console.Write("Enter an option: ");
choice = int.Parse(Console.ReadLine());
} while (choice < 0 || choice > 5);
if (choice == 0) break;
Console.WriteLine("\n");
switch(choice)
{
case 1:
Console.Write("Enter livestock ID: ");
int input = int.Parse(Console.ReadLine());
// Find animal by id
Livestock livestock = null;
for(int i = 0; i < 20; i++)
{
if(animals[i].iD == input)
{
livestock = animals[i]; // Get the animal
}
}
if(livestock != null)
{
livestock.displayInfo();
} else
{
Console.WriteLine("ID not found"); // Invaild ID
}
break;
case 2:
Console.WriteLine("Cow that produced the most Milk:");
break;
case 3:
Console.WriteLine("Goat that produced the least amount of milk:");
break;
case 4:
Console.WriteLine("Calculateion of farm profit:");
break;
case 5:
Console.WriteLine("Livestock that are not profitable:");
break;
case 0:
Environment.Exit(0);
break;
default:
Console.WriteLine("The Option that you have entered is invalid please try again");
break;
}
Console.ReadLine();
}
}
}
public class Livestock
{
private int ID; //ID Number of livestock
private string LivestockType; //Value is either "Cow" or "Goat"
private int YearBorn; //Year of birth with format YYYY (i.e. 2014)
private double CostPerMonth; //The cost per month
private double CostOfVaccination; //Annual vaccination cost
private double AmountMilk; //The amount of milk produced per day in liters
public int iD { get { return ID; } }
public string livestockType { get { return LivestockType; } }
public double costPerMonth { get { return CostPerMonth; } }
public Livestock(int ID, string LivestockType,int YearBorn,double CostPerMonth,double CostOfVaccination,double AmountMilk)
{
this.ID = ID;
this.LivestockType = LivestockType;
this.YearBorn = YearBorn;
this.CostPerMonth = CostPerMonth;
this.CostOfVaccination = CostOfVaccination;
this.AmountMilk = AmountMilk;
}
public void displayInfo()
{
Console.WriteLine();
Console.WriteLine(LivestockType);
Console.WriteLine("ID:\t\t\t {0}",iD);
Console.WriteLine("Year Born:\t\t {0}",YearBorn);
Console.WriteLine("Cost Per Month\t\t ${0}",CostPerMonth);
Console.WriteLine("Cost Of Vaccination:\t ${0}",CostOfVaccination);
Console.WriteLine("Milk Per Day:\t\t {0}liters",AmountMilk);
return;
}
}
class Cow : Livestock
{
public Cow(int ID, string LivestockType, int YearBorn, double CostPerMonth, double CostOfVaccination, double AmountMilk) : base(ID, LivestockType, YearBorn, CostPerMonth, CostOfVaccination, AmountMilk)
{
}
}
you will see case 1 is done I just need to do the same for case 2.
You have declared AmountMilk with a private access specifier and it doesn't have a getter.
Assuming you are using C# 3 or greater, you can use automatic properties, so that you can define properties like
public string SomeProperty { get; set; }
The compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.
Livestock maxCow = animals.Where(animal => animal.LivestockType == "Cow").OrderByDescending(animal => animal.AmountMilk).FirstOrDefault();
The above code uses a Linq expression,
animals.Where(animal => animal.LivestockType == "Cow"
gets all the animals of LiveStockType "Cow" on which we do a descending sort based on the AmountMilk property and FirstOrDefault returns the first element in the sorted collection, if there is no such element it returns null.
The full code:
static void Main(string[] args)
{
Livestock[] animals = new Livestock[20];
int counter = 0;
string myLine;
string[] words;
TextReader tr = new StreamReader("S:/BIT694/livestock.txt");
while ((myLine = tr.ReadLine()) != null)
{
words = myLine.Split(',');
int ID = int.Parse(words[0]);
string LivestockType = words[1];
int YearBorn = int.Parse(words[2]);
double CostPerMonth = double.Parse(words[3]);
double CostOfVaccination = double.Parse(words[4]);
double AmountMilk = double.Parse(words[5]);
if (LivestockType == "Cow")
{
Cow newCow = new Cow(ID, "Cow", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk);
animals[counter] = newCow;
}
else if (LivestockType == "Goat")
{
Goat newGoat = new Goat(ID, "Goat", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk);
animals[counter] = newGoat;
}
counter++;
}
int choice;
for (;;)
{
do
{
Console.WriteLine("--------Menu--------"); // The menue Title
Console.WriteLine();
Console.WriteLine("1) Display livestock information by ID"); // Display the livestock by ID number
Console.WriteLine("2) Display cow that produced the most milk"); // Displays the cow that porduces the most milk
Console.WriteLine("3) Display goat that produced the least amount of milk"); // Displays the goat that produces the least amount of milk
Console.WriteLine("4) Calculate farm profit"); // Calculates the farm profit
Console.WriteLine("5) Display unprofitable livestock"); // Displays the unprofitable livestock
Console.WriteLine("0) Exit"); // Exits the program
Console.WriteLine();
Console.Write("Enter an option: ");
choice = int.Parse(Console.ReadLine());
} while (choice < 0 || choice > 5);
if (choice == 0) break;
Console.WriteLine("\n");
switch (choice)
{
case 1:
Console.Write("Enter livestock ID: ");
int input = int.Parse(Console.ReadLine());
Livestock livestock = animals.Where(animal => animal.ID == input).ToList().FirstOrDefault();
if (livestock != null)
{
livestock.displayInfo();
}
else
{
Console.WriteLine("ID not found"); // Invaild ID
}
break;
case 2:
Console.WriteLine("Cow that produced the most Milk:");
Livestock maxCow = animals.Where(animal => animal.LivestockType == "Cow").OrderByDescending(animal => animal.AmountMilk).FirstOrDefault();
if (maxCow != null)
maxCow.displayInfo();
else
Console.WriteLine("No cow");
break;
case 3:
Console.WriteLine("Goat that produced the least amount of milk:");
break;
case 4:
Console.WriteLine("Calculateion of farm profit:");
break;
case 5:
Console.WriteLine("Livestock that are not profitable:");
break;
case 0:
Environment.Exit(0);
break;
default:
Console.WriteLine("The Option that you have entered is invalid please try again");
break;
}
Console.ReadLine();
}
}
}
public class Livestock
{
public int ID { get; set; } //ID Number of livestock
public string LivestockType {get; set;} //Value is either "Cow" or "Goat"
public int YearBorn { get; set; } //Year of birth with format YYYY (i.e. 2014)
public double CostPerMonth { get; set; }//The cost per month
public double CostOfVaccination { get; set; } //Annual vaccination cost
public double AmountMilk { get; set; } //The amount of milk produced per day in liters
public Livestock(int ID, string LivestockType, int YearBorn, double CostPerMonth, double CostOfVaccination, double AmountMilk)
{
this.ID = ID;
this.LivestockType = LivestockType;
this.YearBorn = YearBorn;
this.CostPerMonth = CostPerMonth;
this.CostOfVaccination = CostOfVaccination;
this.AmountMilk = AmountMilk;
}
public void displayInfo()
{
Console.WriteLine();
Console.WriteLine(LivestockType);
Console.WriteLine("ID:\t\t\t {0}", iD);
Console.WriteLine("Year Born:\t\t {0}", YearBorn);
Console.WriteLine("Cost Per Month\t\t ${0}", CostPerMonth);
Console.WriteLine("Cost Of Vaccination:\t ${0}", CostOfVaccination);
Console.WriteLine("Milk Per Day:\t\t {0}liters", AmountMilk);
return;
}
}
LINQ reference
Because you didn't provide your "Cow" class I have to do some fantasy-code inbetween.
case 2:
Cow cowWithBestMilk; //Used to save the cow with the most milk production
///Lets Search
for( int i = 0; i < animals.Count; ++i)
{
//check lifestock, but only cows
if( LivestockType == "Cow" )
{
//Amount to check of my livestock
double livestockMilk = animals[i].getMilkAmount(); //<-- fantasy code, you have to acces here the AmountMilk of your cow
if(cowWithBestMilk != null)
{
//check if the cow from the lifestock got more milk than the other cow
if( livestockMilk > cowWithBestMilk.getMilkAmount() ) //<-- you can use >= instead of > if you want to have the last cow with the same amount
{
cowWithBestMilk = animals[i];
}
}
else
{
//there was no other cow until now
cowWithBestMilk = animals[i];
}
} //if type cow
} //for
if( cowWithBestMilk != null)
{
Console.WriteLine("Cow that produced the most Milk:" + cowWithBestMilk.getHerIdAsString() ); //<--fantasy code here
}
else
{
Console.WriteLine("Who let the cows out?");
}
break;
So what are we doing there.
First we declare cowWithBestMilk and in the loop we iterate over all livestock and overwrite our cowWithBestMilk when in the iteration over the livestock a cow is found AND it produced a higher amount of milk then our cow(WithBestMilk) from the previous iteration.
At the end when doing the console output, the case that our livestock didn't had any cow is covered by an alternate output.
I hope this helps a little bit.
This code throws a Format Exception when a wrong input is input by the user at the conversion of the string to int. It also doesn't reach the desired output of "All first class seats have been booked" when 32 tickets have been sold, it just throws an index out of range exception. Any help with this would be greatly appreciated. Thanks.
class FirstClassCompartment
{
public void FirstClassTickets()
{
bool[] seats = new bool[32];
List<int> seatsBooked = new List<int>();
int completeBooking = 0;
bool quit = false;
string ticketAmount = "";
int firstClassSeat = 0;
int convertedTicketAmount;
{
Groups firstClass = new Groups();
Console.Clear();
Console.WriteLine("\nWelcome to the First Class booking menu");
Console.WriteLine("");
Console.WriteLine("Please enter the amount of tickets you would like to book");
ticketAmount = Console.ReadLine();
Console.Clear();
Console.WriteLine("\nFirst Class booking menu");
convertedTicketAmount = Convert.ToInt32(ticketAmount);
ticketAmount = firstClass.NumberInGroupFirstClassWest;
}
do
{
Console.Clear();
Console.WriteLine("\nFirst Class booking menu");
Console.WriteLine("");
Console.WriteLine("Please press 1 to complete your first class booking");
completeBooking = Int32.Parse(Console.ReadLine());
switch (completeBooking)
{
case 1:
if (seatsBooked.Count == 0)
{
firstClassSeat++;
seats[firstClassSeat] = true;
seatsBooked.Add(firstClassSeat);
}
else
{
do
{
firstClassSeat++;
if (!seatsBooked.Contains(firstClassSeat))
{
seats[firstClassSeat] = true;
}
}
while (seatsBooked.Contains(firstClassSeat) && seatsBooked.Count < 32);
if (seatsBooked.Count < 32)
{
seatsBooked.Add(firstClassSeat);
}
}
if (seatsBooked.Count > 32)
{
Console.WriteLine("All seats for first class have been booked");
Console.WriteLine("Please press enter to continue...");
}
else
{
Console.WriteLine("Your seat: {0}", firstClassSeat + 1);
Console.WriteLine("Please press enter to continue...");
}
Console.ReadLine();
break;
default:
Console.WriteLine("\nPlease enter a valid input");
quit = true;
break;
}
} while (!quit);
}
}
You need to use Int32.TryParse and check the returned bool.
Look at the logic in you switch statement, you only handle a booking of 1. What if the user books 2 seats?
Edit: Sorry, just realised that switch statement is to handle the menu, not number of bookings
Do you need a list of ints, as well as an array of bools in order to track how many seats have been booked? Couldn't you just use a plain old int, e.g. numberOfSeatsBooked? Then you can do numberOfSeatsBooked += convertedTicketAmount;
I would do this very differently. You should have a class for seats containing a boolean for booked and iterate that rather than a random array of books that may or may not be linked to a random list of booked seats and a number of seats booked. Here's a small example:
public class Seat
{
public int SeatNo { get; set; }
public bool FirstClass { get; set ; }
public bool Booked { get; set; } // could actually be an instance of a booking info class including name and ticket id, etc.
}
public class Flight
{
public List<Seat> Seats = new List<Seat>();
private int _lastSeat = 0;
public void AddSeats(int num, bool firstClass)
{
for (int i = 0; i < num; i++)
{
_lastSeat++;
Seats.Add(new Seat { SeatNo = num, FirstClass = firstClass });
}
}
public int BookNextAvailableSeat(bool firstClass)
{
foreach (Seat seat in Seats.AsQueryable().Where(x => x.FirstClass == firstClass)
{
if (!seat.Booked)
{
seat.Booked = true;
return seat.SeatNo;
}
}
return null;
}
}
Then your:
case 1:
int booking = flight.BookNextAvailableSeat(true);
if (booking != null)
Console.WriteLine("Your seat: {0}", booking);
else
Console.WriteLine("All seats for first class have been booked");
Console.WriteLine("Please press enter to continue...");
break;
Of course you need to instantiate flight and add the 32 seats using:
Flight flight = new Flight();
flight.AddSeats(32, true);