From 4c0f837ec4f77e63b81c4f36b5bb57d007d43c7e Mon Sep 17 00:00:00 2001 From: Josh Ashton Date: Wed, 3 Apr 2024 13:59:00 -0600 Subject: [PATCH] week 13 completed. --- spring24/week13/ParsePhone.java | 72 ++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/spring24/week13/ParsePhone.java b/spring24/week13/ParsePhone.java index 7a321f2..57005b6 100644 --- a/spring24/week13/ParsePhone.java +++ b/spring24/week13/ParsePhone.java @@ -19,7 +19,77 @@ import java.util.Scanner; */ public class ParsePhone { private static boolean isValidNumber(String number) { - return false; // TODO: Implement this method + char[] chars = number.toCharArray(); + + Queue q = new Queue(); + for(int i = 0; i < chars.length; i++) { + if(Character.isDigit(chars[i])) + q.enqueue(Integer.parseInt(chars[i] + "")); + } + + if(q.getSize() != 10) return false; + else return true; + } + + private static class Queue { + private Node head; + private Node tail; + private int size; + + public Queue() { + head = null; + tail = head; + size = 0; + } + + public void enqueue(int value) { + Node newNode = new Node(value); + if(head == null) { + head = newNode; + tail = head; + size++; + return; + } + + tail.setNext(newNode); + tail = newNode; + size++; + } + + public Node dequeue() { + if(head == null) return null; + + Node tmp = head; + head = head.next; + + size--; + return tmp; + } + + public int getSize() { + return size; + } + + private class Node { + private Node next; + private int value; + + public Node(int value) { + this.value = value; + } + + public boolean hasNext() { + return next != null; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + } } public static void main(String[] args) {