Olá, Estou pegando um curso na Udemy de Java OO, e estamos em Construtores. Estou com dificuldade para exibir os parâmetros do construtor.
Segue algoritmo que estou fazendo por conta própria(Um programa de supermercado)
CLASSE PRINCIPAL:
package exercicio;
import actions.Product;
import java.util.Locale;
import java.util.Scanner;
public class Exercicio {
public static void main(String[] args) {
Locale.setDefault(Locale.US);
Product stock;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the product name: ");
String nameOfProduct = sc.nextLine();
System.out.println("Enter it's price: ");
double priceOfProduct = sc.nextDouble();
System.out.println();
stock = new Product(nameOfProduct, priceOfProduct);
System.out.println();
System.out.println("Updated data: "+stock);
System.out.println("Is there some product in stock?(y/n)");
String op = sc.next();
switch(op) {
case "n":
stock = new Product(nameOfProduct, priceOfProduct, 0);
System.out.println("Updated data:"+stock);
break;
case "y":
System.out.println("Enter the quantity in stock: ");
int quantityInStock = sc.nextInt();
stock = new Product(nameOfProduct, priceOfProduct, quantityInStock);
System.out.println("Updated data: "+stock);
break;
}
}
}
CLASSE QUE FICA COM AS INFORMAÇÕES DO PRODUTO:
package actions;
public class Product {
private String nameProduct;
private double priceProduct;
private int quantityInStock;
public Product(String nameProd, double priceProd, int quantityInStock){
this.nameProduct = nameProd;
this.priceProduct = priceProd;
this.quantityInStock = quantityInStock;
}
public Product(String nameProd, double priceProd){
this.nameProduct = nameProd;
this.priceProduct = priceProd;
}
public String getName() {
return this.nameProduct;
}
public void setpriceProduct(double price) {
this.priceProduct = price;
}
public double getPriceProduct() {
return this.priceProduct;
}
public void setQuantityInStock(int quantityInStock) {
this.quantityInStock = quantityInStock;
}
public int getQuantityInStock() {
return this.quantityInStock;
}
public void addProductInStock(int quantity) {
this.quantityInStock+=quantity;
}
public void removeProductFromStock(int quantity) {
this.quantityInStock -= quantity;
}
}
Eu quero exibir as informações do construtor só chamando ele, como em:
stock = new Product(nameOfProduct, priceOfProduct, quantityInStock);
System.out.println("Updated data: " + stock);
System.out.println("Updated data: " + stock);
Sejam sucintos, pelo amor de Deus. Vi uns códigos em outras situações que não consegui compreender.