已完成的代码,但如何将它与抽象类一起使用? [英] Completed code, but how to use it with abstract classes?

查看:80
本文介绍了已完成的代码,但如何将它与抽象类一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经完成了我的代码,它将员工信息作为输入,并且可以根据用户的需求进行排序。但是对于部分要求,我有两个使用抽象类Employee,它有两个子类TempEmployee和Perm

employee。为了实现这个目的,我在抽象类下面放了什么代码?此外,当我排序时,我希望所有的信息都与它一起使用,所以如果我按薪水排序,我想要在订购时赚取薪水的人的姓名和部门,而不仅仅是有序的数字列表。这也可以吗?



So I've finished my code which gets employee information as input and it can sort based on what the user wants. But for part of the requirements, I have two use an abstract class "Employee" with two subclasses "TempEmployee" and "Perm
employee". What of my code do I put under the abstract classes for this to work? Also, when I sort I want all the information to go with it, so If I sort by salary, I want the names and department of the people who make that salary when it orders it, not just an ordered list of the numbers. Is this possible as well?

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;


public class Assignment55_000848913
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    String Continue = "y";
    int Count = 0;
    int SortingChoice;
    ArrayList<String> Names       = new ArrayList<String>();
    ArrayList<String> Department  = new ArrayList<String>();
    ArrayList<String> Designation = new ArrayList<String>();
    ArrayList<Float>  Salary      = new ArrayList<Float>();
    
    //******************************************************//
    
    do
    {
       System.out.println("Enter Employee Name: ");
       String x = in.next();
       Names.add(x);
    
       System.out.println("Enter Employee Designation ('temporary or permanent'): ");
       String y = in.next();
       Designation.add(y);
    
       System.out.println("Enter Employee Department: ");
       String z = in.next();
       Department.add(z);
    
       System.out.println("Enter Employee Salary: ");
       float i = in.nextFloat();
       Salary.add(i);
    
       System.out.println("Do you wish to add another employee? ('y'/'n'): ");
       Continue = in.next();
       
       Count = Count + 1;
    }
    while(Continue.equals("y"));
    
//***********************************************************************//
    
    System.out.println("Enter sorting Criterion Number: 1. Name, 2.Department, 3. Salary. ");
    SortingChoice = in.nextInt();
    
    if(SortingChoice == 1)
    {
      Collections.sort(Names);
         for(int i=0; i<Names.size(); i++)
         {
            System.out.println(Names.get(i));
         }
    }
    
    if(SortingChoice == 2)
    {
      Collections.sort(Department);
         for(int i=0; i<Department.size(); i++)
         {
            System.out.println(Department.get(i));
         }
    }
    
    if(SortingChoice == 3)
    {
      for(int k=0; k<Salary.size(); k++)
      {
        if(Designation.get(k).equals("temporary"))
          {
             Salary.set(k, Salary.get(k)*1920);
          }
      }
           
          
      Collections.sort(Salary);
         for(int i=0; i<Salary.size(); i++)
         {
           System.out.print("$");
           System.out.println(Salary.get(i));
         }
    }
  }
}
 
  public abstract class Employee
  {
    
  }
  
  public class TempEmployee extends Employee
  {
    
  }
  
  public class PermEmployee extends Employee
  {
    
  }

推荐答案

);
System.out.println(Salary.get(i));
}
}
}
}

public abstract class 员工
{

}

public class TempEmployee extends 员工
{

}

public class PermEmployee extends 员工
{

}
"); System.out.println(Salary.get(i)); } } } } public abstract class Employee { } public class TempEmployee extends Employee { } public class PermEmployee extends Employee { }


你已经在entrypoint方法(public static main)中编写了所有代码,作为Sergey指向出。这通常是一种非常糟糕的方式,特别是在像Java这样的面向对象编程语言(aka OOP)中。 OOP语言是一种语言,您可以将代码分类为类和函数。

所谓的正确方法是设置如下代码:



You have written all of your code inside the of the entrypoint method (public static main) as Sergey pointed out. This is generally a really bad way of doing it, especially in a object oriented programming language (aka OOP) like Java. An OOP language is a language where you can sort your code into classes and functions.
The so called 'correct' way of doing this, would be to setup the code like this:

class Employee
   
   public String name
   public String department
   public String designation // You could use a boolean here, since you only use two values
   public float  salary

   // The constructor for creating the object, needs to be passed the name, department,  designation & salary
   
   public Employee (String name, String department, String designation , float salary)
      // Using this.name calls the global varible of name insted of the local variable
      this.name        = name
      this.department  = department
      this.designation = designation
      this.salary      = salary
   
   // The different sorting methods
   public void sortingMethod1 ()
   public void sortingMethod2 () 
   public void sortingMethod3 ()





我看不到你将如何为此实现抽象类,因为这将照顾工作。因为我留下了一些大括号和终结符,所以你也可以让你完善代码。现在从主类,分配一,你可以存储一组员工并改变他们的价值观。





I don't see how you would implement abstract classes for this, since this will take care of the job. You'll also have you polish up the code since I have left some braces and terminators. Now from the main class, the assignment one, you can store an array of employees and change their values.

class Assignment55_000848913
   ArrayList<employee> employees;
   Scanner scan;

   public static main (String[] args)
      scan = new Scanner(System.in) // Initialize scanner
      employees = new ArrayList<>() // Initialize array list

      do
      
         sysout("Enter employee data using this format: name,department,designation,salary") // Short for System.out.print

         String data = scan.nextLine();
         String[] tokens = data.split(",");

         employees.add(new Employee(tokens[0], tokens[1], tokens[2], tokens[3]))
         
         // Ask if user would like to keep adding employees...
      while (...)
         
</employee>


这篇关于已完成的代码,但如何将它与抽象类一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆