Skip to content

Instantly share code, notes, and snippets.

@dan-dm
Last active May 18, 2020 23:54
Show Gist options
  • Save dan-dm/62f5100326bcc34ee5655a972f49df8e to your computer and use it in GitHub Desktop.
Save dan-dm/62f5100326bcc34ee5655a972f49df8e to your computer and use it in GitHub Desktop.
Created with Copy to Gist
src/main/java/Item.java
import java.util.Objects;
public class Item {
private String identifier;
private String name;
public Item(String identifier, String name) {
this.identifier = identifier;
this.name = name;
}
public String getIdentifier() {
return identifier;
}
public String getName() {
return name;
}
@Override
public String toString() {
return this.identifier + ": " + this.name;
}
// This method was automatically created with the "insert code" function of Netbeans
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Item other = (Item) obj;
if (!Objects.equals(this.identifier, other.identifier)) {
return false;
}
return true;
}
}
src/main/java/Main.java
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Item> items = new ArrayList<>();
while (true) {
System.out.println("Identifier? (empty will stop)");
String identifier = scanner.nextLine();
if (identifier.isEmpty()) {
break;
}
System.out.println("Name? (empty will stop)");
String name = scanner.nextLine();
if (name.isEmpty()) {
break;
}
Item item = new Item(identifier, name);
if (!items.contains(item)) {
items.add(item);
}
}
System.out.println("==Items==");
for (Item item : items) {
System.out.println(item);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment