2

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?

3 Answers 3

1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Ok, 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?
int i= artigo.getStockCorrente();
0

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;

2 Comments

Ok, 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?
you just int i=super.getStockCorrente();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.