my program runs but it gives me no output

boo :

I have to make a program using inheritance and I want to print out what I have inputted. But when I run the program, no error founded, it just gives me an empty blank space. What is wrong with my code? Did I make a mistake in calling the variable?

This is my main code:

package labweek7;

import java.awt.Menu;
import java.util.Scanner;
import java.util.Vector;

public class Main {

    Vector<String> menu =new Vector<>();
    Scanner scan = new Scanner(System.in);

    //Employee emp; 
    Employee emp = new EmployeeFullTime(null, 0, null, null); 
    Employee emps = new EmployeePartTime(null, 0, null, null);
    EmployeeFullTime ft = new EmployeeFullTime(null, 0, null, null);
    EmployeePartTime pt = new EmployeePartTime(null, 0, null, null); 

    public Main() {
        int choice = 0;
        int pay;
        int time;
        int salary;


        do{
            System.out.println("ABC EMPLOYEE DATA");
            System.out.println("=================");
            System.out.println("1. Add Employee");
            System.out.println("2. View Employee");
            System.out.println("3. Resign");
            System.out.println("4. Exit");
            System.out.print("Choice: ");
            choice = scan.nextInt();
            scan.nextLine();

            switch(choice){
            case 1:
                String name = "";
                do{
                    System.out.print("Input employee name[must be more than 3 characters]: ");
                    name = scan.next();
                }while(! (name.length()>=3));
                emp.empName.add(name);

                int age;
                do{
                    System.out.print("Input employee age[>=17]: ");
                    age = scan.nextInt();
                }while(!(age>=17));
                emp.empAge.add(age);

                String role = "";
                do{
                    System.out.print("Input employee role[Assistant | Programmer](Case Sensitive): ");
                    role = scan.next();
                }while(!(role.equals("Assistant") || (role.equals("Programmer"))));
                emp.empRole.add(role);

                String type = "";
                do{
                    System.out.print("Input employee type[PartTime | FullTime](Case Sensitive): ");
                    type = scan.next();
                }while(!(type.equals("PartTime")|| type.equals("FullTime")));
                emp.empType.add(type);

                if(type.equals("PartTime")){
                    emp = new EmployeePartTime(name, age, role, type);
                    do{
                        System.out.print("Input pay per hour[>=10000]: ");
                        pay = scan.nextInt();
                    }while(!(pay>=10000));
                    pt.pays.add(pay);

                    do{
                        System.out.print("Input working hour per week[>0]: ");
                        time = scan.nextInt();
                    }while(!(time>0));
                    pt.hours.add(time);

                }
                else{
                    emps = new EmployeeFullTime(name, age, role, type);
                    do{
                        System.out.print("Input base salary[>=10000]: ");
                        salary = scan.nextInt();
                    }while(!(salary>=10000));
                    ft.salary.add(salary);
                }

                String status = "active";

                System.out.println("Success insert employee data");
                System.out.println("");
                System.out.println("Please any key to continue...");
                scan.nextLine();
                break;

            case 2:
                boolean ans = emp.empName.isEmpty();
                if(ans == true){
                    System.out.println("There is no employee data in the list");
                }
                else
                {
                    view(); 
                }


            }
        }while(choice!=4);

    }

    void view(){
        //for(int i=0; i<menu.size(); i++){
            System.out.println("Employee no.");
            if(emp.empType.equals("FullTime")){
                System.out.println("Full Time Employee");
                System.out.println("===================");
                System.out.println("Status: ");
                for(int j=0; j<emps.empName.size(); j++){
//                  if(emps == null){
//                  }
//                  else{
                        System.out.println("Name: " + emps.empName.get(j));
//                  }
                    //System.out.println("Name: " + emps.empName.get(j));
                }
                for(int m=0; m<emp.empAge.size(); m++){
                    System.out.println("Age: " + emps.empAge.get(m));
                }
                for(int n=0; n<emps.empRole.size(); n++){
                    System.out.println("Role: " + emps.empRole.get(n));
                }
                for(int o=0; o<ft.salary.size(); o++){
                    System.out.println("Base salary per month: " + ft.salary.get(o));
                }

                System.out.println("");
            }
            else
            {
                System.out.println("Part Time Employee");
                System.out.println("===================");
//              System.out.println("Status: " );
//              System.out.println("Name: " + emp.empName.get(i));
//              System.out.println("Age: " + emp.empAge.get(i));
//              System.out.println("Role: " + emp.empRole.get(i));
//              System.out.println("Pay per hour: "+pt.pays.get(i));
//              System.out.println("Working hour per week: "+pt.hours.get(i));
//              System.out.println("Salary per month: "+ pt.pays.get(i) * pt.hours.get(i));
//      
            }

        System.out.println("\n\n");
        System.out.println("Press any key to continue...");
    }


    void resign(){

    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main();
    }

}

This is my parent class:

package labweek7;

import java.util.Vector;

public abstract class Employee {

    public String name;
    public int age;
    public String role;
    public String type;

    Vector<String> empName = new Vector<>();
    Vector<Integer> empAge = new Vector<>();
    Vector<String> empRole = new Vector<>();
    Vector<String> empType = new Vector<>();

    public Employee(String name, int age, String role, String type) {
        // TODO Auto-generated constructor stub
        this.name = name;
        this.age = age;
        this.role = role;
        this.type = type;
    }

}

This is my child class 1 :

package labweek7;

import java.util.Vector;

public class EmployeePartTime extends Employee{

    int pay;
    int hour;
    Vector<Integer> pays = new Vector<>();
    Vector<Integer> hours = new Vector<>();

    public EmployeePartTime(String name, int age, String role, String type) {
        super(name, age, role, type);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

This is my child class 2 :

p

ackage labweek7;

import java.util.Vector;

public class EmployeeFullTime extends Employee{

    int gaji; 
    Vector<Integer> salary = new Vector<>(); 

    public EmployeeFullTime(String name, int age, String role, String type) {
        super(name, age, role, type); 
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

Did I make mistake in the for loop? Why does my code not printing the inputted items? Any help is appreciated. Thank You.

Ela Singh :

I have debugged your code in my eclipse work-space.

I'm able to insert the employee data successfully as shown below:

ABC EMPLOYEE DATA
=================
1. Add Employee
2. View Employee
3. Resign
4. Exit
Choice: 1
Input employee name[must be more than 3 characters]: asfds
Input employee age[>=17]: 14
Input employee age[>=17]: 18
Input employee role[Assistant | Programmer](Case Sensitive): Assistant
Input employee type[PartTime | FullTime](Case Sensitive): PartTime
Input pay per hour[>=10000]: 20000
Input working hour per week[>0]: 12
Success insert employee data

But after selecting second option for View Employee I was getting empty output.

ABC EMPLOYEE DATA
=================
1. Add Employee
2. View Employee
3. Resign
4. Exit
Choice: 2
There is no employee data in the list

After debugging it a bit more I came across a condition in Main()

boolean ans = emp.empName.isEmpty();
if(ans == true){
  System.out.println("There is no employee data in the list");
} else {
  view(); 
}

The emp.empName is empty here. All Vectors are empty which belong to Employee class.

Vector<String> empName = new Vector<>();
Vector<Integer> empAge = new Vector<>();
Vector<String> empRole = new Vector<>();
Vector<String> empType = new Vector<>();

As a workaround I just changed emp.empName.isEmpty() to emp.name.isEmpty() to resolve the problem. I hope this helps you in identifying the problem.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=389372&siteId=1