Soldato
Some of you may have seen my other thread in which I have explained I'm attempting to learn Java, with a goal of then moving on to Android and being able to write my own apps. In my youth I did a lot of programming, but never really progressed out of Basic.
I have bought a couple of video tutorial courses on Udemy and working my way through them.
I'm making this thread for me to ask for a little help in understanding some things. At the moment I don't think it's falling into place just yet. Perhaps because I keep thinking of classes like procedures, when they are not, they are objects.
Now, doing ArrayLists I can't get my head around the structure of calling the variable from within the ArrayList. When to use 'this.' and when it's 'Customer' for the class or 'customer' for the variable and when I link these with a '.'
I'm sure it sounds silly for seasoned programmers, but maybe someone can phrase it in a way it will click with me?
Here's a bit of code for example -
And this is Customer -
So the line I still look at and think 'I wouldn't have been able to recall that' is -
Can anyone help me get my head round how it works?
I guess it's the whole thing about Branch in itself is a list of branches, how do I link customer lists to each branch? When do I use this. rather than just the ArrayList name. How/why do I keep just using . to link different commands together? I get confused as to when am I getting customer as a 'card of information' and when am I getting a piece of information from that card.
I suppose the weird thing is, actually all the code makes sense as I read it through, I know what each bit is doing, but I struggle to know it well enough to recall how I should use it, unless I look things up. As I said, I'm looking for it to 'click'.
Any help, or I should just keep cracking on and it will come in time? (I know in the other thread I was told to crack on, but I guess I worry I should be learning quicker.)
(Long post, thanks for taking the time to read)
I have bought a couple of video tutorial courses on Udemy and working my way through them.
I'm making this thread for me to ask for a little help in understanding some things. At the moment I don't think it's falling into place just yet. Perhaps because I keep thinking of classes like procedures, when they are not, they are objects.
Now, doing ArrayLists I can't get my head around the structure of calling the variable from within the ArrayList. When to use 'this.' and when it's 'Customer' for the class or 'customer' for the variable and when I link these with a '.'
I'm sure it sounds silly for seasoned programmers, but maybe someone can phrase it in a way it will click with me?
Here's a bit of code for example -
Code:
package com.AndyCr15;
import java.util.ArrayList;
/**
* Created by AndyCr15 on 01/03/2017.
*/
public class Branch {
private String branchName;
private ArrayList<Customer> customers;
public Branch(String branchName, ArrayList<Customer> customers) {
this.branchName = branchName;
this.customers = new ArrayList<Customer>();
}
public String getName() {
return branchName;
}
public boolean newCustomer(String customerName, double intialAmount) {
if (findCustomer(customerName) == null) {
this.customers.add(new Customer(customerName, intialAmount));
return true;
}
return false;
}
public boolean addCustomerTransaction(String customerName, double amount) {
Customer existingCustomer = findCustomer(customerName);
if (existingCustomer != null) {
existingCustomer.addTransaction(amount);
return true;
}
return false;
}
private Customer findCustomer(String customerName) {
for(int i = 0; i<this.customers.size(); i++){
if (this.customers.get(i).getCustomerName().equals(customerName)) {
return this.customers.get(i);
}
}
return null;
}
}
And this is Customer -
Code:
package com.AndyCr15;
import java.util.ArrayList;
/**
* Created by AndyCr15 on 01/03/2017.
*/
public class Customer {
private String customerName;
private ArrayList<Double> transactions;
public String getCustomerName() {
return customerName;
}
public ArrayList<Double> getTransactions() {
return transactions;
}
public Customer(String customerName, double initialAmount) {
this.customerName = customerName;
this.transactions = new ArrayList<Double>();
addTransaction(initialAmount);
}
public void addTransaction(double amount) {
this.transactions.add(amount);
}
}
So the line I still look at and think 'I wouldn't have been able to recall that' is -
Code:
if (this.customers.get(i).getCustomerName().equals(customerName)) {
return this.customers.get(i);
}
Can anyone help me get my head round how it works?
I guess it's the whole thing about Branch in itself is a list of branches, how do I link customer lists to each branch? When do I use this. rather than just the ArrayList name. How/why do I keep just using . to link different commands together? I get confused as to when am I getting customer as a 'card of information' and when am I getting a piece of information from that card.
I suppose the weird thing is, actually all the code makes sense as I read it through, I know what each bit is doing, but I struggle to know it well enough to recall how I should use it, unless I look things up. As I said, I'm looking for it to 'click'.
Any help, or I should just keep cracking on and it will come in time? (I know in the other thread I was told to crack on, but I guess I worry I should be learning quicker.)
(Long post, thanks for taking the time to read)