简体   繁体   中英

JAVA - Use method from another method class

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?

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();
}

you should declare a object Artigo in Pack

private Artigo artigo;

then you could initialize this object in constructor Pack

public Pack(String descricao, double desconto){
artigo =new Artigo(descricao,desconto);
this.descricao = descricao;
this.DESCONTO = desconto;
}

after that you could use method of artigo by

artigo.sell(1);

as you want to

or you could extend from base class

public class Pack extends Artigo{
    public Pack(String descricao, double desconto){
            super(descricao,desconto);
}
}

if you want to access method of base class you just type super.methodname;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM