在屏幕上显示双对象数组的文本内容

时间:2019-06-01 11:54:42

标签: java matrix

我有一个类型为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: ";
    }

}

矩阵的文本内容应显示在屏幕上。

0 个答案:

没有答案