5
\$\begingroup\$

I have kinda problem with oop in c#, I need to do a task to school but I completely new in oop. At this moment I have no idea how I can even start this, I mean I don't know how to make that program counts people... like e.g. click spacebar and it will make counter of people increase. Maybe someone could help me to solve this out. The task is:

Create a class(called Counter) that will check how many people entered the building(like the gate in the entrance counts how many people entered or how many is inside already). The class should contain a methods that will singly increase amount of people or singly decreases amount of them, the third method should show how many is inside already. Last but not least, the constructor for counter should be set as 0.

I managed to create some code, I'm not sure if it's good solution for this task, for sure there's a way to make it better, or to optimize this, I still learn this, so I just ask you guys for the opinion about it, or maybe some hints.

Sorry for my broken English!

using System;
namespace zaliczenie6test 
 { 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 Counter gate = new Counter(); 
 char wybor;
 Console.WriteLine("If you want to enter the building(through
 the gate) press 'p', if you want to leave the building press
 'd', if you want check how many people is inside press 'c'
 : ");
 do
 {
 Console.Write("Enter your choice: ");
 wybor = char.Parse(Console.ReadLine());
 if (wybor == 'p')
 {
 Counter gate2 = new Counter();
 gate2.IncNr(wybor);
 gate.Count(wybor);
 }
 else if(wybor == 'd')
 {
 Counter gate3 = new Counter();
 gate3.DecNr(wybor);
 gate.Count(wybor);
 }
 } while (wybor != 'c');
 if (wybor == 'c')
 {
 gate.CheckNr(wybor);
 }
 Console.ReadKey();
 }
}
 class Counter
 {
 private int _licznik;
 private int quantity;
 public int Licznik { get { return _licznik; } set { _licznik =
 value; }
 }
 public Counter()
 {
 _licznik = 0;
 }
 public void IncNr(char wybor)
 {
 if(wybor=='p')
 {
 Licznik += 1;
 Console.WriteLine("The amount of people that just enteredthe
 building through the gate: {0}\n",Licznik);
 }
 }
 public void CheckNr(char wybor)
 {
 if(wybor=='c')
 {
 Console.WriteLine("The current amount of people: {0}",
 quantity);
 }
 }
 public void Count(char wybor)
 {
 if(wybor == 'p')
 {
 quantity += 1;
 }
 else if(wybor == 'd')
 {
 quantity -= 1;
 }
 }
 public void DecNr(char wybor)
 {
 if(wybor == 'd')
 {
 Licznik += 1;
 Console.WriteLine("The amount of people that just passed the
 gate toward the exit is: {0}\n", Licznik);
 }
 }
 }
}
asked Dec 18, 2019 at 22:43
\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

First things first, you need to fix your indentation. It'll make it much easier to follow what's happening. Here's your code properly formatted:

using System;
namespace zaliczenie6test 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 Counter gate = new Counter(); 
 char wybor;
 Console.WriteLine("If you want to enter the building(through the gate) press 'p', if you want to leave the building press 'd', if you want check how many people is inside press 'c': ");
 do
 {
 Console.Write("Enter your choice: ");
 wybor = char.Parse(Console.ReadLine());
 if (wybor == 'p')
 {
 Counter gate2 = new Counter();
 gate2.IncNr(wybor);
 gate.Count(wybor);
 }
 else if (wybor == 'd')
 {
 Counter gate3 = new Counter();
 gate3.DecNr(wybor);
 gate.Count(wybor);
 }
 } while (wybor != 'c');
 if (wybor == 'c')
 {
 gate.CheckNr(wybor);
 } 
 Console.ReadKey();
 }
 }
 class Counter
 {
 private int _licznik;
 private int quantity;
 public int Licznik
 {
 get { return _licznik; }
 set { _licznik = value; }
 }
 public Counter()
 {
 _licznik = 0;
 }
 public void IncNr(char wybor)
 {
 if (wybor == 'p')
 {
 Licznik += 1;
 Console.WriteLine("The amount of people that just entered the building through the gate: { 0}\n", Licznik);
 }
 }
 public void CheckNr(char wybor)
 {
 if (wybor == 'c')
 {
 Console.WriteLine("The current amount of people: {0}", quantity);
 }
 }
 public void Count(char wybor)
 {
 if (wybor == 'p')
 {
 quantity += 1;
 }
 else if (wybor == 'd')
 {
 quantity -= 1;
 }
 }
 public void DecNr(char wybor)
 {
 if (wybor == 'd')
 {
 Licznik += 1;
 Console.WriteLine("The amount of people that just passed the gate toward the exit is: { 0}\n", Licznik);
 }
 }
 }
}

You're also mixing English and Polish - try to stick to one language (Licznik vs quantity).

The class should contain a methods that will singly increase amount of people or singly decreases amount of them, the third method should show how many is inside already. Last but not least, the constructor for counter should be set as 0.

This is worded a bit ambiguously, but I'll put that down to the translation. From my understanding, it wants you to make three functions

  1. Increment the counter
  2. Decrement the counter
  3. Return the count

It also mentions the difference between the number of entries and the current amount of people in the building, so you might need an additional method to return the total number of entries (ignoring the exits).

You've got those methods, but they're being passed an argument (the choice made). Those methods don't need any argument - they do one thing only. It's the responsibility of the caller to determine which to call (increment, decrement, or show the count). Also, the methods aren't very clear. What is IncNr? What's the Nr represent here? I suspect this may be 'Increment Number', but that's not very clear. Use descriptive method names.

Your Counter class should have the skeleton like so:

class Counter
{
 private int _totalEntrances;
 private int _currentCount;
 public Counter()
 {
 // ... 
 }
 public void Increment()
 {
 // ... 
 }
 public void Decrement()
 {
 // ... 
 }
 public int GetCurrentCount()
 {
 // ... 
 }
 public int GetTotalEntries()
 {
 // ... 
 }
}

I'll leave it up to you to implement those, but it should be pretty straight forward. Now, for your main method:

if (wybor == 'p')
{
 Counter gate2 = new Counter();
 gate2.IncNr(wybor);
 gate.Count(wybor);
}

Here, you're creating a new counter, increment the count, and then throwing away the counter. This doesn't actually do anything. The only work being done here is gate.Count(wybor). Also be aware that a function named Count() changing a value is very confusing. Judging by the name, it should be returning the count, not changing the count. Also, as we already know it's an entrance, so the code should be:

if (wybor == 'p')
{
 gate.Increment();
}

The same applies to the decrement call.

Currently, your code will exit if the user asks for the count. I'm not sure if this is expected behaviour. I'd expect something like 'q' to quit the program, but 'c' to simply return the count and continue.

answered Dec 19, 2019 at 1:00
\$\endgroup\$
2
\$\begingroup\$

first, thank you for letting us know it's a homework. I'll try to give you some notes.

first let's take the initiation of a class instance Counter gate = new Counter();. In C#, whenever you're using new keyword, you're creating a new instance of that type. So, when you say new Counter(); you're creating a new Counter instance of type class. Each instance is going to be treated independently. You can pass instances back and forth between each other, but in your case you need to work only on one instance in order to store the values and return them correctly.

for the class Counter

private int _licznik;
public int Licznik 
{ 
 get { return _licznik; } 
 set { _licznik = value; }
}

the shorthand for it is :

public int Licznik { get; set; }

The class is okay, but you're overdoing it slightly. The better way of doing it is to divide your work into layers based on logic needs. In your case, you need two layers, one for user input validations, and the other one for the counter.

So, for the user input validations, you'll have to move all user input, messages into one place (in your current work, it should be under Main()). and the counter, should be inside the Counter class.

The Counter class should only acts as a calculator, add and subtracts, no user input required in it. It should have a method of each required functionality (increase, decrease, show total inside), and you just add the math to them. In your case, you need to use only two variables, one for the people entered, and the other one is for the people left. so you'll have to change Licznik and quantity to :

private int entered;
private int left;

Then, use these two variables, in your methods to add and subtract, and show the total. Keep it simple.

After that, you'll need to work with the user input, so all logic inside IncNr, CheckNr, Count, DecNr needs to be moved outside the Counter class into the Main() method (or create a method for it). as these logic are for user validation logic, and you should use them on top of Counter class and not inside it.

Something like this :

public class Counter
{
 private int entered;
 private int left;
 public Counter() 
 { 
 entered = 0;
 left = 0;
 }
 public void Increase()
 {
 entered += 1;
 }
 public void Decrease()
 {
 left += 1;
 }
 public int TotalInside()
 {
 //[To-Do]
 ...
 } 
 public int TotalEntered()
 {
 //[To-Do]
 ...
 }
 public int TotalLeft()
 {
 //[To-Do]
 ...
 } 
}

Now, you'll need work under Main method, take the user input, and check it, if the input is p you just call the increment method, if d call the decrement method, and if c call the method which shows the total.

So, it should be something like this :

if (wybor == 'p')
{ 
 gate.Increase();
 result = gate.TotalEntered();
 Console.WriteLine("The amount of people that just entered the building through the gate: {0}\n", result);
}
else if(wybor == 'd')
{
 gate.Decrease();
 result = gate.TotalLeft();
 Console.WriteLine("The amount of people that just passed the gate toward the exit is: {0}\n", result);
}

the result variable is a variable of type int and declared outside the loop. This should cover it all.

answered Dec 19, 2019 at 1:34
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.