-1

このコードがあり、生成されたデータを保存したいのですが、方法がわかりません。データベースですか? ファイル?コード内のすべてをチャンスにして最初からやり直す必要がありますか?

私はブラジル出身なので、変数と文字列はポルトガル語ですが、コーディングの方法はユニバーサルだと思います。そのため、コードを改善するためのヒントは素晴らしいでしょう。まだ終わっていませんが、問題ではありません。

package sistema_academico;

import javax.swing.JOptionPane;

public class Sistema_Academico
{
    static Aluno Aluno[] = new Aluno[100];
    static Professor Professor[] = new Professor[100];
    static int idA = 0;
    static int idP = 0;

    public static void main(String[] args)
    {
        boolean bSair = true;

            do
            {
                    int nOption = 0;
                    String sEntrada = "";
                    String sConcatenar = "";
                    try
                    {
                     nOption = Integer.parseInt(JOptionPane.showInputDialog("0 - Sair \n" +
                                                    "1 - Cadastrar Aluno \n" +
                                                    "2 - Cadastrar Professor \n" +
                                                    "3 - Pesquisar Aluno \n" +
                                                    "4 - Pesquisar Professor \n" +
                                                    "5 - Alterar Aluno \n" +
                                                    "6 - Alterar Professor \n" +
                                                    "7 - Excluir Aluno \n" +
                                                    "8 - Excluir Professor \n"));
                    }
                    catch(Exception NumberFormat)
                    {

                    }
                     switch (nOption)
                    {           
                    case 1: JOptionPane.showMessageDialog(null, "Cadastrar Aluno");     
                        sEntrada = "";
                        sConcatenar = "";

                        sEntrada = JOptionPane.showInputDialog("Nome: ") + "_";
                        sConcatenar = sEntrada;

                        sEntrada = JOptionPane.showInputDialog("Endereço: ") + "_";
                        sConcatenar += sEntrada;

                        sEntrada = JOptionPane.showInputDialog("Curso: ") + "_";
                        sConcatenar += sEntrada;

                        sEntrada = JOptionPane.showInputDialog("RA: ") + "_";
                        sConcatenar += sEntrada;

                        CadastrarAluno(sConcatenar);

                        break;

                    case 2: JOptionPane.showMessageDialog(null, "Cadastrar Professor");
                        sEntrada = "";
                        sConcatenar = "";

                        sEntrada = JOptionPane.showInputDialog("Nome: ") + "_";
                        sConcatenar = sEntrada;

                        sEntrada = JOptionPane.showInputDialog("Endereço: ") + "_";
                        sConcatenar += sEntrada;

                        sEntrada = JOptionPane.showInputDialog("Curso: ") + "_";
                        sConcatenar += sEntrada;

                        sEntrada = JOptionPane.showInputDialog("Registro: ") + "_";
                        sConcatenar += sEntrada;

                        sEntrada = JOptionPane.showInputDialog("Materia: ") + "_";
                        sConcatenar += sEntrada;

                        CadastrarProfessor(sConcatenar);                    

                        break;

                    case 3: JOptionPane.showMessageDialog(null, "Pesquisar Aluno");
                        sEntrada = "";

                        sEntrada = JOptionPane.showInputDialog("Pesquisar RA:");
                        JOptionPane.showMessageDialog(null, PesquisarAluno(sEntrada));


                        break;

                    case 4: JOptionPane.showMessageDialog(null, "Pesquisar Professor");
                        sEntrada = "";

                        sEntrada = JOptionPane.showInputDialog("Pesquisar Registro:");
                        JOptionPane.showMessageDialog(null, PesquisarProfessor(sEntrada));

                        break;

                    case 5: JOptionPane.showMessageDialog(null, "Alterar Aluno");

                        break;

                    case 6: JOptionPane.showMessageDialog(null, "Alterar Professor");

                        break;

                    case 7: JOptionPane.showMessageDialog(null, "Excluir Aluno");

                        break;

                    case 8: JOptionPane.showMessageDialog(null, "Excluir Professor");

                        break;

                    case 0:
                        bSair = false;
                        break;

                    default:
                        JOptionPane.showMessageDialog(null, "Opção Invalida");
                        break;
                    }
            }while(bSair);
    }// Metodo Principal



    public static void CadastrarAluno(String Al)
    {   
        idA++;
        String array[] = Al.split("_");
        Aluno[idA] = new Aluno();
        Aluno[idA].setNome(array[0]);
        Aluno[idA].setEndereco(array[1]);       
        Aluno[idA].setCurso(array[2]);
        Aluno[idA].setRA(array[3]);     
    }

    public static void CadastrarProfessor(String Pf)
    {
        idP++;
        String array[] = Pf.split("_");
        Professor[idP] = new Professor();
        Professor[idP].setNome(array[0])    ;
        Professor[idP].setEndereco(array[1]);
        Professor[idP].setCurso(array[2]);
        Professor[idP].setRegistro(array[3]);
        Professor[idP].setMateria(array[4]);
    }



    public static String PesquisarAluno(String RA)
    {
        for(int i = 0; i < idA; i++)
        {
            if(Aluno[idA].getRA().equals(RA))
            {
                return Aluno[idA].getNome();
            }
        }
        return "RA não Cadastrado";
    }

    public static String PesquisarProfessor(String RE)
    {
        for(int i = 0; i < idP; i++)
        {
            if(Professor[idP].getRegistro().equals(RE))
            {
                return Professor[idP].getNome();
            }
        }
        return "Registro não Cadastrado";
    }

    public static void AlterarAluno(String Al)
    {

    }

    public static void AlterarProfessor(String Pf)
    {

    }

    public static void ExcluirAluno(String Al)
    {

    }

    public static void ExcluirProfessor(String Pf)
    {

    }
}// Classe Principal
4

1 に答える 1

0

すべての変数とメソッドを「静的」として宣言したことがわかります。これは、別の環境でメソッドを呼び出そうとすると深刻な問題を引き起こす可能性があります。最初に、それらがあなたのケースで一意であることを確認してください。

コードによって生成されたすべての値を保存する場合は、データベースを使用してすべての出力を記録することをお勧めします。

  1. 教授などのテーブルを作成します (ここに複数のテーブルがある場合)、名前、コースなどの属性 .... (何が残っているか知っています)

  2. それらのそれぞれの間に関係を作成します

  3. mySQL を使用して、生成されたデータを関連するテーブルに挿入します。

お役に立てれば :)

于 2012-10-30T11:27:38.167 に答える