216 lines
21 KiB
HTML
216 lines
21 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>MP4_CalebFontenot.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #000000; background-color: #ffffff; font-family: monospace}
|
|
pre {color: #000000; background-color: #ffffff; font-family: monospace}
|
|
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
|
|
.ST0 {color: #969696; font-family: monospace; font-weight: bold}
|
|
.ST1 {font-family: monospace; font-weight: bold}
|
|
.comment {color: #969696}
|
|
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST4 {font-family: monospace; font-style: italic}
|
|
.ST2 {color: #ce54b8; font-family: monospace; font-style: italic}
|
|
.string {color: #1e9347}
|
|
.literal {color: #336bdd}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * To change this license header, choose License Headers in Project Properties.</span>
|
|
<span class="comment"> * To change this template file, choose Tools | Templates</span>
|
|
<span class="comment"> * and open the template in the editor.</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> mp4_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.util.Scanner;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> <span class="ST1">MP4_CalebFontenot</span> {
|
|
|
|
<span class="literal">static</span> <span class="literal">boolean</span> <span class="ST2">debug</span> = <span class="literal">false</span>;
|
|
<span class="comment">// Yes, I know the debugger exists. This is just a bit easier imho</span>
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> * </span><span class="ST0">If</span> <span class="ST0">the</span> <span class="ST0">result</span> <span class="ST0">from</span> <span class="ST0">Step</span><span class="ST0"> 4 </span><span class="ST0">is</span> <span class="ST0">divisible</span> <span class="ST0">by</span><span class="ST0"> 10, </span><span class="ST0">the</span> <span class="ST0">card</span> <span class="ST0">number</span> <span class="ST0">is</span> <span class="ST0">valid</span><span class="ST0">; </span><span class="ST0">otherwise</span><span class="ST0">, </span><span class="ST0">it</span> <span class="ST0">is</span> <span class="ST0">invalid</span><span class="ST0">.</span> <span class="comment">For</span> <span class="comment">example</span><span class="comment">, </span><span class="comment">the</span> <span class="comment">number</span><span class="comment"> 4388576018402626 </span><span class="comment">is</span> <span class="comment">invalid</span><span class="comment">, </span><span class="comment">but</span> <span class="comment">the</span> <span class="comment">number</span><span class="comment"> 4388576018410707 </span><span class="comment">is</span> <span class="comment">valid</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST0">@param</span> CreditCard
|
|
<span class="comment"> * </span><span class="ST0">@return</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST3">isValid</span>(String CreditCard) {
|
|
<span class="literal">int</span> sumResult = MP4_CalebFontenot.<span class="ST4">sum</span>(MP4_CalebFontenot.<span class="ST4">doubleDigitsAndSumSingleDigits</span>(CreditCard), MP4_CalebFontenot.<span class="ST4">addOddNumbersFromRightToLeft</span>(CreditCard));
|
|
<span class="literal">int</span> divisionResult = sumResult % 10;
|
|
<span class="literal">if</span> (<span class="ST2">debug</span>) {
|
|
System.<span class="ST2">out</span>.println(sumResult + <span class="string">"</span><span class="string">, </span><span class="string">"</span> + divisionResult);
|
|
}
|
|
<span class="literal">if</span> (divisionResult == 0) {
|
|
<span class="literal">return</span> <span class="literal">true</span>;
|
|
} <span class="literal">else</span> {
|
|
<span class="literal">return</span> <span class="literal">false</span>;
|
|
}
|
|
}
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> * </span><span class="ST0">Sum</span> <span class="ST0">the</span> <span class="ST0">results</span> <span class="ST0">from</span> <span class="ST0">Steps</span><span class="ST0"> 1, 2 </span><span class="ST0">and</span> <span class="ST0">Step</span><span class="ST0"> 3</span><span class="ST0">.</span><span class="comment"> 37 + 38 = 75</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST0">@param</span> step1andSep2 <span class="comment">result</span> <span class="comment">form</span> <span class="comment">step</span><span class="comment"> 1 </span><span class="comment">and</span> <span class="comment">step</span><span class="comment"> 2</span>
|
|
<span class="comment"> * </span><span class="ST0">@param</span> step3 <span class="comment">result</span> <span class="comment">form</span> <span class="comment">step</span><span class="comment"> 3</span>
|
|
<span class="comment"> * </span><span class="ST0">@return</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">sum</span>(<span class="literal">int</span> step1andStep2, <span class="literal">int</span> step3) {
|
|
<span class="literal">return</span> step1andStep2 + step3;
|
|
}
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> * </span><span class="ST0">Add</span> <span class="ST0">all</span> <span class="ST0">digits</span> <span class="ST0">in</span> <span class="ST0">the</span> <span class="ST0">odd</span> <span class="ST0">places</span> <span class="ST0">from</span> <span class="ST0">right</span> <span class="ST0">to</span> <span class="ST0">left</span> <span class="ST0">in</span> <span class="ST0">the</span> <span class="ST0">card</span> <span class="ST0">number</span><span class="ST0">.</span><span class="comment"> 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST0">@param</span><span class="comment"> -</span>creditCard <span class="comment">the</span> <span class="comment">credit</span> <span class="comment">card</span> <span class="comment">number</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">processed</span><span class="comment">.</span>
|
|
<span class="comment"> * </span><span class="ST0">@return</span> <span class="comment">the</span> <span class="comment">sum</span> <span class="comment">of</span> <span class="comment">digits</span> <span class="comment">at</span> <span class="comment">odd</span> <span class="comment">position</span> <span class="comment">from</span> <span class="comment">right</span> <span class="comment">to</span> <span class="comment">left</span><span class="comment">.</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">addOddNumbersFromRightToLeft</span>(String creditCard) {
|
|
<span class="literal">if</span> (<span class="ST2">debug</span>) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Now executing addOddNumbersFromRightToLeft()</span><span class="string">"</span>);
|
|
}
|
|
<span class="comment">// Iterate through string</span>
|
|
<span class="literal">int</span> creditCardLength = creditCard.length();
|
|
<span class="literal">int</span> currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
|
|
<span class="comment">//System.out.println(creditCard.length());</span>
|
|
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = (creditCardLength - 1); i >= 0; i--) {
|
|
currentDigit = Character.<span class="ST4">getNumericValue</span>(creditCard.charAt(i)); <span class="comment">// Parses current digit as an integer so we can do math on it</span>
|
|
<span class="literal">if</span> (currentDigit % 2 != 0) { <span class="comment">// Is this number an odd number?</span>
|
|
sum += currentDigit;
|
|
<span class="literal">if</span> (<span class="ST2">debug</span>) {
|
|
System.<span class="ST2">out</span>.println(currentDigit);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">sum: </span><span class="string">"</span> + sum);
|
|
}
|
|
}
|
|
}
|
|
|
|
<span class="literal">return</span> sum;
|
|
}
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> * </span><span class="ST0">Double</span> <span class="ST0">every</span> <span class="ST0">second</span> <span class="ST0">digit</span> <span class="ST0">from</span> <span class="ST0">right</span> <span class="ST0">to</span> <span class="ST0">left</span><span class="ST0">.</span> <span class="comment">If</span> <span class="comment">doubling</span> <span class="comment">of</span> <span class="comment">a</span> <span class="comment">digit</span> <span class="comment">results</span> <span class="comment">in</span> <span class="comment">a</span> <span class="comment">two</span><span class="comment">-</span><span class="comment">digit</span> <span class="comment">number</span><span class="comment">, </span><span class="comment">add</span> <span class="comment">up</span> <span class="comment">the</span> <span class="comment">two</span> <span class="comment">digits</span> <span class="comment">to</span> <span class="comment">get</span> <span class="comment">a</span> <span class="comment">single</span><span class="comment">-</span><span class="comment">digit</span> <span class="comment">number</span><span class="comment">.</span><span class="comment"> 2 * 2 = 4 2 * 2 = 4 4 * 2 = 8 1 * 2 = 2 6 * 2 = 12 (1 + 2 = 3) 5 * 2 = 10 (1 + 0 = 1) 8 * 2 = 16 (1 + 6 = 7) 4 * 2 = 8 </span><span class="comment">Step</span><span class="comment"> 2 </span><span class="comment">Now</span> <span class="comment">add</span> <span class="comment">all</span> <span class="comment">single</span><span class="comment">-</span><span class="comment">digit</span> <span class="comment">numbers</span> <span class="comment">from</span> <span class="comment">Step</span><span class="comment"> 1</span><span class="comment">.</span><span class="comment"> 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST0">@param</span><span class="comment"> -</span>creditCard <span class="comment">the</span> <span class="comment">credit</span> <span class="comment">card</span> <span class="comment">number</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">processed</span><span class="comment">.</span>
|
|
<span class="comment"> * </span><span class="ST0">@return</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">doubleDigitsAndSumSingleDigits</span>(String creditCard) {
|
|
<span class="literal">if</span> (<span class="ST2">debug</span>) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Now executing doubleDigitsAndSumSingleDigits()</span><span class="string">"</span>);
|
|
}
|
|
<span class="comment">// Iterate through string</span>
|
|
<span class="literal">int</span> creditCardLength = creditCard.length();
|
|
<span class="literal">int</span> currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
|
|
<span class="comment">//System.out.println(creditCard.length());</span>
|
|
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = (creditCardLength - 1); i >= 0; i--) {
|
|
<span class="literal">if</span> (i % 2 == 0) {
|
|
currentDigit = Character.<span class="ST4">getNumericValue</span>(creditCard.charAt(i)); <span class="comment">// Parses current digit as an integer so we can do math on it</span>
|
|
<span class="literal">if</span> (<span class="ST2">debug</span>) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">current digit: </span><span class="string">"</span> + currentDigit);
|
|
}
|
|
digitProduct = currentDigit * 2;
|
|
<span class="literal">if</span> (digitProduct > 9) { <span class="comment">// Product is larger than 9. Typecast the integer back into a string so we can seperate the digits and add them together.</span>
|
|
String workingString = Integer.<span class="ST4">toString</span>(digitProduct);
|
|
tensDigit = Character.<span class="ST4">getNumericValue</span>(workingString.charAt(0));
|
|
onesDigit = Character.<span class="ST4">getNumericValue</span>(workingString.charAt(1));
|
|
<span class="literal">if</span> (<span class="ST2">debug</span>) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">tensDigit is </span><span class="string">"</span> + tensDigit);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">onesDigit is </span><span class="string">"</span> + onesDigit);
|
|
}
|
|
digitProduct = (tensDigit + onesDigit);
|
|
}
|
|
sum += digitProduct;
|
|
}
|
|
|
|
}
|
|
<span class="literal">if</span> (<span class="ST2">debug</span>) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">The sum is: </span><span class="string">"</span> + sum);
|
|
}
|
|
<span class="literal">return</span> sum;
|
|
}
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> * </span><span class="ST0">Displays</span> <span class="ST0">Q</span><span class="ST0">/</span><span class="ST0">q</span> <span class="ST0">or</span> <span class="ST0">C</span><span class="ST0">/</span><span class="ST0">c</span><span class="ST0">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">user</span> <span class="comment">enters</span> <span class="comment">C</span><span class="comment">, </span><span class="comment">it</span> <span class="comment">read</span> <span class="comment">the</span> <span class="comment">credit</span> <span class="comment">number</span> <span class="comment">and</span> <span class="comment">returns</span> <span class="comment">it</span> <span class="comment">as</span> <span class="comment">String</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">user</span> <span class="comment">types</span> <span class="comment">Q</span><span class="comment">/</span><span class="comment">q</span> <span class="comment">returns</span><span class="comment"> 0</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST0">@return</span> <span class="comment">a</span> <span class="comment">string</span> <span class="comment">with</span> <span class="comment">credit</span> <span class="comment">card</span> <span class="comment">number</span> <span class="comment">or</span><span class="comment"> 0</span><span class="comment">.</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST3">menu</span>() {
|
|
String userInput = <span class="string">""</span>;
|
|
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>);
|
|
|
|
<span class="literal">while</span> (!userInput.equals(<span class="string">"</span><span class="string">0</span><span class="string">"</span>))<span class="comment">//user has not entered Q/q</span>
|
|
{
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Press Q/q to quit</span><span class="string">"</span>);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Press C/c to enter a credit card</span><span class="string">"</span>);
|
|
userInput = input.next();
|
|
<span class="literal">if</span> (userInput.equalsIgnoreCase(<span class="string">"</span><span class="string">q</span><span class="string">"</span>)) {
|
|
userInput = <span class="string">"</span><span class="string">0</span><span class="string">"</span>;
|
|
}
|
|
<span class="literal">if</span> (userInput.equalsIgnoreCase(<span class="string">"</span><span class="string">c</span><span class="string">"</span>)) {
|
|
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Please enter a credit card number: </span><span class="string">"</span>);
|
|
userInput = input.next();
|
|
<span class="literal">try</span> { <span class="comment">//Test to see if user has given us a string that looks like a credit card number</span>
|
|
<span class="comment">//Does it sucessfully convert to an integer?</span>
|
|
Long.<span class="ST4">parseLong</span>(userInput);
|
|
|
|
} <span class="literal">catch</span>(Exception e) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Invalid input: String doesn't look like a credit card, it failed successfully convert into an integer.</span><span class="string">"</span>);
|
|
<span class="comment">//break;</span>
|
|
}
|
|
<span class="literal">if</span> (userInput.length() != 16) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Invalid input: String doesn't look like a credit card, incorrect length.</span><span class="string">"</span>);
|
|
<span class="literal">if</span> (<span class="ST2">debug</span>) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">length: </span><span class="string">"</span> + userInput.length());
|
|
}
|
|
} <span class="literal">else</span> {
|
|
<span class="literal">break</span>;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
<span class="literal">return</span> userInput;
|
|
}
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST0">@param</span> args
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">main</span>(String[] args) {
|
|
|
|
String sampleCreditCard = <span class="string">"</span><span class="string">4388576018410707</span><span class="string">"</span>;
|
|
String userInput = <span class="ST4">menu</span>();
|
|
<span class="literal">if</span> (userInput == <span class="string">"</span><span class="string">0</span><span class="string">"</span>) { <span class="comment">// Stop program execution if credit card not entered</span>
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Credit card not entered!</span><span class="string">"</span>);
|
|
<span class="comment">//System.exit(0);</span>
|
|
<span class="literal">return</span>;
|
|
}
|
|
|
|
<span class="literal">boolean</span> creditCardValidity = MP4_CalebFontenot.<span class="ST4">isValid</span>(userInput);
|
|
String validityString;
|
|
<span class="literal">if</span> (creditCardValidity) {
|
|
validityString = <span class="string">"</span><span class="string">valid</span><span class="string">"</span>;
|
|
} <span class="literal">else</span> {
|
|
validityString = <span class="string">"</span><span class="string">invalid</span><span class="string">"</span>;
|
|
}
|
|
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">The credit card </span><span class="string">"</span> + userInput + <span class="string">"</span><span class="string"> is </span><span class="string">"</span> + validityString);
|
|
}
|
|
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|