dimanche 14 février 2021

Java atm program (No output on console) [closed]

I was working on this project and cant seem to see the desired out put of my project can someone help me with this? I fixed some of my codes and still no output is showing in my compiler here is the code of the project

ATM.java

import java.util.Scanner;
public class ATM {
    public static void main(String []args) {
        
        //init scanner
        Scanner sc = new Scanner(System.in);
        
        //init bank
        Bank theBank = new Bank("B-cash");
        
        // add a usee, creates a new savings account
        User aUser= theBank.addUser("Lulu", "Sanchez", "3213");
        
        
        //add a checking account
        Account newAccount = new Account("Checking", aUser, theBank);
        aUser.addAccount(newAccount);
        theBank.addAccount(newAccount);
        
        User curUser;
        while (true) {
            // stay in the login yuntil successful bcos shit
            curUser = ATM.mainMenuPrompt(theBank, sc);
            
            // stay in main menu until user quits
            ATM.printUserMenu(curUser, sc);
        }
    }
    /**
     * 
     * @param theBank
     * @param sc
     * @return
     */
    public static User mainMenuPrompt(Bank theBank, Scanner sc) {
        //inits
        String toAcct;
        String pin;
        User authUser;
        
        //promprt the user for user id pin combo
        
        do {
            
            System.out.printf("\n\nWelcome to %s\n\n", theBank.getName());
            System.out.println("Enter user ID:");
            toAcct = sc.nextLine();
            System.out.println("Enter Pin: ");
            pin = sc.nextLine();
            
            //try to get user obnject
            authUser = theBank.userLogin(toAcct, pin);
            if(authUser == null) {
                System.out.println("Incorrect user ID/pin combination. "+ "please try again");
                
            }
            
        } while(authUser == null); // contineue looping until login is successful
        return authUser;
    }
    
    public static void printUserMenu(User theUser, Scanner sc) {
        //print summary of data
        theUser.printAccountsSummary();
        
        
        //init
        int choice; 
        
        //user menu
        do {
            System.out.printf("Welcome back %s, what would you like to do?", theUser.getFirstName());
            System.out.println("   1) Show account transaction history");
            System.out.println("   2) Withdrawal");
            System.out.println("   3) Deposit money?");
            System.out.println("   4) Transfer");
            System.out.println("   5) Quit");
            System.out.print("Enter choice: ");
            choice = sc.nextInt();
            
            if(choice < 1 || choice > 5) {
                System.out.println("Enter a different number from 1-5");
                
                
            }
            
            }while(choice <1 || choice >5 );
        //process the choice
        switch (choice) {
        case 1:
            ATM.showTransHistory(theUser, sc);
            break;
        case 2:
            ATM.withdrawFunds(theUser, sc);
            break;
        case 3:
            ATM.depositFunds(theUser, sc);
            break;
        case 4:
            ATM.transferFunds(theUser, sc);
            break;
            
        }
        //redisplay this menu unless user wants to quit
        if (choice != 5) {
            ATM.printUserMenu(theUser, sc);
            
        }
    }
    /**
     * shows transaction history and stuff
     * @param theUser the logged in user object
     * @param sc scanner objeect for user input
     */
    public static void showTransHistory(User theUser, Scanner sc) {
        int theAcct;
        
        do {
            //get account whose transaction huistory to look at
            System.out.printf("Enter the number: (1=%d) of the account\n" + " whose transactions you want to see: ", theUser.numAccounts());
            theAcct = sc.nextInt()-1;
            if(theAcct < 0 || theAcct >= theUser.numAccounts()) {
                System.out.println("Invalid Account. Please try again.");
                
            }
            
            ;
        }while (theAcct < 0 || theAcct >= theUser.numAccounts());
        theUser.printAcctTransHistory(theAcct);
    }
    public static void transferFunds(User theUser, Scanner sc) {
        //ints
        int fromAcct;
        int toAcct;
        double amount;
        double acctBal;
        
        //get the account to trasfer from yes
        do {
            System.out.printf("Enter the number (1-%d) of the account\n" + "To trasfer from:  ");
            fromAcct = sc.nextInt()-1;      
             if (fromAcct < 0 || fromAcct >= theUser.numAccounts()) {
                 System.out.println("Invalid account. Please try again.");
             }
            }while(fromAcct < 0 || fromAcct >= theUser.numAccounts());
        acctBal = theUser.getAcctBalance(fromAcct);
        
        // what account to trasfer to
        do {
            System.out.printf("Enter the number (1-%d) of the account\n" + "To trasfer to:  ");
            toAcct = sc.nextInt()-1;        
             if (toAcct < 0 || toAcct >= theUser.numAccounts()) {
                 System.out.println("Invalid account. Please try again.");
             }
            }while(toAcct < 0 || toAcct >= theUser.numAccounts());
        // get the am,ount to fucking transfer
        do {
            System.out.printf("Enter the amount to transfer (max $%.02f): $", acctBal);
            amount = sc.nextDouble();
            if (amount < 0) {
                System.out.println("You don't have cash, you're broke");
                
            }else if (amount > acctBal) {
                System.out.printf("Amount must not be greater than\n" + "balance of $%.02f\n", acctBal);
                
            }
            }while(amount < 0 || amount > acctBal);
        //do the transfer
        theUser.addAcctTransaction(fromAcct, -1*amount, String.format("Trasfer to account %s", theUser.getAcctUUID(fromAcct)));
        theUser.addAcctTransaction(toAcct, amount, String.format("Trasfer to account %s", theUser.getAcctUUID(fromAcct)));
    }
    
    /**
     * Withdrawing from an account
     * @param theUser the logged in user
     * @param sc scanner for inputs
     */
    public static void withdrawFunds(User theUser, Scanner sc) {

        //ints
                int fromAcct;
                double amount;
                double acctBal;
                String memo;
                //get the account to trasfer from yes
                do {
                    System.out.printf("Enter the number (1-%d) of the account\n" + "To trasfer from:  ");
                    fromAcct = sc.nextInt()-1;      
                     if (fromAcct < 0 || fromAcct >= theUser.numAccounts()) {
                         System.out.println("Invalid account. Please try again.");
                     }
                    }while(fromAcct < 0 || fromAcct >= theUser.numAccounts());
                acctBal = theUser.getAcctBalance(fromAcct);
                
                // get the am,ount to fucking transfer
                do {
                    System.out.printf("Enter the amount to transfer (max $%.02f): $", acctBal);
                    amount = sc.nextDouble();
                    if (amount < 0) {
                        System.out.println("You don't have cash, you're broke");
                        
                    }else if (amount > acctBal) {
                        System.out.printf("Amount must not be greater than\n" + "balance of $%.02f\n", acctBal);
                        
                    }
                    }while(amount < 0 || amount > acctBal);
                // eat of the previous input
                sc.nextLine();
                
                //get a memo
                System.out.println("Enter a memo: ");
                memo = sc.nextLine();
                
                //do the withdraw
                theUser.addAcctTransaction(fromAcct, -1*amount, memo);
        
    }
    /**
     *  deposit funds
     * @param theUser the user object
     * @param sc scanner for input of said object
     */
    public static void depositFunds(User theUser, Scanner sc) {
        //ints
    int toAcct;
    double amount;
    double acctBal;
    String memo;
    //get the account to trasfer from yes
    do {
        System.out.printf("Enter the number (1-%d) of the account\n" + "To trasfer from:  ");
        toAcct = sc.nextInt()-1;        
         if (toAcct < 0 || toAcct >= theUser.numAccounts()) {
             System.out.println("Invalid account. Please try again.");
         }
        }while(toAcct < 0 || toAcct >= theUser.numAccounts());
    acctBal = theUser.getAcctBalance(toAcct);
    
    // get the am,ount to fucking transfer
    do {
        System.out.printf("Enter the amount to transfer (max $%.02f): $", acctBal);
        amount = sc.nextDouble();
        if (amount < 0) {
            System.out.println("You don't have cash, you're broke");
            
        }else if (amount > acctBal) {
            System.out.printf("Amount must not be greater than\n" + "balance of $%.02f\n", acctBal);
            
        }
        }while(amount < 0 || amount > acctBal);
    // eat of the previous input
    sc.nextLine();
    
    //get a memo
    System.out.println("Enter a memo: ");
    memo = sc.nextLine();
    
    //do the withdraw
    theUser.addAcctTransaction(toAcct, -1*amount, memo);

    }


}

BANK.java

import java.util.ArrayList;
import java.util.Random;
public class Bank {
    private String bankName;
    //call the list of users and accounts to an array list
    private ArrayList<User> users;
    private ArrayList<Account> accounts;
    
    public Bank(String name) {
        this.bankName = name;
        this.users = new ArrayList<User>();
        this.accounts = new ArrayList<Account>();
    }
    public String getNewUserUUID() {
        //inits
        String UUID;
        Random rng = new Random();
        int len = 7;
        boolean nonUnique = false;
        //continue generating id if not unique
        do {
            //generate the number
            UUID = "";
            for (int c = 0; c < len; c++) {
                UUID += ((Integer)rng.nextInt(10)).toString();
            }
            for (User u : this.users) {
                if (UUID.compareTo(u.getUUID()) == 0) {
                    nonUnique = true;
                    break;
                }
            }
        }
        while (nonUnique = true) ;
        return UUID;
    }
    // generate of each account
    public String getNewAccountUUID() {
        //inits
        String UUID;
        Random rng = new Random();
        int len = 10;
        boolean nonUnique = false;
        //continue generating id if not unique
        do {
            //generate the number
            UUID = "";
            for (int c = 0; c < len; c++) {
                UUID += ((Integer)rng.nextInt(10)).toString();
            }
            for (Account a : this.accounts) {
                if (UUID.compareTo(a.getUUID()) == 0) {
                    nonUnique = true;
                    break;
                }
            }
        }
        while (nonUnique = true) ;
    return UUID;
    }
    public void addAccount(Account anAcct) {
        this.accounts.add(anAcct);
    }
    /*
     * create a new user of the bank
     * die 
     */
    public User addUser(String firstName, String lastName, String pin) {
        //create a new user object and will add to the list of accoubtns
        User newUser = new User(firstName, lastName, pin, this);
        this.users.add(newUser);
        
        //createe a new savings account for the user and add to user and bank accounts
        Account newAccount = new Account("Savings", newUser, this);
        newUser.addAccount(newAccount);
        this.accounts.add(newAccount);
        
        return newUser;
    }
    
    public User userLogin(String userID, String pin) {
        
        // search through list of uiseers
        for (User u: this.users) {
            // check user ID is correct
            if (u.getUUID().compareTo(userID)== 0 && u.validatePin(pin)) {
                return u;
            }
        }
        return null;
    }
    public String getName() {
        return this.bankName;
        
    }
    
}

transactions

import java.util.Date;
public class Transaction {
    //amount of transaction
    private double amount;
    //when did the user made the transaction
    private Date timeStamp;
    //ntoes for the transaction
    private String memo;
    //who owns this account
    private Account inAccount;
    /*
     * create a bew transaction
     * @param amount the amoung transacted
     * @param inAccount the accoung the transaction beloings to
     */
    public Transaction(double amount, Account inAccount) {
        this.amount = amount;
        this.inAccount = inAccount;
        this.timeStamp = new Date();
        this.memo = "";
    }
    
    /*
     * if data is changed in the first automatically change the second
     */
    public Transaction(double amount, String memo, Account inAccount) {
        // call two arg constructor
        this(amount, inAccount);
        
        //setting the memo
        this.memo = memo;
        
    }
    /**
     * 
     *
     * @return the amount
     */
    public double getAmount() {
        return this.amount;
    }
    /**
     *  string summarizing the said transaction
     * @return the summary string
     */
    public String getSummaryLine() {
        if (this.amount >= 0) {
            return String.format("%s : $%0.2f : %s", this.timeStamp.toString(), this.amount, this.memo);
        }else {
            return String.format("%s : $(%0.2f) : %s", this.timeStamp.toString(), 
                    this.amount, this.memo);
        }
    }
    
}

accounts

import java.util.ArrayList;
public class Account {
    //Selected users credentials
    private String name;
    private String UUID;
    //only one holder of one user
    private User holder;
    //listing of all the transactions made by this user
    private ArrayList<Transaction> transactions;
    
    public Account(String name, User holder, Bank theBank) {
        //set the account name and holder
        this.name = name;
        this.holder = holder;
        
        //userUd id
        this.UUID = theBank.getNewAccountUUID();
        
        //initalize transactions
        this.transactions = new ArrayList<Transaction>();
        
    
    
    }
    public String getUUID() {
        return this.UUID;
    }
    /**
     * 
     * @return the string summary
     */
    public String getSummaryLine() {
        double balance = this.getBalance();
        
        //format the summary line, depending on the wheter the balance is negative turns red or say that is negative
        if(balance >=0) {
            return String.format("%s : $%.02f : ", this.UUID, balance, this.name);
        }else {
            return String.format("%s : $(%.02f) : ", this.UUID, balance, this.name);
        }
        }
    public double getBalance() {
        double balance = 0;
        for (Transaction t : this.transactions) {
            balance += t.getAmount();
        }
        return balance;
    }
    
    /**
     * print the transaction
     */
    public void printTransHistory() {
        System.out.printf("nTransactioj history for account %s", this.UUID);
        for(int t = this.transactions.size()-1; t >= 0; t--) {
            System.out.printf(this.transactions.get(t).getSummaryLine());
        }
        System.out.println();
    }
    /**
     * 
     * @param amount index of the amoount
     * @param memo
     */
    public void addTransaction(double amount, String memo) {
        // create a new transaction object and add it to our list
        Transaction newTrans = new Transaction(amount, memo, this);
        this.transactions.add(newTrans);
    }
}

USERS

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
public class User {
    //Credentials of users 
    private String firstName;
    private String lastName;
    private String UUID;
    //ecnrypted pin for opening the accounts
    private byte pinHash[];
    //list of all the users in the atm
    private ArrayList<Account> accounts;

    public User(String firstName, String lastName, String pin, Bank theBank) {
        //set user's name
        this.firstName = firstName;
        this.lastName = lastName;
        //store pin in a hash
        //for something
        //for hashing the pin
        /*
         * @firstName the name
         * @lastName the name
         * UUID is the id of user
         * pinHash is the pin
         * thebank is the object that the user is using 
         */
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            this.pinHash = md.digest(pin.getBytes());
        } catch (NoSuchAlgorithmException e) {
            System.err.println("System Malfunction");
            
            e.printStackTrace();
            System.exit(1);
        }
        //get user id
        this.UUID = theBank.getNewUserUUID();
        // create a list of accounts
        this.accounts = new ArrayList<Account>();
    
        //print log msg
         System.out.printf("New user %s, %s with ID %s created. \n", lastName, firstName, this.UUID);
    }
    
    //add an account for the user
    public void addAccount(Account anAcct) {
        this.accounts.add(anAcct);
    }
    
    public String getUUID(){
        return this.UUID;
    }
    
    
    
    /* 
     * check wheter pin is matches the true user pin
     */
        public boolean validatePin(String aPin) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                return MessageDigest.isEqual(md.digest(aPin.getBytes()), this.pinHash);
            } catch (NoSuchAlgorithmException e) {
                System.err.println("System Malfunction");
                
                e.printStackTrace();
                System.exit(1);
            
            }
            return false;
        }
        public String getFirstName() {
            return this.firstName;
        }
        public void printAccountsSummary() {
            System.out.printf("\n\n%s's accounts summary", this.firstName);
            for (int a = 0; a < this.accounts.size(); a++) {
                System.out.printf("%d) %s\n", a+1, this .accounts.get(a).getSummaryLine());
                
            }
            System.out.println();
        }
        
        /**
         * 
         * @return the number of accounts
         */
        public int numAccounts() {
            return this.accounts.size();
            }
        
        
        /**
         * 
         * @param acctIdx the index of the account to use
         */
        
        public void printAcctTransHistory(int acctIdx) {
            this.accounts.get(acctIdx).printTransHistory();
        }
        /**
         * get balance of an account
         * @param acctIdx index of account using
         * @return return the balance of the account
         */
        public double getAcctBalance(int acctIdx) {
            return this.accounts.get(acctIdx).getBalance(); 
            }
        /**
         * get the id of the account
         * @param acctIdx index of the account
         * @return return the id of the account
         */
        public String getAcctUUID(int acctIdx) {
            return this.accounts.get(acctIdx).getUUID();
            
        }
        public void addAcctTransaction(int acctIdx, double amount, String memo) {
            this.accounts.get(acctIdx).addTransaction(amount, memo);
        }
    }


it seems that the main interface on the console does not show in short nothing shows on the console I can just type on it




Aucun commentaire:

Enregistrer un commentaire