Solidity Compilations: UnimplementedFeature - solidity

I am getting an Error in the IDE:
UnimplementedFeatureError: Copying of type struct ChatGroups.Message memory[] memory to storage not yet supported.
And I really don't know what to do with everything that I made ( It's commented in French )
There is the full code, and I really tried to find how can I implement what do I want with a different approach if someone could help for that and if I can preserv this code.
Thanks a lot for the people that are going to help !
pragma solidity ^0.6.12;
contract ChatGroups {
struct Group {
string name;
address[] members;
Message[] messages;
}
struct Message {
uint id;
string text;
address author;
bool deleted;
}
Group[] groups;
address[] users;
event GroupCreated(uint groupId, string groupName);
event MessagePublished(uint groupId, uint messageId, string messageText, address messageAuthor);
event MessageDeleted(uint groupId, uint messageId);
event UserAddedToGroup(uint groupId, address user);
event UserRemovedFromGroup(uint groupId, address user);
function createGroup(string memory groupName) public {
require(isUser(msg.sender), "Unauthorized user");
groups.push(Group({
name: groupName,
members: new address[](1),
messages: new Message[](0)
}));
groups[groups.length - 1].members[0] = msg.sender;
emit GroupCreated(groups.length, groupName);
}
function publishMessage(uint groupId, string memory messageText) public {
require(isUser(msg.sender), "Unauthorized user");
Group storage group = groups[groupId - 1];
require(isMember(group, msg.sender), "Unauthorized user");
group.messages.push(Message({
id: group.messages.length + 1,
text: messageText,
author: msg.sender,
deleted: false
}));
emit MessagePublished(groupId, group.messages[group.messages.length - 1].id, messageText, msg.sender);
}
function deleteMessage(uint groupId, uint messageId) public {
require(isUser(msg.sender), "Unauthorized user");
Group storage group = groups[groupId - 1];
require(isMember(group, msg.sender) && group.messages[messageId - 1].author == msg.sender, "Unauthorized user");
group.messages[messageId - 1].deleted = true;
emit MessageDeleted(groupId, messageId);
}
function removeUser(uint groupId, address user) public {
require(isUser(msg.sender), "Unauthorized user");
Group storage group = groups[groupId - 1];
require(isAdmin(group, msg.sender), "Unauthorized user");
require(isMember(group, user), "Unauthorized user");
uint index = getMemberIndex(group, user);
group.members[index] = group.members[group.members.length - 1];
group.members.length--;
emit UserRemovedFromGroup(groupId, user);
}
function addUser(uint groupId, address user) public {
require(isUser(msg.sender), "Unauthorized user");
Group storage group = groups[groupId - 1];
require(isAdmin(group, msg.sender), "Unauthorized user");
require(!isMember(group, user), "User already member of group");
group.members.push(user);
emit UserAddedToGroup(groupId, user);
}
function isUser(address user) private view returns (bool) {
for (uint i = 0; i < users.length; i++) {
if (users[i] == user) {
return true;
}
}
return false;
}
function isMember(Group memory group, address user) public view returns (bool) {
if (group.members[0] == user) {
return true;
}
for (uint i = 1; i < group.members.length; i++) {
if (group.members[i] == user) {
return true;
}
}
return false;
}
function isAdmin(Group memory group, address user) public view returns (bool) {
if (group.members[0] == user) {
return true;
}
for (uint i = 1; i < group.members.length; i++) {
if (group.members[i] == user) {
return true;
}
}
return false;
}
function getMemberIndex(Group storage group, address user) private view returns (uint) {
for (uint i = 0; i < group.members.length; i++) {
if (group.members[i] == user) {
return i;
}
}
return group.members.length;
}
}
While compiling it, I'm getting this error, and I really have no solutions in my mind...

In your smart contract, there are some issues related to your logic in different operation. Thought, the main issue that you shared in this thread refers to a struct inside another struct (in this case, I say about Message[] struct).
The error say that you cannot pass a struct array initializing at runtime because this object is memorized inside storage space.
In details, you have to change the implementation for valorized attribute related to group struct (method: createGroup(string memory groupName)). The change to make is:
Group storage group = groups.push();
group.name = groupName;
group.members = new address[](1);
group.members[0] = msg.sender;
In this way, you create before a space into storage memory array, and then you fill it with the variable (if them has been valorized). At this point, you have to change the method called publishMessage() changing this line:
Group storage group = groups[groupId - 1];
with:
Group storage group = groups[groupId];
This same improvement you have to do in: deleteMessage() and addUser().
In following lines, I put your smart contract with my changes:
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;
pragma experimental ABIEncoderV2;
contract ChatGroups {
struct Group {
string name;
address[] members;
Message[] messages;
}
struct Message {
uint id;
string text;
address author;
bool deleted;
}
Group[] groups;
address[] users;
event GroupCreated(uint groupId, string groupName);
event MessagePublished(uint groupId, uint messageId, string messageText, address messageAuthor);
event MessageDeleted(uint groupId, uint messageId);
event UserAddedToGroup(uint groupId, address user);
event UserRemovedFromGroup(uint groupId, address user);
function createGroup(string memory groupName) public {
require(isUser(msg.sender), "Unauthorized user");
// NOTE: I changed at this point your original implementation
Group storage group = groups.push();
group.name = groupName;
group.members = new address[](1);
group.members[0] = msg.sender;
emit GroupCreated(groups.length, groupName);
}
function publishMessage(uint groupId, string memory messageText) public {
require(isUser(msg.sender), "Unauthorized user");
Group storage group = groups[groupId];
require(isMember(group, msg.sender), "Unauthorized user");
group.messages.push(Message({
id: group.messages.length + 1,
text: messageText,
author: msg.sender,
deleted: false
}));
emit MessagePublished(groupId, group.messages[group.messages.length - 1].id, messageText, msg.sender);
}
function deleteMessage(uint groupId, uint messageId) public {
require(isUser(msg.sender), "Unauthorized user");
Group storage group = groups[groupId];
require(isMember(group, msg.sender) && group.messages[messageId - 1].author == msg.sender, "Unauthorized user");
group.messages[messageId - 1].deleted = true;
emit MessageDeleted(groupId, messageId);
}
function removeUser(uint groupId, address user) public {
require(isUser(msg.sender), "Unauthorized user");
Group storage group = groups[groupId - 1];
require(isAdmin(group, msg.sender), "Unauthorized user");
require(isMember(group, user), "Unauthorized user");
uint index = getMemberIndex(group, user);
group.members[index] = group.members[group.members.length - 1];
// NOTE: The attribute length() is only read-only, you cannot modify or handle the length of array using in this way!
group.members.length;
emit UserRemovedFromGroup(groupId, user);
}
function addUser(uint groupId, address user) public {
require(isUser(msg.sender), "Unauthorized user");
Group storage group = groups[groupId];
require(isAdmin(group, msg.sender), "Unauthorized user");
require(!isMember(group, user), "User already member of group");
group.members.push(user);
emit UserAddedToGroup(groupId, user);
}
function isUser(address user) private view returns (bool) {
for (uint i = 0; i < users.length; i++) {
if (users[i] == user) {
return true;
}
}
return false;
}
function isMember(Group memory group, address user) public view returns (bool) {
if (group.members[0] == user) {
return true;
}
for (uint i = 1; i < group.members.length; i++) {
if (group.members[i] == user) {
return true;
}
}
return false;
}
function isAdmin(Group memory group, address user) public view returns (bool) {
if (group.members[0] == user) {
return true;
}
for (uint i = 1; i < group.members.length; i++) {
if (group.members[i] == user) {
return true;
}
}
return false;
}
function getMemberIndex(Group storage group, address user) private view returns (uint) {
for (uint i = 0; i < group.members.length; i++) {
if (group.members[i] == user) {
return i;
}
}
return group.members.length;
}
}
P.S.: I think that you have to integrate users before call the various method inside users array. Although, the smart contract logic give you this error: "Unauthorized user". I think you should think better about this aspect

Related

VM Exception while processing transaction: reverted with reason string 'message'

I don't understand what's wrong, why I am getting this error on function testing.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "#openzeppelin/contracts/utils/Address.sol";
contract Voting {
address public owner;
uint public counter;
uint public minCandidates = 2;
uint public maxCandidates;
uint public immutable Comission;
struct Candidate {
uint balance;
bool isExistOnThisVoting;
}
struct _Voting {
bool started;
address Winner;
uint StartDate;
uint WinnerBalance;
uint Bank;
uint Period;
mapping(address => Candidate) Candidates;
}
mapping(uint => _Voting) private Votings;
modifier onlyOwner() {
require(msg.sender == owner, "Sorry, but you are not an owner!");
_;
}
constructor(uint _maxCandidates, uint _comission) {
owner = msg.sender;
Comission = _comission;
maxCandidates = _maxCandidates;
}
function addVoting(address[] calldata _candidates, uint _period) public onlyOwner {
require(minCandidates <= _candidates.length && _candidates.length < maxCandidates,
"The number of candidates must comply with the voting rules!"
);
Votings[counter].Period = _period;
for( uint i = 0; i < _candidates.length; i++) {
addCandidate(counter, _candidates[i]);
}
emit votingDraftCreated(counter);
counter++;
}
function editVotingPeriod(uint _id, uint _newPeriod) public onlyOwner {
require(Votings[_id].started = false, "The voting has already begun!");
Votings[_id].Period = _newPeriod;
}
function addCandidate(uint _id, address _candidate) public onlyOwner {
require(address(_candidate) != address(0), "This candidate with zero address!");
require(Address.isContract(_candidate) == false, "A contract can't be a candidate!");
require(Votings[_id].started, "The voting has already begun!");
Votings[_id].Candidates[_candidate].isExistOnThisVoting = true;
emit candidateInfo(_id, _candidate, true);
}
function deleteCandidate(address _candidate, uint _id) public onlyOwner {
require(Votings[_id].started, "The voting has already begun!");
Votings[_id].Candidates[_candidate].isExistOnThisVoting = false;
emit candidateInfo(_id, _candidate, false);
}
function startVoting(uint _id) public onlyOwner {
Votings[_id].started = true;
Votings[_id].StartDate = block.timestamp;
emit votingStarted(_id, block.timestamp);
}
function takePartInVoting(uint _id, address _candidate) public payable {
require(Address.isContract(msg.sender) == false, "A contract can't vote!");
require(Votings[_id].started, "The voting doesn't start!");
require(Votings[_id].StartDate + Votings[_id].Period > block.timestamp, "The voting has ended!");
require(checkCandidate(_id, _candidate), "This candidates does not exist in this voting!");
Votings[_id].Candidates[_candidate].balance += msg.value;
Votings[_id].Bank += msg.value;
if (Votings[_id].Candidates[_candidate].balance > Votings[_id].WinnerBalance) {
Votings[_id].WinnerBalance = Votings[_id].Candidates[_candidate].balance;
Votings[_id].Winner = _candidate;
}
}
function withDrawPrize(uint _id) public {
require(Votings[_id].started, "The voting doesn't start!");
require(Votings[_id].StartDate + Votings[_id].Period < block.timestamp, "The voting is not ended yet!");
require(msg.sender == Votings[_id].Winner, "You are not a winner!");
require(Votings[_id].Bank > 0, "You have already receive your prize!");
uint amount = Votings[_id].Bank;
uint ownerComission = (Comission * amount) / 100;
uint clearAmount = amount - ownerComission;
Votings[_id].Bank = 0;
payable(owner).transfer(ownerComission);
payable(msg.sender).transfer(clearAmount);
}
function checkCandidate(uint _id, address _candidate) public view returns(bool) {
return(Votings[_id].Candidates[_candidate].isExistOnThisVoting);
}
function getVotingInfo(uint256 _id) public view returns (
bool,
uint256,
uint256,
uint256,
uint256,
address
) {
return(
Votings[_id].started,
Votings[_id].StartDate,
Votings[_id].WinnerBalance,
Votings[_id].Bank,
Votings[_id].Period,
Votings[_id].Winner);
}
function setMaxCandidates(uint _maxCandidates) public onlyOwner {
require(minCandidates <= _maxCandidates, "Minimum number of candidates is 2");
maxCandidates = _maxCandidates;
}
event candidateInfo(uint indexed id, address indexed candidate, bool existOnThisVoting);
event votingDraftCreated(uint indexed id);
event votingStarted(uint indexed id, uint startDate);
}
Here is the contract and the test for it:
const { time, loadFixture } = require("#nomicfoundation/hardhat-network-helpers");
const { ethers } = require("hardhat");
const { expect } = require("chai");
describe("Voting", function () {
async function deploy() {
const [owner, user] = await ethers.getSigners();
const Voting = await ethers.getContractFactory("Voting");
const myVoting = await Voting.deploy(5, 10);
return { myVoting, owner, user };
}
it("An owner can change voting's period", async function () {
const { myVoting, owner } = await loadFixture(deploy);
await myVoting.connect(owner).editVotingPeriod(0, 200);
const _votingInfo = await myVoting.getVotingInfo(0);
expect(_votingInfo[2]).to.equal(200);
});
it("Owner can create another voting", async function () {
const { myVoting, owner, user } = await loadFixture(deploy);
const counter_before = await myVoting.counter();
let candidates = new Array();
for (i = 1; i < 4; i++) candidates.push(owner.address);
await myVoting.connect(owner).addVoting(candidates, 100);
const is_candidate = await myVoting.checkCandidate(counter_before, user.address);
expect(is_candidate).to.equal(false);
});
And only two tests are failing with message -> Error: VM Exception while processing transaction: reverted with reason string 'The voting has already begun!'
Could somebody help me to understand why did I get this error?
I don't understand why I got this error.
I found a bug in the solidity code. You're using = instead of == in this line:
require(Votings[_id].started = false, "The voting has already begun!");
I'm not sure this alone will fix your problem, but try again!

TypeError: Explicit type conversion not allowed from "uint256" to "address"

i make simple code, but have error "TypeError: Explicit type conversion not allowed from "uint256" to "address"." Can you help me? I even asked the gpt bot about this error, but it confused me even more)))
in the beginning there was a simple idea - 10k nft, 30 of them immediately after the deposit, the minimum price is indicated and everyone can buy without restriction
`pragma solidity ^0.8.4;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
contract Sptzrawef is ERC721 {
// Define the token name, symbol, and maximum supply
uint256 public totalSupply = 10000;
string public tokenURI;
// Define the owner of the contract
address public owner;
// Define the minimum price of the token
uint256 public minPrice;
// Define the mapping for the token ownership
mapping(address => mapping(uint256 => bool)) public tokenOwnership;
// The constructor function
constructor() ERC721("Sptzrawef", "SP") {
tokenURI = "https://nftstorage.link/ipfs/ba.................q3oq/";
owner = msg.sender;
minPrice = 0.025 ether;
totalSupply = 10000;
// Mint 30 tokens immediately upon deployment
for (uint256 i = 1; i <= 30; i++) {
tokenOwnership[msg.sender][i] = true;
}
}
// Function to mint new tokens
function mint(address _to, uint256 _tokenId) public {
require(msg.sender == owner);
require(_tokenId <= totalSupply);
require(tokenOwnership[_to][_tokenId] == false);
tokenOwnership[_to][_tokenId] = true;
totalSupply++;
}
// Function to buy the token
function buy(uint256 _tokenId) public payable {
require(msg.value >= minPrice);
require(tokenOwnership[msg.sender][_tokenId] == false);
require(_tokenId <= totalSupply);
tokenOwnership[msg.sender][_tokenId] = true;
}
function balanceOf() public view returns (uint256) {
return totalSupply;
}
function tokenOfOwnerByIndex(address _owner, uint256 _index) public view returns (uint256) {
uint256 count = 0;
for (uint256 i = 1; i <= totalSupply; i++) {
if (tokenOwnership[_owner][i]) {
if (count == _index) {
return i;
}
count++;
}
}
return 0;
}
function ownerOf(uint256 _tokenId) public view override returns (address) {
require(_tokenId <= totalSupply);
for (uint256 i = 0; i < totalSupply; i++) {
address wallet = address(i); - error at this line
if (tokenOwnership[wallet][_tokenId]) {
return wallet;
}
}
return address(0);
}
}`
I think you are using ownerOf function in the wrong way.
try to create a new "getWalletAddress" function, for example, and use it in this way:
function getWalletAddress(uint256 _tokenId) public view returns (address) {
require(_tokenId <= totalSupply);
for (uint256 i = 0; i < totalSupply; i++) {
address wallet = ownerOf(i);
if (tokenOwnership[wallet][_tokenId]) {
return wallet;
}
}
return address(0);
}
}
Or if you want to override the "ownerOf" function then you should go for this code:
function ownerOf(uint256 _tokenId) public view overrides returns (address) {
require(_tokenId <= totalSupply);
for (uint256 i = 0; i < totalSupply; i++) {
address wallet = _ownerOf(i);
if (tokenOwnership[wallet][_tokenId]) {
return wallet;
}
}
return address(0);
}
}
I hope this help resolve your issue
address is 20bytes and uint256 is 32bytes.
Therefore, you need to use uint160 for the type of i to convert it to address.
Or please convert i into uint160 before converting it to address
address wallet = address(uint160(i));

Adding a return function to Truffles Pet Shop

I am following the Truffle tutorials, and am on https://www.trufflesuite.com/tutorials/pet-shop. I would like to add a return function that sets the adopters[PetId] in the solidity contract back to the 0 address. I currently have:
function returnPet(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = msg.sender;
return petId;
}
in my Adoption.sol
and in my app.js is:
handleReturn: function(event) {
event.preventDefault();
var petId = parseInt($(event.target).data('id'));
var adoptionInstance;
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error);
}
var account = accounts[0];
App.contracts.Adoption.deployed().then(function(instance) {
adoptionInstance = instance;
return adoptionInstance.returnPet(petId);
}).then(function(result) {
return App.markAdopted();
}).catch(function(err) {
console.log(err.message);
});
});
but it hits the catch and returns: invalid address.
In short:
How do I zero out an address in an array of addresses?emphasized text
Your current code in returnPet() sets the adopters[petId] value to msg.sender, which means "the caller address".
I would like to add a return function that sets the adopters[PetId] in the solidity contract back to the 0 address
You can do it by setting the value to address(0).
So your function will then look like
function returnPet(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = address(0);
return petId;
}

Solidity smart contract for lottery, but I can only give the prize for the first one. I want it to give it to all winners

Can't make it gives the prize for all winners, just the first one person that putted the right combination. I think there is a problem with findWinners function but I can't find it. If I make address A the good combination, then B with a not good one, and finally C with also a good one, it only gives the prize to A.
pragma solidity ^0.5.1;
contract Lottery {
Game[] ListGames;
// uint public nbGames = ListGames.length;
uint price = 100000;
uint[6] winnerGame;
// winnerGame = [1,2,3,4,5,6];
address payable[] ListWinners;
uint winnersPayed = 0;
uint expiredGames = 0;
struct Game {
address payable gamer;
uint[6] numbers;
uint date;
bool valid;
}
function setWinner(uint[6] memory _winnerGame) public {
winnerGame = _winnerGame;
}
function findWinners() public returns (address payable[] memory) {
uint size = ListGames.length;
bool win = true;
for (uint j = 0; j < size; j++) {
for (uint i=0; i<6 ; i++) {
if ((ListGames[j].numbers[i] != winnerGame[i]) || !ListGames[j].valid) {
win = false;
}
}
if (win) {
ListGames[j].valid = false;
expiredGames ++;
ListWinners.push(ListGames[j].gamer);
}
}
return ListWinners;
}
function payer() public returns (uint) {
uint nbWinners = ListWinners.length;
uint prize;
if (nbWinners - winnersPayed != 0) {
prize = address(this).balance / (nbWinners- winnersPayed);
for (uint i = 0; i < nbWinners; i++) {
if(ListWinners[i] != address(0)) {
ListWinners[i].transfer(prize);
delete ListWinners[i];
winnersPayed++;
require(ListWinners[i] == address(0));
}
}
}
return nbWinners;
}
modifier costs(uint amount) {
require(msg.value >= amount);
_;
}
function postGame(uint[6] memory _numbers) public payable costs(price) {
Game memory newGame = Game(msg.sender, _numbers, block. timestamp, true);
ListGames.push(newGame);
}
function numberValidGames() public view returns (uint) {
return ListGames.length - expiredGames;
}
function getWinnersList() public view returns (address payable [] memory) {
return ListWinners;
}
function getTime() public view returns (uint) {
return block.timestamp;
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
}

Java.lang.String errors & not printing to error file

For this project the end result was for there to be 2 error reports sent to an error file at as well as a listing of account summary information printed out. While i can get a majority of the account information printed out such as Balance before transaction and transaction to the account or if there were insufficient funds for the transaction, that's all that will print out as it should be. I'm receiving no errors or exceptions so i'm in all honesty not too sure where the issue at hand may be. I was hoping a second pair of eyes on my code could possibly point out where my issue may be, below is my code of the Account.java, CheckingAccount.java CreditCard.java and lastly D4.java which contains the main method.
Account.java
public class Account {
protected String accountNo, institution, name;
protected double balance;
public Account (String accountNo, String name, String institution, double balance) {
this.name = name;
this.accountNo = accountNo;
this.balance = balance;
this.institution = institution;
}
public String getAccount() {
return accountNo;
}
public boolean debitAccount(double amt) {
return false;
}
public boolean creditAccount(double amt) {
return false;
}
}
CheckingAccount.java
public class CheckingAccount extends Account {
public CheckingAccount(String acctNo, String name, String inst, double balance) {
super(acctNo, name, inst, balance);
this.name = name;
this.accountNo = acctNo;
this.balance = balance;
this.institution = institution;
}
public double getBalance()
{
return balance;
}
public boolean debitAccount(double amt) {
balance += amt;
return false;
}
public boolean creditAccount(double amt) {
balance -= amt;
return false;
}
}
CreditCard.java
public class CreditCard extends Account {
private double creditLimit;
private double availableCredit;
public CreditCard(String acctNo, String name, String inst, double limit, double balance) {
super(acctNo, name, inst, 0);
this.creditLimit = creditLimit;
this.availableCredit = availableCredit;
this.balance = balance;
}
public boolean debitAccount(double amt) {
balance -= amt;
return false;
}
public double getCreditLimit(){
return creditLimit;
}
public double getBalance()
{
return balance;
}
public boolean creditAccount(double amt) {
balance += amt;
return false;
}
}
D4.java
import java.io.*;
import java.util.*;
public class D4 {
public static void main(String[] args) throws FileNotFoundException
{
Boolean valid;
String transactionFile = args[0];
String theaccount, transaction;
File transactions = new File(transactionFile);
Scanner infile = new Scanner(transactions);
File errorFile = new File(args[1]);
PrintWriter error = new PrintWriter(errorFile);
Vector<Account> account = new Vector<Account>();
while(infile.hasNext())
{
transaction = infile.nextLine();
valid = performTrans(account, transaction, error, errorFile);
}
}
private static Account findAccount(Vector<Account> a, String acctNo) {
for(int index = 0; index < a.size(); index ++)
{
if (a.elementAt(index).getAccount().equals(acctNo))
{
return a.elementAt(index);
}
}
return null;
}
private static boolean Checkingacct(Account a)
{
if(a instanceof CheckingAccount)
{
return true;
}
else
{
return false;
}
}
private static boolean Creditcrd(Account a)
{
if(a instanceof CreditCard)
{
return true;
}
else
{
return false;
}
}
private static String errorLog(Vector<Account> a, String transaction)
{
String[] trans = transaction.split(":");
String error;
if(findAccount(a, trans[1])==null)
{
error = ("Invalid account: " + transaction);
System.out.println(error);
return error;
}
else
{
Account acc = findAccount(a, trans[1]);
if( trans[0] == "debit")
{
error = ("Transaction denied: " + transaction);
System.out.println(error);
return error;
}
else
{
return null;
}
}
}
private static boolean performTrans(Vector<Account> account, String transaction, PrintWriter log, File errorFile)
{
String[] pieces = transaction.split(":");
String trans = pieces[0];
System.out.println(pieces);
if(trans.equals("create"))
{
if( pieces[1].equals("checking"))
{
CheckingAccount checking = new CheckingAccount(pieces[2], pieces[3], pieces[4], Double.parseDouble(pieces[5]));
account.add(checking);
return true;
}
else if (pieces[1].equals("credit"))
{
CreditCard creditCard = new CreditCard(pieces[2], pieces[3], pieces[4], 0, Double.parseDouble(pieces[5]));
account.add(creditCard);
return true;
}
else
{
System.out.println("not sure what to put here");
return false;
}
}
else if(trans.equals("debit"))
{
if(findAccount(account, pieces[1]) == null)
{
return false;
}
else
{
Account a = findAccount(account, pieces[1]);
double amount = Double.parseDouble(pieces[2]);
if(Checkingacct(a) == true)
{
CheckingAccount checking = (CheckingAccount) a;
System.out.println("Balance before transaction: " + checking.getBalance());
checking.creditAccount(amount);
System.out.println("Transaction to account: " + amount);
System.out.println("Balance after transaction: " + checking.getBalance() + "\n");
return true;
}
else if(Creditcrd(a) == true)
{
CreditCard creditCard = (CreditCard) a;
System.out.println("Balance before transaction: " + creditCard.getBalance());
System.out.println("Transaction to account: " + amount);
if(amount + creditCard.getBalance() > creditCard.getCreditLimit())
{
System.out.println("Insufficient funds for transaction");
return false;
}
else
{
creditCard.creditAccount(amount);
return true;
}
}
}
}
else if(trans.equals("credit"))
{
if(findAccount(account, pieces[1]) == null)
{
System.out.println("Print Error Message");
return false;
}
else
{
Account a = findAccount(account, pieces[1]);
double amount = Double.parseDouble(pieces[2]);
if(Creditcrd(a) == true)
{
CheckingAccount checking = (CheckingAccount) a;
System.out.println("Balance before transaction: " + checking.getBalance());
checking.debitAccount(amount);
System.out.println("Transaction to account: " + amount);
System.out.println("Balance after transaction: " + checking.getBalance() + "\n");
return true;
}
else if(Creditcrd(a) == true)
{
CreditCard creditCard = (CreditCard) a;
System.out.println(creditCard.getBalance());
return true;
}
}
}
else if(trans.equals("report"))
{
return true;
}
return false;
}
}
The text file im attempting to read from is called D4.txt and the information inside of it is
create:checking:10-3784665:Chase:Joe Holder:2000
create:credit:1234567898765432:First Card:Bob Badger:4000
create:checking:11-3478645:Dime:Melissa Martin:1000
report
debit:10-3784665:523.67
debit:1234567898765432:3500
credit:10-3784665:50
credit:11-3478645:30
debit:10-839723:200
debit:1234567898765432:600
report
The two errors im supposed to be able to print out and see in the errorFile.txt or whatever you choose to call it is and is where the main problem is as this information for some reason is not being processed and printed onto the outputfle.
Invalid account: debit:10839723:200
Transaction denied: debit:1234567898765432:600
The information printed to the console is supposed to look like
Account Summary:
Checking account #103784665
Bank: Chase
Name on account: Joe Holder
Balance: 1526.33
Credit Account #1234567898765432
Bank: First Card
Issued to: Bob Badger
Credit Limit: 4000
Balance: 3500.00
Available credit: 500.00
Checking account #113478645
Bank: Dime
Name on account: Melissa Martin
Balance: 1030.00
End account summary.
But this is also a part of the issue as currently when i run the code whats being printed out to the console is
run D4 d4.txt errorFile.txt
[Ljava.lang.String;#1d540a51
[Ljava.lang.String;#b31c562
[Ljava.lang.String;#3db12bab
[Ljava.lang.String;#4ff0c6b8
[Ljava.lang.String;#4d40d320
Balance before transaction: 2000.0
Transaction to account: 523.67
Balance after transaction: 1476.33
[Ljava.lang.String;#2c9cc42e
Balance before transaction: 4000.0
Transaction to account: 3500.0
Insufficient funds for transaction
[Ljava.lang.String;#2bb07c61
[Ljava.lang.String;#483b594b
[Ljava.lang.String;#31470572
[Ljava.lang.String;#20aedee
Balance before transaction: 4000.0
Transaction to account: 600.0
Insufficient funds for transaction
[Ljava.lang.String;#2ea4aa4d
I know this is a lot of information to sort through and i just want to thank anyone and everyone in advance for your help and hope its something simple that im just overlooking!