Reputation: 55
I have classic "bank account" assignment question. I've learnt how to add objects to a list, but I don't know how to do it via a user input. I can't find anything that's exactly what I'm looking for.
I want to make a method called enterDetails
that will prompt the user to input a 'name' as a string, and an 'amount' as a double. This then needs to create an object that gets stored in ArrayList<Account>accounts = new ArrayList<>();
The step needs to be repeated until the user inputs q for quit.
This is my code so far, not sure if I'm even heading in the right direction.
class Bank
{
ArrayList<Account>accounts = new ArrayList<>();
public static void enterDetails()
{
int amount = Scanner.nextInt();
for (int i = 0; i < amount ; i++) {
System.out.println("ENTER NAME");
Scanner addName = new Scanner(System.in);
String name = (addName.nextLine());
System.out.println("Enter Current balance");
Scanner addBalance = new Scanner(System.in);
double balance = (addBalance.nextDouble());
}
}
}
Upvotes: 0
Views: 10599
Reputation: 2190
package com.stackOverflow.practice.banking;
public class Account {
private String name;
private double amount;
public Account(String name, double amount) {
this.name = name;
this.amount = amount;
}
public double getAmount() {
return this.amount;
}
public String getName() {
return this.name;
}
}
After creating the Account class in order to create an Account datatypes, create the Bank class:
package com.stackOverflow.practice.banking;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.Collator;
public class App {
public static void main(String... args) throws IOException, MyException{
ArrayList<Account> ac = new ArrayList<Account>();
Scanner scan = new Scanner(System.in);
boolean isRunning = true;
while(isRunning){
System.out.println("Please enter your name in the account(quit to exit program): ");
String name = scan.next();
if(name.equalsIgnoreCase("quit")) {
break;
}
System.out.println("Please enter an amount for your account: ");
double amount = scan.nextDouble();
Account a = new Account(name, amount);
ac.add(a);
}
for(Account t: ac) {
System.out.println("Name: " + t.getName() + "\nAmount: " + t.getAmount());
System.out.println();
}
}
}
Here is the output:
Please enter your name in the account(quit to exit program):
Simeon
Please enter an amount for your account:
200.00
Please enter your name in the account(quit to exit program):
Henry
Please enter an amount for your account:
100.00
Please enter your name in the account(quit to exit program):
Harry
Please enter an amount for your account:
13.99
Please enter your name in the account(quit to exit program):
quit
Name: Simeon
Amount: 200.0
Name: Henry
Amount: 100.0
Name: Harry
Amount: 13.99
Here we simply create the account class with the private fields name and amount. As you noted, the name needs to be a String, and the amount has to be a double, so we set them to those datatypes. We then create a constructor in order to initialize those private fields and give them a value when we create an instance of the class. We add name and amount as the arguments. That is where the user will pass the values to the constructor so that the instance fields are given values. We then create a getName() and getAmount() method in order to test whether or not our Objects are working correctly:
In the main code, we create an Arraylist<> that stores the datatype "Account". We then create a Scanner object that will store user input. isRunning will be the flag that we will use to control the while loop, but in this situation we could simply say while(true) because we use a break statement instead of setting isRunning to false. I just wanted to created a boolean expressing in order to clarify the code, but you can just as easily say while(true). We give the user a prompt to enter their name. We also create a conditional statement that will break out of the loop if they enter quit (or q in your case because you stated that you want the user to enter q for quit). If the user does not enter quit, we store their name in a String variable. We then prompt the user to enter a double for amount. We store that in the variable "amount". Once we have these values, we create a new instance of an Account Object, and we pass the values that the user entered to the constructor. We then use the Arraylist.add(Object) method to store the Objects within the Arraylist.
When we eventually break out of the loop, we run a for-each loop to make sure the Objects were stored to the Arraylist properly. We explicitly call the getName() and getAmount() methods on each Object in the Arraylist. As we can see from the output that we printed, the Objects were stored properly.
As a warning, this is just pseudo code with an explanation about storing Objects to an Arraylist. It is not an exhaustive explanation, and you will have to modify the code to your specific liking. This is a broad and simple example.
Upvotes: 2