Мне нужно создать программу, используя наследование, и я хочу распечатать введенные данные. Но когда я запускаю программу, она просто дает мне пустое пространство. Что не так с моим кодом? Я сделал ошибку при вызове переменной?
Это мой основной код
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."+ i);
if(emp.empType.equals("FullTime")){
System.out.println("Full Time Employee");
System.out.println("===================");
System.out.println("Status: " );
for(int j=0; j<emp.empName.size(); j++){
System.out.println("Name: " + emps.empName.get(j));
}
System.out.println("Age: " + emps.empAge.get(i));
System.out.println("Role: " + emps.empRole.get(i));
System.out.println("Base salary per month: " + ft.salary.get(i));
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...");
//}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main();
}
}
Это код родительского класса
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;
}
}
Это мой подкласс 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
}
}
Это мой подкласс 2
package 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
}
}
Я сделал ошибку в for l oop? Почему мой код не печатает введенные элементы? Любая помощь приветствуется. Спасибо.