列/行

时间:2017-12-15 22:12:11

标签: java concurrency rows

我试图将输出显示为列而不是我目前所获得的列,而且它们遍布整个地方。我希望它看起来像这样:

帐户"帐户1":

0(1)16 16,

1(0)12 12,

2(2)0 0

我尝试使用\ t作为分隔符和printf,但无法弄明白。 如果代码格式不正确,我很抱歉,我仍然是编码

的新手

我感谢任何帮助或提示

Account.java

import java.util.ArrayList;

public class Account {
private static String name;
private int balance;
int Transactions; 

private static ArrayList<String> array = new ArrayList<String>(); // array list

/**
 * @param args
 */
public static void main(String[] args) {
    // Check to make sure program has been called with correct number of
    // command line arguments
    if (args.length != 3) {
        System.err.println("Error: program should take exactly three command line arguments:");
        System.err.println("\t<No. of card holders> <main acct starting bal.> <backup acct. starting bal.>");
        System.exit(0);
    }
    // And then make sure that those args are all integers
    try {
        int numCards = Integer.parseInt(args[0]);
        Account account = new Account("Account1", Integer.parseInt(args[1]));

        // Your code to create and manage the threads should go here.
        Thread[] threads = new Thread[numCards];
        for (int i = 0; i < numCards; i++) {
            threads[i] = new Thread(new CardHolder(i, account));
            threads[i].start();
        }
        for (int i = 0; i < numCards; i++) {
            threads[i].join();
        }
    } catch (Exception e) {
        System.err.println("All three arguments should be integers");
        System.err.println("\t<No. of card holders> <main acct starting bal.> <backup acct. starting bal.>");
    }
    printStatement();
}

// Create an account - initalisation goes in here
public Account(String name, int bal) {
    Account.name = name;
    this.balance = bal;
}

// Deposit <balance> into the account
public synchronized void deposit(int cardholder, int balance) {
    balance = balance + balance;
    notify();
    array.add(array.size()+"("+cardholder+")"+"\t"+"\t"+balance+"\t"+ balance+"\n");
    cardholder++;
}

// Withdraw <balance> from the account
public synchronized void withdraw(int cardholder, int balance) {

    if (balance < balance) {  //
        try {
            System.err.println("Not enough money");
            wait();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    balance = balance - balance;
    array.add(array.size()+"("+cardholder+")"+"\t"+balance+"\t"+"\t"+ balance+"\n");
    cardholder++;
}


// Print out the statement of transactions
public static void printStatement() { 
    System.out.printf("%s\n", "Account \"" +  name + "\":"); // cant figure out how to arrange it into rows/columns

    System.out.println(array);
 }
}

CardHolder.java

public class CardHolder implements Runnable {
private int id;
private Account account;
final static int numIterations = 20;

public CardHolder(int id, Account account) {
    this.id = id;
    this.account = account;
}

/*
 * run method is what is executed when you start a Thread that
 * is initialised with an instance of this class.
 * You will need to add code to keep track of local balance (cash
 * in hand) and report this when the thread completes.
 */
public void run() {
    for (int i = 0; i < numIterations; i++) {
        // Generate a random amount from 1-10
        int amount = (int)(Math.random()*10)+1;
        // Then with 50/50 chance, either deposit or withdraw it
        if (Math.random() > 0.5) {
            account.withdraw(id, amount); 
        } else {
            account.deposit(id, amount); 
        }
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } 
    }
    System.out.println("THREAD "+ id + " finished"); 

 }
}

1 个答案:

答案 0 :(得分:0)

您的主要问题是您在存款和取款方式中采用了不同的方式 \ t

在存款中你有:

array.add(array.size()+"("+cardholder+")"+"\t"+"\t"+balance+"\t"+ balance+"\n");

退出:

array.add(array.size()+"("+cardholder+")"+"\t"+balance+"\t"+"\t"+ balance+"\n");

请再次检查您想要拥有多少个,并确保在两种方法中使用相同的方式。

总的来说很少。正如@Kevin在上面的评论中所说,你应该在某些地方使用 this.balance 来平衡,因为你会得到错误的结果。

最后,如果你想在新行中打印没有逗号的所有Arraylist,你可以使用(从Java 8开始)

array.forEach(System.out::print);

array是列表的名称。还要确保导入Arrays包以使用这种方式。

import java.util.Arrays;

当然,如果您不喜欢这种方式,您也可以使用简单的for循环:

for (int i=0; i<array.size();i++){
    System.out.print(array.get(i));
} 

然后您将结果作为列并以相同的方式进行格式化。