Commit 6eb61a92 by Joe Student

Revert "Revert "cleared out code""

This reverts commit 3367387b.
parent 3367387b
......@@ -7,20 +7,7 @@ public class RecursionDriver {
*/
public static void main(String[] args) {
System.out.println(myFactorialRec(5));
System.out.println(myFactorialIter(5));
testIsPalindrome("A man, a plan, a canal, Panama!");
testIsPalindrome("Madam, I'm Adam");
testIsPalindrome("Whatever words you want!");
testIsPalindrome("Marge let a moody baby doom a telegram.");
System.out.println(replaceRec("Java", 'a', 'o'));
System.out.println(replaceIter("Java", 'a', 'o'));
//http://www.cs.arizona.edu/icon/oddsends/palinsen.htm
System.out.println( reverseCharsRec("Marge let a moody baby doom a telegram."));
System.out.println( reverseCharsIter("Marge let a moody baby doom a telegram."));
......@@ -37,64 +24,21 @@ public class RecursionDriver {
// ===============================================
// ==a factorial function using recursion
// ===============================================
private static int myFactorialRec(int nVal) {
//base-case
if (nVal == 1)
return 1;
//recursive case(s)
else {
return nVal * (myFactorialRec(nVal - 1));
}
}
// ===============================================
// ==a factorial function using iteration
// ===============================================
private static int myFactorialIter(int nVal){
int nResult = 1;
for(int nC = nVal; nC > 0; nC--){
nResult *= nC; //nResult = nResult * nC;
}
return nResult;
}
// ===============================================
// ==a char replace function using recursion
// ===============================================
public static String replaceRec(String str, char cOld, char cNew) {
if (str.length() == 0)
return "";
else if (str.charAt(0) == cOld)
return cNew + replaceRec(str.substring(1), cOld, cNew);
else
return str.charAt(0) + replaceRec(str.substring(1), cOld, cNew);
}
// ===============================================
// ==a char replace function using iteration
// ===============================================
public static String replaceIter(String str, char cOld, char cNew) {
String strReturn = "";
//for each char in str; replace
for (int nC = 0; nC < str.length(); nC++) {
if(str.charAt(nC) == cOld){
strReturn += cNew;
}
else {
strReturn += str.charAt(nC);
}
}//end for
return strReturn;
}
......@@ -108,91 +52,25 @@ public class RecursionDriver {
// ===============================================
// ==a char reverse function using recursion
// ===============================================
private static String reverseCharsRec(String str) {
//base case
if ((null == str) || (str.length() <= 1)) {
return str;
}
//recursive case(s)
return reverseCharsRec(str.substring(1)) + str.charAt(0);
}
// ===============================================
// ==a char reverse function using recursion
// ===============================================
private static String reverseCharsAndRemoveRec(String str) {
//base case
if (str.length() == 1) {
if (!Character.isLetter(str.charAt(0))){
return "";
} else {
return String.valueOf(str.charAt(0));
}
}
//recursive case(s)
if (!Character.isLetter(str.charAt(0))){
return reverseCharsAndRemoveRec(str.substring(1));
} else {
return reverseCharsAndRemoveRec(str.substring(1)) + str.charAt(0);
}
}
private static String removeNotLettersRec(String str){
//base case
if (str.length() == 1) {
if (!Character.isLetter(str.charAt(0))){
return "";
} else {
return String.valueOf(str.charAt(0));
}
}
//recursive case(s)
if (!Character.isLetter(str.charAt(0))){
return removeNotLettersRec(str.substring(1));
} else {
return str.charAt(0) + removeNotLettersRec(str.substring(1));
}
}
private static void testIsPalindrome(String str){
String strForward = removeNotLettersRec(str);
String strBackward = reverseCharsAndRemoveRec(str);
if (strForward.equalsIgnoreCase(strBackward)){
System.out.println(strForward + " == " + strBackward + " : this is a palindrome.");
} else {
System.out.println(strForward + " != " + strBackward + " : this is NOT a palindrome.");
}
}
// ===============================================
// ==a char reverse function using iteration
// ===============================================
private static String reverseCharsIter(String str) {
String strRet = "";
//iterate over string backwards building return string
for (int nC = str.length()-1; nC >=0 ; nC--) {
//strRet += str.charAt(nC);
strRet = appendChar(strRet,str.charAt(nC) );
}
return strRet;
}
private static String appendChar(String strBase, char cEnd){
return strBase + String.valueOf(cEnd);
}
......
......@@ -14,44 +14,5 @@ public class Coin {
private static DecimalFormat sDecimalFormat = new DecimalFormat("$0.00");
public Coin(double value) {
mValue = value;
}
public Coin(String strVal){
strVal = strVal.toUpperCase();
double dVal = 0.0;
switch (strVal){
case "Q":
dVal = 0.25;
break;
case "D":
dVal = 0.10;
break;
case "N":
dVal = 0.05;
break;
default:
dVal = 0.00;
break;
}
setValue(dVal);
}
public double getValue() {
return mValue;
}
public void setValue(double value) {
mValue = value;
}
@Override
public String toString(){
return sDecimalFormat.format(getValue());
}
}
......@@ -31,66 +31,12 @@ public class Driver {
vndMachine.stockMe(prdProducts);
outer:
while (true) {
System.out.println("Type 'i' for insert coins or 's' for select product or type 'exit' to quit");
String strInitial = sScanner.nextLine();
strInitial = strInitial.toUpperCase();
String strMoney, strSelect;
switch (strInitial){
case "I":
strMoney = addMoney();
vndMachine.insertCoins(strMoney);
System.out.println("Credits : " + vndMachine.howMuchInserted());
break;
case "S":
System.out.println(vndMachine.showSelection());
strSelect = makeSelection();
if (strSelect.equalsIgnoreCase("AABBCC")){
ArrayList<Coin> conCashOuts = vndMachine.cashOut();
System.out.println("Jackpot!");
for (Coin conCashOut : conCashOuts) {
System.out.println(conCashOut);
}
break;
}
Product prdProduct = vndMachine.vend(strSelect);
if (prdProduct != null)
System.out.println("Thank you and Enjoy: " + prdProduct);
else {
System.out.print("You inserted " + vndMachine.howMuchInserted() + " : Insufficient coins or out of stock");
ArrayList<Coin> conReturns = vndMachine.returnCoins();
System.out.println(", here is your money back: ");
for (Coin conReturn : conReturns) {
System.out.println(conReturn);
}
}
break;
case "EXIT":
break outer;
default:
break;
}
}
System.out.println("Thank you for vending");
}
private static String addMoney(){
System.out.println("Insert Coins like so: q q d d n");
return sScanner.nextLine().toUpperCase();
}
private static String makeSelection(){
System.out.println("Please choose a product such as 'A2'");
return sScanner.nextLine().toUpperCase();
}
}
......@@ -71,192 +71,6 @@ public class VendMachine {
}
public void stockMe(ArrayList<Product> prdProducts){
//for each row
//for each col
//prdStocks = new Product[SLOTS];
//if nC < prdProducts.size()
//Product prdItem = prdProducts.get(nC++);
//else
//labelled break outer loop
//for each slot 0-9
//prdStocks[slot] = prdItem;
//map.put(row+col, prdStocks ); //where row+col would resolve to something like "A1"
mapProducts = new TreeMap<>();
int nCounter = 0;
Product[] prdStocks;
Product prdItem;
outerloop:
for (int nR = 0; nR < ROWS ; nR++) {
for (int nC = 0; nC < COLS ; nC++) {
prdStocks = new Product[SLOTS];
if (nCounter < prdProducts.size()){
prdItem = prdProducts.get(nCounter++);
} else {
break outerloop;
}
for (int nS = 0; nS < SLOTS ; nS++) {
prdStocks[nS] = prdItem;
}
mapProducts.put(getProperId(nR, nC), prdStocks);
}
}
}
public String showSelection(){
StringBuilder stb = new StringBuilder();
Product[] prdProducts;
for (String strKey : mapProducts.keySet()) {
prdProducts = mapProducts.get(strKey);
stb.append(strKey + " : ");
boolean bOutStock = true; //assume it's out of stock
for (Product prdProduct : prdProducts) {
if (prdProduct != null){
bOutStock = false;
stb.append(prdProduct.toString());
stb.append("\n");
break;
}
}
if (bOutStock){
stb.append("Out of Stock");
stb.append("\n");
}
}
stb.append("Credits : " + howMuchInserted() + "\n");
return stb.toString();
}
public void insertCoins(ArrayList<Coin> conPassed){
for (Coin con : conPassed) {
mPurchaseMoneys.add(con);
}
}
public void insertCoins(String strCoinValue){
ArrayList<Coin> conCoins = new ArrayList<>();
String[] strCoins = strCoinValue.split(" ");
for (String strCoin : strCoins) {
mPurchaseMoneys.add(new Coin(strCoin));
}
insertCoins(conCoins);
}
private double getFunds(){
double dReturn = 0.0;
for (Coin conMoney : mPurchaseMoneys) {
dReturn += conMoney.getValue();
}
return dReturn;
}
public String howMuchInserted(){
double dReturn = 0.0;
for (Coin purchaseMoney : mPurchaseMoneys) {
dReturn += purchaseMoney.getValue();
}
return sDecimalFormat.format(dReturn);
}
public Product vend(String strKey){
Product prdReturn = null;
Product[] prdProds = mapProducts.get(strKey);
if (prdProds == null){
return prdReturn;
}
for (int nC = 0; nC <prdProds.length ; nC++) {
if(prdProds[nC] != null){
if (getFunds() >= prdProds[nC].getPrice()){
transferFunds();
prdReturn = prdProds[nC];
prdProds[nC] = null;
}
break;
}
}
return prdReturn;
//get the product array at the proper key
//assume we're out of stock
//start at beginning and iterate entire
//if you reach a product
//if sufficient funds
//return Product
//else return null
}
public ArrayList<Coin> returnCoins(){
ArrayList<Coin> conTemps = mPurchaseMoneys;
mPurchaseMoneys = new ArrayList<>();
return conTemps;
}
public ArrayList<Coin> cashOut(){
ArrayList<Coin> conTemp = mBanks;
//reset the bank
mBanks = new ArrayList<>();
return conTemp;
}
private void transferFunds(){
for (Coin conMoney : mPurchaseMoneys) {
mBanks.add(conMoney);
}
//reset the user's money and give NO change
mPurchaseMoneys = new ArrayList<>();
}
private String getProperId(int nRow, int nCol){
nCol++;
switch (nRow){
case 0:
return "A" + nCol;
case 1:
return "B" + nCol;
case 2:
return "C" + nCol;
default:
return "";
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment