-3

I tried to run Sales 2 file but it's not working. Please help me solve it. Thank you.

Sales file

public class Sales {
    private int transactionNo;
    private int salespersonNo;
    private String name;
    private int salesAmount;
    
    //constructor
    Sales (int trans, int sP, String name1, int sA){
        transactionNo = trans;
        salespersonNo = sP;
        name = name1;
        salesAmount = sA; 
    }
    
    void displayInformation(){
        System.out.println(transactionNo + "" + salespersonNo + "" + name + "" + salesAmount);
    } 
    
    //setter method
    public void setTransactionNo(int trans1){
        transactionNo = trans1;
    }
    
    public void setSalespersonNo(int salesP1){
        salespersonNo = salesP1;
    }
    
    public void setName(String name1){
        name = name1;
    }
    
    public void setSalesAmount(int salesA1){
        salesAmount = salesA1;
    }
    
    //getter method
    public int getTransactionNo(){
        return transactionNo;
    }
    
    public int getSalespersonNo(){
        return salespersonNo;
    }
    
    public String getName(){
        return name;
    }
    
    public int getSalesAmount(){
        return salesAmount;
    }
}

Sales2 file - This is the file to run the code to let users input their details to calculate and produce the commission report for each salesperson.

import java.util.Scanner;

public class Sales2 {

    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println("SALES COMMISSON");
    System.out.println("TNO#    SALESNO#    NAME            AMOUNT  COMM RATE   COMMISSION ");
    Sales sales1 = new Sales();
    sales1.setTransactionNo(sc.nextInt());
    sales1.setSalespersonNo(sc.nextInt());
    sales1.setName(sc.nextLine());
    sales1.setSalesAmount(sc.nextInt());
    
    sales1.displayInformation();
    }    
}
Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    Does this help? https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – Abra Jun 05 '21 at 08:56
  • *"it's not working"* is not a question - please see [ask]. – kaya3 Jun 05 '21 at 09:51

3 Answers3

2

You created a constructor with 4 arguments:

Sales (int trans, int sP, String name1, int sA) { //...

But you are calling an no-argument constructor:

Sales sales1 = new Sales();

Either:

  1. Call the 4-argument constructor with dummy values:

     Sales sales1 = new Sales(-1, -1, null, -1);
    
  2. Or add a no-argument constructor:

     Sales () {
     }
    
  3. Or move all data input into constructor:

     Sales sales1 =
         new Sales(sc.nextInt(), sc.nextInt(), 
                   sc.nextLine(), sc.nextInt());
     //sales1.setTransactionNo(sc.nextInt());
     //sales1.setSalespersonNo(sc.nextInt());
     //sales1.setName(sc.nextLine());
     //sales1.setSalesAmount(sc.nextInt());
    

Just my guess at what error you might be facing.

Andrew NS Yeow
  • 341
  • 2
  • 8
0

you need to add a sc.nextLine() to consume the dangling "\n" after the user enters a number.

sales1.setTransactionNo(sc.nextInt());
sc.nextLine();
sales1.setSalespersonNo(sc.nextInt());
sc.nextLine();
sales1.setName(sc.nextLine());
sales1.setSalesAmount(sc.nextInt());
sc.nextLine();

Note that your current implementation would crash if the user enters an invalid input like "abc" instead of 123. you can consider moving the userinput to another method.

Something like the following should work.

import java.util.Scanner;
 
public class Sales2 {

  public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println("SALES COMMISSON");
    System.out.println("TNO#    SALESNO#    NAME            AMOUNT  COMM RATE   COMMISSION ");
    Sales sales1 = new Sales();
    sales1.setTransactionNo(getIntFromUser(sc));
    sales1.setSalespersonNo(getIntFromUser(sc));
    sales1.setName(sc.nextLine());
    sales1.setSalesAmount(getIntFromUser(sc));
    
    sales1.displayInformation();
  }    

  static int getIntFromUser(Scanner sc){
     while(true){ //keeps trying until user enters something valid.
        try{
           String userInput = sc.nextLine().trim(); // get user input in String
           int result = Integer.parseInt(userInput); //convert to integer

          return result; // int is return if no error, otherwise it will crash and repeat again
        }catch(Exception e){
            System.out.println("Invalid input. Try again");
        }
     }
  }
}
Angel Koh
  • 12,479
  • 7
  • 64
  • 91
0

Try to add the default constructor. As you have provided a parameterized constructor, to call new Sales() (the default constructor) you should add that as well.

public Sales() {}
Samasha
  • 369
  • 1
  • 5
  • 16