Home > Back-end >  Java: Double scanner in a method
Java: Double scanner in a method

Time:10-07

I'm a beginner and currently following a java course. They give exercises to make a program to calculate gross prices, net prices and VAT depending on what is given. There is this one exercise that I don't understand. So I have to make a function where I prompt the user for the gross price and the VAT. With this I have to make another function to calculate the net price. But if I'm not mistaken, you can't return 2 values. How exactly do I prompt the user twice in a single function? Sorry if the question is stupid. Here is what I have so far:

import java.util.Scanner;
public class Ex1_7_c {

    public static void main(String[] args) {

        getUser_doubleInput("Enter the gross price", "Enter the VAT");
    }
    public static double calculNet(double grossPrice, double VAT) {
        double result = grossPrice   (grossPrice/100*VAT);
        return result;
    }
    public static double getUser_doubleInput(String enterGross, String enterVAT) {
        Scanner doubleInput = new Scanner(System.in);
        System.out.println(enterGross);
        double inputGross = doubleInput.nextDouble();
        System.out.println(enterVAT);
        double inputVAT = doubleInput.nextDouble();
        return inputVAT;
    }
}

CodePudding user response:

You can return one object which will contain values you want. You can use a Map or an ArrayList, etc:

  public static Map<String,Double> getUserDoubleFromStdin(String enterGross, String enterVAT) {
        Scanner doubleInput = new Scanner(System.in);
        System.out.println(enterGross);
        double inputGross = doubleInput.nextDouble();
        System.out.println(enterVAT);
        double inputVAT = doubleInput.nextDouble();
        return Map.of("inputGross",inputGross,"inputVAT",inputVAT);
    }

CodePudding user response:

Sounds like you might use a custom object containing both gross price and VAT. The logic for calculating net price might also reside in this class.

public class PriceAndVat {
    private double grossPrice;
    private double vat;
    private double net = -1; // net price is calculated lazily, initial value of -1 should signify that it hasn't been yet evalueated
    
    public PriceAndVat(double grossPrice, double vat) {
        this.grossPrice = grossPrice;
        this.vat = vat;
    }
    
    public double getNet() {
        if (net == -1) {
            net = grossPrice   (grossPrice / 100 * vat);
        }
        return net;
    }
    
    // getters, etc.
}

public static PriceAndVat getPriceAndVatFromUser(String enterGross, String enterVAT) {
    Scanner doubleInput = new Scanner(System.in);
    
    System.out.println(enterGross);
    double inputGross = doubleInput.nextDouble();
    System.out.println(enterVAT);
    double inputVAT = doubleInput.nextDouble();
    
    return new PriceAndVat(inputGross, inputVAT);
}

Sidenote: names like getUser_doubleInput and Ex1_7_c are not aligned with the Java naming conventions

CodePudding user response:

Here is another question similar to yours - there are good answers there :)

How to return 2 values from a Java method?

In my opinion you should calculate and return 1 single value and there is no need to return more than one value.

import java.util.Scanner;
public class Ex1_7_c {

    public static void main(String[] args) {

        double result = getUser_doubleInput("Enter the gross price", "Enter the VAT");
        System.out.println("result is: " result);
    }

    public static double getUser_doubleInput(String enterGross, String enterVAT) {
        Scanner doubleInput = new Scanner(System.in);
        System.out.println(enterGross);
        double inputGross = doubleInput.nextDouble();
        System.out.println(enterVAT);
        double inputVAT = doubleInput.nextDouble();
        return inputGross   ( inputGross / 100 * inputVAT );
    }
}
  • Related