2. A member function that returns a special error code can also be accomplished
ID: 3757960 • Letter: 2
Question
2.
A member function that returns a special error code can also be accomplished by throwing an exception. The following class maintains an account balance, where each member function is implemented as an inline function.
class Account {
private:
double balance;
public:
Account():balance(0)
{
}
Account(double initialDeposit):balance( initialDeposit)
{
}
double getBalance()
{
return balance;
}
// returns new balance or -1 if error
double deposit(double amount)
{
if (amount > 0)
balance += amount;
else
return -1; // code indicating error
return balance;
}
// returns new balance or -1 if invalid amount
double withdraw(double amount)
{
if((amount > balance) || (amount < 0))
return -1;
else
balance -= amount;
return balance;
}
};
First, use separate compilation to redistribute the information in this class into a header file (.h) and an implementation file (.cpp). Next, modify the deposit() and withdraw() member functions such that they throw appropriate exceptions instead of returning -1 as an error code. To this end, please define the following three exception classes as derived classes of the standard exception class: (1) Exception_Negative_Deposit, corresponding to the case of depositing a negative amount of money, (2) Exception_Overdraw, corresponding to withdrawing more than the account's balance, and (3) Exception_Negative_Withdraw corresponding to withdrawing a negative amount from an account. You can place these three exception classes in one header file (both the declaration and the implementation). Finally, write test code that attempts to withdraw and deposit invalid amounts and catches the exceptions that are thrown in a try-catch statement.
Explanation / Answer
import java.io.*;
import java.util.*;
public class Bank{
static Customer customer = new Customer();
static Console menu = System.console();
public static void main(String[] args){
mainMenu();
}
public static void mainMenu(){ //main menu of system with options
System.out.println("Welcome to Java Bankn");
System.out.println("1. New Customer");
System.out.println("2. Customer Services");
System.out.println("3. Exitn");
String input = menu.readLine("%s", "Select option: ");
int menuOption = Integer.parseInt(input);
switch(menuOption){
case 1:
newCustomer();
break;
case 2:
viewCustomer();
break;
case 3:
System.exit(0);
default:
System.out.println("Please enter correct option: ");
mainMenu();
}
}
public static void newCustomer(){
System.out.println("Complete details to create new customern");
String IDNumber = menu.readLine("%s", "Enter client ID Number: ");
String firstName = menu.readLine("%s", "Enter First Names: ");
String lastName = menu.readLine("%s", "Enter Surname: ");
String telNumber = menu.readLine("%s", "Enter contact number: ");
String emailAddres = menu.readLine("%s", "Enter email address: ");
String confirm = menu.readLine("%s", "Are you sure you want to create customer? Y/N ");
if(confirm.equalsIgnoreCase("y")){
System.out.println("Creating customer...");
Customer customer = new Customer(IDNumber, firstName, lastName, telNumber, emailAddres);
Customer.customers.add(customer);
System.out.println(customer);
String proceed = menu.readLine("%s", "To proceed please press enternn");
if(proceed.equalsIgnoreCase(""))
mainMenu();
}
else
mainMenu();
}
public static void viewCustomer(){ //method call from mainMenu() to display options for customer
System.out.println("Customer Servicesn");
searchCustomer(); //to return and work with a selected customer from the searchCustomer method
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Transfer");
System.out.println("4. Balance");
System.out.println("5. Transaction History");
System.out.println("6. Delete Account");
System.out.println("7. Return to previous menun");
String input = menu.readLine("%s", "Select option: ");
int menuOption = Integer.parseInt(input);
switch(menuOption){
// case 1:
// String withAmnt = menu.readLine("%s", "Enter amount to withdraw");
// int withdrawAmount = Integer.parseInt(withAmnt);
// Customer.currentBalance -= withdrawAmount;
// return "Current Balance: " + currentBalance;
// viewCustomer();
// withdraw();
// case 2:
// deposit();
// case 3:
// transfer();
// case 4:
// balance();
//case 5:
//transactionHistory();
case 6:
Customer.customers.remove(customer);
mainMenu();
case 7:
mainMenu();
default:
System.out.println("nPlease enter correct option: ");
viewCustomer();
}
}
public static void searchCustomer(){ //method call to search customers with given options
System.out.println("Select option to search with: n");
if(Customer.customers != null){
System.out.println("No customers exist");
String exit = menu.readLine("%s", "To return to Main Menu please press enter: nn");
if(exit.equalsIgnoreCase(""))
mainMenu();
}
System.out.println("1. ID Number");
System.out.println("2. Name");
System.out.println("3. Account Number");
System.out.println("4. Return to previous menun");
String input = menu.readLine("%s", "Select option: ");
int inputOption = Integer.parseInt(input);
switch(inputOption){
case 1:
IDCompare idCompare = new IDCompare();
Collections.sort(Customer.customers, idCompare);
String idnumber = menu.readLine("%s", "Enter ID Number: ");
Customer.customers.lastIndexOf(idnumber);
case 2:
FirstNameCompare fnCompare = new FirstNameCompare();
Collections.sort(Customer.customers, fnCompare);
String name = menu.readLine("%s", "Enter Name: ");
Customer.customers.lastIndexOf(name);
case 3:
AccountNumberCompare acnumCompare = new AccountNumberCompare();
Collections.sort(Customer.customers, acnumCompare);
String account = menu.readLine("%s", "Enter Account Number: ");
Customer.customers.lastIndexOf(account);
case 4:
mainMenu();
default:
System.out.println("Please select correct option: ");
searchCustomer();
}
viewCustomer();
}
}
class Customer{// implements Comparable{
static List customers = new ArrayList();
String IDNumber;
String firstName;
String lastName;
String telNumber;
String emailAddres;
double currentBalance;
int accountNumber = 100001;
public Customer(){}
public Customer(String id, String fname, String lname, String tel){
this.IDNumber = id;
this.firstName = fname;
this.lastName = lname;
this.telNumber = tel;
}
public Customer(String id, String fname, String lname, String tel, String email){
this.IDNumber = id;
this.firstName = fname;
this.lastName = lname;
this.telNumber = tel;
this.emailAddres = email;
}
public String getIDNumber(){
return IDNumber;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getTelNumber(){
return telNumber;
}
public String getEMail(){
return emailAddres;
}
public int getAccountNumber(){
return accountNumber;
}
public String toString(){
return "Customer Details:nID Number: " + IDNumber + "nAccount Number: " + accountNumber + "nFirst Name: " + firstName + "nSurname: " + lastName + "nTelephone Number: " + telNumber + "nEmail Addres: " + emailAddres;
}
public int compareTo(Customer customer){
return firstName.compareTo(customer.getFirstName());
}
{ accountNumber++; }
}
class IDCompare implements Comparator{
public int compare(Customer customerOne, Customer customerTwo){
return customerOne.getIDNumber().compareTo(customerTwo.getIDNumber());
}
}
class FirstNameCompare implements Comparator{
public int compare(Customer customerOne, Customer customerTwo){
return customerOne.getFirstName().compareTo(customerTwo.getFirstName());
}
}
class AccountNumberCompare implements Comparator{
public int compare(Customer customerOne, Customer customerTwo){
return new Double(customerOne.getAccountNumber()).compareTo(new Double(customerTwo.getAccountNumber()));
}
}
class CustomerCollection{
// List customers = new ArrayList();
// customers.add(new Customer(id, fname, lname, tel, email);
}
class TransactionHistory{
List transaction = new ArrayList();
public void viewTransactionHistory(){}
public void newTransaction(){}
}
class Transactions{
double currentBalance;
double amountDeposit;
double amountWithdraw;
public double deposit(){
currentBalance += amountDeposit;
return currentBalance;
}
//public double transfer(){}
public double withdraw(){
currentBalance -= amountWithdraw;
return currentBalance;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.