Hello Stack Overflow,
I recently started c# and created a bakery project where a person can buy bread or pastry.
My project works just fine but one of the requirements was to use {get;set} and a constructor.
Here is my bread.cs file:
using System.Collections.Generic; namespace Bakery.Models { public class Bread { // public Bread(int aNumofBread) { // int numOfBread = aNumofBread; // } public int CalcBread(int numOfBread) { int priceOfLoaf = 5; if(numOfBread % 3 == 0) { return (numOfBread * priceOfLoaf) - ((numOfBread/3) * priceOfLoaf); } else if(numOfBread % 3 == 1) { return (numOfBread * priceOfLoaf) - ((numOfBread-1)/3 * priceOfLoaf); } else if (numOfBread % 3 == 2) { return (numOfBread * priceOfLoaf) - ((numOfBread-2)/3 *priceOfLoaf); } else return numOfBread * priceOfLoaf; } } };
As you can see I did not have a constructor in my class.
Here is what my program.cs looks like:
using System.Collections.Generic; namespace Bakery.Models { public class Program { public static void Main() { Console.ForegroundColor = ConsoleColor.Gray; Console.BackgroundColor = ConsoleColor.Black; string bakeryAscii = @" .... .' `. .' .-'``-._ `. | / - - ` | / |'<o> <o> | \ (| '` |) \ -==- / `.____.' | | _ _.'`-.__.-'`._/_ .'| |`-. /\ .-'| |`. _.' \ \ `' `' / / `._ { `. | `-.____.-' | .' } /`. `./ / __ __ \ \.' .'\ / `.| | / \/ \ | |.' \ ( ( \ \ \ / / / ) ) `. \ Y| `. .' |Y / .' \ \ ||_ _ _\/_ _ _|| / / `. \|' `|/ .' _______/ _ >--------------< _ \______.##._ ((((_( )_)))) .##. | / ```` `--------------' ''''\ | | | ( Welcome to Faisal's Bakery! \ | |-' ) ) `--' ( _ _.---.__.-' `-.___.--' `------' "; Console.ForegroundColor = ConsoleColor.DarkBlue; Console.WriteLine(bakeryAscii); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Please [y] to place your order, [m] to view the menu and [n] to exit"); string continueAnswer = Console.ReadLine().ToLower(); if (continueAnswer == "y") { Console.WriteLine("How many loaves of bread would you like to purchase today?"); string stringNumOfBread = Console.ReadLine(); int numOfBread = 0; bool checkBreadInput = Int32.TryParse(stringNumOfBread, out numOfBread); if (checkBreadInput == true) { if (numOfBread >= 0 ) { Bread bread = new Bread(); int breadPrice = bread.CalcBread(numOfBread); Console.WriteLine("You have bought " + numOfBread + " loaves of bread for: $" + breadPrice); Console.WriteLine("Would you like to buy some Pastry's today? [y] or [n]"); string pastryAnswer = Console.ReadLine().ToLower(); if (pastryAnswer == "y") { Console.WriteLine("Please enter the amount of pastry's you would like to buy:"); string stringNumOfPastry = Console.ReadLine(); int numOfPastry = 0; bool checkPastryInput = Int32.TryParse(stringNumOfPastry, out numOfPastry); if (checkPastryInput == true) { if (numOfPastry >= 0) { Pastry pastry = new Pastry(); int pastryPrice = pastry.CalcPastry(numOfPastry); Console.WriteLine("You have bought " + numOfPastry + " pastry's"); Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Green; int total = breadPrice + pastryPrice; Console.WriteLine("Your total bill is $" + total); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Goodbye"); } else ErrorNegativeNumber(); } else Error(); } else if (pastryAnswer == "n") { Console.WriteLine("Thank you for Coming in, your total bill is $" + breadPrice); Console.WriteLine("Goodbye"); } else Error(); } else { ErrorNegativeNumber(); } } else Error(); } else if (continueAnswer == "m") { Menu(); } else if (continueAnswer == "n") { Console.WriteLine("Goodbye"); } else { Error(); } } public static void Menu() { Console.WriteLine("--------------------------------"); Console.ForegroundColor = ConsoleColor.Yellow; string menu = @"Fresh Bread: $5 each (loaf) Delicious Pastry: $2 each ** Special of the week ** Bread: Buy 2, get 1 free! Pastry: 3 for $5!"; Console.WriteLine(menu); Console.WriteLine("--------------------------------"); Console.WriteLine("Press enter to continue"); string menuAnswer = Console.ReadLine(); Main(); } public static void Error() { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error, Invalid Input"); Console.WriteLine("Press Enter to Restart"); Console.ReadLine(); Main(); } public static void ErrorNegativeNumber() { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error, Invalid Input. Negative Input detected. You owe us bread!"); Console.WriteLine("Press Enter to Restart"); Console.ReadLine(); Main(); } } }
Thank you so much for your help, I know I can refactor my program.cs to make it more DRY.. but right now im just focused on using auto-implented properties.
I got really confused on this one.. I hope I can get some help, ty.
https://stackoverflow.com/questions/67326495/having-a-difficult-time-implementing-a-constructor-in-c-sharp-dotnet April 30, 2021 at 07:53AM
没有评论:
发表评论