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

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    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));
    
    
    }
    
    
    
    }
    
    }
    Thanks for your help
    Best regards
    Thibault[
    Last edited by Salem; 10-24-2011 at 11:03 PM. Reason: remove pointless tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Moved to correct forum (apparently).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    Code:
    public static void main() {
        int count = 0;
        double cost = 0.0;
        int age;
    
        while (true) {
            Console.Write("Enter swimmers age (-1 to end): ");
            String input = Console.ReadLine();
            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. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    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 not provide gift-wrapped answers.

    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. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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));
                swimmerTypes.Add(typeof(AdultSwimmer), new Predicate<int>(a => a >= 16 && a <= 65));
    
                List<Swimmer> swimmers = new List<Swimmer>();
                int age;
    
                do
                {
                    Console.Write("Swimmer's age (-1 to end): ");
                    if (!int.TryParse(Console.ReadLine(), out age))
                    {
                        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);
                            swimmers.Add(swimmer);
                        }
                    }
                } 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)
            {
            }
        }
    
        public class AdultSwimmer : Swimmer
        {
            public AdultSwimmer()
                : base("Adult", 5.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!
    
    Your total cost is: 22.40
    Last edited by itsme86; 10-25-2011 at 11:42 AM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    OOP hurts my brain.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! beginner level question
    By seking10788 in forum C Programming
    Replies: 6
    Last Post: 07-07-2011, 08:15 AM
  2. Replies: 7
    Last Post: 10-16-2010, 11:59 PM
  3. Replies: 20
    Last Post: 09-29-2010, 11:56 AM
  4. Problem with extremely beginner exercise
    By Molokai in forum C++ Programming
    Replies: 11
    Last Post: 05-08-2007, 09:05 AM

Tags for this Thread