Тема: Допоможіть дописати програму
Програма представляє собою стек, куди записуються вагони. Вагон має тип і унікальний порядковий номер.
Є функція Generate, яка рандомізують тип вагона і номер його.
Але, після того як вона виводить, я хочу що б вона показала ці номери, наприклад, в типі вагона 'A'. Але, воно його не відображає.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
interface IStack {
Boolean Empty();
object Pop();
object Push(object elements);
object Peek();
void Generate(int qty);
void Display();
void DisplayAll();
}
class Carriage {
public int CarriageNumber {
get; protected set;
}
public string CarriageType {
get; protected set;
}
public Carriage(string type, int number) {
this.CarriageNumber = number;
this.CarriageType = type;
}
public override string ToString() {
return CarriageNumber.ToString() + "/" + CarriageType;
}
}
class Stack : IStack {
int StackSize;
int top;
object[] item;
public int StackSizeProperty {
get { return StackSize; }
set { StackSize = value; }
}
public Stack() {
StackSizeProperty = 11;
item = new object[StackSize];
top = 0;
}
public Stack(int capacity) {
StackSizeProperty = capacity;
item = new object[StackSizeProperty];
top = 0;
}
public bool Empty() {
if (top == 0) {
return true;
}
return false;
}
public void Generate(int qty) {
Random rnd = new Random();
Console.Write("Enter number of trains: ");
qty = int.Parse(Console.ReadLine());
string[] car_type = { "a", "b" };
for (int elements = qty; elements > 0; elements--) {
item[elements] = rnd.Next(1, 100);
int type = rnd.Next(car_type.Length);
Console.WriteLine("Item " + elements + ": " + item[elements] + "/" + car_type[type]);
}
}
public object Push(object elements) {
if (top == StackSize) {
Console.WriteLine("Stack is full.\n");
return "Stack is full.";
}
else {
item[++top] = elements;
Console.WriteLine("Item was been pushed.\n");
return "item was been pushed.";
}
}
public void Entering(string CarriageType, int CarriageNumber) {
Console.Write("\n\tEnter the type of train: ");
CarriageType = Console.ReadLine();
Console.Write("\tEnter the number of train: ");
CarriageNumber = int.Parse(Console.ReadLine());
}
public object Pop() {
if (Empty()) {
Console.WriteLine("Stack is empty.\n");
return "Nothing.";
}
else {
return item[top--];
}
}
public object Peek() {
if (Empty()) {
Console.WriteLine("Stack is empty.\n");
return "Nothing.";
}
else {
return item[top];
}
}
public void Display() {
for (int elements = top; elements > 0; elements--) {
Console.WriteLine("Item " + elements + ": " + item[elements]);
}
}
public void DisplayAll() {
Console.WriteLine("All items: ");
for (int elements = top; elements > 0; elements--) {
Console.WriteLine("\t" + item[elements]);
}
}
public void Exit() {
do{ } while (Console.ReadKey().Key != ConsoleKey.Escape);
}
public int Entering() {
Console.Write("\tEnter the number of train: ");
return int.Parse(Console.ReadLine());
}
}
class MainProgramStacks {
static void Main() {
Stack AtypeStack = new Stack();
Stack BtypeStack = new Stack();
int mm_choice = 0, type_choice = 0, peek_choice = 0,
rmv_choice = 0, display_choice = 0, qty =0,
CarriageNumberA = 0, CarriageNumberB = 0;
while (true) {
Console.WriteLine("- -- --- - Menu - --- -- -");
Console.WriteLine("- 1 - Add an element -");
Console.WriteLine("- 2 - Peek top element -");
Console.WriteLine("- 3 - Remove top element -");
Console.WriteLine("- 4 - Display stacks -");
Console.WriteLine("- -- --- - -- - --- -- -");
mm_choice = int.Parse(Console.ReadLine());
switch (mm_choice) {
case 1:
Console.Clear();
while (type_choice != 4) {
Console.WriteLine("- 1 - Add an element to type A: ");
Console.WriteLine("- 2 - Add an element to type B: ");
Console.WriteLine("- 3 - Generate elements: ");
Console.WriteLine("- 4 - Exit ");
type_choice = int.Parse(Console.ReadLine());
switch (type_choice) {
case 1:
AtypeStack.Push(new Carriage("a", AtypeStack.Entering()));
break;
case 2:
BtypeStack.Push(new Carriage("b", AtypeStack.Entering()));
break;
case 3:
AtypeStack.Generate(qty);
break;
}
}
break;
case 2:
Console.Clear();
while (peek_choice != 3) {
Console.WriteLine("- 1 - Peek top element of A type: ");
Console.WriteLine("- 2 - Peek top element of B type: ");
Console.WriteLine("- 3 - Exit ");
peek_choice = int.Parse(Console.ReadLine());
switch (peek_choice) {
case 1:
AtypeStack.Peek();
break;
case 2:
BtypeStack.Peek();
break;
}
}
break;
case 3:
Console.Clear();
while (rmv_choice != 3) {
Console.WriteLine("- 1 - Remove element from type A: ");
Console.WriteLine("- 2 - Remove element from type B: ");
Console.WriteLine("- 3 - Exit -");
rmv_choice = int.Parse(Console.ReadLine());
switch (rmv_choice) {
case 1:
AtypeStack.Pop();
break;
case 2:
BtypeStack.Pop();
break;
}
}
break;
case 4:
Console.Clear();
while (display_choice != 4) {
Console.WriteLine("- 1 - Display elements of type A: ");
Console.WriteLine("- 2 - Display elements of type B: ");
Console.WriteLine("- 3 - Display all elements: ");
Console.WriteLine("- 4 - Exit -");
display_choice = int.Parse(Console.ReadLine());
switch (display_choice) {
case 1:
AtypeStack.Display();
break;
case 2:
BtypeStack.Display();
break;
case 3:
AtypeStack.DisplayAll();
BtypeStack.DisplayAll();
break;
}
}
break;
}
}
}
}