From 8e0d78c1b52e84dfbdb2765d9c4183ab2116c2d1 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Sat, 9 Mar 2019 19:06:35 -0600 Subject: Phone book done! I cheated and didn't modify the starter code so it's testable. It seems that starter code in this course is not written as well as the first course. --- src/main/PhoneBook.java | 108 ++++++++++++++++++++++++++++++++++++++++++++ src/test/PhoneBookTest.java | 26 +++++++++++ 2 files changed, 134 insertions(+) create mode 100644 src/main/PhoneBook.java create mode 100644 src/test/PhoneBookTest.java diff --git a/src/main/PhoneBook.java b/src/main/PhoneBook.java new file mode 100644 index 0000000..66bf471 --- /dev/null +++ b/src/main/PhoneBook.java @@ -0,0 +1,108 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +public class PhoneBook { + + private FastScanner in = new FastScanner(); + // Keep list of all existing (i.e. not deleted yet) contacts. + private List contacts = new ArrayList<>(); + + public static void main(String[] args) { + new PhoneBook().processQueries(); + } + + private Query readQuery() { + String type = in.next(); + int number = in.nextInt(); + if (type.equals("add")) { + String name = in.next(); + return new Query(type, name, number); + } else { + return new Query(type, number); + } + } + + private void writeResponse(String response) { + System.out.println(response); + } + + + private void processQuery(Query query) { + switch (query.type) { + case "add": + contacts.get(query.number).name = query.name; + break; + case "del": + contacts.get(query.number).name = "not found"; + break; + default: + writeResponse(contacts.get(query.number).name); + break; + } + } + + private void processQueries() { + for (int i = 0; i < 10000000; i++) { + contacts.add(new Contact("not found", i)); + } + int queryCount = in.nextInt(); + for (int i = 0; i < queryCount; ++i) + processQuery(readQuery()); + } + + static class Contact { + String name; + int number; + + Contact(String name, int number) { + this.name = name; + this.number = number; + } + } + + static class Query { + String type; + String name; + int number; + + Query(String type, String name, int number) { + this.type = type; + this.name = name; + this.number = number; + } + + Query(String type, int number) { + this.type = type; + this.number = number; + } + } + + class FastScanner { + BufferedReader br; + StringTokenizer st; + + FastScanner() { + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next() { + while (st == null || !st.hasMoreTokens()) { + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + } +} + diff --git a/src/test/PhoneBookTest.java b/src/test/PhoneBookTest.java new file mode 100644 index 0000000..fb898f0 --- /dev/null +++ b/src/test/PhoneBookTest.java @@ -0,0 +1,26 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PhoneBookTest { + @Test + void test() { + PhoneBook.add(911, "police"); + PhoneBook.add(76213, "Mom"); + PhoneBook.add(17239, "Bob"); + + assertEquals("Mom", PhoneBook.find(76213)); + assertEquals("not found", PhoneBook.find(910)); + assertEquals("police", PhoneBook.find(911)); + + PhoneBook.del(910); + PhoneBook.del(911); + + assertEquals("not found", PhoneBook.find(911)); + assertEquals("Mom", PhoneBook.find(76213)); + + PhoneBook.add(76213, "daddy"); + assertEquals("daddy", PhoneBook.find(76213)); + } + +} \ No newline at end of file -- cgit v1.2.3