Skip to content

Instantly share code, notes, and snippets.

@janellbaxter
Created September 25, 2018 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janellbaxter/fc1b667740bf84c42b7582e0c885a1a0 to your computer and use it in GitHub Desktop.
Save janellbaxter/fc1b667740bf84c42b7582e0c885a1a0 to your computer and use it in GitHub Desktop.
ToastR 2.0 Example for Introduction to Programming showing classes, objects, and other concepts
using System;
/*
using static System.Console;
allows access to properties and methods
of the static class Console without having
to include the name of the class
instead of: Console.ReadKey();
we can use: ReadKey();
*/
using static System.Console;
namespace Toastr
{
class Program
{
static void Main()
{
//start up the application welcome
Welcome();
//ask player what they want to order
Order.TakeOrder();
//print the order
Order.Print();
//keep window open
//we'll learn about other options soon :)
ReadKey();
}
static private void Welcome()
{
Title = "Toastr";
//the @ symbol allows multi-line strings
//helpful for ASCII design
//changeing foreground color
ForegroundColor = ConsoleColor.DarkYellow;
WriteLine(@"
_ _ _ _ _ _
( )) ( )) ( )) ( )) ( )) ( ))
| o.o || | o.o || | o.o || | o.o || | o.o || | o.o ||
| - || | - || | - || | - || | - || | - ||
'-----' '-----' '-----' '-----' '-----' '-----'
_____ _ _____
|_ _|___ ___ ___| |_| __ |
| | | . | .'|_ -| _| -|
|_| |___|__,|___|_| |__|__|
");
WriteLine(" Welcome to Toastr. We got toast.\n\n");
WriteLine(" Press any key to start...");
//reset to default foreground and background
ResetColor();
//Hold for key press
ReadKey();
//Clear console screen
Clear();
}
}
class Item
{
public string Name;
public double Calories;
public double Price;
public int Available;
public Item()
{
//default constructor has no parameters
}
public Item(string name, double calories, double price)
{
//constructor is overloaded
//this variation takes three parameters
Name = name;
Calories = calories;
Price = price;
Available = 10;
}
public void Scream()
{
WriteLine(Name + " screams!");
}
}
class Order
{
public static string Name;
public static Item[] Items = new Item[2];
public static int[] Quantity = { 0, 0 };
public static double Total;
static Item fireball = new Item("Fireball", 12.69, 2);
static Item cakecheese = new Item("CakeCheese", 2000, 30);
public static void Print()
{
WriteLine("\n\t====================");
WriteLine("\t" + Name + "'s order:");
WriteLine("\t" + Quantity[0] + " " + Items[0].Name + ": $" + (Quantity[0] * Items[0].Price));
WriteLine("\t" + Quantity[1] + " " + Items[1].Name + ": $" + (Quantity[1] * Items[1].Price));
Total = (Quantity[0] * Items[0].Price) + (Quantity[1] * Items[1].Price);
WriteLine("\t" + "Your total is $" + Total);
WriteLine("\t====================\n");
//class request :)
//the double pipe || is an OR
//we can see if anything was bought by asking if the quantity
//of the first item is greater than zero
//OR if the second is greater than zero
//this is just one possible way!
//for example, we could also have used
//if ((Quantity[0]+Quantity[1]) > 0)
//can you think of other options?
if (Quantity[0] > 0 || Quantity[1] > 0)
{
Write("You take a bite of a delicious looking ");
//example of a nested if statement
if (Quantity[0] > 0)
{
Write(Items[0].Name + "... \n");
ForegroundColor = ConsoleColor.Red;
Items[0].Scream();
}
else
{
WriteLine(Items[1].Name + "... \n");
ForegroundColor = ConsoleColor.Magenta;
Items[1].Scream();
}
}
else
{ WriteLine("You seem a bit hungry. Maybe you should have gotten an item from the bakery!"); }
}
public static void TakeOrder()
{
string input = "";
Write("What is your name? ");
Name = ReadLine();
WriteLine("What would you like to order? Today we have:");
WriteLine("a)" + fireball.Name + "($" + fireball.Price + ")");
WriteLine("b)" + cakecheese.Name + "($" + cakecheese.Price + ")");
Write("Please enter a or b: ");
input = ReadLine();
if (input == "a")
{
Items[0] = fireball;
Items[1] = cakecheese;
Write("Excellent, " + Name + "! We pride ourselves on our " + fireball.Name + ". How many would you like? ");
}
else
{
Items[0] = cakecheese;
Items[1] = fireball;
Write("Good choice " + Name + ". " + cakecheese.Name + " is quite delicious. How many would you like? ");
}
//ReadLine always reads in as a string
//We want to store a number, so we are using Convert.ToInt32(x);
//where x in the parantheses is whatever we want to convert.
//we are going to convert what someone types in (via ReadLine())
//so we can put the ReadLine() method call directly inside the parantheses
//and pass that as an argument to the ToInt32() method
Quantity[0] = Convert.ToInt32(ReadLine());
if (Quantity[0] > 0)
{
Write(Name + ", your order so far is " + Quantity[0] + " of our world-famous " + Items[0].Name + ". Would you also like " + Items[1].Name + "? (enter yes or no) ");
}
else
{
Write("Alright, no " + Items[0].Name + ". Would you like " + Items[1].Name + "? (enter yes or no) ");
}
input = ReadLine();
//ToLower() transforms the string into all lowercase
//The player can type any form of yes: Yes, yEs, YES, etc.
if (input.ToLower() == "yes")
{
Write("I knew you had excellent taste. How many would you like? ");
Quantity[1] = Convert.ToInt32(Console.ReadLine());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment