Java 创建方法并返回对象
我被这项任务困住了,这是这项任务的最后一部分,这件事让我难以接受。我们从这个代码开始Java 创建方法并返回对象,java,Java,我被这项任务困住了,这是这项任务的最后一部分,这件事让我难以接受。我们从这个代码开始 import java.util.*; import java.io.*; public class TestEmployee2 { public static void main(String[] args) throws IOException { Employee e1 = new Employee2(7, "George Costanza"); e1.setDepartmen
import java.util.*;
import java.io.*;
public class TestEmployee2
{
public static void main(String[] args) throws IOException
{
Employee e1 = new Employee2(7, "George Costanza");
e1.setDepartment("Front Office");
e1.setPosition("Assistant to the Traveling Secretary");
e1.setSalary(50000.0);
e1.setRank(2);
e1.displayEmployee();
//Employee e2 = createEmployeeFromFile();
//e2.displayEmployee();
}
}
我们被告知创建一个名为createEmployeeFromFile()的方法;。在这种方法中,我们将使用扫描仪从.txt文件中读取数据,并使用这些数据创建Employee对象。现在我对两件事感到困惑。首先介绍我应该使用的方法类型,以及如何使用.txt文件中的数据创建对象。这是我们第一次这样做,这对我来说很困难。Employee1工作得很好,但是当我尝试创建我的方法时,我被困在创建它的方式上
这是我现在的代码草稿
import java.util.*;
import java.io.*;
public class TestEmployee2
{
public static void main(String[] args) throws IOException
{
EckEmployee2 e1 = new EckEmployee2(7, "George Costanza");
EckEmployee2 e2 = createEmployeeFromFile();
e1.setDepartment("Front Office");
e1.setPosition("Assistant to the Traveling Secretary");
e1.setSalary(50000.0);
e1.setRank(2);
e2.setNumber();
e2.setName();
e2.setDepartment();
e2.setPosition();
e2.setSalary();
e2.setRank();
e1.displayEmployee();
e2.displayEmployee();
}
createEmployeeFromFile(){
File myFile = new File("employee1.txt");
Scanner kb = new Scanner(myFile);
}
}
我不希望得到答案,只是有人给我指出正确的方向。非常感谢您的帮助
这是我的主类代码
public class EckEmployee2 {
private int rank;
private double number;
private double salary;
private String name;
private String department;
private String position;
public EckEmployee2() {
number = 0;
name = null;
department = null;
position = null;
salary = 0;
rank = 0;
}
public EckEmployee2(double number, String name) {
this.number = number;
this.name = name;
}
public EckEmployee2(double number, String name, String department, String position, double salary, int rank) {
this.number = number;
this.name = name;
this.department = department;
this.position = position;
this.salary = salary;
this.rank = rank;
}
public void setNumber(double num) {
this.number = num;
}
public double getNumber() {
return this.number;
}
public void setName(String nam) {
this.name = nam;
}
public String getName() {
return this.name;
}
public void setDepartment(String dept) {
this.department = dept;
}
public String getDepartment() {
return this.department;
}
public void setPosition(String pos) {
this.position = pos;
}
public String getPosition() {
return this.position;
}
public void setSalary(double sal) {
this.salary = sal;
}
public double getSalary() {
return this.salary;
}
public void setRank(int ran) {
this.rank = ran;
}
public int getRank() {
return this.rank;
}
public boolean checkBonus() {
boolean bonus = false;
if (rank < 5) {
bonus = false;
} else if (rank >= 5)
bonus = true;
return bonus;
}
public void displayEmployee() {
if (checkBonus() == true) {
System.out.println("-------------------------- ");
System.out.println("Name: " + name);
System.out.printf("Employee Number: %09.0f\n" , number, "\n");
System.out.println("Department: \n" + department);
System.out.println("Position: \n" + position);
System.out.printf("Salary: %,.2\n" , salary);
System.out.println("Rank: \n" + rank);
System.out.printf("Bonus: $\n", 1000);
System.out.println("-------------------------- ");
} else if (checkBonus() == false)
System.out.println("--------------------------");
System.out.println("Name: " + name);
System.out.printf("Employee Number: %09.0f\n" , number, "\n");
System.out.println("Department: " + department);
System.out.println("Position: " + position);
System.out.printf("Salary: %,.2f\n" , salary);
System.out.println("Rank: " + rank);
System.out.println("-------------------------- ");
}
}
公共类EckEmployee2{
私有整数秩;
私人双倍号码;
私人双薪;
私有字符串名称;
私人弦乐部;
私有字符串位置;
公共雇员2(){
数字=0;
name=null;
部门=空;
位置=空;
工资=0;
秩=0;
}
public EckEmployee2(双倍数字,字符串名称){
这个数字=数字;
this.name=名称;
}
public EckEmployee2(双倍数字、字符串名称、字符串部门、字符串职位、双倍工资、整数级别){
这个数字=数字;
this.name=名称;
这个部门=部门;
这个位置=位置;
这个。薪水=薪水;
这个.等级=等级;
}
公共无效集合编号(双数值){
this.number=num;
}
公共双getNumber(){
返回此.number;
}
公共void集合名(字符串名称){
this.name=nam;
}
公共字符串getName(){
返回此.name;
}
公共部门(字符串部门){
这个部门=部门;
}
公共部门(){
退回本部门;
}
公共无效设置位置(字符串位置){
这个位置=位置;
}
公共字符串getPosition(){
返回此位置;
}
公共无效设置日历(双sal){
这个。薪水=萨尔;
}
公共双薪制{
退还此工资;
}
公共无效setRank(int-ran){
this.rank=ran;
}
public int getRank(){
返回此.rank;
}
公共布尔校验奖金(){
布尔加值=假;
如果(排名<5){
奖金=假;
}否则如果(排名>=5)
奖金=真;
回报奖金;
}
公众雇员(){
如果(checkBonus()==真){
System.out.println(“-----------------------------------”);
System.out.println(“名称:”+Name);
System.out.printf(“员工编号:%09.0f\n”,编号“\n”);
System.out.println(“部门:\n”+部门);
System.out.println(“位置:\n”+位置);
System.out.printf(“工资:%,.2\n”,工资);
System.out.println(“秩:\n”+秩);
System.out.printf(“奖金:$\n”,1000);
System.out.println(“-----------------------------------”);
}else if(checkBonus()==false)
System.out.println(“-----------------------------------”);
System.out.println(“名称:”+Name);
System.out.printf(“员工编号:%09.0f\n”,编号“\n”);
System.out.println(“部门:”+部门);
系统输出打印项次(“位置:+位置);
System.out.printf(“工资:%,.2f\n”,工资);
System.out.println(“秩:“+秩”);
System.out.println(“-----------------------------------”);
}
}
为了让事情更清楚,这里有一些指示
在TestEmployee2中创建一个名为createEmployeeFromFile()的方法,该方法将从文件中读取数据,并创建、填充和返回Employee对象。它将从中读取的文件名为employee1.txt,这是提供的。硬编码方法中的文件名。此文件包含员工的编号、姓名、部门、职位、工资和职级。创建一个Scanner对象,并使用Scanner类的方法读取文件中的数据,并使用此数据创建Employee对象。最后返回employee对象
在java中,要从一个方法返回一个值,可以将该对象类型添加到方法签名中,如下所示,简而言之,java方法签名如下 'modifier(public、private、protected)'return type(void/nothing、int、long、Object等).'methodName(命名方法)'parameters(作为参数的任何对象或原语)' 如果您有一个解析输入文本的employee contstructor,并且假设数据是分隔符,则可以使用String.split(splitString);其中splitString是分割数据的字符,即逗号“,”
public EckEmployee2 getEmployee()
{
尝试
{
/**
*当您运行文件时,这将打印java工作目录所在的位置
*
*/
System.out.println(System.getProperty(“user.dir”);
/**
*获取文件
*/
File myFile=新文件(“employee1.txt”);
/**
*制造扫描仪
*/
扫描仪kb=新扫描仪(myFile);
/**
*将文件数据存储到其中的列表
*/
列表行=新的ArrayList();
/**
*将文件中的所有行添加到列表“行”
*/
while(kb.hasNext())
{
add(kb.next());
}
/**
*现在您已经有了文件中的数据,假设这里只有一行,您就可以解析数据来生成
*你的“雇员”
*/
如果(lines.size()>0)
{
最终字符串行=行。获取(0);
返回新的EckEmployee2(行);
}
}
/**
*如果未找到您要查找的文件、该文件不存在或您正在搜索,则会引发此错误
*在错误的目录中。
*/
catch(filenotfounde异常)
{
e、 printStackTrace();
}
/**
*如果引发异常或文件为空,则返回null
*/
返回null;
}
首先,请使用您的方法cre
public EckEmployee2 getEmployee()
{
try
{
/**
* This will print where your java working directory is, when you run the file
*
*/
System.out.println(System.getProperty("user.dir"));
/**
* Gets the file
*/
File myFile = new File("employee1.txt");
/**
* Makes the scanner
*/
Scanner kb = new Scanner(myFile);
/**
* A list to store the data of the file into
*/
List<String> lines = new ArrayList<>();
/**
* Adds all the lines in the file to the list "lines"
*/
while (kb.hasNext())
{
lines.add(kb.next());
}
/**
* Now that you have the data from the file, assuming its one line here, you can parse the data to make
* your "Employee"
*/
if (lines.size() > 0)
{
final String line = lines.get(0);
return new EckEmployee2(line);
}
}
/**
* This is thrown if the file you are looking for is not found, it either doesn't exist or you are searching
* in the wrong directory.
*/
catch (FileNotFoundException e)
{
e.printStackTrace();
}
/**
* Return null if an exception is thrown or the file is empty
*/
return null;
}