So here is the problem, i'm making a project that has this objectives:
make the following app.
1. The data unit of the app, is a Student class with the following attributes
. ID, No., Name, Age
2. Menu with 4 options
1- op1 - Create student (collects data from a student)
Note: for a Person object and adds it to the collection or array of objects
2- op2 - Student list (shows collection or array data on the screen)
3- op3 - Save (stores the collection or array in a binary file)
4- op4 - Load (reads the collection or array from the binary file and shows on the screen)
3. Evaluation:
1. Create 5 Students
2. Save and close the program
3. Booting and Loading Data * /
This are the Steps and objectives of my project. So here is my actual question:
- When i try to create a student for my student list, whenever i create a new one it always occupies the 1st Index of the array 0, so i can only create 1 student. I will paste the code bellow, the problem is somewhere in Case "1" of Switch.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace Cap07_TestePratico
{
class Program
{
static void Main(string[] args)
{
Aluno[] arrayAluno = new Aluno[5];
arrayAluno[0] = new Aluno();
arrayAluno[1] = new Aluno();
arrayAluno[2] = new Aluno();
arrayAluno[3] = new Aluno();
arrayAluno[4] = new Aluno();
string titulo = "MENU"; string opcao1 = "1- Cria Aluno"; string opcao2 = "2- Lista Aluno"; string opcao3 = "3- Save"; string opcao4 = "0- para sair";string opcao5 = "4- Load";
desenhaMenu(titulo, opcao1, opcao2, opcao3, opcao4,opcao5,arrayAluno);
}
static void desenhaMenu(String titulo, String opt1, String opt2, String opt3, String opt4,String opt5,Aluno[] arrayAluno)
{
string opcaoo;
String fileAndLocation = "C:\\Users\\Hugo\\Desktop\\testeC.bin";
do
{
int tamanho = titulo.Length;
int tamanho1 = opt1.Length;
int tamanho2 = opt2.Length;
int tamanho3 = opt3.Length;
int tamanho4 = opt4.Length;
int tamanho5 = opt5.Length;
for (int contador = 0; contador < 50; contador++)
{
System.Console.Write(" ");
}
#region LINHA DE CIMA DO MENU
for (int i = 0; i < 20; i++)
{
System.Console.Write("*" + " ");
}
System.Console.WriteLine();
System.Console.Write(" ");
#endregion
#region CENTRA O TITULO
for (int i = 1; i <= 49; i++)
{
System.Console.Write(" ");
}
System.Console.Write("*");
for (int i = 1; i <= 18 - tamanho / 2; i++)
{
System.Console.Write(" ");
}
System.Console.Write(titulo);
for (int i = 1; i <= 18 - tamanho / 2; i++)
{
System.Console.Write(" ");
}
if (tamanho % 2 == 0)
{
System.Console.Write(" ");
}
System.Console.WriteLine("*");
#endregion
#region CAIXA DO MENU
for (int i = 0; i < 2; i++)
{
for (int contador = 0; contador < 50; contador++)
{
System.Console.Write(" ");
}
System.Console.Write("*" + " ");
for (int k = 2; k < 20; k++)
{
System.Console.Write(" ");
}
System.Console.WriteLine("*");
}
for (int contador = 0; contador < 50; contador++)
{
System.Console.Write(" ");
}
for (int i = 0; i < 20; i++)
{
System.Console.Write("*" + " ");
}
System.Console.WriteLine();
#endregion
#region parte lateral do menu
for (int i = 2; i < 3; i++)
{
for (int contador = 0; contador < 50; contador++)
{
System.Console.Write(" ");
}
System.Console.Write("*" + " ");
for (int k = 2; k < 20; k++)
{
System.Console.Write(" ");
}
System.Console.WriteLine("*");
}
#endregion
#region 1ª opção
for (int i = 1; i <= 49; i++)
{
System.Console.Write(" ");
}
System.Console.Write(" ");
System.Console.Write("*");
System.Console.Write(" ");
System.Console.Write(opt1);
for (int i = 1; i <= 34 - tamanho1; i++)
{
System.Console.Write(" ");
}
System.Console.Write(" ");
System.Console.WriteLine("*");
#endregion
#region 2ª opção
for (int i = 1; i <= 49; i++)
{
System.Console.Write(" ");
}
System.Console.Write(" ");
System.Console.Write("*");
System.Console.Write(" ");
System.Console.Write(opt2);
for (int i = 1; i <= 34 - tamanho2; i++)
{
System.Console.Write(" ");
}
System.Console.Write(" ");
System.Console.WriteLine("*");
#endregion
#region 3ª opção
for (int i = 1; i <= 49; i++)
{
System.Console.Write(" ");
}
System.Console.Write(" ");
System.Console.Write("*");
System.Console.Write(" ");
System.Console.Write(opt3);
for (int i = 1; i <= 34 - tamanho3; i++)
{
System.Console.Write(" ");
}
System.Console.Write(" ");
System.Console.WriteLine("*");
#endregion
#region 3ª opção
for (int i = 1; i <= 49; i++)
{
System.Console.Write(" ");
}
System.Console.Write(" ");
System.Console.Write("*");
System.Console.Write(" ");
System.Console.Write(opt5);
for (int i = 1; i <= 34 - tamanho5; i++)
{
System.Console.Write(" ");
}
System.Console.Write(" ");
System.Console.WriteLine("*");
#endregion
#region 4ª opção
for (int i = 1; i <= 49; i++)
{
System.Console.Write(" ");
}
System.Console.Write(" ");
System.Console.Write("*");
System.Console.Write(" ");
System.Console.Write(opt4);
for (int i = 1; i <= 34 - tamanho4; i++)
{
System.Console.Write(" ");
}
System.Console.Write(" ");
System.Console.WriteLine("*");
#endregion
#region centra e faz a linha de baixo do menu
for (int contador = 0; contador < 50; contador++)
{
System.Console.Write(" ");
}
for (int i = 0; i < 20; i++)
{
System.Console.Write("*" + " ");
}
System.Console.WriteLine();
#endregion
#region Centra e faz "Digite a opção" e switch
for (int contador = 0; contador < 50; contador++)
{
System.Console.Write(" ");
}
System.Console.Write("Digite a opção: ");
opcaoo = System.Console.ReadLine();
switch (opcaoo)
{
case "1":
Console.Clear();
Console.WriteLine("Opção 1 Escolhida");
arrayAluno[0].setId(1);
arrayAluno[1].setId(2);
arrayAluno[2].setId(3);
arrayAluno[3].setId(4);
arrayAluno[4].setId(5);
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Insira o numero de processo do aluno");
arrayAluno[i].setNumeroDeProcesso(Convert.ToInt32(Console.ReadLine()));
Console.WriteLine("Insira o nome do aluno");
arrayAluno[i].setNome(Console.ReadLine());
do
{
Console.WriteLine("Insira a sua idade");
arrayAluno[i].setIdade(Convert.ToInt32(Console.ReadLine()));
} while (arrayAluno[i].getIdade() < 0 || arrayAluno[i].getIdade() > 20);
Console.Clear();
break;
}
break;
case "2":
Console.Clear();
for (int i = 0; i < 5; i++)
{
Console.WriteLine(arrayAluno[i].dizOla());
}
break;
case "3":
Console.Clear();
Serializar(fileAndLocation, arrayAluno);
break;
case "4":
Console.Clear();
Console.WriteLine("DesSerializado para " + fileAndLocation);
Aluno[] arrayAluno99 = (Aluno[])(DesSerializar(fileAndLocation));
for (int i = 0; i < 5; i++)
{
Console.WriteLine(arrayAluno99[i].dizOla());
}
break;
case "0":
System.Console.Clear();
System.Console.WriteLine("Você saiu");
System.Console.WriteLine("Clique para avançar");
System.Console.ReadKey();
System.Console.Clear();
System.Environment.Exit(0);
break;
default:
System.Console.Clear();
System.Console.WriteLine("A opção esta errada introduza denovo");
System.Console.WriteLine("Clique para avançar");
System.Console.ReadKey();
System.Console.Clear();
break;
}
#endregion
} while (opcaoo != "0");
}
static void Serializar(string fileLocaton, Object obj)
{
try
{
Stream streamToFile = File.OpenWrite(fileLocaton);
BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(streamToFile, obj);
streamToFile.Close();
Console.WriteLine("Objeto Serializado para " + fileLocaton);
}
catch (IOException e)
{
System.Console.WriteLine("ERRO: Impossivel criar ou abrir o ficheiro: ");
System.Console.WriteLine(e.Message);
}
catch (Exception e)
{
System.Console.WriteLine("ERRO: Ocorreu um erro inesperado: ");
System.Console.WriteLine(e.Message);
}
}
static Object DesSerializar(string fileLocation)
{
try
{
Stream streamFromFile = File.OpenRead(fileLocation);
BinaryFormatter desSerializer = new BinaryFormatter();
Object obj = desSerializer.Deserialize(streamFromFile);
streamFromFile.Close();
return obj;
}
catch (IOException e)
{
System.Console.WriteLine("ERRO: Impossivel aceder ao ficheiro");
System.Console.WriteLine(e.Message);
return null;
}
catch (Exception e)
{
System.Console.WriteLine("ERRO: Ocorreu um erro inesperado");
System.Console.WriteLine(e.Message);
return null;
}
}
}
}
(The comments are in Portuguese)
Edit: Thanks for the answers, i made a question here a week ago or something with another problem, and OriginalGrieff told me about Debbuging at the time i didn't give it much importance but now i can see how important it his to have this skill. Thank you all i'll put some effort in learning it
However in this case i know what my code is doing, and what it is suposed to do , but i don't really know how to fix it.
What I have tried:
Observation: Im still a beginner so try to make it simple. But if it is a harder solution i have no problem at all learning it.