d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With A Program. All My Fg. > 600fg For Some Help Yo.
12Next
Add Reply New Topic New Poll
Member
Posts: 853
Joined: Jul 13 2012
Gold: 600.00
Jul 31 2015 08:56am
Code
class program
{

static public List<soda> sodas { get; set; }
public class soda
{
public int pos { get; private set; }
public string name { get; private set; }
public int cost { get; private set; }
public string type { get; private set; }

public soda(int _pos, string _name, int _cost, string _type)
{
pos = _pos;
name = _name;
if (_cost < 0)
cost = 0;
else
cost = _cost;
type = _type;
}
}
static void Main(string[] args)
{
ShowMenu();
}
private static void ShowMenu()
{
Console.Clear();
Console.WriteLine("Välj ett alternativ:");
Console.WriteLine("[1] Lägg till ny läsk");
Console.WriteLine("[2] Ta bort läsk");
Console.WriteLine("Sortera läskbacken");
Console.WriteLine("Byt plats på läsk...?");
Console.WriteLine("[4] Titta i läskbacken");

ConsoleKeyInfo info = Console.ReadKey();
int selection;
int.TryParse(info.KeyChar.ToString(), out selection);

switch (selection)
{
case 1:
AddRecord();
ShowMenu();
break;
case 2:
RemoveRecord();
ShowMenu();
break;
case 3:
SortRecord();
ShowMenu();
break;
case 4:
ShowRecords();
ShowMenu();
break;
default:
ShowMenu();
break;
}
}
private static void ShowRecords()
{
Console.Clear();
if (sodas == null)
Console.WriteLine("Finns ingen läsk att visa");
else
{
foreach (soda item in sodas)
{
Console.WriteLine("Position: {0}, Dryck: {1}, Pris: {2}, Typ: {3}", item.pos, item.name, item.cost, item.type);
}
}
Console.WriteLine("Tryck för att komma till huvudmenyn");
Console.ReadKey();
}
private static void AddRecord()
{
Console.Clear();
int pos = 0;
string name = "";
int cost = 0;
string type = "";
if (sodas == null) sodas = new List<soda>();
while (pos == 0)
{
Console.WriteLine("Position: ");
string position = Console.ReadLine();
int.TryParse(position, out pos);
}
Console.WriteLine("Namn: ");
name = Console.ReadLine();

while (cost == 0)
{
Console.WriteLine("Pris: ");
string price = Console.ReadLine();
int.TryParse(price, out cost);
}


Console.WriteLine("Typ: ");
type = Console.ReadLine();

sodas.Add(new soda(pos, name, cost, type));

}
private static void RemoveRecord()
{
Console.Clear();
if (sodas == null)
{
Console.WriteLine("Läskbacken är tom!");
Console.ReadKey();
return;
}
Console.WriteLine("Namn på läsk");
string name = Console.ReadLine();
if (sodas.Select(x => x.name).Contains(name))
{
var itemToRemove = sodas.Where(x => x.name.Equals(name)).First();
if (itemToRemove != null)
sodas.Remove(itemToRemove);
}
else
{
Console.WriteLine("INte funnen");
Console.ReadKey();
}
}
private static void SortRecord()
{
Console.Clear();
{
for (int i = 0; i < 25; i++)
{
int nrLeft = 25 - i;
for (int j = 0; j < nrLeft; j++)
{
if (position[j] > pos[j + 1])
{
int temp = pos[j];
pos[j] = pos[j + 1];
pos[j + 1] = temp;
}
}
}
}
}
}

I am trying to do object oriented program for my last homework kinda. I need help. How do I limit the list to 24, also need help with the sort one, its supposed to sort after cost.
Then I need help with the Sort record code, it doesnt work.
I might add that I am trying to make a Sodabottle Container that can have 24 bottles maximum. You should be able to add bottles and remove them. Sort a list by price and move bottles around in the list.
Also I got a bunch of code from other people if it is important I can share'em.

This post was edited by Steamie on Jul 31 2015 09:05am
Member
Posts: 853
Joined: Jul 13 2012
Gold: 600.00
Jul 31 2015 11:05am
Actually I rewrote my code to fit OOP. I'll post it if anyone is interested in helping.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 31 2015 05:14pm
sodas.OrderByDescending(x => x.cost).Take(24);
Member
Posts: 853
Joined: Jul 13 2012
Gold: 600.00
Aug 1 2015 03:55am
Quote (carteblanche @ Aug 1 2015 01:14am)
sodas.OrderByDescending(x => x.cost).Take(24);


Thank you.

So so far I have this:
Code
class Program
{
static void Main(string[] args)
{
sodacrate One = new sodacrate();
One.ShowMenu();
Console.ReadKey();
}
}
//--------------------------------------------------------------------------
public class sodacrate
{
private List<soda> sodas;

public sodacrate()
{
sodas = new List<soda>();
}
public void add_soda(string _name, int _cost, string _type)
{
sodas.Add(new soda(_name, _cost, _type));
}
public void remove_soda()
{
if (sodas == null)
{
Console.WriteLine("Läskbacken är tom!");
Console.ReadKey();
return;
}
Console.WriteLine("Namn på läsk:");
string name = Console.ReadLine();
if (sodas.Select(x => x.name).Contains(name))
{
var itemToRemove = sodas.Where(x => x.name.Equals(name)).First();
if (itemToRemove != null)
sodas.Remove(itemToRemove);
}
else
{
Console.WriteLine("Inte funnen");
Console.ReadKey();
}
}
public void change_sodas()
{
Console.WriteLine("Position: ");
string pos2 = Console.ReadLine();
int.TryParse(pos2, out pos);
Console.WriteLine("Namn på läsk du vill lägga in:");
string name = Console.ReadLine();
string[] sodas2 = sodas.Select(x => x.Replace(pos, name)).ToArray();
}
public void show_sodas()
{
foreach (var tmp_soda in sodas)
if (tmp_soda != null)
Console.WriteLine(tmp_soda);
else
Console.WriteLine("Tom plats");
}
public void sort_sodas()
{
sodas.OrderByDescending(x => x.cost).Take(24);
}
public void Swap<T>(this List<T> list, int index1, int index2)
{
T temp = list[index1];
list[index1] = list[index2];
list[index2] = temp;
}
public void ShowMenu()
{
Console.Clear();
Console.WriteLine("Välj ett alternativ:");
Console.WriteLine("[1] Lägg till ny läsk");
Console.WriteLine("[2] Ta bort läsk");
Console.WriteLine("[3] Visa läskback");
Console.WriteLine("[4] Byt läsk i läskbacke");
Console.WriteLine("[5] Sortera läskbacken");
Console.WriteLine("[6] Byt plats på läsk i läskbacken");

ConsoleKeyInfo info = Console.ReadKey();
int selection;
int.TryParse(info.KeyChar.ToString(), out selection);

switch (selection)
{
case 1:
add_soda();
ShowMenu();
break;
case 2:
remove_soda();
ShowMenu();
break;
case 3:
show_sodas();
ShowMenu();
break;
case 4:
change_sodas();
ShowMenu();
break;
case 5:
sort_sodas();
ShowMenu();
break;
case 6:
Swap<T>();
ShowMenu();
break;
default:
ShowMenu();
break;
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------
}
public class soda
{
public string name { get; private set; }
public int cost { get; private set; }
public string type { get; private set; }

public soda(string _name, int _cost, string _type)
{
{
string name = "";
int cost = 0;
string type = "";

Console.WriteLine("Namn:");
name = Console.ReadLine();

while (cost == 0)
{
Console.WriteLine("Pris: ");
string price = Console.ReadLine();
int.TryParse(price, out cost);
}

Console.WriteLine("Typ: ");
type = Console.ReadLine();
}
}
}

Some errors show up and stuff tho so that's rip. Do I write them out here?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 1 2015 11:10am
Quote
Some errors show up and stuff tho so that's rip. Do I write them out here?


why would you do such a thing? it's way more fun for everyone to stare at your code and guess what your errors are. it's not like you're trying to get help from us or anything.
Member
Posts: 853
Joined: Jul 13 2012
Gold: 600.00
Aug 2 2015 06:19am
Quote (carteblanche @ Aug 1 2015 07:10pm)
why would you do such a thing? it's way more fun for everyone to stare at your code and guess what your errors are. it's not like you're trying to get help from us or anything.


Im sorry :( Posting the errors right away.
Code
public class sodacrate

Error says: "Extension method must be defined in a non-generic static class"

Code
int.TryParse(pos2, out pos);
Console.WriteLine("Namn på läsk du vill lägga in:");
string name = Console.ReadLine();
string[] sodas2 = sodas.Select(x => x.Replace(pos, name)).ToArray();

Error says: "The name 'pos' does not exist in the current context" and "'soda' does not contain a definition for 'Replace' and no extension method 'Replace' accepting a first argument of type 'soda' could be found (are you missing a using directive or an assembly reference?)"
Code
case 1:
add_soda();
ShowMenu();
break;

Code
case 6:
Swap<T>();
ShowMenu();
break;

Error says: "There is no argument given that corresponds to the required formal parameter '_name' of 'sodacrate.add_soda(string, int, string)'" and "There is no argument given that corresponds to the required formal parameter 'list' of 'sodacrate.Swap<T>(List<T>, int, int)'" and "The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)"

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 2 2015 12:57pm
as a beginner, you should be constantly compiling your code. don't try writing everything then compiling at the end. when you come across a compilation error, you should resolve it immediately. looks like you either copy/pasted stuff from a site without understanding it.

Quote
Error says: "Extension method must be defined in a non-generic static class"

exactly what the error says. you have an extension method in a non-static class. either get rid of the extension method or make it static. why do you have an extension method? looks like you copy/pasted it from somewhere.

Quote
"The name 'pos' does not exist in the current context"


you really need to read the errors. where do you define pos?

Quote
"There is no argument given that corresponds to the required formal parameter '_name' of 'sodacrate.add_soda(string, int, string)'"


you defined it with parameters but you're not providing any.

the other problems are things you seemed to have copy/pasted from somewhere. go figure out how to accomplish the task without copy/pasting.

This post was edited by carteblanche on Aug 2 2015 01:02pm
Member
Posts: 853
Joined: Jul 13 2012
Gold: 600.00
Aug 3 2015 04:26am
Quote (carteblanche @ Aug 2 2015 08:57pm)
as a beginner, you should be constantly compiling your code. don't try writing everything then compiling at the end. when you come across a compilation error, you should resolve it immediately. looks like you either copy/pasted stuff from a site without understanding it.


How do I complile a code..? Like run it?
Also I defined the _name of sodacrate.add_soda in the public class soda, I would like to think. Cause if _type is defined then _name should be defined because the both are exactly the same.
And didnt I define pos with
Code
string pos2 = Console.ReadLine();
int.TryParse(pos2, out pos);

Like the user enters a int value and the int value is pos2, then after TryParse out comes pos. So pos should be pos2 after it checked if its a number.

"Extension method must be defined in a non-generic static class" - I have no idea what this means. I followed "help code" as it's called. I was given a task and in the task explaining what to do it provided code.

I did copy paste two things tho, one code block you provided me with and the Swap<T>. I'll the swap thing I guess since I dont understand it at all.

This post was edited by Steamie on Aug 3 2015 04:37am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 3 2015 07:35am
Quote (Steamie @ Aug 3 2015 06:26am)
How do I complile a code..? Like run it?

in visual studio, there should be an option to "Build". i think it's under the "Project" menu, but i could be wrong.

Quote
Also I defined the _name of sodacrate.add_soda in the public class soda, I would like to think. Cause if _type is defined then _name should be defined because the both are exactly the same.

you need to specify the parameters in your method call since you defined your function as requiring those parameters.
Quote
public void add_soda(string _name, int _cost, string _type)

Quote
And didnt I define pos with
Code
string pos2 = Console.ReadLine();
int.TryParse(pos2, out pos);

Like the user enters a int value and the int value is pos2, then after TryParse out comes pos. So pos should be pos2 after it checked if its a number.

No. the TryParse method accepts the first param and returns a value in the second param. you still need to define the variable. you defined pos2 but not pos.

Quote
"Extension method must be defined in a non-generic static class" - I have no idea what this means. I followed "help code" as it's called. I was given a task and in the task explaining what to do it provided code.

i suggest being careful about using provided code you don't understand.

This post was edited by carteblanche on Aug 3 2015 07:36am
Member
Posts: 15,717
Joined: Aug 20 2007
Gold: 481.00
Aug 3 2015 09:02am
if you dont figure something this simple out yourself you are wasting your time doing the homework cause even if you cheat your way through the class you wont ever get a job because you have no skills and cant program or think for yourself or critically think

you will save yourself a lot of time and trouble just learning how to do this, its extremely simple i could teach a 10 year old this program in 2 hours

This post was edited by t9x on Aug 3 2015 09:03am
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll