Thread: Need Help for a programming exercise (studying it at a beginner's level)

1. Need Help for a programming exercise (studying it at a beginner's level)

Hello,

I am stuck in a programming exercice I would be glad if you take some time to help me.
The instructions are as follow:for the local swimming pool that displays the admission cost for a group of people based on their age.The program should continue to prompt the user to enter an age until-1 is entered,and then display the total number of people in the group and the total cost for that group.Admission fees are as follows:
-under 16's-2£50
-over 65-3£ and
-all other swimmers-5£
a 20%
cross-posted-on-another-forum

should be applied to groups of more than 6 people

Code:
```public static void main(String[] args){
GUI gui = new GUI();
double cost = 0.00;
int age =0;
int total = 0;

while (age!=-1)
{age++;

[group_price = discount(list_of_ages) * sum(individual_price(list_of_ages));

}

}

}```
Best regards
Thibault[

2. Moved to correct forum (apparently).

3. Code:
```public static void main() {
int count = 0;
double cost = 0.0;
int age;

while (true) {
Console.Write("Enter swimmers age (-1 to end): ");
if (Int32.TryParse(input, ref age)) {
if (age == -1) break;
count++;
if (age < 16) cost += 2.5;
else if (age > 65) cost += 3;
else cost += 5;
} else {
Console.WriteLine("Enter numbers only!");
}
}

if (count > 6) cost *= .8;

Console.WriteLine("Total swimmers = {0}, cost = {1:0.00}", count, cost);
}```
Does not check for stupid user input of ages, like -53.

4. you really should not be handing out answers like that. it's clear that you are relatively new to this forum, so I will explain this to you:

we do not do people's homework for them.

we do provide guidance to help people find their own answers.
we do take this policy very seriously.

consider yourself warned. the next person who says something to you about this will likely not be as nice.

5. This seems like more fun:
Code:
```    class Program
{
static void Main(string[] args)
{
Dictionary<Type, Predicate<int>> swimmerTypes = new Dictionary<Type, Predicate<int>>();
swimmerTypes.Add(typeof(ChildSwimmer), new Predicate<int>(a => a < 16));
swimmerTypes.Add(typeof(SeniorSwimmer), new Predicate<int>(a => a > 65));

List<Swimmer> swimmers = new List<Swimmer>();
int age;

do
{
Console.Write("Swimmer's age (-1 to end): ");
{
Console.WriteLine("Age must be an integer.");
}
else if (age != -1)
{
Type swimmerType = swimmerTypes.Keys.FirstOrDefault(type => swimmerTypes[type](age));
if (swimmerType == null)
{
Console.WriteLine("No swimmers meet that age criteria.");
}
else
{
Swimmer swimmer = (Swimmer)swimmerType.GetConstructor(new Type[] { }).Invoke(null);
}
}
} while (age != -1);

Console.WriteLine("");

IEnumerable<IGrouping<Type, Swimmer>> swimmerGroups = swimmers.GroupBy(s => s.GetType());
foreach(IGrouping<Type, Swimmer> group in swimmerGroups)
{
Swimmer swimmer = group.First();
Console.WriteLine("{0}: {1} x {2:0.00} = {3:0.00}", swimmer, group.Count(), swimmer.Cost, group.Count() * swimmer.Cost);
}

double totalCost = swimmers.Sum(s => s.Cost);

if (swimmers.Count > 6)
{
Console.WriteLine("\nYour group of {0} gets 20% off. That's a savings of {1:0.00}!", swimmers.Count, totalCost - totalCost * 0.8);
totalCost *= 0.8;
}

Console.WriteLine("\nYour total cost is: {0:0.0.0}", totalCost);
}
}

public abstract class Swimmer
{
public string Name { get; private set; }
public double Cost { get; private set; }

public Swimmer(string name, double cost)
{
Name = name;
Cost = cost;
}

public override string ToString()
{
return Name;
}
}

public class ChildSwimmer : Swimmer
{
public ChildSwimmer()
: base("Child", 2.5)
{
}
}

public class SeniorSwimmer : Swimmer
{
public SeniorSwimmer()
: base("Senior", 3.0)
{
}
}

{
{
}
}```
Code:
```Swimmer's age (-1 to end): 37
Swimmer's age (-1 to end): 15
Swimmer's age (-1 to end): 8
Swimmer's age (-1 to end): 27
Swimmer's age (-1 to end): 52
Swimmer's age (-1 to end): 68
Swimmer's age (-1 to end): 22
Swimmer's age (-1 to end): -1

Adult: 4 x 5.00 = 20.00
Child: 2 x 2.50 = 5.00
Senior: 1 x 3.00 = 3.00

Your group of 7 gets 20% off. That's a savings of 5.60!