lundi 30 novembre 2015

Code Coverage Test

Task: Using the bankAccount() method, draw its flow graph and list all the possible paths in the flow of the method. List all possible paths

You should list paths that include up to 2 menu choices followed by a quit eg. ‘make a deposit’, followed by ‘print a balance’ then ‘quit’ could be one valid path.

I have completed this task using flowchart. As i am new to this, I would appreciate if someone could look at my solution and let me know if it's good. I've put many hours into this so hopefully i've done the right thing.

The flowchart (my solution): enter image description here

The code that contains the bankAccount() method:

import  java.text.DateFormat;
import  java.text.SimpleDateFormat;
import  java.util.*;

public  class   Bank {

    public  int bankAccount(int customerNumber, int startingBalance) {
        Scanner stdin = new Scanner(System.in);
        System.out.println("Welcome to  the Bank.");
        Customer myCustomer = Customer.getWithCustomerNumber(customerNumber);   // get the customer
        String name = myCustomer.name; // get the customer name
        int balance = startingBalance; // get the starting balance
        DateFormat dateFormat   =   new SimpleDateFormat("yyyy/MM/dd    HH:mm:ss");     // Get the date and time
        Date date = new Date();
        int choice  =   0; //1=make deposit, 2=make withdrawal, 3=print balance, 4=quit

        while (choice != 4) { // process until the user quits
            System.out.println("Here are your menu options:");
            System.out.println("1. Make a deposit.");
            System.out.println("2. Make a withdrawal.");
            System.out.println("3. Print your balance.");
            System.out.println("4. Quit.");
            System.out.println("Please enter the number of your choice.");
            choice = stdin.nextInt();
            switch (choice) {
                case 1: {   //handles deposit
                    System.out.println("Enter   the amount  of  your    deposit.");
                    int deposit =   stdin.nextInt();
                    balance =   balance +   deposit;
                    System.out.println(dateFormat.format(date)+"Customer " + name + " new balance is $" + balance + ".");
                    break;
                }
                case 2: { // handles withdrawal
                    System.out.println("Enter   the amount  of  your    withdrawal.");
                    int withdrawal  =   stdin.nextInt();
                    if  (balance - withdrawal >= 0) {
                        balance = balance - withdrawal;
                        System.out.println(dateFormat.format(date) + "Customer " + name + " new balance is $" + balance + ".");
                    }   
                    else    
                        System.out.println(dateFormat.format(date)  +   " Customer  " + name +  " Insufficient  funds for withdrawal. Balance is $" +balance+".");
                    break;
                }
                case 3: { //prints current balance
                    System.out.println(dateFormat.format(date)+"Customer "+name+" balance is $" +balance+".");
                    break;
                }
                case 4: {
                    break; // do nothing... quits
                }
                default: {
                    System.out.println("Sorry   that    is  not a   valid   choice.");
                }
            }
        }
        System.out.println("Thank you for using the bank!");
        return  balance;
        }
}

Aucun commentaire:

Enregistrer un commentaire