Bankteller console application - c#

I'm making a simple application to simulate the bankteller problem.
What I'm trying to simulate is:
You have 4 counters in a store. 1 counter is open. Customers start coming in and enter the line for the first counter.
When the fourth customer enters the line for the first counter, another counter should open. The line should be equally divided between the 2 counters.When the customer at the second counter is helped and no new customers enter the line, the counter should close. Basically 4 is too many.
I can't seem to figure it out. I know I need to use a queue. But how? Could someone give me an example in console application? Preferable C#.
Thanks in advance.
Here is what I tried so far,
register class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class Register
{
private int customerCount;
public Queue<Customer> Line = new Queue<Customer>();
public Register()
{
customerCount = 2;
}
public Register(int customerCount)
{
this.customerCount = customerCount;
}
public int getCustomers()
{
return customerCount;
}
}
}
Customer class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class Customer
{
private int checkoutTime;
public Customer()
{
checkoutTime = 3;
}
public Customer(int checkoutTime)
{
this.checkoutTime = checkoutTime;
}
public int GetCheckoutTime()
{
return checkoutTime;
}
}
}
Register manager:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class RegisterManager
{
public List<Register> registers = new List<Register>();
Register r1 = new Register();
Customer c1 = new Customer();
public RegisterManager()
{
registers.Add(r1);
}
public void ManageCustomers()
{
for (int i = 0; i < registers.Count; i++)
{
registers.Insert(i, new Register());
if (i / 4 <= registers..Line.Count)
{
}
}
}
}
}

Related

Why my Console.WriteLine(); does not work on referenced external project?

I'm getting an error on Console.WriteLine();
HelperLibrary Project I already reference it to my main project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MathClasses
{
public class MyMaths
{
public void Divisibility(int number, int divby)
{
int divisibility = number % divby;
if (divisibility == 0)
{
Console.WriteLine($"{number} is divisible by {divby}");
}
else
{
Console.WriteLine($"{number} is not divisible by {divby}");
}
}
}
}
and here is my main project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathClasses;
using MiscClasses;
using PrinterClasses;
namespace ChallengeOne
{
class Program
{
static void Main(string[] args)
{
MyMaths myMaths = new MyMaths();
int number = Convert.ToInt32(Console.ReadLine());
int divby = Convert.ToInt32(Console.ReadLine());
myMaths.Divisibility(number, divby);
}
}
}
Note:
my instructor says that my 2nd project doesn't recognize my using System;
the error says
The name 'Console' does not exist in the current context

How to declare an instance of another class?

I have following question, right now like 2 minutes ago a guy posted his code.
He couldn't use his instance obj , and I don't know why he deleted his question.
He had also some usings, I think he had using static N.Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int Y(int a)
{
a = 10;
Console.WriteLine(a);
return a;
}
}
}
}
namespace WindowsFormsApp1
{
class Class1
{
N.Form1 obj = new N.Form1();
public void X(int a)
{
var v = obj.Y(a);
Console.WriteLine(v);
}
}
}
So first I've seperate the method, and made it public so I can create instance and use it in another class.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int Y(int a)
{
a = 10;
Console.WriteLine(a);
return a;
}
}
And then instead of using N.From1, I used the namespace for declaring instance WindowsFormsApp1.Form1 obj = new WindowsFormsApp1.Form1();
My Class1 looks like this now:
class Class1
{
WindowsFormsApp1.Form1 obj = new WindowsFormsApp1.Form1();
public void X(int a)
{
var v = obj.Y(a);
Console.WriteLine(v);
}
}
So there isn't any error, and my question is, is this the right way to declare and instance and use it later? Also are you allowed to use the namespace ?
Thanks,

I want to make a increasing number using winform MVP pattern in C#

Hi I'm practicing using winform MVP pattern in C#.
I made Models, Presenters and Views folders, and they has a each class.
(Models has Data.cs, Presenters has Datapresenter.cs and View have interface.cs and Form.cs)
I used 'FlowLayoutPanel'. and I made Label to make numbers. Like this.
My progress so far.
Here is Data.cs (Model)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LayoutSample.Models
{
public class Data
{
public string label { get; set; }
public string CalculateArea()
{
return label;
}
}
}
Here is DataPresenter.cs (Presenter)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using LayoutSample.Models;
using LayoutSample.Views;
namespace LayoutSample.Presenters
{
public class DataPresenter
{
IFlowLabel LabelView;
public DataPresenter(IFlowLabel view)
{
LabelView = view;
}
public void CalculateArea()
{
Data data = new Models.Data();
data.label = string.Copy(LabelView.label);
var th = new Thread(() =>
{
for (int i = 0; i < 100; i++)
{
Label label = new Label();
Panel flowLayoutPanel1 = new Panel();
label.Text = i.ToString();
flowLayoutPanel1.Controls.Add(label);
Thread.Sleep(10);
}
});
th.Start();
}
}
}
Here is interface.cs(View)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LayoutSample.Views
{
public interface IFlowLabel
{
string label { get; set; }
}
}
and this is Form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LayoutSample.Models;
using LayoutSample.Presenters;
using LayoutSample.Views;
namespace LayoutSample
{
public partial class Form1 : Form, IFlowLabel
{
public Form1()
{
InitializeComponent();
}
string IFlowLabel.label
{
get
{
return flowLayoutPanel1.ToString();
}
set
{
if (flowLayoutPanel1.InvokeRequired)
{
flowLayoutPanel1.Invoke(new MethodInvoker(() =>
{
flowLayoutPanel1.Text = value;
}));
}
else
{
flowLayoutPanel1.Text = value;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 100; i++)
{
Label label = new Label();
label.AutoSize = false;
label.Width = 50;
label.Text = i.ToString();
flowLayoutPanel1.Controls.Add(label);
}
DataPresenter presenter = new DataPresenter(this);
presenter.CalculateArea();
}
}
}
From here, I want to make the numbers increasing.
How could I increase them at same time?
p.s
I've changed DataPresenter.cs
I've chagned public void Calculate Area() to
public void CalculateArea()
{
Data data = new Models.Data();
data.label = string.Copy(LabelView.label);
var th = new Thread(() =>
{
for ( int i = 1; i < 101; i++)
{
for (int j=1; j<101;j++)
{
Label label = new Label();
label.Text = j.ToString();
Console.WriteLine(label);
}
Thread.Sleep(1000);
}
});
th.Start();
}
I can watch the numbers increasing via console, but I can't see the change in WimForm. How can I bring the increment to WinForm??

The sequence number jumps by 2

I am printing out the sequence numbers and it jumps by 2 even though I am incrementing it by only 1.
I have a object TradeSequenceNo with a static variable.
I am assuming that a1 and b2 will have values 1,2 but I see that it has values 2,4. how can I make sure that I can get only increments by 1 for everytime I call TradeSequenceNo.NextSequanceNo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class TradeSequenceNo
{
private static int sequenceno;
public string id
{
get
{
return "NextTradeID";
}
}
public static int NextSequanceNo
{
get
{
sequenceno++;
return sequenceno;
}
}
}
public class Program
{
static void Main(string[] args)
{
int a1 = TradeSequenceNo.NextSequanceNo;
int b2 = TradeSequenceNo.NextSequanceNo;
}
}
}
The behaviour you posted is because of the debugger. Every time you check for property value in debugger it will be called resulting in increment of the value. Thus if you visit the same value multiple times in debugger you will get higher number depending upon how many times you checked the variable value. Just execute the following code (without debugging) and you will realize that code is correct and is behaving correctly:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class TradeSequenceNo
{
private static int sequenceno;
public string id
{
get
{
return "NextTradeID";
}
}
public static int NextSequanceNo
{
get
{
sequenceno++;
return sequenceno;
}
}
}
public class Program
{
static void Main(string[] args)
{
int a1 = TradeSequenceNo.NextSequanceNo;
int b2 = TradeSequenceNo.NextSequanceNo;
System.Console.WriteLine(a1);
System.Console.WriteLine(b2);
System.Console.ReadKey();
}
}
}

C# - Tier Separation - How to use these delegates?

Here's the relevant code:
ClickMeGame.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public class ClickMeGame
{
public OnClickMe onClickMeCallback;
public int score;
public ClickMeGame()
{
score = 0;
}
private void IncrementScore()
{
score++;
}
}
}
ClickMeCallBackDefinitions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public delegate void OnClickMe();
}
MainWindow.cs (Windows Form)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary;
namespace ClickMe
{
public partial class mainWindow : Form
{
private ClickMeGame game;
public mainWindow()
{
InitializeComponent();
game = new ClickMeGame();
game.onClickMeCallback = clickMeButton_Click();
}
private void clickMeButton_Click(object sender, EventArgs e)
{
UpdateUI();
}
private void UpdateUI()
{
scoreLabel.Text = string.Format("The score is: {0}", game.score);
}
}
}
So what I'm trying to do is, when the user clicks a button present on the form, I want a label on the form to update with the game score which increments with every click.
I'm learning about/want to be able to do this with delegates in that I want to separate the project into 2 tiers; Presenation and Logic. I know it's unnecessary to do so, but I'd like to make it such that when you click the button, the Windows Form receives information about the game score via delegates/callback methods. I'm unsure how to do this, but I tried making the callback definition and referencing it, but I'm lost from there.
Assuming that the UI button uses the click event clickMeButton_Click then here you go.
public partial class mainWindow : Form
{
private ClickMeGame game;
public mainWindow()
{
InitializeComponent();
game = new ClickMeGame();
game.onClickMeCallback = param => UpdateUI();
}
private void clickMeButton_Click(object sender, EventArgs e)
{
game.onClickMeCallback.Invoke();
}
private void UpdateUI()
{
scoreLabel.Text = string.Format("The score is: {0}", game.score);
}
}

Categories