使用链接列表作为输出?

时间:2016-03-08 05:06:38

标签: java arrays list linked-list

我试图写一个银行帐户,询问用户是否要查看记录,删除记录和其他内容。 This is what I'm trying to produce

我的程序几乎完成了我遇到的唯一问题就是获得输出。当我尝试生成输出时,我得到了一些奇怪的错误。当输出工作。我将它放在一个链表中。

public class Bank
{

public static void main(String args[]){
    ArrayList<Customer> accounts = new ArrayList<Customer>();

    Customer customer1 = new Customer(1, "Savings", "Dollars", 800);
    Customer customer2 = new Customer(2, "Checking", "Euro", 1900);
    Customer customer3 = new Customer(3, "Checking", "Pound", 8000);

    accounts.add(customer1);
    accounts.add(customer2);
    accounts.add(customer3);


    int customerID=4;
    String choice;
    int deposit;
    int withdraw;
    Scanner in = new Scanner(System.in);
    Customer operation = new Customer();
    boolean flag = true;

    String accountType;
    String currencyType;
    int balance;

    while(flag){
      System.out.println("Select a choice:");
      System.out.println("1. Existing customer");
      System.out.println("2. New customer");
      System.out.println("3. Quit");


      String input = in.next();

        //existing user
        if(input.equals("1")){

          System.out.println("Enter CustomerID: ");
          customerID = in.nextInt();


          System.out.println("Would you like to: ");
          System.out.println("1. Deposit ");
          System.out.println("2. Withdraw ");
          System.out.println("3. Display account info ");
          System.out.println("4. Check balance ");

          choice = in.next();

          if(choice.equals("1")){
            System.out.println("How much would you like to deposit?");
            deposit = in.nextInt();
            operation.deposit(deposit);
          }

          else if (choice.equals("2")){
           System.out.println("How much would you like to withdraw?");
            withdraw = in.nextInt();
            operation.withdraw(withdraw);

          }

          else if (choice.equals("3")){
            operation.display(accounts.get(customerID));
          }

          else if (choice.equals("4"))
            operation.balance(accounts.get(customerID));

          else
            System.out.println("Invalid");
        }

        //new user
         else if(input.equals("2")){
          //add new user to arraylist

           int newID = customerID + 1;

           System.out.println("Enter account type: ");
           accountType = in.next();
           System.out.println("Enter currency type: ");
           currencyType = in.next();
           System.out.println("Enter initial balance: ");
           balance = in.nextInt();

           System.out.println("Your customer ID will be: " + newID);


           accounts.add(new Customer(newID, accountType, currencyType, balance));


        }

        else if(input.equals("3")){

          System.out.println("Thanks for using this bank!");
          flag = false;
        }


        else{

          System.out.println("Invalid");

        }
      }

    }
}

我的第二堂课:

 public class Customer 
{

String accountType, currencyType, info;
public int customerID, balance, amount;
Scanner input = new Scanner(System.in);


public Customer(int customerID, String accountType, String currencyType, int balance)
{
    this.accountType = accountType;
    this.currencyType = currencyType;
    this.customerID = customerID;
    this.balance = balance;
    this.amount = amount;
}

public Customer() {
    // TODO Auto-generated constructor stub
}

public int deposit(int amount){

    amount = input.nextInt();
    if (amount >= 500) {
      System.out.println("Invalid");

    }
    else{
      balance = balance + amount;

    }
    return balance;
}

public int withdraw(int amount){

    if (balance < amount) {
      System.out.println("Invalid amount.");
    }
    else if (amount >= 500) {
      System.out.println("Invalid");
    }
    else {
      balance = balance - amount;

    }
    return balance;
}


public void display(ArrayList<Customer> accounts)
{
    System.out.println("CustomerID:" + accounts.customerID);
    System.out.println("Account Type:" + accounts.accountType);
    System.out.println("Currency Type: " + accounts.currencyType);
    System.out.println("Balance:" + accounts.balance);

}

public void balance(ArrayList<Customer> accounts) {
    System.out.println("Balance:" + accounts.balance);
}

}

1 个答案:

答案 0 :(得分:0)

java.util.ArrayList不具备customerIDaccountTypecurrencyTypebalance等属性或方法。

我猜您的方法display()balance()不会接受任何参数,应该使用实例的属性进行打印。

public void display()
{
    System.out.println("CustomerID:" + customerID);
    System.out.println("Account Type:" + accountType);
    System.out.println("Currency Type: " + currencyType);
    System.out.println("Balance:" + balance);

}

public void balance() {
    System.out.println("Balance:" + balance);
}

然后,这些行

operation.display(accounts.get(customerID));
operation.balance(accounts.get(customerID));

应该是

accounts.get(customerID).display();
accounts.get(customerID).balance();

似乎还有许多事情需要纠正 - 例如,使用withdraw()