1 svar
168 visningar
will sheza 25 – Fd. Medlem
Postad: 23 nov 2020 21:12

soda crate final

Hej ,är typ klar med mitt program med får inte till try och catch att fungera,alla alternativ och svar skrivs ut om man skriver hello istället för en variabel.
ett annat problem är att valen som görs i menyn hamnar ovanför menyn i terminal fönstret.

Mvh william


[code]
class MainClass

{

public class Soda
{


private string name;
private int price;

 

public string Name { get { return name; } set { name = value; } }

public int Price { get { return price; } set { price = value; } }

public Soda(string _name, int _price)
{

name = _name;
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?");

int choice = 0;

do

{

Console.WriteLine("\n Menu");

Console.WriteLine(" 1: Add a drink");
Console.WriteLine(" 2: Show your drinks");
Console.WriteLine(" 3: Total price");
Console.WriteLine(" 4: Terminate the program");

try

{

choice = int.Parse(Console.ReadLine());
}
catch(Exception ex)

{

Console.WriteLine("Sorry you can only enter varibales not words");
Console.WriteLine(ex);

 


}

 

switch (choice)
{

case 1:
add_soda();
break;
case 2:
print_crate();
break;
case 3:
calc_total();
break;
case 4:

Console.WriteLine("Program terminated");
Environment.Exit(0);


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("2. Coca cola,", "Energy drink");
Console.WriteLine("3. Ramlösa,", "Mineral Water");
Console.WriteLine("4. Peroni,", "Energy drink");
Console.WriteLine("5. Guiness,", "Energy drink" );
Console.WriteLine("6. Pepsi Max,", "Energy drink");
Console.WriteLine("7. Monster drink,", "Energy drink");
Console.WriteLine("8. Powerking,", "Energy drink");
Console.WriteLine("9. Fanta,", "Energy drink" );
Console.WriteLine("10. 7-up,", "Energy drink");

 

int input = 0;

for (int i = 0; i < Bottles.Length; i++)

{
while (!int.TryParse(Console.ReadLine(), out input) || !(input <= 10 && input >= 1))
{
Console.WriteLine("Sorry invalid choice");

}
switch (input)

{

case 1:
Console.WriteLine("Red bull has been added");
Bottles[Amount_Drinks]= new Soda("Redbull", 10);
Amount_Drinks++;
break;
case 2:
Console.WriteLine("Coca cola has been added");
Bottles[Amount_Drinks] = new Soda("Coca cola", 15);
Amount_Drinks++;
break;
case 3:
Console.WriteLine("water has been added");
Bottles[Amount_Drinks] = new Soda("water",8);
Amount_Drinks++;
break;
case 4:
Console.WriteLine("Peroni has been added");
Bottles[Amount_Drinks] = new Soda("Peroni", 10);
Amount_Drinks++;
break;
case 5:
Console.WriteLine("Guiness has been added");
Bottles[Amount_Drinks] = new Soda("Guiness", 18);
Amount_Drinks++;
break;
case 6:
Console.WriteLine("Pepsi Max has been added");
Bottles[Amount_Drinks] = new Soda("Pepsi Max", 16);
Amount_Drinks++;
break;
case 7:
Console.WriteLine("Monster has been added");
Bottles[Amount_Drinks] = new Soda("Monster", 25);
Amount_Drinks++;
break;
case 8:
Console.WriteLine("Powerking has been added");
Bottles[Amount_Drinks] = new Soda("Powerking", 15);
Amount_Drinks++;
break;
case 9:
Console.WriteLine("Fanta has been added");
Bottles[Amount_Drinks] = new Soda("Fanta", 6);
Amount_Drinks++;
break;
case 10:
Console.WriteLine("7-Up 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("Index: {0}. Name: {1}, Price: {2}", i, Bottles[i].Name, Bottles[i].Price);
}
else
{

Console.WriteLine("Empty space");
}
}
}

public int calc_total()
{

int total_price = 0;

 

foreach (var soda in Bottles)
if (soda != null)
total_price += soda.Price;

Console.WriteLine("the total price is " + total_price + " swedish kronor");

return total_price;

}

public static void Main(string[] args)
{
Sodacrate drinkholder = new Sodacrate();

var sodacrate = new Sodacrate();
sodacrate.Run();
drinkholder.print_crate();
drinkholder.calc_total();
drinkholder.add_soda();
Console.ReadKey();

 


}
}
}

}


[/code]

frosthage 1 – Fd. Medlem
Postad: 2 dec 2020 21:25 Redigerad: 2 dec 2020 21:26

Anledningen är att programmet fortsätter att köra efter att koden i catch blocket är klart. Det du förmodligen vill göra är att skriva continue; som sista statement i catch-blocket. Detta gör att do while loopen avbryts och börja om från början igen. 

catch(Exception ex)

{

Console.WriteLine("Sorry you can only enter varibales not words");
Console.WriteLine(ex);

continue;
}

Svara Avbryt
Close