Вывести на экран текстовое содержимое двойного массива объектов - PullRequest
0 голосов
/ 01 июня 2019

У меня есть матрица объекта типа CorporateClient, и я должен напечатать текстовое содержимое матрицы в методе displayMatrix (). Я не понимаю, что я должен делать и как я должен это делать. Что означает текстовое содержание матрицы? Нужно ли переопределять метод toString () для каждого объекта из матрицы и вызывать его при обходе матрицы, или есть другие способы его печати?

Я ничего не пробовал, потому что я не уверен в том, что мне делать, если мои предположения верны или нет, или если есть лучшие способы напечатать текстовое содержание матрицы.

    private Object[][] matrix;
      private List<CorporateClient> listClients;
      private Stream<CorporateClient> streamClients;
      private Predicate<CorporateClient> predicate;
      private MyR[] threadsArrayWorkerTasks;


    public List<CorporateClient> getListClients() {
        return listClients;
    }
    public void setListClients(List<CorporateClient> listClients) {
        this.listClients = listClients;
    }
    public Stream<CorporateClient> getStreamClients() {
        return streamClients;
    }
    public void setStreamClients(Stream<CorporateClient> streamClients) {
        this.streamClients = streamClients;
    }
    public Predicate<CorporateClient> getPredicate() {
        return predicate;
    }
    public void setPredicate(Predicate<CorporateClient> predicate) {
        this.predicate = predicate;
    } 

      public MyR[] getThreadsArrayWorkerTasks()
      {
          MyR[] copiere=new MyR[threadsArrayWorkerTasks.length];//creeam o variabila in care vom copia vectorul,element cu element, si ii dam dimensiunea vectorului pe care-l copiem

          for(int i=0;i<threadsArrayWorkerTasks.length;i++)
          {
               copiere[i]=threadsArrayWorkerTasks[i];
          }
          return copiere;
      }

      public void setThreadsArrayWorkerTasks(MyR[] _threadsArrayWorkerTasks)
      {
          threadsArrayWorkerTasks=new MyR[_threadsArrayWorkerTasks.length];

          for(int i=0;i<_threadsArrayWorkerTasks.length;i++)
          {
              threadsArrayWorkerTasks[i]=_threadsArrayWorkerTasks[i];
          }
      }

//    Metoda set pentru campul matrix (public void setMatrix(Object[][] matrixInstance) throws Exception)
//    * seteaza campul marix cu o clona profunda (deep clone) a parametrului matrixInstance 
//    * prin aplicarea medodei clone fiecarui obiect din matrixInstance 
//    * (se aplica cast-ul la clasa CorporateClient pentru fiecare obiect)
//    * 


      public void setMatrix(Object[][] matrixInstance)throws Exception
      {
          matrix=new Object[matrixInstance.length][matrixInstance[0].length];

          for(int i=0;i<matrixInstance.length;i++)
          {

              for(int j=0;j<matrixInstance[0].length;j++)
              {
                  CorporateClient auxiliar=(CorporateClient)matrixInstance[i][j];
                  //un obiect de tip Object nu poate apela clone(),deoarece nu o vede.asa ca va trebui sa convertim matrixInstance la clasa corpclient, deoarece
                  //matrixInstance nu este de tip obiect,matrix este de tip obiect, deci nu poate apela metoda clone(),de asta nu avem nevoie sa o convertim
                  //Object nu implementeaza cloneable
                  matrix[i][j]=(CorporateClient)auxiliar.clone();


              }
          }
      }

      public Object[][] getmatrix()
      {
          Object[][] creare=new Object[matrix.length][matrix[0].length];
          for(int i=0;i<matrix.length;i++)
          {
              for(int j=0;j<matrix[0].length;j++)
              {
                 // creare[i][j]=matrix[i][j];//se face copie la referinta aici , nu la obiect, prin urmare nu e bine scris asa
                  CorporateClient auxiliare=(CorporateClient)matrix[i][j];
                  try {
                    creare[i][j]=(CorporateClient)auxiliare.clone();//auxiliare e un container de obiecte, nu e o matrice!!!!!!!!!!!!!!!!!!!!
                } catch (CloneNotSupportedException e) {

                    e.printStackTrace();
                }
              }
          }
          return creare;
      }

      //Create the method displayMatrix() -> void that is displaying on screen the textual content of the matrix ;

      public void displayMatrix()
      {

      }

А код из класса объектов, которые находятся внутри матрицы:

public class CorporateClient extends Client implements Cloneable , Comparable<CorporateClient> {
    private int nbOfEmployees;
    private static final long serialVersionUID=1;

    public CorporateClient(int clientId, String name, int vatCode, float ammountOnLastInvoice, int nbOfEmployees) throws Exception {
        super(clientId, name, vatCode, ammountOnLastInvoice);
        if(ammountOnLastInvoice<0) {
            throw new Exception("Numarul este negativ!");
        }else if(nbOfEmployees<0) {
            throw new Exception("Numarul este negativ!");
        }else {
            this.nbOfEmployees=nbOfEmployees;
        }
    }

    public int getNbOfEmployees() {
        return nbOfEmployees;
    }


    public void setNbOfEmployees(int nbOfEmployees)throws Exception {
        if(nbOfEmployees<0)
        {
            throw new Exception("Numarul de angajati nu poate fii mai mic decat 0!");
        }
        this.nbOfEmployees = nbOfEmployees;
    }


    public static long getSerialversionuid() {
        return serialVersionUID;
    }


    @Override
    public String getAbstractClientId() {
        String rezultat=this.getAbstractClientId()+"-"+this.getNbOfEmployees();
        return rezultat;
    }


    @Override
    public boolean equals(Object obj) {
        CorporateClient obiect=(CorporateClient)obj;
        if(obj instanceof CorporateClient) {
            if(obiect.getClientId()==this.getClientId() && obiect.getName()==this.getName() && obiect.getVatCode()== this.getVatCode() && obiect.getAmmountOnLastInvoice() ==this.getAmmountOnLastInvoice() && obiect.getNbOfEmployees()==this.getNbOfEmployees()) {
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        CorporateClient client= (CorporateClient)super.clone();
        client.nbOfEmployees=nbOfEmployees;
        return client;
    }

    @Override
    public int compareTo(CorporateClient o) {
        int test=0;
        if(this.nbOfEmployees==o.nbOfEmployees) {
            test=0;
        }else if(this.nbOfEmployees<o.nbOfEmployees) {
            test=1;
        }else {
            test=-1;
        }
        return test;
    }

    //* Supra-scrieti si implementati metoda getAbstractClientId() ce returneaza un obiect de tip String 
    //* ce reprezinta concatenarea valoarea mostenita pentru clientId caracterul "-" si valoarea capului nbOfEmployees 
    @Override
    public String toString() {
        return this.getClientId() +"Id-ul clientului este: "+ this.getName()+" ,numele clientului este: "+ this.getVatCode()+ " ,codul de tva este: "+ this.getAmmountOnLastInvoice()+" ,ultima suma facturata este: "+ this.getNbOfEmployees()+" ,numarul de angajati: ";
    }

}

Текстовое содержимое матрицы должно отображаться на экране.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...