Home > Back-end >  Java Programming - Method Overloading for Individual Savings account with two classes
Java Programming - Method Overloading for Individual Savings account with two classes

Time:04-16

We were tasked to create an individual savings account by using method overloading with two classes (one that has no modifier and one is public), inputting values depends on the choice by the user (1-3) here is my current code, I'm getting errors on case 2 and case 3's Enter your values: it says "-> expected and Variable price is already defined in main (String[])".

Here's the first class.

class AmountDue {
        double computeAmountDue (double price) {
            return price * 1.12;
    }

    double computeAmountDue (double price, int quantity) {
            return price * quantity * 1.12;
    }

    double computeAmountDue (double price, int quantity, double discount) {
            return (price * quantity - discount) * 1.12;
    }

}

Here's the second class.

    import java.util.Scanner;
public class RunAmountDue {
 public static void main(String[] args) {
            System.out.println("Press any of the following then enter values separated by spaces:");
            System.out.println("1 - Price only.");
            System.out.println("2 - Price and quantity.");
            System.out.println("3 - Price, quantity, and disccount amount.");
            
            Scanner input = new Scanner(System.in);
            AmountDue due = new AmountDue();
            double price = input.nextDouble();
            int quantity = input.nextInt();
            double discount = input.nextDouble();
            int choice = 0;
            
            System.out.println("Enter your choice: ");
            switch (choice) {
                case '1':
                System.out.println("Enter your values: "   price);
                System.out.println("Amount due is    : "   due.computeAmountDue(price));
            
                case '2':
             System.out.println("Enter your values: "   (price, quantity));
                System.out.println("Amount due is    : "   due.computeAmountDue(price, quantity));
            
                case '3':
             System.out.println("Enter your values: "   price, quantity, discount));
                System.out.println("Amount due is    : "   due.computeAmountDue(price, quantity, discount));
            }
            
            input.close();
    }

}

CodePudding user response:

Your problem is not about overloading of methods. You can't use 3 parameters for System.out.println method. You need to concatenate them as a single String:
change them to:

case '2':

System.out.println("Enter your values: "   (price   ", "   quantity));

case '3':

System.out.println("Enter your values: "   price   ", "   quantity   ", "   discount));

EDITED VERSION:
Ok, after read your comment change your code as:

public static void main(String[] args) {
    System.out.println("Press any of the following then enter values separated by spaces:");
    System.out.println("1 - Price only.");
    System.out.println("2 - Price and quantity.");
    System.out.println("3 - Price, quantity, and disccount amount.");

    Scanner input = new Scanner(System.in);
    AmountDue due = new AmountDue();
    double price = input.nextDouble();
    int quantity = input.nextInt();
    double discount = input.nextDouble();

    System.out.println("Enter your choice: ");

    String choice = input.next();

    switch (choice) {
        case "1":
            System.out.println("Enter your values: "   price);
            System.out.println("Amount due is    : "   due.computeAmountDue(price));

        case "2":
            System.out.println("Enter your values: "   (price   ", "   quantity));
            System.out.println("Amount due is    : "   due.computeAmountDue(price, quantity));

        case "3":
            System.out.println("Enter your values: "   (price   ", "   quantity   ", "   discount));
            System.out.println("Amount due is    : "   due.computeAmountDue(price, quantity, discount));
    }

    input.close();
}

CodePudding user response:

code fixed, I shouldn't have declared

double price = input.nextDouble();
int quantity = input.nextInt();
double discount = input.nextDouble();

outside the switch statement, for every cases, I need to declare that again because every cases only accept limited values depending on the choice of the user (1-3) and I also added break; after each cases because it couldn't read it as different cases.

CodePudding user response:

First of all, just right before the start of the switch statement, you have intentionally set the value of choice to zero
int choice = 0;

which means you don't have the user's choice saved with you. When it comes to the cases you have not defined case 0.

You should get the user's choice, and then take the input inside the switch block depending upon the choice that was entered. It will also help you validate the choice input.

  • Related