soda crate
hej uppgiften lyder att för e nivå:
1.Lägga till en läsk i vektorn(läskbacken)
2.Skriva ut innehållet i vektorn(läskbacken)
3.Beräkna och skriva ut värdet på läsbaken
4.avsluta programmet.
Jag undrar om den är på e nivå om jag kan bara fixa problemet med koden. exempel på felet.
case 1:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks]= new Soda("Redbull", 10); // här är new Soda röd funkar inte
Amount_Drinks++;
break;
så är det med mina case där användaren ska skriva in sina läsk.hittar ni något annat säg till.
Mvh william
class MainClass
{
class Soda
{
private string Name;
private string Type;
public string Namn { get { return Name; } set { Namn = value; } }
public string Typ { get { return Type; } set { Type = value; } }
public int Price { get { return Price; } set { Price = value; } }
public Soda(string _Name, string _Type, int _Price)
{
Name = _Name;
Type = _Type;
Price = _Price;
}
}
class Sodacrate
{
private Soda[] Bottles;
private int Amount_Drinks = 0;
public Sodacrate()
{
Bottles = new Soda[24];
}
public void Run()
{
Console.WriteLine("Hello and welcome to soda crate!");
Console.WriteLine("In this program you will assemble your own soda crate with all your favourite drinks");
Console.WriteLine("What would you like to do first?");
string choice;
do
{
Console.WriteLine("\n Menu");
Console.WriteLine(" 1: Add a drink");
Console.WriteLine(" 2: Remove a drink");
Console.WriteLine(" 3: Show your drinks");
Console.WriteLine(" 4: Total price");
Console.WriteLine(" 0: Terminate the program");
choice = Console.ReadLine();
switch (choice)
{
case "1":
add_soda();
break;
case "2":
print_crate();
break;
case "3":
calc_total();
break;
default:
Console.WriteLine("Sorry this choice is not available");
break;
}
} while (choice != "0");
}
public void add_soda()
{
Console.WriteLine("Choose a drink to add to your trolley");
Console.WriteLine("1. Redbull,", "Energy drink");
Console.WriteLine("1. Coca cola,", "Energy drink");
Console.WriteLine("1. Ramlösa,", "Mineral Water");
Console.WriteLine("1. Peroni,", "Energy drink");
Console.WriteLine("1. Guiness,", "Energy drink" );
Console.WriteLine("1. Pepsi Max,", "Energy drink");
Console.WriteLine("1. Monster drink,", "Energy drink");
Console.WriteLine("1. Powerking,", "Energy drink");
Console.WriteLine("1. Fanta,", "Energy drink" );
Console.WriteLine("1. 7-up,", "Energy drink");
int input = 0;
for (int i = 0; i < Bottles.Length; i++)
{
while (!int.TryParse(Console.ReadLine(), out input) || !(input <= 4 && input >= 1))
{
Console.WriteLine("Fel inmatning. Var vänlig och välj bland alternativ ovanför.");
}
switch (input)
{
case 1:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks]= new Soda("Redbull", 10); // här är new Soda röd funkar inte
Amount_Drinks++;
break;
case 2:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks] = new Soda("Coca cola", 15);
Amount_Drinks++;
break;
case 3:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks] = new Soda("water",8);
Amount_Drinks++;
break;
case 4:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks] = new Soda("Peroni", 10);
Amount_Drinks++;
break;
case 5:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks] = new Soda("Guiness", 18);
Amount_Drinks++;
break;
case 6:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks] = new Soda("Pepsi Max", 16);
Amount_Drinks++;
break;
case 7:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks] = new Soda("Monster", 25);
Amount_Drinks++;
break;
case 8:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks] = new Soda("Powerking", 15);
Amount_Drinks++;
break;
case 9:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks] = new Soda("Fanta", 6);
Amount_Drinks++;
break;
case 10:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks] = new Soda("7-Up", 15);
Amount_Drinks++;
break;
default:
Console.WriteLine("Sorry your desired drink may not be on the list");
break;
}
}
}
public void print_crate()
{
for(int i =0;i < Bottles.Length; i++)
{
if(Bottles[i] != null)
{
Console.WriteLine(Bottles);
}
else
Console.WriteLine("Empty space");
}
}
public double calc_total()
{
int total_price = 0;
foreach (var soda in Bottles)
if (soda != null)
total_price += soda.Price;
return total_price;
}
public static void Main(string[] args)
{
Sodacrate Läskback = new Sodacrate();
var sodacrate = new Sodacrate();
sodacrate.add_soda();
sodacrate.calc_total();
sodacrate.Run();
}
}
}
}
Vad fungerar inte? Jag förstod inte.