Let's suppose I've the following Class Product
:
public class Product {
// Variables.
private String name; // Name
private Double price; // Price
Product() {} // Default constructor with no parameters.
Product(String name, Double price) { // Constructor with parameters.
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price= price;
}
public String toString() { // Overriding "toString()".
return "\nName: " + this.name + "\nPrice: " + this.price;
}
public boolean equals(Object obj) { // Overriding equals()
if(this == obj) {
return true;
}
if(obj == null || obj.getClass() != this.getClass()) {
return false;
}
Product product = (Product) obj;
return this.name.equals(product.name)&& this.price.equals(product.price);
}
}
Now, let's suppose I've an ArrayList
in my Main.class
and my Main
looks something like this:
import java.util.*;
import java.io.*;
public class Main {
private static BufferedReader r = new BufferedReader (new InputStreamReader(System.in));
private static String readln() throws IOException{
return r.readLine();
}
private static long readInput() throws IOException{ // Use this to read input for the menu options.
return Integer.valueOf(readln());
}
public static void menu(){ // Menu
System.out.println("-------------------------" +
"\nAdd new product(1)" +
"\nSearch for product(2)" +
"\nDelete product(3)" +
"\nShow all products(4)" +
"\nReturn the number of products(5)" +
"\nExit(-1)" +
"\n-------------------------");
}
public static void main (String args[]) throws IOException{
// This is the ArrayList for the "Product".
ArrayList<Product> products = new ArrayList<Product>();
int option = 0;
do {
menu();
option = (int)readInput();
switch (option){
case 1:{
System.out.println("Insert product name: ");
String name= readln();
System.out.println("Insert product price: ");
Double price = Double.parseDouble(readln());
products.add(new Product(name, price));
break;
}
case 2:{
System.out.println("Insert product name: ");
String name= readln();
System.out.println("Insert product price: ");
Double price= Double.parseDouble(readln());
if ((products.contains(new Product (name, price)))){
System.out.println("Works!");
}
break;
}
case 3:{
break;
}
case 4:{
break;
}
case 5:{
System.out.println("Number of products: " + products.size());
//This prints with no problems, therefor the objects DO exist in the ArrayList.
break;
}
}
}while((option > 0) && (option < 6));
}
}
According to this in order to insert an object into an ArrayList
you need to write it like this "ArrayListName.add(new ObjectName(param1, param2));
" or you can create an object called object1
and then add it with ArrayListName.add(object1);
In my case, from what I understand, I'm inserting objects into the ArrayList
but those objects do not really exist, because if I tried to use the overridden toString()
method, it does not print anything. If my understanding is wrong, why does it not print anything? According to this, my method is correct.
If I've understood this correctly, objects do not need a variable to point to them, but if you've directly inserted them into an ArrayList
, like I have, how are you supposed to get the index position of an object? Because the equals()
in my case, compares objects, so you can't use it to search the ArrayList
. Nor can you try something like "products.contains(name, price);
" because, .contains()
uses equals()
.
I was also thinking of doing something like this, but it's only useful if you want to create a new Class
and not an object like product1
, in my case. I gave up on it as well because forName()
kept saying that it can't find the Class
for some reason that I could not find out why.
What about the "Delete" option? Would it work the same way as the "Search" one?
Edit: for the equals()
the last line, you can also put:
if( (this.price.equals(product.getPrice())) && (this.name.equals(product.getName())) ) {
return true;
}