From 2ef5049aafc7042f718ebd3e4f4e833c9e2f2d80 Mon Sep 17 00:00:00 2001 From: Denny Nguyen Date: Thu, 25 Oct 2018 16:36:32 -0400 Subject: [PATCH 1/2] Add AsteriskPrinter --- AsteriskPrinter/AsteriskDrawer.java | 62 ++++++++++++++++++++ AsteriskPrinter/AsteriskPrinterMain.java | 39 +++++++++++++ AsteriskPrinter/NumberInput.java | 72 ++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 AsteriskPrinter/AsteriskDrawer.java create mode 100644 AsteriskPrinter/AsteriskPrinterMain.java create mode 100644 AsteriskPrinter/NumberInput.java diff --git a/AsteriskPrinter/AsteriskDrawer.java b/AsteriskPrinter/AsteriskDrawer.java new file mode 100644 index 0000000..8986874 --- /dev/null +++ b/AsteriskPrinter/AsteriskDrawer.java @@ -0,0 +1,62 @@ +public class AsteriskDrawer { + + static void displayOptions() { + System.out.println("1) Box"); + System.out.println("2) Left-Aligned Triangle"); + System.out.println("3) Right-Aligned Triangle"); + System.out.println("4) Centered Triangle"); + System.out.println("5) Exit"); + } + + static void askForNumRows() { + System.out.println(); + System.out.println("Input the number of rows you want."); + } + + //draws lines with the same number of asterisks as the number of columns. + static void drawBox(int numRows) { + for (int i = 0; i < numRows; i++) { + System.out.println(); + for (int j = 0; j < numRows; j++) { + System.out.print("*"); + } + } + } + + //draws LeftAlignedTriangle by drawing 1 asterisk then increasing by 1 for each row. + static void drawLeftAlignedTriangle(int numRows) { + for (int i = 0; i < numRows; i++) { + System.out.println(); + for (int j = 0; j <= i; j++) { + System.out.print("*"); + } + } + } + + //draws blank spaces and then draws the correct number of asterisks + static void drawRightAlignedTriangle(int numRows) { + for (int i = numRows; i > 0; i--) { + System.out.println(); + for (int j = numRows - i; j > 0; j--) { + System.out.print(" "); + } + for (int k = 0; k < i; k++) { + System.out.print("*"); + } + } + } + + //draws blank spaces and then draws the correct number of asterisks + static void drawCenterAlignedTriangle(int numRows) { + int maxRowLength = numRows * 2 - 1; + for (int i = 1; i <= maxRowLength; i += 2) { + System.out.println(); + for (int j = 0; j < (maxRowLength - i) / 2; j++) { + System.out.print(" "); + } + for (int k = 0; k < i; k++) { + System.out.print("*"); + } + } + } +} diff --git a/AsteriskPrinter/AsteriskPrinterMain.java b/AsteriskPrinter/AsteriskPrinterMain.java new file mode 100644 index 0000000..9fa240d --- /dev/null +++ b/AsteriskPrinter/AsteriskPrinterMain.java @@ -0,0 +1,39 @@ +import java.util.*; + +public class AsteriskPrinterMain { + public static void main(String[] args) { + Scanner keyType = new Scanner(System.in); + askAndDraw(keyType); + } + + static void askAndDraw(Scanner keyType) { + while (true) { + System.out.println("Please type in the number of the option you would like to choose."); + AsteriskDrawer.displayOptions(); + switch (NumberInput.noNegIntInput(keyType)) { + case 1: //Box + AsteriskDrawer.askForNumRows(); + AsteriskDrawer.drawBox(NumberInput.noNegIntInput(keyType)); + break; + case 2: //Left-aligned-triangle + AsteriskDrawer.askForNumRows(); + AsteriskDrawer.drawLeftAlignedTriangle(NumberInput.noNegIntInput(keyType)); + break; + case 3: //Right-center-triangle + AsteriskDrawer.askForNumRows(); + AsteriskDrawer.drawRightAlignedTriangle(NumberInput.noNegIntInput(keyType)); + break; + case 4: //Center triangle + AsteriskDrawer.askForNumRows(); + AsteriskDrawer.drawCenterAlignedTriangle(NumberInput.noNegIntInput(keyType)); + break; + case 5: //Exit + return; + default: + System.out.println("Not a valid option."); + break; + } + System.out.println(); + } + } +} diff --git a/AsteriskPrinter/NumberInput.java b/AsteriskPrinter/NumberInput.java new file mode 100644 index 0000000..66a93b7 --- /dev/null +++ b/AsteriskPrinter/NumberInput.java @@ -0,0 +1,72 @@ +import java.util.*; + +public class NumberInput { + + //Methods will not allow user to advance until a number in the correct format is inputted + + public static int intInput(Scanner keyboard){ //checks if input is a int and return. If not, then will give error and force user to retry + int iOutput = -1; + while (true) { + try { + iOutput = keyboard.nextInt(); + break; + } catch (InputMismatchException I) { + System.out.println("You did not input a whole number. Please try again."); + keyboard.next(); + } + } + return iOutput; + } + + public static int noNegIntInput(Scanner keyboard){ //checks if input is a int and return. If not or is negative, then will give error and force user to retry + int iOutput = -1; + while (iOutput == -1) { + try { + iOutput = keyboard.nextInt(); + if (iOutput < 0) { + iOutput = -1; + System.out.println("You put in a number less than 0. Please try again."); + } + } catch (InputMismatchException I) { + System.out.println("You did not input a whole number. Please try again."); + keyboard.next(); + } + } + return iOutput; + } + + public static double dInput(Scanner keyboard) { //checks if input is a double and return. If not, then will give error and force user to retry + double dOutput = 0; + boolean noInput = true; + while (noInput) { + try { + dOutput = keyboard.nextDouble(); + noInput = false; + } catch (InputMismatchException I) { + System.out.println("You did not input a number. Please try again."); + keyboard.next(); + } + } + return dOutput; + } + + public static double noNegDInput(Scanner keyboard) { //checks if input is a double and return. If not or negative, then will give error and force user to retry + double dOutput = 0; + boolean noInput = true; + while (noInput) { + try { + dOutput = keyboard.nextDouble(); + noInput = false; + + if (dOutput < 0) { //checks if number is negative and will reset @noInput to force user to retry + System.out.println("You put in a number less than 0. Please try again."); + noInput = true; + } + } catch (InputMismatchException I) { + System.out.println("You did not input a number. Please try again."); + keyboard.next(); + } + } + return dOutput; + } +} \ No newline at end of file From bcf27986f63b56b887bb24309bdb09df167b71da Mon Sep 17 00:00:00 2001 From: Denny Nguyen Date: Thu, 25 Oct 2018 16:44:14 -0400 Subject: [PATCH 2/2] Add BankSimulator --- BankSimulator/BankDriver.java | 34 ++++++++++++++++++++++++++ BankSimulator/Banking.java | 7 ++++++ BankSimulator/CheckingAccount.java | 29 +++++++++++++++++++++++ BankSimulator/CreditAccount.java | 38 ++++++++++++++++++++++++++++++ BankSimulator/SavingsAccount.java | 32 +++++++++++++++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 BankSimulator/BankDriver.java create mode 100644 BankSimulator/Banking.java create mode 100644 BankSimulator/CheckingAccount.java create mode 100644 BankSimulator/CreditAccount.java create mode 100644 BankSimulator/SavingsAccount.java diff --git a/BankSimulator/BankDriver.java b/BankSimulator/BankDriver.java new file mode 100644 index 0000000..7d5e68f --- /dev/null +++ b/BankSimulator/BankDriver.java @@ -0,0 +1,34 @@ +public class BankDriver { + public static void main(String[] args) { + Banking[] myAccounts = new Banking[3]; + myAccounts[0] = new CheckingAccount(200.00); + myAccounts[1] = new SavingsAccount(500.00, 0.5); + myAccounts[2] = new CreditAccount(13.1, 1000.0); + + System.out.println("INITIAL ACCOUNTS"); + for (Banking account : myAccounts) { + System.out.println(account); + } + System.out.println(); + + myAccounts[0].withdraw(75.75); + myAccounts[1].deposit(100.00); + myAccounts[2].withdraw(350.25); + + System.out.println("TRANSACTIONS ROUND 1"); + for (Banking account : myAccounts) { + System.out.println(account); + } + System.out.println(); + + myAccounts[0].deposit(50.00); + myAccounts[1].withdraw(500.00); + myAccounts[2].deposit(30.00); + + System.out.println("TRANSACTIONS ROUND 2"); + for (Banking account : myAccounts) { + System.out.println(account); + } + System.out.println(); + } +} \ No newline at end of file diff --git a/BankSimulator/Banking.java b/BankSimulator/Banking.java new file mode 100644 index 0000000..78383f0 --- /dev/null +++ b/BankSimulator/Banking.java @@ -0,0 +1,7 @@ +public interface Banking { + double deposit(double amount); + + double withdraw(double amount); + + double getBalance(); +} diff --git a/BankSimulator/CheckingAccount.java b/BankSimulator/CheckingAccount.java new file mode 100644 index 0000000..50bd673 --- /dev/null +++ b/BankSimulator/CheckingAccount.java @@ -0,0 +1,29 @@ +public class CheckingAccount implements Banking { + private double balance; + + public CheckingAccount(double balance) { + this.balance = balance; + } + + //Increase @balance by @amount + public double deposit(double amount) { + return balance += amount; + } + + //Decrease @balance by @amount and return @balance. If @amount is greater than @balance, return -1 + public double withdraw(double amount) { + if (amount > balance) { + System.out.println("Not enough funds."); + return -1; + } + return balance -= amount; + } + + public double getBalance() { + return balance; + } + + public String toString() { + return "Checking Account Balance: " + "$" + balance; + } +} diff --git a/BankSimulator/CreditAccount.java b/BankSimulator/CreditAccount.java new file mode 100644 index 0000000..fad7d1b --- /dev/null +++ b/BankSimulator/CreditAccount.java @@ -0,0 +1,38 @@ +public class CreditAccount implements Banking { + private double balance = 0; + private double interest; + private double creditLine; + private double available; + + public CreditAccount(double interest, double creditLine) { + this.interest = interest; + this.creditLine = creditLine; + available = creditLine; + } + + public double deposit(double amount) { + balance -= amount; + available += amount; + return balance; + } + + public double withdraw(double amount) { + if (amount > available) { + System.out.println("Not enough funds."); + return -1; + } + + balance += amount; + available -= amount; + + return balance; + } + + public double getBalance() { + return balance; + } + + public String toString() { + return "Credit Account Balance: " + "$" + balance + " Interest Rate: " + interest + "%" + " Credit Line: " + "$" + creditLine + " Available to Spend: " + "$" + available; + } +} diff --git a/BankSimulator/SavingsAccount.java b/BankSimulator/SavingsAccount.java new file mode 100644 index 0000000..a0821c3 --- /dev/null +++ b/BankSimulator/SavingsAccount.java @@ -0,0 +1,32 @@ +public class SavingsAccount implements Banking { + private double balance; + private double interest; + + public SavingsAccount(double balance, double interest) { + this.balance = balance; + this.interest = interest; + } + + //Increase @balance by @amount + public double deposit(double amount) { + return balance += amount; + } + + //Decrease @balance by @amount and return @balance. If @amount is greater than @balance, return -1 + public double withdraw(double amount) { + if (amount > balance) { + System.out.println("Not enough funds."); + return -1; + } + + return balance -= amount; + } + + public double getBalance() { + return balance; + } + + public String toString() { + return "Savings Account Balance: " + "$" + balance + " Interest Rate: " + interest + "%"; + } +}