某公司需求编写一个简易的人事管理系统智能合约程序,便于管理公司员工人事方面的相关事宜,功能如下:
(1)新员工入职:公司(合约拥有者)添加新员工基本信息,包括新员工账户地址、姓名、年龄、性别、身份证号码、电话号码等
(2)修改员工信息:员工可以通过输入账户地址,更新自己的电话号码
(3)查看员工信息:员工通过输入账户地址查看员工信息
(4)员工实时考勤:员工每天上下班都需要打卡记录考勤,考勤信息记录员工的账户地址、上/下班、当前的时间戳
(5)查看员工考勤记录:员工输入账户地址查看考勤记录
(6)员工离职:公司(合约拥有者)清除员工在公司的个人信息记
录
(7)统计公司员工数量:公司(合约拥有者)查看在职员工总数量
二、编程题(共 40 分)
请使用 web3 库或 Truffle 框架实现以上简易人事管理系统的智能合约程序的部署,并调用合约程序完成如下功能:
(1)新员工入职信息
姓名 年龄 性别 身份证 电话号码
罗阳 25 男 511027199705118412 13678241148
王芳 23 女 510070199911024012 13508834124
(2)查看员工罗阳信息
(3)修改罗阳的电话号码为 13579110418
(4)王芳下班考勤签退
(5)罗阳从公司离职
该回答引用ChatGPTsolidity
pragma solidity ^0.8.0;
contract EmployeeManagement {
struct Employee {
string name;
uint age;
string gender;
string id;
string phone;
bool employed;
uint lastCheckIn;
}
address public owner;
mapping(address => Employee) public employees;
uint public totalEmployees;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function.");
_;
}
function addEmployee(string memory _name, uint _age, string memory _gender, string memory _id, string memory _phone) public onlyOwner {
require(!employees[msg.sender].employed, "Employee already exists.");
employees[msg.sender] = Employee(_name, _age, _gender, _id, _phone, true, 0);
totalEmployees++;
}
function updatePhone(string memory _phone) public {
require(employees[msg.sender].employed, "Employee does not exist.");
employees[msg.sender].phone = _phone;
}
function checkIn() public {
require(employees[msg.sender].employed, "Employee does not exist.");
employees[msg.sender].lastCheckIn = block.timestamp;
}
function checkOut() public {
require(employees[msg.sender].employed, "Employee does not exist.");
employees[msg.sender].lastCheckIn = 0;
}
function removeEmployee() public onlyOwner {
require(employees[msg.sender].employed, "Employee does not exist.");
employees[msg.sender].employed = false;
totalEmployees--;
}
}
javascript
const Web3 = require('web3');
const solc = require('solc');
const fs = require('fs');
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
// Compile the contract
const input = fs.readFileSync('EmployeeManagement.sol');
const output = solc.compile(input.toString(), 1);
const bytecode = output.contracts[':EmployeeManagement'].bytecode;
const abi = JSON.parse(output.contracts[':EmployeeManagement'].interface);
// Deploy the contract
const contract = new web3.eth.Contract(abi);
contract.deploy({ data: bytecode })
.send({ from: '0x123...', gas: 1500000, gasPrice: '30000000000000' })
.then((instance) => {
console.log('Contract deployed at address:', instance.options.address);
// Add a new employee
instance.methods.addEmployee('罗阳', 25, '男', '511027199705118412', '13678241148')
.send({ from: '0x123...', gas: 100000 })
.then(() => {
console.log('Employee added.');
// Get employee information
instance.methods.employees('0x123...').call()
.then((employee) => {
console.log('Employee information:', employee);
// Update employee phone number
instance.methods.updatePhone('13579110418')
.send({ from: '0x123...', gas: 100000 })
.then(() => {
console.log('Employee phone number updated.');
// Check out
instance.methods.checkOut()
.send({ from: '0x123...', gas: 100000 })
.then(() => {
console.log('Employee checked out.');
// Remove employee
instance.methods.removeEmployee()
.send({ from: '0x123...', gas: 100000 })
.then(() => {
console.log('Employee removed.');
});
});
});
});
});
});
没有更多的注释更详细的注释
引用GPT
以下是一个简单的智能合约程序,实现了上述人事管理系统的功能,使用的是Solidity语言,可以使用Remix等在线编译器进行编译和部署。
pragma solidity ^0.8.0;
contract EmployeeManagement {
address public owner;
uint public totalEmployees;
struct Employee {
string name;
uint age;
string gender;
string idNumber;
string phoneNumber;
bool isActive;
mapping (uint => Attendance) attendances;
}
struct Attendance {
bool arrived;
bool departed;
uint timestamp;
}
mapping (address => Employee) public employees;
constructor() {
owner = msg.sender;
totalEmployees = 0;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function.");
_;
}
function addEmployee(address _address, string memory _name, uint _age, string memory _gender, string memory _idNumber, string memory _phoneNumber) public onlyOwner {
require(!employees[_address].isActive, "This employee already exists.");
employees[_address] = Employee({
name: _name,
age: _age,
gender: _gender,
idNumber: _idNumber,
phoneNumber: _phoneNumber,
isActive: true
});
totalEmployees++;
}
function updateEmployeePhoneNumber(address _address, string memory _phoneNumber) public {
require(msg.sender == _address, "You can only update your own phone number.");
require(employees[_address].isActive, "This employee does not exist.");
employees[_address].phoneNumber = _phoneNumber;
}
function checkIn() public {
require(employees[msg.sender].isActive, "This employee does not exist.");
employees[msg.sender].attendances[block.timestamp].arrived = true;
employees[msg.sender].attendances[block.timestamp].timestamp = block.timestamp;
}
function checkOut() public {
require(employees[msg.sender].isActive, "This employee does not exist.");
employees[msg.sender].attendances[block.timestamp].departed = true;
employees[msg.sender].attendances[block.timestamp].timestamp = block.timestamp;
}
function removeEmployee(address _address) public onlyOwner {
require(employees[_address].isActive, "This employee does not exist.");
delete employees[_address];
totalEmployees--;
}
function getEmployee(address _address) public view returns (string memory, uint, string memory, string memory, string memory) {
require(employees[_address].isActive, "This employee does not exist.");
return (employees[_address].name, employees[_address].age, employees[_address].gender, employees[_address].idNumber, employees[_address].phoneNumber);
}
function getAttendance(address _address, uint _timestamp) public view returns (bool, bool, uint) {
require(employees[_address].isActive, "This employee does not exist.");
return (employees[_address].attendances[_timestamp].arrived, employees[_address].attendances[_timestamp].departed, employees[_address].attendances[_timestamp].timestamp);
}
}
部署合约之后,可以按照以下步骤调用合约进行测试:
调用addEmployee函数添加新员工信息:
addEmployee("0x1234567890123456789012345678901234567890", "罗阳", 25, "男", "511027199705118412", "13678241148");
addEmployee("0x0987654321098765432109876543210987654321", "王芳", 23, "女", "510070199911024012", "13508834124");
调用getEmployee函数查看罗阳的信息:
getEmployee("0x1234567890123456789012345678901234567890");
调用updateEmployeePhoneNumber函数修改罗阳的电话号码:
updateEmployeePhoneNumber("0x1234567890123456789012345678901234567890", "13579110418");
调用checkOut函数记录王芳的下班考勤签退:
checkOut({from: "0x0987654321098765432109876543210987654321"});
调用removeEmployee函数将罗阳从公司离职:
removeEmployee("0x1234567890123456789012345678901234567890");
调用getEmployee函数或者getAttendance函数查看员工信息或者考勤记录:
getEmployee("