简体   繁体   中英

Using Linked List as the output?

I'm trying to write a bank account that asks the user if they want to see records, remove records, and other stuff. This is what I'm trying to produce

My program is nearly done the only problem I'm having is getting the output. I'm getting some weird error when i tried producing the output. When the output works. I'll put it inside a linked list.

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");

        }
      }

    }
}

My second class:

 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);
}

}

java.util.ArrayList won't have properties or methods such as customerID , accountType , currencyType and balance .

I guess your methods display() and balance() shoudn't take any arguments and should use the properties of the instance to print.

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);
}

Then, these lines

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

should be

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

There seems many more things to be corrected -- for example, usage of withdraw() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM