0

I am VERY new to java. I am trying to understand how getters and setters work, and in an effort to do so created a program that utilizes getters and setters as opposed to directly accessing the variables. I am not sure if it was something related to the way I did that, or something else, but the program just spits out Product@5f2050f6 when I try to print product1. The program is supposed to allow you to search for products, and enter the quantity sold. It then adds that quantity you typed to the amount it already has stored. It's then supposed to print it, but I haven't been able to get any to print.

public class Product {
    public double productPrice;
    public double totalSales;
    
    
    public Product(double productPrice) { 
        this.productPrice = productPrice;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }

    public double getTotalSales() {
        return totalSales;
    }

    public void setTotalSales(double totalSales) {
        this.totalSales = totalSales;
    }
    public double updateTotalSales (int qntySold){
        double temp1;
        getTotalSales();
        temp1 = totalSales + (qntySold * getProductPrice());
        setTotalSales(totalSales + temp1);
        return 0;
    }
    
}

This is the method class file, the main class file is below

import java.util.Scanner;

public class ProductDriver {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        Product product1 = new Product(2.98);
        Product product2 = new Product(4.50);
        Product product3 = new Product(9.98);
        Product product4 = new Product(4.29);
        Product product5 = new Product(8.67);

        System.out.print("Enter product number: "); //start of loop, technically
        Integer prodNum = scanner.nextInt();
        while (prodNum != 0)
        {
            System.out.print("Quantity sold: ");
            Integer amnt = scanner.nextInt();
            switch (prodNum) { // switch statement looking for correct product
                case 1: product1.updateTotalSales(amnt);
                    break;
                case 2: product2.updateTotalSales(amnt);
                    break;
                case 3: product3.updateTotalSales(amnt);
                    break;
                case 4: product4.updateTotalSales(amnt);
                    break;
                case 5: product5.updateTotalSales(amnt);
                    break;
                default: System.out.println("Not a valid product number.");
            }
            System.out.print("Enter product number: ");
            prodNum = scanner.nextInt();
        }
        System.out.println("Total Sales");
        System.out.println("===========");
        System.out.println(product1);
        
        scanner.close();
    }
}

The result is always Product@5f2050f6 when I try to print product1. I'm not sure what the problem is, any help would be appreciated, and like I said, I am very new to java.

  • using `System.out.println(product1.totalSales);` – flyingfox Sep 26 '22 at 02:42
  • System.out.println(product1) will use the default toString() method from his parent class Object, you can write a toString() method in Product to override the default toString(). – banzhe Sep 26 '22 at 02:49
  • Thank you guys, product1.totalSales seemed to have worked. The toString() method also worked, thanks again folks. – inkystinky33 Sep 26 '22 at 03:36

0 Answers0