C# Programmet stänger ner sig när vektorn är full. Behöver en Sort och Find också. Väldigt Grön på detta
Jag har nyss gjort om min kod och implementerat en klass för Bottles, med en konstruktor och egenskaperna smaker och pris.
Jag vill kunna skriva ut egenskaperna snyggt, ordnat och fint senare i metoden print_crate. Istället för endast input, som nu. Ex. har man valt 2 st Cola i metoden add_crate, så skrivs endast ut
Cola 4
Cola 4
ut nu utan att spara varken Cola(smaken) eller 4(priset).
Any whoooo.. När läskbacken är fylld i metoden add_soda med 25 st, och man lägger till en till,så skrivs ett felmeddelande ut på senast valda läsken: IndexOutOfRangeException was unhandled. Additional Information: Indes was outside the bounds of the array.
/* Just recently I've redone my code and implemented "myCrate" as an array and also wrote the class Bottles with a constructor.
Any how, after the implementation - my program shuts down and gives me an error, on the line which holds the most recently added item, whenever the array has been filled with more than 25 items. What I need it to do, and did before those implementations, is to write " - Your crate is full!". I've heard that this may be sorted out with get-set, but I have no clue how and haven't seen a good example anywhere. */
//Check if there is space in the crate. If there's 25, write: "Your crate is full!".
for (int i = 0; i < 25; i++)
{
if (amountBottles == 25)
{
Console.WriteLine(" - Your crate is full!");
continue;
}
else
{
amountBottles++;
break;
}
}
Also, here's my method print_crate. This writes out all the content, although the contents are stacking in a pile. How do I write the content so it writes the same items on the same line? ie
4 Cola
instead of
Cola
Cola
Cola
Cola
public void print_crate()
{
int keepshopping1 = 0;
do
{
Console.Clear();
Console.WriteLine("*******************************************************");
Console.WriteLine("** Contents of your Soda Crate **");
Console.WriteLine("*******************************************************");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Purchase more bottles?\n" + "[1] to Purchase, [2] to Remove bottles or [0] to go back to Main Menu. ");
Console.WriteLine("\n\n");
Console.WriteLine("Amount of bottles in your crate: " + amountBottles, myCrate.Length);
int cnt = amountBottles;
foreach (var sodabottles in myCrate)
{
if (sodabottles != null)
Console.WriteLine(sodabottles);
{
cnt++;
}
/*else
Console.WriteLine("Empty space");*/
}
Later on, I would like to have a method for sorting the bottles in myCrate by Price and Name and hopefully a search method too, where you can write "Cola", and it will write out how many Cola´s the crate holds.
The entire code:
using System;
namespace sodacrate
{
/*public struct Bottles
{
}*/
class Bottles //create the class Bottles to gather information about the contents of the crate
{
string flavor;// = { "Cola", "Water", "Orange", "Raspberry", "GrapeTonic" };
int price; // 4, 5, 6, 7, 8
private string v1;
private int v2;
private int v3;
//METOD: KONSTRUKTOR
public Bottles(string flavor, int price)
{
this.flavor = flavor;
this.price = price;
}
public Bottles(string v1, int v2, int v3)
{
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
//Property for flavor
public string Flavor
{
get { return flavor; }
set { flavor = value; }
}
//Property for price
public int Price
{
get { return price; }
set { price = value; }
}
public override string ToString()
{
return string.Format(Flavor + " " + Price);
//return string.Format("The bottle {0} costs {2} G.", flavor, price);
}
}
class Sodacrate
{
Bottles[] myCrate = new Bottles[25];
private int amountBottles = 0; //Identifierare. Keep track of amount of bottles in myCrate
public int crateValue = 0; //Total value of the crate content
public int i { get; private set; }
//public string[] flavor = new string[] { "Cola", "Water", "Orange", "Raspberry", "GrapeTonic" }; //The flavors of the beverages
//private int i;
public void Run()
{
int temp = 0;
do
{
Console.Clear();
Console.WriteLine("\n\n\n\n");
Console.WriteLine("*********************************************");
Console.WriteLine("** Welcome to your Sodacrate! **");
Console.WriteLine("*********************************************");
Console.WriteLine("* *");
Console.WriteLine("* These are your options: *");
Console.WriteLine("* *");
Console.WriteLine("* 1. Add soda to your crate *");
Console.WriteLine("* 2. Print the content *");
Console.WriteLine("* 3. Calculate the content *");
Console.WriteLine("* 4. Sort sodas *");
Console.WriteLine("* 5. Find sodas *");
Console.WriteLine("* 6. Remove bottles *");
Console.WriteLine("* 0. Quit *");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("*********************************************");
Console.WriteLine("\n\n\n\n");
temp = int.Parse(Console.ReadLine());
switch (temp)
{
case 1:
add_soda();
break;
case 2:
print_crate();
break;
case 3:
calc_total();
break;
case 4:
sort_sodas();
break;
case 5:
find_soda();
break;
case 6:
remove_soda();
break;
case 0:
Console.WriteLine("Shutting down program"); //avsluta programmet.
break;
default:
Console.WriteLine("Incorrect Input"); //write this if other input than 0-6 i made
break;
}
} while (temp != 0);
}
public void add_soda()
{
int adding = 0;
do
{
//Console.Clear();
//menu for choosing flavor
Console.WriteLine("\n\n\n");
Console.WriteLine("*****************************************************");
Console.WriteLine("** Which flavor do you like? **");
Console.WriteLine("*****************************************************");
Console.WriteLine("* *");
Console.WriteLine("* Choose by selecting 1-5 and ENTER or 0 to go back *");
Console.WriteLine("* *");
Console.WriteLine("* 1. COLA. Costs 4 G *");
Console.WriteLine("* 2. WATER. Costs 5 G *");
Console.WriteLine("* 3. ORANGE. Costs 6 G *");
Console.WriteLine("* 4. RASPBERRY Costs 7 G *");
Console.WriteLine("* 5. GRAPE TONIC Costs 8 G *");
Console.WriteLine("* 0. Return to Main Menu *");
Console.WriteLine("* *");
Console.WriteLine("*****************************************************");
Console.WriteLine("\n\n\n\n");
adding = int.Parse(Console.ReadLine());
//input 0-5 and then ENTER to execute
switch (adding)
{
case 1:
myCrate[i] = new Bottles("Cola", 4);
i++;
Console.WriteLine("Cola");
break;
case 2:
myCrate[i] = new Bottles("Water", 5);
i++;
Console.WriteLine("Water");
break;
case 3:
myCrate[i] = new Bottles("Orange", 6);
i++;
Console.WriteLine("Orange");
break;
case 4:
myCrate[i] = new Bottles("Raspberry", 7);
i++;
Console.WriteLine("Raspberry");
break;
case 5:
myCrate[i] = new Bottles("GrapeTonic", 8);
i++;
Console.WriteLine("Grape Tonic");
break;
default:
Console.WriteLine("Incorrect Input");
break;
}
//Check if there´s space in the crate. If there´s already 25 bottles in the crate, write " - Your crate is full".
for (int i = 0; i < 25; i++)
{
if (amountBottles == 25)
{
Console.WriteLine(" - Your crate is full!");
continue;
}
else
{
amountBottles++;
break;
}
}
}while (adding != 0);
}
public void print_crate()
{
int keepshopping1 = 0;
do
{
Console.Clear();
Console.WriteLine("*******************************************************");
Console.WriteLine("** Contents of your Soda Crate **");
Console.WriteLine("*******************************************************");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Purchase more bottles?\n" + "[1] to Purchase, [2] to Remove bottles or [0] to go back to Main Menu. ");
Console.WriteLine("\n\n");
Console.WriteLine("Amount of bottles in your crate: " + amountBottles, myCrate.Length);
int cnt = amountBottles;
foreach (var sodabottles in myCrate)
{
if (sodabottles != null)
Console.WriteLine(sodabottles);
{
cnt++;
}
/*else
Console.WriteLine("Empty space");*/
}
keepshopping1 = int.Parse(Console.ReadLine());
switch (keepshopping1)
{
case 1:
add_soda();
break;
case 2:
remove_soda();
break;
case 0: //back to main menu
break;
default:
Console.WriteLine("Incorrect Input"); //skrivs ut om annat än en siffra 1,2 eller 0 anges.
break;
}
} while (keepshopping1 != 0);
}
public void calc_total()
{
int keepshopping2 = 0;
do
{
Console.Clear();
Console.WriteLine("*******************************************************");
Console.WriteLine("** Cost of your Soda Crate **");
Console.WriteLine("*******************************************************");
Console.WriteLine();
for (int k = 0; k < myCrate.Length; k++)
//value per bottle. As for now, every bottle costs 5 G.
//i aim for having different costs of the bottles. Cola, 4G, Water 5G, Orange, 6G etcetera.
crateValue = amountBottles * 5;
{
Console.WriteLine("This will be " + crateValue + " G's, sir.");
Console.WriteLine("\n\n");
Console.WriteLine("Continue shopping?\n" + "[1] to Continue, [2] to Remove soda or [0] to go back to Main Menu. ");
Console.WriteLine("\n\n");
}
keepshopping2 = int.Parse(Console.ReadLine());
switch (keepshopping2)
{
case 1:
add_soda();
break;
case 2:
remove_soda();
break;
case 0: //back to main menu
break;
default:
Console.WriteLine("Incorrect Input"); //skrivs ut om annat än siffra 1,2 eller 0 anges.
break;
}
} while (keepshopping2 != 0);
//Keep in mind to NOT count empty spaces in the array
}
public void find_soda() //maybe another identifier?
{
//Search for a name
//Could use the string-methods ToLower() or ToUpper()
}
public void sort_sodas()
{
//Able to sort the array holding bottles and with bubble sort
int max = myCrate.Length;
//Outer loop for complete [bottles]
for (int i = 1; i < max; i++)
{
//Inner loop for row by row
int nrLeft = max - i;
for (int j = 0; j < (max - i); j++)
{
if (myCrate[j].Price > myCrate[j + 1].Price)
{
int temp = myCrate[j].Price;
myCrate[j] = myCrate[j + 1];
myCrate[j + 1].Price = temp;
}
}
}
}
public void SortBottlesByName()
{
//sort with lambda
/*this.listBottles.Sort((obj1, obj2) =>
obj1.Name.CompareTo(obj2.Name)
);*/
}
public void SortBottlesByPrice()
{
//sort with lambda
/*this.listBottles.Sort((obj1, obj2) =>
obj1.Price.CompareTo(obj2.Price)
);*/
}
public void remove_soda()
{
//enter code here
}
}
class Program
{
public static void Main(string[] args)
{
var Sodacrate = new Sodacrate();
Sodacrate.Run();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}