Java JUnit Testing Collections: Set Logic exception handling Task: Time to test!
ID: 3707749 • Letter: J
Question
Java JUnit Testing
Collections: Set
Logic exception handling
Task: Time to test!
Getting Started
To begin this lab, create a new Java project named abc123-lab9, and create the following packages and classes:
bank.Account.java
bank.Bank.java
This lab does not deal with synchronized or multithreading. Correct solutions to this lab will not be thread-safe.
Banks & Accounts
Bank.java will define functionality for a bank. Banks must have a name and a set of accounts. Use a java.util.Set implementation of your choosing for this data structure, and the Account.java implementation for the accounts stored.
Bank.java must have a single constructor to properly initialize banks. In addition, there should be an overloaded method called addAccount which can add Account objects to the bank given either a name for the account, or both a name for the account and a starting balance. This method will create an Account object and add it to the set stored in the Bank object, nothing will be returned.
Creating a new object would be as follows:
Bank bank = new Bank("Gringotts");
Account.java will define functionality for an account. Accounts must have a name associated with them, an account number, and a balance. As described above, an Account object can be defined using either all three of these (name, number, and balance) or only with the name and number. (This indicates the constructor must be overloaded - one for each of these cases.) If a starting balance is not provided, it will be assumed to be 0. There should be getters and setters for all of these three variables.
Account.java must have two additional methods: deposit and withdraw.
deposit will be an object method that takes in an amount of money and adds the amount to the balance of an account. Deposits should inherently only the positive values. This method will return the new balance of the account.
withdraw will be an object method that takes in an amount of money and reduces the balance by that amount. Withdrawls should inherently only be positive values. Withdrawls larger than the current balance should not be permitted. This method will return the new balance of the account.
Creating a new object would be as follows:
Account account1 = new Account("Amy Smith", 2202);
or
Account account2 = new Account("Bob Smith", 1584, 100.0);
JUnit Testing
Create a JUnit 4 Test Case, called AccountTest.java. This class will test only the functionality in the Account class, defined above. Implement the following methods:
setUp() - Method runs before each test is run.
testInit() - Test the constructors.
testDepositPositive() - Test depositing a positive amount into the account.
testDepositNegative() - Test depositing a negative amount into the account.
testWithdrawPositive() - Test withdrawing a positive amount from the account.
testWithdrawNegative() - Test withdrawing a negative amount from the account.
testWithdrawOverdraft() - Test withdrawing more than the balance from the account.
No test methods should be created for the getters or setters in the Account class. Each method should have the appropriate JUnit annotation. For each test listed, consider the preconditions and postconditions of the method in testing. Consider any input (parameters), logical requirements (described in the class descriptions above), and outputs (returned values). What could go wrong when the method is called? Equally important, what could be wrong after the method is called?
In the process of implementing these tests, you should run them and verify they are passing. If a test does not pass, first assess the test (is it set up correctly?), then verify your implementation meets the requirements (see Account.java above). A completed submission for this lab will correctly assess all test cases, and running AccountTest.java will result in a pass (green bar in the JUnit view).
Note that your submitted Eclipse project must include the dependecy for JUnit 4. To ensure this, export your code from Eclipse to an archive file, as in the lab instructions!
Explanation / Answer
package bankmanagment;
import java.util.*;
class Account{
String name,acc_type;
int Acc_num,Acc_Balance;
Account(){
}
Account(String n,int acc_num,int b,String a_t){
name=n;
Acc_num=acc_num;
Acc_Balance=b;
acc_type=a_t;
}
} // end class
class create_account extends Account{
create_account(String n,int acc_num,int b,String a_t){ // pass name and account type
name=n;
Acc_num=acc_num;
Acc_Balance=b;
acc_type=a_t;
}
create_account(){
super();
}
void insert(String n,int acc_num,String a_t){ // input user name, account number and type
name=n;
acc_type=a_t;
Acc_num=acc_num; // generate random number
Acc_Balance=0;
}
void display_details(){
System.out.println("Depositor Name :" +name);
System.out.println("Account Number : "+Acc_num);
System.out.println("Account Balance : "+Acc_Balance);
System.out.println("Account Type : "+acc_type);
}
void deposite(int acc_num,int money){
Acc_Balance=money;
}
int withdraw(int withd){
Acc_Balance=Acc_Balance-withd;
return Acc_Balance;
}
} // end class
public class Main {
public static void main(String args[]){
String user_name=null,type;
type = null;
int balance=0,tmp=0;
int withd=0,cb=0;
// to generate Random Account Number
int aNumber = 0;
aNumber = (int)((Math.random() * 9000)+1000);
create_account user = new create_account("user",0,0,"savings"); // initilaize -- name,acc_number,Balance,Type
Scanner in = new Scanner(System.in);
Scanner strng=new Scanner(System.in);
int userChoice;
boolean quit = false;
do {
System.out.println("1. Create Account");
System.out.println("2. Deposit money");
System.out.println("3. Withdraw money");
System.out.println("4. Check balance");
System.out.println("5. Display Account Details");
System.out.println("0. to quit: ");
System.out.print("Enter Your Choice : ");
userChoice = in.nextInt();
switch (userChoice) {
case 1:
System.out.print("Enter your Name : ");
user_name=strng.nextLine();
System.out.print("Enter Accout Type : ");
type=in.next();
user.insert(user_name, aNumber, type); // inserted
System.out.println(" Your Account Details Dont Forget Account Number ");
System.out.println("**************************");
user.display_details();
break;
case 2: // deposite
System.out.print("Enter your account Number : ");
tmp=in.nextInt();
if(tmp==user.Acc_num){
System.out.print("Enter Amount Of Money : ");
balance=in.nextInt();
user.Acc_Balance=balance;
System.out.println(" Successfully Deposited.");
}
else
System.out.println("Wrong Accoount Number.");
break;
case 3: // withdraw money
System.out.print("Enter your account Number : ");
tmp=in.nextInt();
if(tmp==user.Acc_num){
if(user.Acc_Balance==0)
System.out.print("Your Account is Empty.");
else{
System.out.print("Enter Amout Of Money : ");
withd=in.nextInt();
if(withd>user.Acc_Balance){
System.out.print("Enter Valid Amout of Money : ");
withd=in.nextInt();
}
else
cb= user.withdraw(withd);
System.out.println("Your Current Balance : "+cb);
}
}
else
System.out.println("Wrong Accoount Number.");
break;
case 4: // check balance
System.out.print("Enter your Account Number : ");
tmp=in.nextInt();
if(tmp==user.Acc_num){
System.out.println("Your Current Balance : "+user.Acc_Balance);
}
else
System.out.println("Wrong Accoount Number.");
break;
case 5: // display all info
System.out.print("Enter your Account Number :");
tmp=in.nextInt();
if(tmp==user.Acc_num){
user.display_details();
}else
System.out.println("Wrong Accoount Number.");
break;
case 0:
quit = true;
break;
default:
System.out.println("Wrong Choice.");
break;
}
System.out.println(" ");
} while (!quit);
System.out.println("Thanks !");
} // end main function
} // end mian class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.