Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /**
  7. * Created by axel on 7/12/15.
  8. */
  9. public class Blackhawk5 {
  10.  
  11. public static List<AddressBookEntry> entries = new ArrayList<>();
  12.  
  13. public static void main(String[] args) throws Exception{
  14. System.out.println("What's their name? Type 'end' to stop, and 'list' to list them.");
  15. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  16.  
  17. String line;
  18. Question question = Question.NAME;
  19.  
  20. String currentName = null;
  21. String currentPhone = null;
  22. String currentAddress = null;
  23.  
  24. while ((line = input.readLine()) != null){
  25. if (line.equalsIgnoreCase("end")){
  26. System.exit(0);
  27. }
  28. else if (line.equalsIgnoreCase("list")){
  29. if (entries.size() == 0){
  30. System.out.println("No entries found! :(");
  31. continue;
  32. }
  33. for (AddressBookEntry entry : entries){
  34. entry.print();
  35. }
  36. }
  37. else{
  38. if (question == Question.NAME) {
  39. currentName = line;
  40. System.out.println("Set name to: "+line);
  41. question = Question.NUMBER; //advance to next question
  42. System.out.println("What's their number?");
  43. continue;
  44. }
  45. else if (question == Question.NUMBER) {
  46. currentPhone = line;
  47. System.out.println("Set phone to: "+line);
  48. question = Question.ADDRESS;
  49. System.out.println("What's their address?");
  50. continue;
  51. }
  52. else if (question == Question.ADDRESS){
  53. currentAddress = line;
  54. System.out.println("Set address to: "+line);
  55. question = Question.NAME; //reset this to beginning
  56. AddressBookEntry entry = new AddressBookEntry(currentName, currentPhone, currentAddress);
  57. entries.add(entry);
  58. System.out.println("Entry added!");
  59. System.out.println("What's their name? Type 'end' to stop, and 'list' to list them.");
  60. }
  61. }
  62. }
  63. }
  64.  
  65. public enum Question{
  66. NAME,
  67. NUMBER,
  68. ADDRESS;
  69. }
  70.  
  71. public static class AddressBookEntry{
  72. String name;
  73. String phone;
  74. String address;
  75.  
  76. public AddressBookEntry(String name, String phone, String address) {
  77. this.name = name;
  78. this.phone = phone;
  79. this.address = address;
  80. }
  81.  
  82. public String getName() {
  83. return name;
  84. }
  85.  
  86. public void setName(String name) {
  87. this.name = name;
  88. }
  89.  
  90. public String getPhone() {
  91. return phone;
  92. }
  93.  
  94. public void setPhone(String phone) {
  95. this.phone = phone;
  96. }
  97.  
  98. public String getAddress() {
  99. return address;
  100. }
  101.  
  102. public void setAddress(String address) {
  103. this.address = address;
  104. }
  105.  
  106. public void print(){
  107. System.out.println("Name: "+name + "|||Address: "+address+"|||Phone: "+phone);
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement