In: Computer Science
Every time I run this code I get two errors. It's wrote in C# and I can't figure out what I'm missing or why it's not running properly. These are two separate classes but are for the same project. using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RecursiveMethod { class Program { static bool ordering = true; static string str; static int quantity = 0; static string Invoic = string.Empty; static double itemPrice; static double totalCost = 0; static void Main(string[] args) { int menuOption; int Item = 0; Console.WriteLine("Enter your FirstName"); //First Name string sFirstName = Console.ReadLine(); Console.WriteLine("Enter your LastName"); //Last Name string sLastName = Console.ReadLine(); Console.WriteLine("Enter your ID"); //Last Name string sStudentID = Console.ReadLine(); str = sFirstName + sLastName; //Call this methos to show student info staring of the programm displayStudentInfo(str,sStudentID); //display your message function displayMessage(str); string invoice = string.Empty; while (ordering) { Console.WriteLine("Enter your item"); menuOption = Convert.ToInt32(Console.ReadLine()); double cost; switch (menuOption) { case 1: Item = 1; ProgressLogic.addItem(1, invoice, out Invoic, out quantity,out itemPrice); invoice = Invoic; cost = displayTotal(itemPrice, quantity); break; case 2: Item = 2; ProgressLogic.addItem(2, invoice, out Invoic, out quantity, out itemPrice); invoice = Invoic; cost = displayTotal(itemPrice, quantity); break; case 0: done(totalCost, invoice); Console.ReadLine(); break; default: Console.WriteLine("Invalid Option"); break; } } } /// <summary> /// Show the Message /// </summary> /// <param name="str"></param> public static void displayMessage(string str) { Console.WriteLine("Welcome " + str + " to dave onlines coffee shop"); } /// <summary> /// Show the Student Info /// </summary> /// <param name="studentFullName"></param> /// <param name="iStudentID"></param> public static void displayStudentInfo(string studentFullName, string iStudentID) { Console.WriteLine("Welcome " + studentFullName + " Student ID - " + iStudentID); Console.WriteLine("Please choose the following Products code. Enter 0 to Exit"); Console.WriteLine("Product 1 kona bled -> $14.95"); Console.WriteLine("Product 2 cafe verona -> $9.95"); } /// <summary> /// Done Method to show the information based on the total cost calculations /// </summary> /// <param name="totalCost"></param> public static void done(double totalCost, string strInvoice) { ordering = false; Console.WriteLine("Customer Name"); Console.WriteLine("\n"+ strInvoice); Console.WriteLine("\n"+"Total Cost"+ totalCost + "$"); } /// <summary> /// Disaply the total cost /// </summary> /// <param name="itemPrice"></param> /// <param name="quant"></param> /// <returns></returns> public static double displayTotal(double itemPrice, double quant) { double cost = (itemPrice * quant); totalCost += cost; return totalCost; } } }
using Microsoft.SqlServer.Server; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RecursiveMethod { public class ProgressLogic { //This method will do the programming logic and will return the calculations in the out parameter public static double addItem(int item, string oldInvoiceString, out string Invoice, out int quantity, out double itemPrice) { //we need to initialize the out parameters first so definitily it will some value instead of nothig //ow it will show error Invoice = ""; quantity = 0; itemPrice = 00.00; if (item == 1) { itemPrice = 14.95; Console.WriteLine("Enter Quantity"); //Last Name quantity = Convert.ToInt32( Console.ReadLine()); Invoice = oldInvoiceString + " Product 1 kona Blend -> $" + itemPrice + " * " + quantity + " = " + itemPrice * quantity + "\n"; } if (item == 2) { itemPrice = 9.95; Console.WriteLine("Enter Quantity"); //Last Name quantity = Convert.ToInt32(Console.ReadLine()); Invoice = oldInvoiceString + "Product 2 cafe verona-> $" + itemPrice + " * " + quantity + " = " + itemPrice * quantity + "\n"; } return itemPrice; } } }
Run it Now.. In the same file..Just copy paste... I'm attaching a result from the o/p
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RecursiveMethod
{
class Program
{
static bool ordering = true;
static string str;
static int quantity = 0;
static string Invoic = string.Empty;
static double itemPrice;
static double totalCost = 0;
public class ProgressLogic
{
//This method will do the programming logic and will return the calculations in the out parameter
public static double addItem(int item, string oldInvoiceString, out string Invoice, out int quantity, out double itemPrice) {
//we need to initialize the out parameters first so definitily it will some value instead of nothig
//ow it will show error
Invoice = "";
quantity = 0;
itemPrice = 00.00;
if (item == 1) {
itemPrice = 14.95;
Console.WriteLine("Enter Quantity"); //Last Name
quantity = Convert.ToInt32( Console.ReadLine());
Invoice = oldInvoiceString + " Product 1 kona Blend -> $" + itemPrice + " * " + quantity + " = " + itemPrice * quantity + "\n";
}
if (item == 2)
{
itemPrice = 9.95;
Console.WriteLine("Enter Quantity"); //Last Name
quantity = Convert.ToInt32(Console.ReadLine());
Invoice = oldInvoiceString + "Product 2 cafe verona-> $" + itemPrice + " * " + quantity + " = " + itemPrice * quantity + "\n";
}
return itemPrice;
}
}
static void Main(string[] args)
{
int menuOption;
int Item = 0;
Console.WriteLine("Enter your FirstName"); //First Name
string sFirstName = Console.ReadLine();
Console.WriteLine("Enter your LastName"); //Last Name
string sLastName = Console.ReadLine();
Console.WriteLine("Enter your ID"); //Last Name
string sStudentID = Console.ReadLine();
str = sFirstName + sLastName;
//Call this methos to show student info staring of the programm
displayStudentInfo(str,sStudentID);
//display your message function
displayMessage(str);
string invoice = string.Empty;
while (ordering)
{
Console.WriteLine("Enter your item");
menuOption = Convert.ToInt32(Console.ReadLine());
double cost;
switch (menuOption)
{
case 1:
Item = 1;
ProgressLogic.addItem(1, invoice, out Invoic, out quantity,out itemPrice);
invoice = Invoic;
cost = displayTotal(itemPrice, quantity);
break;
case 2:
Item = 2;
ProgressLogic.addItem(2, invoice, out Invoic, out quantity, out itemPrice);
invoice = Invoic;
cost = displayTotal(itemPrice, quantity);
break;
case 0:
done(totalCost, invoice);
Console.ReadLine();
break;
default:
Console.WriteLine("Invalid Option");
break;
}
}
}
/// <summary>
/// Show the Message
/// </summary>
/// <param name="str"></param>
public static void displayMessage(string str)
{
Console.WriteLine("Welcome " + str + " to dave onlines coffee shop");
}
/// <summary>
/// Show the Student Info
/// </summary>
/// <param name="studentFullName"></param>
/// <param name="iStudentID"></param>
public static void displayStudentInfo(string studentFullName, string iStudentID)
{
Console.WriteLine("Welcome " + studentFullName + " Student ID - " + iStudentID);
Console.WriteLine("Please choose the following Products code. Enter 0 to Exit");
Console.WriteLine("Product 1 kona bled -> $14.95");
Console.WriteLine("Product 2 cafe verona -> $9.95");
}
/// <summary>
/// Done Method to show the information based on the total cost calculations
/// </summary>
/// <param name="totalCost"></param>
public static void done(double totalCost, string strInvoice)
{
ordering = false;
Console.WriteLine("Customer Name");
Console.WriteLine("\n"+ strInvoice);
Console.WriteLine("\n"+"Total Cost"+ totalCost + "$");
}
/// <summary>
/// Disaply the total cost
/// </summary>
/// <param name="itemPrice"></param>
/// <param name="quant"></param>
/// <returns></returns>
public static double displayTotal(double itemPrice, double quant)
{
double cost = (itemPrice * quant);
totalCost += cost;
return totalCost;
}
}
}
// using Microsoft.SqlServer.Server;
// using System;
// using System.Collections.Generic;
// using System.Linq;
// using System.Text;
// using System.Threading.Tasks;
// namespace RecursiveMethod
// {
// public class ProgressLogic
// {
// //This method will do the programming logic and will return the calculations in the out parameter
// public static double addItem(int item, string oldInvoiceString, out string Invoice, out int quantity, out double itemPrice) {
// //we need to initialize the out parameters first so definitily it will some value instead of nothig
// //ow it will show error
// Invoice = "";
// quantity = 0;
// itemPrice = 00.00;
// if (item == 1) {
// itemPrice = 14.95;
// Console.WriteLine("Enter Quantity"); //Last Name
// quantity = Convert.ToInt32( Console.ReadLine());
// Invoice = oldInvoiceString + " Product 1 kona Blend -> $" + itemPrice + " * " + quantity + " = " + itemPrice * quantity + "\n";
// }
// if (item == 2)
// {
// itemPrice = 9.95;
// Console.WriteLine("Enter Quantity"); //Last Name
// quantity = Convert.ToInt32(Console.ReadLine());
// Invoice = oldInvoiceString + "Product 2 cafe verona-> $" + itemPrice + " * " + quantity + " = " + itemPrice * quantity + "\n";
// }
// return itemPrice;
// }
// }
// }
Feel free to ask ?