我想写一个工资单代码,我必须使用set和get,但是我不知道怎么办? [英] i want to write a payroll code and i must use set and get but i don't know how?

查看:103
本文介绍了我想写一个工资单代码,我必须使用set和get,但是我不知道怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package pay;
public class payment {
private String employeename;
private String payhours;
private String workhours;
private float tax;
private String absenthours;
private String paymonth;
public payment(String e,String p)
{
    setemployeename(e);
        setpayhours(p);
}
    payment() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
public String getemployeename()
{
    return(employeename);
}
public String getpayhours()
{
    return(payhours);
}
public String setemployeement()
{
    return(employeename);
}
public String setpayhour()
{
    return(payhours);
}
public String paymonth()
{
    return (payhours*30);
}
    private void setEmployeename(String e) {
       throw new UnsupportedOperationException("Not yet implemented");
    }
    private void SetPayhours(String p) {
      throw new UnsupportedOperationException("Not yet implemented");
    
    }
}







package pay;
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
 String employeename;
 String payhours;
  Scanner input=new Scanner(System.in);
        System.out.println("Please enter the employeename:");
        employeename=input.nextLine();
        System.out.println("Please enter the payhours in a day:");
        payhours=input.nextLine();
       // paymentclass pay = new  paymentclass(employeename,payhours);
       payment mypayment = new payment();
      // System.out.printf("employeename is %s",employeename);
       System.out.printf("\npaymonth is %s",payhours);

    }

}



payrollcode:写出员工姓名,工时工资,缺勤工资,税款和...



payrollcode:write employeename,hourspay,absentpay,tax and...

推荐答案

在上一个答案中,您的变量类型错误.在类payment中,唯一的字符串应为employeename,因此请定义:
Further to the previous answer, your variable types are wrong. In the class payment, the only string should be employeename, so define:
private String employeename;
private float payhours;
private float workhours;
private float tax;
private float absenthours;
private float paymonth;




我不会给您任何代码,但会给您一些建议.

注释是您的朋友,而JavaDoc注释则加倍.

您的名字使代码难以理解,我建议您遵循行之有效的类名惯例,即以大写字母开头,变量和成员以小写字母开头,所有后续单词以大写字母开头;因此PaymentemployeeName

接下来,方法应由动词引导,访问器与float getPayMonth()与设置器setPayMonth(float)配对.

在构造函数中,应尽量避免调用任何成员方法.

访问成员变量时,将其范围限定为this.payMonth,将类变量的范围限定为Payment.taxRate.

这一切有什么帮助?单独看一下您的构造函数,并遵循上面的内容:




I''m not going to give you any code, but I will give you some advice.

Comments are your friend and JavaDoc comments doubly so.

Your names make the code a little hard to read, may I suggest you follow the well established convention of class names beginning with a capital leter, variables and members with lower case, and all subsequent words with a capital; so Payment, employeeName, etc.

Next up, methods should be verb led, accessors are float getPayMonth() twinned with a setter setPayMonth(float).

In a constructor, you should try to avoid calling any member methods.

When accessing a member variable, scope it as this.payMonth, class variables as Payment.taxRate.

How does all this help? Look at your constructor alone and follow the above:

/**
/  The Payment class manages the payroll for
/  a single empoyee.
*/
public class Payment
{
  // member variables

  /**
  / Constructor for an instance of Payment.
  / Takes employee''s name and hours
  */
  public Payment(String employeeName,
      float payHours)
  {
    this.employeeName = employeeName;
    this.payHours = payHours;
  }

  // other stuff
}



现在您可以看到发生了什么.代码越优雅,因为可读性强,出现问题的机会就越少.听起来愚蠢",但是20多年后,我也发现它是真的.



Now you can see what is happening. The more elegant the code the less chance of a problem because it is readable. Sounds ''stoopid'' but after 20+ years, I''ve also found it''s true.


我猜测是将字符串乘以整数不是什么你想在那里做.您可能想在相乘之前将薪水小时数转换为数值.

这只是一个猜测,因为您还没有解释您实际上想要要执行的代码.可能这是您下次希望在问题中包含的一些信息,请使用帖子正文询问问题和标题以进行总结.
I''m guessing that multiplying a string by an integer is not what you want to do there. You probably want to convert pay hours to a numeric value before multiplying.

This is only a guess since you haven''t explained what you actually want the code to do. That might be some information you wan to to include in your question next time, and please, use the body of the post to ask the question and the title to summarize.


package pay;
public class payment {
    private String employeename;
    private String payhours;
    private String workhours;
    private float tax;
    private String absenthours;
    private String paymonth;
    public payment(String e,String p)
    {
        setEmployeename(e);
        SetPayhours(p);
    }
    payment() {
        //throw new UnsupportedOperationException("Not yet implemented");
        System.out.println("throw");
    }
    public String getemployeename()
    {
        return(employeename);
    }
    public String getpayhours()
    {
        return(payhours);
    }
    public String setemployeement()
    {
        return(employeename);
    }
    public String setpayhour()
    {
        return(payhours);
    }
    public String paymonth()
    {
        return Integer.toString((Integer.parseInt(payhours)*30));
    }
    private void setEmployeename(String e) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    private void SetPayhours(String p) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

package pay;

import java.util.Scanner;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String employeename;
		String payhours;
		Scanner input=new Scanner(System.in);
		System.out.println("Please enter the employeename:");
		employeename=input.nextLine();
		System.out.println("Please enter the payhours in a day:");
		payhours=input.nextLine();
		// paymentclass pay = new  paymentclass(employeename,payhours);
		payment mypayment = new payment();
		// System.out.printf("employeename is %s",employeename);
		System.out.printf("\npaymonth is %s",payhours);
	}

}
}


这篇关于我想写一个工资单代码,我必须使用set和get,但是我不知道怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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