1

Below is my code focus area is case 1 and case 2. please suggest some simple ways to accept full name considering I am a beginner:

package Tester;

import java.util.Scanner;

import com.app.org.Emp;
import com.app.org.Mgr;
import com.app.org.Worker;

public class TestOrganization {
    
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("How many people you want to recruit");
        Emp[] org = new Emp[sc.nextInt()]; //holder array ...its not a emp object...its array type of object
        boolean exit = false;
        int index = 0;
        while(!exit) {
        System.out.println("Options\n"+"1.Hire Manager\n"+"2.Hire Worker\n"+"3.Display information of all employees\n"
                + "4.Update Performance Bonus or Hourly Rate");
        switch (sc.nextInt()) {
        case 1:{
            if(index<org.length && index>=0) {
            System.out.println("Manager Details:  id,  name,  deptId, basic,  performBonus");
            org[index++] = new Mgr(sc.nextInt(),sc.nextLine(),sc.next(),sc.nextDouble(),sc.nextInt());
            }else {
                System.out.println("Invalid!!! index");
            }
            break;
        }
        case 2:{
            if(index<org.length && index>=0) {
                System.out.println("Worker Details: id, name, deptId, basic, hw, hr");
                org[index++] = new Worker(sc.nextInt(),sc.nextLine(),sc.next(),sc.nextDouble(),sc.nextDouble(),sc.nextDouble());
            }else System.out.println("invalid!!! index");
            break;
        }
        case 3:{
            System.out.println("Employees Details");
            for (Emp e : org) {
                if(e!=null)
                System.out.println(e);
            }
            
        }

input: How many people you want to recruit 5 Options 1.Hire Manager 2.Hire Worker 3.Display information of all employees 4.Update Performance Bonus or Hourly Rate 1 Manager Details: id, name, deptId, basic, performBonus 101 samarth shukla

  • 1
    do add your input – Ashish Mishra Jul 05 '21 at 07:06
  • I edited your code segment a bit for ease of understanding. I see a break at the end so maybe it's interfering with some kind of loop that may cause the problem. Also try reading the inputs separately and have a println after each read to see if it reads correctly instead of doing it in one go like that. It may help you figure out the problem or at least narrows it down – Long Doan Jul 05 '21 at 07:37
  • Thank you Long Doan for editing...it really helping me out to display my problems in a more systematic way – Samarth Shukla Jul 05 '21 at 08:07

4 Answers4

1

Accept input in some variables and pass them to the object constructor.

Check this for more deatils https://stackoverflow.com/a/13102066/7575841

package tester;

import java.util.Scanner;
import com.app.org.Employee;
import com.app.org.Manager;
import com.app.org.Worker;

public class TestOrganisation {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        
        System.out.print("What is the strength of organisation ? ");
        
        Employee[] employees = new Employee[cin.nextInt()];
        
        int count = 0;
        boolean again = true;
        
        do {
            System.out.println("***** Welcome To Virtual Organization *****");
            System.out.println("1. Hire Manager");
            System.out.println("2. Hire Worker");
            System.out.println("3. Display All");
            System.out.println("4. Update Performance Bonus/Hourly Rate");
            System.out.println("0. Wrap Up");
            System.out.print("Enter option number : ");
            
            switch(cin.nextInt() ) {
              case 1:
                if (count < employees.length) {
                  System.out.println("Hire a new manager.");
                  System.out.print("Enter Employee ID : ");
                  int id = cin.nextInt();
                  System.out.print("Enter Departemnt ID : ");
                  int deptID = cin.nextInt();
                  cin.nextLine();
                  System.out.print("Enter Name : ");
                  String name = cin.nextLine();
                  System.out.print("Enter Basic Salary : ");
                  double basic = cin.nextDouble();
                  System.out.print("Enter Performance Bonus : ");
                  double perfBonus = cin.nextDouble();
                  employees[count++] = new Manager(id, deptID, name, basic, perfBonus);
                  System.out.println("Manager Hired Successfully.\n");
                }
                else {
                  System.out.println("\nNo more vacancy available.\n");
                }
                break;
              case 2:
                if (count < employees.length) {
                  System.out.println("Hire a new worker.");
                  System.out.print("Enter Employee ID : ");
                  int id = cin.nextInt();
                  System.out.print("Enter Departemnt ID : ");
                  int deptID = cin.nextInt();
                  cin.nextLine();
                  System.out.print("Enter Name : ");
                  String name = cin.nextLine();
                  System.out.print("Enter Basic Salary : ");
                  double basic = cin.nextDouble();
                  System.out.print("Enter Hourly Rate : ");
                  double hourlyRate = cin.nextDouble();
                  System.out.print("Enter Hours Worked : ");
                  int hoursWorked = cin.nextInt();
                  employees[count++] = new Worker(id, deptID, name, basic, hourlyRate, hoursWorked);
                  System.out.println("Wroker Hired Successfully.\n");
                }
                else {
                  System.out.println("\nNo more vacancy available.\n");
                }
                break;
              case 3:
                if (count == 0) {
                  System.out.println("\nOnly Ghosts Work Here.\n");
                }
                else {
                  System.out.println("\nList of employees work in organization.");
                  for (Employee e: employees) {
                    if (e instanceof Worker) {
                      System.out.print(e);
                      System.out.println(", whose net salary is " + ((Worker)e).computeNetSalary());
                    }
                    else if (e instanceof Manager) {
                      System.out.print(e);
                      System.out.println(", whose net salary is " + ((Manager)e).computeNetSalary());
                    }
                    else {
                      System.out.println("Position is either empty or a ghost works here.");
                    }
                  }
                  System.out.println();
                }
                break;
              case 4:
                if (count == 0) {
                  System.out.println("\nOnly Ghosts Work Here.\n");
                }
                else {
                  System.out.print("Enter Employee ID : ");
                  int empID = cin.nextInt();
                  for (Employee e: employees) {
                    if (e != null) {
                      if(e.equals(empID)) {
                        if (e instanceof Worker) {
                        System.out.print("Enter new hourly rate : ");
                        double hourlyRate = cin.nextDouble();
                        ((Worker) e).setHourlyRate(hourlyRate);
                        System.out.println("Hourly Rate Updated.\n");
                      }
                      else if (e instanceof Manager) {
                        System.out.print("Enter new performance bonus : ");
                        double prefBonus = cin.nextDouble();
                        ((Manager) e).setPerfBonus(prefBonus);
                        System.out.println("Performance Bonus Updated.\n");
                      }
                      else {
                        System.out.printf("\nEmployee with %id is a ghost.\n", empID);
                      }
                    }
                      break;
                    }
                  }
                }
                break;
                case 0:
                  again = false;
                  System.out.println("Good Bye. Have a nice day.");
                  break;
                default:
                    System.out.println("Invalid Option. Please Try Again.");
            }
        } while(again);
        cin.close();
    }
}

Anshuman
  • 758
  • 7
  • 23
0

Mixing the nextInt/nextDouble/... with the nextLine from scanner is always a bad idea. The nextInt/nextDouble/... leave an newline character back on the Stream, which means when nextLine is called, it only picks up the newline-character, which is probably not what you want

krankkk
  • 124
  • 1
  • 5
0

Aside from what I commented, you can try reading each input on a separated line, using Integer.parseInt(read.nextLine()) for integer and Double.parseDouble() for double.

read.nextLine() for regular string input

Long Doan
  • 91
  • 8
  • what you are suggesting involves arguments ...i am taking user inputs – Samarth Shukla Jul 05 '21 at 08:19
  • Not quite, you can still create new Mgrs object the way you are doing, the only thing I suggested is to separate each input onto a new line and encase them in the parse function. It may look tidier but I think it's better practise to get input into a variable and then pass the variables into the object constructor – Long Doan Jul 05 '21 at 08:29
0

Actual and Formal argument lists differ in length constructor java. Better to use nextLine() first and then the rest.

Example :-

  class Codechef
{
 public static void main (String[] args) throws java.lang.Exception
 {
    // your code goes here
      Scanner sc = new Scanner(System.in);
    Mgr obj = new Mgr(sc.nextLine(), sc.nextDouble(), sc.nextInt());

  }
}

class Mgr{
 public Mgr(String name, Double dNo, int id ){
    System.out.println(id +" and "+ name +"dNo "+ dNo);
 }
}