I am trying to create a method in my code that searches through an array list (in my case customerList, which contains Customer objects) and will add something new to it if that something isn't found in the ArrayList...
Here is how I have it all set up....
public class CustomerDatabase {
private ArrayList <Customer> customerList = null;
public CustomerDatabase() {
customerList = new ArrayList<Customer>();
}
and this is the method I'm trying to make. I'm trying to get it so that it will add a Customer with given name "n" to the end of the ArrayList if it isn't found in the ArrayList...
public void addCustomer(String n)
{
for(Customer c:customerList)
if (!customerList.contains(n))
customerList.add(n);
}
I'm aware that something is wrong with the whole .add and then a String thing but I'm not sure where I went wrong. Any input would be great!
You're confusing your Customer class with its name property. You can't check if a list of Custom contains a String because it never will. But you can check if any customers in the list have the property you're looking for. If you don't find any, then you have to construct a new object with that string:
public void addCustomer(String name) {
for (Customer c : customerList) {
if (c.getName().equals(name)) {
// duplicate found
return;
}
}
// no duplicates; add new customer
customerList.add(new Customer(name));
}
This assumes Customer has a constructor Customer(String name) and a method String getName(). Adapt as necessary.
Customer is a class and you made an array list of Customer class type.there is no direct way to compare name(String) with Customer class object.
You should change your code like-
public void addCustomer(String name) {
for (Customer c : customerList) {
if (!c.getName().equals(name)) {
Customer c=new Customer();
c.setName(name);
customerList.add(c);
}
}
}
And in Customer Class
Class Customer{
private String name;
//getter and setter method for name.
}
Related
Image of variable hierarchy (Please Check)
I want this specific variable to be a list of usernames, and each of these users will have some bookings... I tried adding an arraylist within an arraylist but that doesnt allow the user to name the usernames, they have to be predefined, please give me a method to do this.
You can store any kind of object in an ArrayList.
You could define your own class Booking that contains information about a booking:
public class Booking {
// ... whatever information is necessary for a booking
}
And then define a class User that contains information about a user, including a list of bookings:
public class User {
private String name;
private List<Booking> bookings = new ArrayList<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Booking> getBookings() {
return bookings;
}
// ... other methods as necessary
}
And then you can make an ArrayList of User objects, where each User object contains a list of Booking objects:
List<User> users = new ArrayList<>();
Took me a bit to figure this out but Im just wondering if there is a cleaner way to do this
this is the gist of my main
public class Main {
private static Bank Chase = new Bank();
//This is the function in main to add a transaction to a specified customer of a branch
public static void addTransaction() {
System.out.println("Enter the name of the branch");
String branch = scanner.nextLine();
System.out.println("Enter the name of the person");
String name = scanner.nextLine();
System.out.println("Enter the amount you would like to add");
double amount = scanner.nextDouble();
scanner.nextLine();
Chase.getBranchList().get(Chase.branchIndex(branch)).getCustomerList().get(Chase.getBranchList().get(Chase.branchIndex(branch)).customerIndex(name)).addTransaction(amount);
}
}
This last line is really long and confusing to others this is what it does
//gets the branchlist -> gets the specified branch -> gets the customerlist -> finds the specified customer -> adds transaction
these are the other relevant parts of the classes the function references
public class Bank {
private ArrayList<Branch> branchList = new ArrayList<Branch>();
public ArrayList<Branch> getBranchList() {
return branchList;
}
public int branchIndex(String name){
for(Branch branch: branchList){
if(branch.getName().equals(name)){
return branchList.indexOf(branch);
}
}
return -1;
}
}
public class Branch {
private String branchName;
private ArrayList<Customer> customerList;
public ArrayList<Customer> getCustomerList() {
return customerList;
}
public int customerIndex(String name){
for(Customer customer: customerList){
if(customer.getName().equals(name)){
return customerList.indexOf(customer);
}
}
return -1;
}
public class Customer {
private String customerName;
private ArrayList<Double> transactions = new ArrayList<Double>();
public Customer(String customerName, double amount) {
this.customerName = customerName;
this.transactions = new ArrayList<Double>();
transactions.add(amount);
}
public String getName() {
return customerName;
}
public void addTransaction(double transaction){
transactions.add(transaction);
}
}
So is there any more readable way of accessing these elements that are in object ArrayLists? I think the last line in addTransaction() looks a bit redundant.
Rather than one long line you could
a) split the code into multiple lines
Chase.getBranchList().get(Chase.branchIndex(branch))
.getCustomerList()
.get(Chase.getBranchList()
.get(Chase.branchIndex(branch))
.customerIndex(name))
.addTransaction(amount);
b) stored the returned values of each get into a local variable, especially the code that it re-calling the same methods e.g. Chase.branchIndex(branch) and Chase.getBranchList()
At the moment you are assuming unique customer/branch names, and then cycling through your array list to find the customer by name. This assumption is fine, if it's a valid assumption but could mean that there are more optimal solutions. I would recommend a refactor of your code to utilise a java hash map:
https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
Basically, this will mean that you can access the customer/bank directly by name and will simplify your code greatly! It will also have performance benefits.
For your scenario this refactor would look similar to this:
public class Branch
{
private HashMap<String, Customer> _customers;
private String _branchName;
public Branch(String branchName)
{
_branchName = branchName;
_customers = new HashMap<String, Customer>();
}
public Customer getCustomer(String customerName)
{
return _customers.get(customerName);
}
}
If you follow the same for Bank, you should be able to access a Customer and add a transaction as follows:
Chase.getBranch(branch).getCustomer(name).addTransaction(transaction);
Let me know if you need help converting Bank :)
You are on the right track, but you've got some minor design flaws.
Step 1: Add a method called getBranchByName(String branchName) to your Bank class that returns a Branch object and get rid of your branchIndex() method:
public Branch getBranchByName(String branchName) {
return branchList.stream()
.filter(branch -> branch.getBranchName().equals(branchName))
.findAny()
.get();
}
Step 2: Add a method called getCustomerByName(String name) to your Customer class that returns a Customer object and get rid of your customerIndex() method:
public Customer getCustomerByName(String name) {
return customerList.stream()
.filter(customer -> customer.getCustomerName().equals(name))
.findAny()
.get();
}
Step 3: Now, method call in your main() method becomes more compact, simple and easy to read:
Chase.getBranchByName(branchName).getCustomerByName(customerName).addTransaction(amount);
Note: I've used Java 8 streams as you can observe. If you are not allowed to use Java 8 streams, you can just stick with classic imperative style of programming by writing for() loops as you have done earlier. As a quick example, if you want to write getBranchByName(String branchName) in old fashioned Java 7 style, your loop looks like this:
for(Branch branch : branchList) {
if(branch.getBranchName().equals(branchName)){
return branch;
}
}
we have
class Student
{
String name,
int age,
String specialization
}
and
class Students
{
List<String> names,
List<Integer> age,
List<String> specialization
}
Students object is basically a structure that holds field values of Student class,
What is the best way to fill Students object without using reflection.
Edit: we have a specific requirement of having Students class as it is, the reason for this is we don't always want all the information in Student class and if we have List it would allocate memory for the fields that we are not interested in.
Don't create class Students. Hold a list of Student
List<Student> students = new ArrayList<Student>();
And to access a student data you can use
students.get(0).name;
As a side note, you should learn about getters and setters.
I wouldn't recommend creating a class named "Students" for this purpose. Your intention is to create a collection to hold the Student objects.
In this case, do the following:
List<Student> students = new ArrayList();
Also, pay attention to the capitalization: class is a keyword and should be spelled all lower-case.
EDIT After seeing a comment from venkat:
If you really need to create a class called Students then following should work (also similar answer provided above by another SO user):
class Students {
List<Student> students = new ArrayList();
}
This should work, but I would highly recommend not to use these type of class with the plural names!
PS: I am a CS prof teaching programming languages in a university and a long time developer/consultant.
Class Students {
List<Student> students;
}
Maybe you want to use a Decorator-Pattern (I don't think that i saves memory):
Implement a base class with the default field:
public class BaseClass implements INameGettable {
protected String name;
public BaseClass(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Add the default interface:
public interface INameGettable {
String getName();
}
Add a decorator to for an additional field e.g. age:
public class Decorator implements INameGettable {
protected INameGettable nameable;
protected int age;
public Decorator(INameGettable nameable, int age) {
this.nameable = nameable;
this.age = age;
}
public String getName() {
return nameable.getName();
}
public int getAge() {
return this.age;
}
}
Usage:
// First object contains only name
INameable namegettable = new BaseClass("Test1");
namegettable.getName();
// Second object contains name and age
Decorator agegettable = new Decorator(new BaseClass("Test2"), 77);
agegettable.getName();
agegettable.getAge();
Going for the obvious answer here.
class Students
{
List<String> names;
List<Integer> age;
List<String> specialization;
public Student(List<Student> students) {
addStudents(students);
}
private void addStudents(List<Student> students) {
names = students.stream
.map(Student::getName)
.collect(Collectors.toList())
age = students.stream
.map(Student::getAge)
.collect(Collectors.toList())
specialization = students.stream
.map(Student::getSpecialization)
.collect(Collectors.toList())
}
}
I'm having problems getting a method from one class to work if I put the objects into a set.
So I have
public class Employee {
/* instance variables */
private String firstName;
private String employeeNumber;
public Employee(String employNum) {
super();
this.employeeNumber = employNum;
}
...
public String getFirstName() {
return this.firstName;
}
There is lots of other code which I can post if needed but I'm not allowed to change the Employee class.
So for my code I have to create a class for a Set of Employees which I've done with
public class Records {
public Set<Employee> employeeSet = new HashSet<Employee>();
public Records() {
}
}
Now I need a method that will print the details of all employees in the set. Here is my attempt so far
public void printEmployeeNames() {
for (String employee : employeeSet) {
System.out.println(this.employeeSet.getFirstName());
}
}
The problem I'm having is that it won't compile as it says
"incompatible types"
and highlights employeeSet in
for (String employee : employeeSet)
My other problem is that it can't access the method for getFirstName(). I've tried to isolate the method using
public void printEmployeeNames() {
System.out.println(this.employeeSet.getFirstName());
}
This also won't compile as it states
"cannot find symbol - method getFirstName()".
Edit.
Thanks for the help with this problem, I changed it to this and it worked.
public void printEmployees()
{
for (Employee employee: employeeSet)
{
System.out.println(employee.getFirstName());
}
}
this here makes no sense:
for (String employee: employeeSet)
{
System.out.println(this.employeeSet.getFirstName());
}
since the employeeSet is a Set and sets dont have a method called getFirstName
you have to do:
for (Employee employee: employeeSet) //for every EMPLOYEE in the employeeSet
{
System.out.println(employee.getFirstName()); //from that employ get the name
}
AND create in the Employee class the respective Setter and getters
in this case:
private String firstName;
/**
* #return the employeeNumber
*/
public final String getEmployeeNumber() {
return firstName;
}
That should be
for (Employee employee: employeeSet)
{
System.out.println(employee.getFirstName());
}
Set doesn't have a firstname method. Your employee object have have.
First of all, have you heard of encapsulation? The declaration public Set<Employee> employeeSet is an example of a bad practice, and you should use a private field with some sort of getter. The reason your for loop is raising errors is that you made two mistakes:
employeeSet is a List<Employee>, whereas you are asking for a String when iterating over it. This is incorrect - change the type of employee to Employee.
You are trying to access getFirstName() from your field employeeSet. This won't work, as Set has no such method. I believe you meant to call the method on employee.
Also, you may simplify your code to the following one-liner with Java 8 streams:
public void printEmployeeNames() {
employeeSet.stream().map(Employee::getFirstName).forEach(System.out::println);
}
So I'm stuck on some homework trying to create an instance of the Employee class from my class called records.
public class Employee implements Comparable<Employee>
{
/* instance variables */
private String Name;
private String employeeNumber;
/**
* Constructor for Employee
*/
public Employee(String employNum)
{
super();
this.employeeNumber = employNum;
}
Next I need to create a Record class that will create a HashSet of Employees details, this is where I need help.
public class Records
{
private HashSet employeeSet;
public Records()
{
Set<Employee> employeeSet = new HashSet<Employee>();
}
Then I want it to have a method for adding a new employee and then putting their records in the set.
public enrollEmployee(String num)
{
Employee newEmp = new Employee(num);
employeeSet.add(newEmp);
}
}
I can't get that last bit to create a new employee, it doesn't come out with an error and it compiles correctly. Just no new employee.
*Adding the employeeSet.add(newEmp); caused a compiler warning and that method won't run due to a NullPointerException, probably because the employee isn't actually created.
For more info, when I create an employee the name should come out as "null" if only an employee number is entered but I still need to store that information.
Edited to update information. There is more detail for the Employee class which I have left out, I'm only supposed to be creating the Records class.
Last update, thank you for the help. After reading the replies this is what I got to work for me.
public class Records
{
private Set<Employee> employeeSet = new HashSet<Employee>();
public Records()
{
}
public void enrollEmployee(String num)
{
Employee newEmp = new Employee(num);
employeeSet.add(newEmp);
}
}
Heres the new solution based on what you are looking for in the comments
public class Record
{
private Set<Employee> employeeSet = new HashSet<Employee>();
public Record()
{
newEmployee("1");
newEmployee("2");
newEmployee("3");
}
public void newEmployee(String employNumber)
{
Employee newEmp = new Employee(employNumber);
employeeSet.add(newEmp);
}
}
The method that you created was never called on... So an employee was never created. Therefore, by calling on the newEmployee method in the Record Constructor, a new employee is created