首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据控制台交互示例处理7名员工的验证信息

根据控制台交互示例处理7名员工的验证信息
EN

Stack Overflow用户
提问于 2015-02-27 03:16:36
回答 1查看 265关注 0票数 0

所以,在我为Java编写的大学编程课程中,我们需要创建一个程序,它首先、最后、正常时间、病假时间和假设的7名员工的小时工资处理。我几乎肯定,我的教授正试图强调更多面向对象的编程和枚举数据类型在这个项目中的使用。幸运的是,我们仁慈的主人提供了一个控制台交互示例,如下所示:

代码语言:javascript
复制
Employee #1:
Enter the employee’s first name: XXXXXXX
Enter the employee’s last name: XXXXXXX
Enter the number of hours worked: XX.XX
Enter the number of vacation/sick hours: XX.XX
Enter the hourly wage: XX.XX
Employee #2:
Enter the employee’s first name: XXXXXXX
Enter the employee’s last name:
Invalid! The employee’s name must be at least on character long.
Enter the employee’s last name: XXXXXXX
Enter the number of hours worked: -10.00
Invalid! The hours worked must be between 0 and 80 hours.
Enter the number of hours worked: XX.XX
Enter the number of vacation/sick hours: XX.XX
Enter the hourly wage: XX.XX
…
Name Hours V/S Gross Pay Tax Take-Home
XXXXXXX, XXXXXXX XX.XX XX.XX XXXX.XX XXXX.XX XXXX.XX
XXXXXXX, XXXXXXX XX.XX XX.XX XXXX.XX XXXX.XX XXXX.XX
…
Total XXXX.XX XXXX.XX XXXXXX.XX XXXXXX.XX XXXXXX.XX

我已经完成了每个验证方法,我们只需要使用两个类,驱动程序和雇员与任何静态的方法,我们认为我们应该使用关心效率。我的类当前如下:驱动程序类

代码语言:javascript
复制
package project2;

import java.util.Scanner;

/**
 * This program calculates an employee's weekly
 * take-home wages based on the number of hours
 * and the hourly wage.
 * @author ***********
 *
 */
public class Driver 
{

    // Employee's First Name
    static String firstName;

    //Employee's Last Name
    static String lastName;

    //The amount of regular hours worked
    static int regHours;

    //The amount of vacation/sick hours
    static int vsHours;

    //The hourly wage of pay
    static float wage;

    //The deducted tax
    static String taxDeduction;

    //Total pay to take home
    static String takeHome;

    //Number of employees (assumed)
    static final int iters = 7;

    /**
     * Manages interaction with user
     * throughout program.
     * @param args
     */
    public static void main(String[] args) 
    {

        Scanner keyboard = new Scanner ( System.in );


    }

    /**
     * Checks to see if there is an
     * alphabetical character in
     * an employee's name.
     */
    public static boolean validName ( String name )
    {
        boolean hasOneAlpha = name.matches(".[a-zA-Z]+.*");
        return hasOneAlpha;
    }

    /**
     * Validates the regular hours of an
     * employee to not be greater than 80
     * hours.
     */
    public static boolean validHours ( int hours )
    {
        if ( hours > 80 )
            return false;
        else
            return true;
    }

    /**
     * Validates the sick or vacation hours
     * of an employee to not be greater than
     * 40 hours.
     */
    public static boolean validSick ( int vsHours )
    {
        if ( vsHours > 40 )
            return false;
        else
            return true;
    }

    /**
     * Validates the wage of an employee
     * to ensure the wage is less than $100/hr
     * and not less than or equal to 0.
     */
    public static boolean validWage ( float wage )
    {
        if ( wage > 100.00 || wage <= 0.0 )
            return false;
        else
            return true;
    }
}   

我的雇员班:

代码语言:javascript
复制
package project2;

public class Employee 
{

    String firstName;
    String lastName;
    int regHours;
    int vsHours;
    float wage;
    static float earnings;
    float tax;


    public Employee(String firstName, String lastName, int regHours,
            int vsHours, float wage)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.regHours = regHours;
        this.vsHours = vsHours;
        this.wage = wage;
    }

    /**
     * Establishes tax rate based on
     * earnings.
     * @return 
     */
    public float taxDeduction()
    {
        earnings = totalPay();
        if ( earnings > 900 )
            tax = (float) 0.15;
        else if ( earnings >= 300 )
            tax = (float) 0.13;
        else
            tax = (float) 0.11;
        return ( earnings * tax );
    }

    /**
     * Calculates the net pay after
     * a tax deduction.
     */
    public float netPay()
    {
        earnings = totalPay();
        float taxDeduction = taxDeduction();

        return ( earnings - taxDeduction );
    }

    /**
     * Calculates the total amount paid
     * to the employee.
     */
    public float totalPay()
    {
        int totalHours = ( regHours + vsHours );
        if (( vsHours <= 20 ) && ( totalHours > 40 ))
            if ( totalHours - 40 >= vsHours )
                earnings = (float) (( 40 * wage) + (( totalHours - 40 - vsHours ) * 1.5 * wage) + (( vsHours ) * 1.25 * wage ));
            else
                earnings = (float) (( 40 * wage ) + (( totalHours - 40 ) * 1.25 * wage ));
        else if ( regHours > 40 )
            earnings = (float) ( totalHours * wage + ( regHours - 40 ) * .5 * wage );
        else
            earnings = totalHours * wage;

        return earnings;
    }

}

我猜我真正想问的是,我如何遍历员工,分别处理每个员工的信息,并专门为总计输出一个一行的Employee对象。批评走了,还在学习。我将有更多的休息时间来更经常地学习,但不幸的是,那是在我的预产期之后

我衷心感谢任何人能够阅读这一切,任何帮助都是非常感谢的!分享知识毕竟是生活的目的。<3CS

EN

回答 1

Stack Overflow用户

发布于 2015-02-27 03:35:43

下面的sudo可以在您的主要方法中进行一些修改。

代码语言:javascript
复制
for(int i=0;i<iters;i++)
   fname <- read
   while(validName(fname) != true)
     fname <- read
   lname <- read
   while(validName(lname) != true)   
      lname <- read
   regHours <- read
   while(validHours(regHours) != true)
      regHours <- read
  // like that write for the remaining properties
  Employee emp = new Employee(firstName,lastName,regHours,vsHours,wage)
  System.out.println("Name Hours V/S Gross Pay Tax Take-Home"); 
 // get what ever you want according to your logic like emp.taxDeduction()
  System.out.prinltln("Total ")
  System.out.println(emp.totalPay())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28756950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档