Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by axel on 7/12/15.
- */
- public class Blackhawk5 {
- public static List<AddressBookEntry> entries = new ArrayList<>();
- public static void main(String[] args) throws Exception{
- System.out.println("What's their name? Type 'end' to stop, and 'list' to list them.");
- BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
- String line;
- Question question = Question.NAME;
- String currentName = null;
- String currentPhone = null;
- String currentAddress = null;
- while ((line = input.readLine()) != null){
- if (line.equalsIgnoreCase("end")){
- System.exit(0);
- }
- else if (line.equalsIgnoreCase("list")){
- if (entries.size() == 0){
- System.out.println("No entries found! :(");
- continue;
- }
- for (AddressBookEntry entry : entries){
- entry.print();
- }
- }
- else{
- if (question == Question.NAME) {
- currentName = line;
- System.out.println("Set name to: "+line);
- question = Question.NUMBER; //advance to next question
- System.out.println("What's their number?");
- continue;
- }
- else if (question == Question.NUMBER) {
- currentPhone = line;
- System.out.println("Set phone to: "+line);
- question = Question.ADDRESS;
- System.out.println("What's their address?");
- continue;
- }
- else if (question == Question.ADDRESS){
- currentAddress = line;
- System.out.println("Set address to: "+line);
- question = Question.NAME; //reset this to beginning
- AddressBookEntry entry = new AddressBookEntry(currentName, currentPhone, currentAddress);
- entries.add(entry);
- System.out.println("Entry added!");
- System.out.println("What's their name? Type 'end' to stop, and 'list' to list them.");
- }
- }
- }
- }
- public enum Question{
- NAME,
- NUMBER,
- ADDRESS;
- }
- public static class AddressBookEntry{
- String name;
- String phone;
- String address;
- public AddressBookEntry(String name, String phone, String address) {
- this.name = name;
- this.phone = phone;
- this.address = address;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPhone() {
- return phone;
- }
- public void setPhone(String phone) {
- this.phone = phone;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public void print(){
- System.out.println("Name: "+name + "|||Address: "+address+"|||Phone: "+phone);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement