java - register problem
Posted
by
Jake
on Stack Overflow
See other posts from Stack Overflow
or by Jake
Published on 2010-12-28T18:49:29Z
Indexed on
2010/12/28
18:54 UTC
Read the original article
Hit count: 494
Hi!
When i try to register a person with the name Eric for example, and then again registrating Eric it works. This should not happen with the code i have. Eric should not be registrated if theres already an Eric in the list. Here is my full code:
import java.util.*;
import se.lth.cs.pt.io.*;
class Person {
private String name;
private String nbr;
public Person (String name, String nbr) {
this.name = name;
this.nbr = nbr;
}
public String getName() {
return name;
}
public String getNumber() {
return nbr;
}
public String toString() {
return name + " : " + nbr;
}
}
class Register {
private List<Person> personer;
public Register() {
personer = new ArrayList<Person>();
}
// boolean remove(String name) {
// }
private Person findName(String name) {
for (Person person : personer) {
if (person.getName() == name) {
return person;
}
}
return null;
}
private boolean containsName(String name) {
return findName(name) != null;
}
public boolean insert(String name, String nbr) {
if (containsName(name)) {
return false;
}
Person person = new Person(name, nbr);
personer.add(person);
Collections.sort(personer, new A());
return true;
}
//List<Person> findByPartOfName(String partOfName) {
//}
//List<Person> findByNumber(String nbr) {
//}
public List<Person> findAll() {
List<Person> copy = new ArrayList<Person>();
for (Person person : personer) {
copy.add(person);
}
return copy;
}
public void printList(List<Person> personer) {
for (Person person : personer) {
System.out.println(person.toString());
}
}
}
class A implements Comparator < Person > {
@Override
public int compare(Person o1, Person o2) {
if(o1.getName() != null && o2.getName() != null){
return o1.getName().compareTo(o2.getName());
}
return 0;
}
}
class TestScript {
public static void main(String[] args) {
new TestScript().run();
}
void test(String msg, boolean status) {
if (status) {
System.out.println(msg + " -- ok");
} else {
System.out.printf("==== FEL: %s ====\n", msg);
}
}
void run() {
Register register = new Register();
System.out.println("Vad vill du göra:");
System.out.println("1. Lägg in ny person.");
System.out.println("2. Tag bort person.");
System.out.println("3. Sök på del av namn.");
System.out.println("4. Se vem som har givet nummer.");
System.out.println("5. Skriv ut alla personer.");
System.out.println("0. Avsluta.");
int cmd = Keyboard.nextInt("Ange kommando (0-5): ");
if (cmd == 0 ) {
} else if (cmd == 1) {
String name = Keyboard.nextLine("Namn: ");
String nbr = Keyboard.nextLine("Nummer: ");
System.out.println("\n");
String inlagd = "OK - " + name + " är nu inlagd.";
String ejinlagd = name + " är redan inlagd.";
test("Skapar nytt konto", register.insert(name, nbr) == true);
System.out.println("\n");
} else if (cmd == 2) {
} else if (cmd == 3) {
} else if (cmd == 4) {
} else if (cmd == 5) {
System.out.println("\n");
register.printList(register.findAll());
System.out.println("\n");
} else {
System.out.println("Inget giltigt kommando!");
System.out.println("\n");
}
}
}
© Stack Overflow or respective owner