Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions AsteriskPrinter/AsteriskDrawer.java
Original file line number Diff line number Diff line change
@@ -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("*");
}
}
}
}
39 changes: 39 additions & 0 deletions AsteriskPrinter/AsteriskPrinterMain.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
72 changes: 72 additions & 0 deletions AsteriskPrinter/NumberInput.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
34 changes: 34 additions & 0 deletions BankSimulator/BankDriver.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
7 changes: 7 additions & 0 deletions BankSimulator/Banking.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public interface Banking {
double deposit(double amount);

double withdraw(double amount);

double getBalance();
}
29 changes: 29 additions & 0 deletions BankSimulator/CheckingAccount.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
38 changes: 38 additions & 0 deletions BankSimulator/CreditAccount.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
32 changes: 32 additions & 0 deletions BankSimulator/SavingsAccount.java
Original file line number Diff line number Diff line change
@@ -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 + "%";
}
}