### Instruction:
Write a Java program that simulates a basic blockchain system. The program should be able to create new blocks containing data, validate the integrity of the chain, and detect any tampering attempts. In addition, implement a proof-of-work mechanism to secure the blockchain.


### Response:

Here's a simple Java program that simulates a basic blockchain system:

```java
import java.util.ArrayList;
import java.util.Date;

class Block {
    public String hash;
    public String previousHash;
    private String data;
    private long timeStamp;
    private int nonce;

    public Block(String data, String previousHash) {
        this.data = data;
        this.previousHash = previousHash;
        this.timeStamp = new Date().getTime();
        this.hash = calculateHash();
    }

    public String calculateHash() {
        return StringUtil.applySha256(previousHash + Long.toString(timeStamp) + Integer.toString(nonce) + data);
    }

    public void mineBlock(int difficulty) {
        String target = new String(new char[difficulty]).replace('\0', '0');
        while (!hash.substring(0, difficulty).equals(target)) {
            nonce++;
            hash = calculateHash();
        }
        System.out.println("Block mined: " + hash);
    }
}

class StringUtil {
    public static String applySha256(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(input.getBytes("UTF-8"));
            StringBuffer hexString = new StringBuffer();

            for (int i = 0; i < hash.length; i++) {
                String hex = Integer.toHexString(0xff & hash[i]);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

public class BlockchainDemo {
    public static ArrayList<Block> blockchain = new ArrayList<>();
    public static int difficulty = 5;

    public static void main(String[] args) {
        blockchain.add(new Block("First block", "0"));
        System.out.println("Mining first block...");
        blockchain.get(0).mineBlock(difficulty);

        blockchain.add(new Block("Second block", blockchain.get(blockchain.size() - 1).hash));
        System.out.println("Mining second block...");
        blockchain.get(1).mineBlock(difficulty);

        blockchain.add(new Block("Third block", blockchain.get(blockchain.size() - 1).hash));
        System.out.println("Mining third block...");
        blockchain.get(2).mineBlock(difficulty);

        System.out.println("\nBlockchain is valid: " + isChainValid());

        String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(blockchain);
        System.out.println("\nThe blockchain:");
        System.out.println(blockchainJson);
    }

    public static Boolean isChainValid() {
        for (int i = 1; i < blockchain.size(); i++) {
            Block currentBlock = blockchain.get(i);
            Block previousBlock = blockchain.get(i - 1);

            if (!currentBlock.hash.equals(currentBlock.calculateHash())) {
                System.out.println("Current hashes not equal");
                return false;
            }

            if (!previousBlock.hash.equals(currentBlock.previousHash)) {
                System.out.println("Previous hashes not equal");
                return false;
            }
        }
        return true;
    }
}
```

This program creates a simple blockchain with three blocks. Each block contains data, the hash of the previous block, and its own hash. The `isChainValid()` method checks the integrity of the chain by comparing each block's hash to its calculated hash and ensuring that the previous block's hash matches the current block's previous hash.

The proof-of-work mechanism is implemented through the `mineBlock()` method in the `Block` class. This method requires a certain number of leading zeros in the hash, determined by the difficulty level. The higher the difficulty, the more secure the blockchain but also the longer it takes to mine a block.