JAVA - Use method from another method class
24,629
your problem is accessing one class data into other class this can be done by using get method even you can set data by keeping set Method. ex:
class A
{
private int price;
public int getPrice()
{
return price;
}
public void setPrice()
{
this.price=price;
}
}
Now if i want to access price data in Class B then
class B
{
//create object of class A in any method where you want to access price data
A a=new A();
int price =a.getPrice();
}
Author by
Tiago Brites
Updated on November 02, 2020Comments
-
Tiago Brites over 3 years
I have 2 classes; The first has this code:
public class Artigo { private String descricao; private double preco; private int stockCorrente; public Artigo(String descricao, double preco){ Artigo(descricao, preco, 0); } private void Artigo(String descricao, double preco, int stockCorrente){ this.descricao = descricao; if (preco < 0 || stockCorrente < 0) { System.out.println("Erro: Valores negativos."); System.exit(0); } else this.preco = preco; this.stockCorrente = stockCorrente; } public String getDescricao(){ return descricao; } public double getPreco(){ return preco; } public void setPreco(double preco){ this.preco = preco; } public int getStockCorrente(){ return stockCorrente; } public boolean isStockEnough(int nUnits){ if (stockCorrente < nUnits){ return false; }else{ return true; } } public boolean sell(int nUnits){ if (isStockEnough(nUnits)){ stockCorrente = stockCorrente - nUnits; return true; }else{ return false; } } public void recharge(int nUnits){ stockCorrente = nUnits; } public boolean equals(Artigo otherProducts){ return this.descricao.equalsIgnoreCase(otherProducts.descricao); } public String toString(){ return descricao + ", preço: " + preco + ", stock: " + stockCorrente; }
And then my other class:
public class Pack { private String descricao; private int nArtigos; private double DESCONTO; public Pack(String descricao, double desconto){ this.descricao = descricao; this.DESCONTO = desconto; } public String getDescricao(){ return descricao; } public int getnArtigos(){ return nArtigos; } public double getDesconto(){ return DESCONTO; } public int getStockCorrente(){ return stockCorrente; } public boolean equals(Pack otherpacks){ return this.descricao.equalsIgnoreCase(otherpacks.descricao); } public String toString(){ return descricao + " com os artigos [ " + " ], com desconto de " + DESCONTO + ", com preco de " + "PRECO" + ", com stock corrente de " + "STOCK" ; } public boolean existArtigo(Artigo otherProducts){ } public boolean addArtigo(Artigo newArtigo){ } public boolean haveStock(int nUnits){ } public boolean sell(int nUnits){ } public double getPreco(){ }
In this class, all the methods are required and needed. The biggest part of them are empty because I don't know what to do in there. I mean, I know what to do, what I don't know it HOW to do.
The request is: Add articles to the pack, get the name and the current stock of each. Here I need to get them connecting with the methods of the other class, right? But How?
-
Tiago Brites about 11 yearsOk, this is not helping me. How can i get in the class pack on the method getStockCorrente the value of stockCorrente on the class Artigo?
-
Tiago Brites about 11 yearsOk, this is not helping me. How can i get in the class pack on the method getStockCorrente the value of stockCorrente on the class Artigo?
-
Leo about 11 yearsyou just int i=super.getStockCorrente();
-
Leo about 11 yearsint i= artigo.getStockCorrente();