Skip to content

Instantly share code, notes, and snippets.

@blarmon
Created December 1, 2017 21:43
Show Gist options
  • Save blarmon/1ef1de19dbc903bfd021fb8dfd109d48 to your computer and use it in GitHub Desktop.
Save blarmon/1ef1de19dbc903bfd021fb8dfd109d48 to your computer and use it in GitHub Desktop.
package bannobankapp;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Bank {
public String bankName;
Map<String, User> userMap = new HashMap<String, User>();
public Bank (String firstBankName) {
bankName = firstBankName;
}
public void createUser() {
//collect username from console
System.out.println("What is the new users name?");
Scanner userNameScanner = new Scanner(System.in);
String userName = userNameScanner.nextLine();
//collect balance from console
System.out.println("What is the new users account balance?");
Scanner balanceScanner = new Scanner(System.in);
Double balance = balanceScanner.nextDouble();
///create a new user with the appropriate username and account balance and add it to the hash
userMap.put(userName, new User(userName, balance));
userMap.get(userName).transactionList.add("Original balance: " + balance);
}
public void deleteUser() {
System.out.println("What is name of the user you would like to delete?");
Scanner userNameScanner = new Scanner(System.in);
String userToDelete = userNameScanner.nextLine();
if (userMap.containsKey(userToDelete)) {
userMap.remove(userToDelete);
}
else
System.out.println("User does not exist.");
}
public void getUser() {
System.out.println("What user would you like to see?");
Scanner userNameScanner = new Scanner(System.in);
String userToDisplay = userNameScanner.nextLine();
if (userMap.containsKey(userToDisplay)) {
System.out.println("Username: " + userMap.get(userToDisplay).userName + " Balance: " + userMap.get(userToDisplay).balance);
for (int i = 0; i < userMap.get(userToDisplay).transactionList.size(); i++) {
System.out.println(userMap.get(userToDisplay).transactionList.get(i));
}
}
else
System.out.println("User does not exist.");
}
//deletes and recreates user with new username and balance
public void updateUser() {
System.out.println("Which user would you like to update?");
Scanner updateUserScanner = new Scanner(System.in);
String userToUpdate = updateUserScanner.nextLine();
if (userMap.containsKey(userToUpdate)) {
//TODO give the OPTION of changing a users name?? Do so by saving oldinfo in temp variables, deleting, and readding user
System.out.println("Amount to add to balance? (Use negative numbers to subtract)");
Double amountToAdd = updateUserScanner.nextDouble();
userMap.get(userToUpdate).updateBalance(amountToAdd);
userMap.get(userToUpdate).transactionList.add("Balance changed by: " + amountToAdd);
//userMap.remove(userToUpdate);
//userMap.put(newUserName, new User(newUserName, newBalance));
}
else
System.out.println("User does not exist.");
}
//TODO fix double formatting for big numbers and add a "no users above given balance threshold message"
public void getAllUsersAboveBalance() {
System.out.println("What you like to use a balance threshold? (Y/N)");
Scanner balanceThresholdInputScanner = new Scanner(System.in);
String balanceThresholdInput = balanceThresholdInputScanner.nextLine().toUpperCase();
double balanceThreshold = 0;
double totalBalance = 0;
if (balanceThresholdInput.charAt(0) == 'Y') {
System.out.println("What is your balance threshold?");
Scanner balanceThresholdScanner = new Scanner(System.in);
balanceThreshold = balanceThresholdScanner.nextDouble();
}
for (Map.Entry<String, User> entry : userMap.entrySet())
{
if (entry.getValue().getBalance() > balanceThreshold) {
System.out.println("User Name: " + entry.getValue().getUserName() + " Account Balance: " + entry.getValue().getBalance());
totalBalance += entry.getValue().getBalance();
}
}
System.out.println("Total balance for all users above threshold (" + balanceThreshold + "): " + totalBalance);
//can't use lambda expression cuz only final values can be used in a lambda :(
/*
userMap.forEach((k, v) -> {
double balanceThreshold = 0;
if (balanceThresholdInput.charAt(0) == 'Y') {
System.out.println("What is your balance threshold?");
Scanner balanceThresholdScanner = new Scanner(System.in);
balanceThreshold = balanceThresholdScanner.nextDouble();
}
if (v.getBalance() > balanceThreshold) {
System.out.println("User Name: " + v.getUserName() + " Account Balance: " + v.getBalance());
}
else {
System.out.println("User Name: " + v.getUserName() + " Account Balance: " + v.getBalance());
}
});
*/
}
}
package bannobankapp;
import java.util.Scanner;
public class BannoBankApp {
public static void main(String[] args) {
Bank defaultBank = new Bank("defaultBank");
int loopBreak = 1;
while(true) {
System.out.println("What would you like to do?");
System.out.println("(G)et a user - (C)reate new user - (U)pdate a user - (D)elete a user - (S)how all users - (E)nd program");
Scanner userDecisionScanner = new Scanner(System.in);
String userDecision = userDecisionScanner.nextLine();
userDecision = userDecision.toUpperCase();
if (userDecision.charAt(0) == 'G')
defaultBank.getUser();
else if (userDecision.charAt(0) == 'C')
defaultBank.createUser();
else if (userDecision.charAt(0) == 'U')
defaultBank.updateUser();
else if (userDecision.charAt(0) == 'D')
defaultBank.deleteUser();
else if (userDecision.charAt(0) == 'S')
defaultBank.getAllUsersAboveBalance();
else if (userDecision.charAt(0) == 'E') {
System.out.println("c u :)");
break;
}
else
System.out.println("That's not an option cuck!");
}
}
}
package bannobankapp;
import java.util.ArrayList;
public class User {
public String userName;
public double balance;
public ArrayList<String> transactionList = new ArrayList<String>();
public User (String firstUserName, double firstBalance) {
userName = firstUserName;
balance = firstBalance;
transactionList.add("Account created");
}
public double getBalance () {
return balance;
}
public void updateBalance (double addToBalance) {
balance += addToBalance;
}
public String getUserName () {
return userName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment