Modify the code so input format is checked (InputMismatchException and obviously
ID: 3564074 • Letter: M
Question
Modify the code so input format is checked (InputMismatchException and obviously the NumberformatException and also when an user withdraws more than the amount he has in his account ( OverWithdrawException) etc. Show use of at least three different exception with one new exception created.
import java.util.ArrayList;
import java.util.Date;
public class Account {
private int id = 0;
private double balance = 0.0;
private double annualInterestRate = 0.0;
private Date dateCreated ;
private String name;
//a new data field named transactions whose type is ArrayList that stores that transaction for the accounts.
private ArrayList<Transaction> transaction = new ArrayList<Transaction>();
public Account()
{
}
public Account(int id,double bal){
this();
this.id = id;
this.balance = bal;
}
public Account(int id,String name,double bal){
this();
this.id = id;
this.balance = bal;
this.name = name;
}
public int getId()
{
return id;
}
public double getBalance(){
return balance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public void setId(int id){
this.id = id;
}
public void setBalance(double bal){
this.balance = bal;
}
public void setannualInterestRate(double rate)
{
this.annualInterestRate = rate;
}
public Date getDateCreated(){
return dateCreated;
}
public double getMonthlyInterestRate(){
return annualInterestRate / 12;
}
public double getMonthlyInterest(){
return (balance * annualInterestRate) / (100 * 12);
}
//A method named withdraw that withdraws a specified amount from the account.
public void withdraw(double amt)
{
if(balance > amt){
balance -= amt;
Transaction t = new Transaction('w', amt, this.balance, "Amount withdraw");
transaction.add(t);
}
else{
System.out.println("In sufficient balance");
}
}
//A method name deposit that deposits a specified amount to the account.
public void deposit(double amt){
balance = balance + amt;
Transaction t = new Transaction('d', amt, this.balance, "Amount deposit");
transaction.add(t);
}
public void print()
{
System.out.println("Name:"+name+" Interest Rate:"+annualInterestRate+" Balance:"+balance);
for(Transaction t : transaction ){
System.out.println(t.print());
}
}
}
/**
*a new data field named transactions whose type is ArrayList
*that stores that transaction for the accounts.
*Each transaction is an instance of the Transaction class.
*/
class Transaction{
private Date date;
private double balance;
private double amount;
private char type;
private String description;
public Transaction(char type,double amt,double bal,String desc){
balance = bal;
this.type = type;
this.amount = amt;
description = desc;
}
public String print()
{
return "Balance:"+balance+" Amount:"+amount+" Type:"+type+" Description:"+description;
}
}
Explanation / Answer
// Please let me know if you have any issues.
import java.util.ArrayList;
import java.util.Date;
public class Account {
private int id = 0;
private double balance = 0.0;
private double annualInterestRate = 0.0;
private Date dateCreated ;
private String name;
//a new data field named transactions whose type is ArrayList that stores that transaction for the accounts.
private ArrayList<Transaction> transaction = new ArrayList<Transaction>();
public Account()
{
}
public Account(int id,double bal){
this();
this.id = id;
this.balance = bal;
}
public Account(int id,String name,double bal){
this();
this.id = id;
this.balance = bal;
this.name = name;
}
public int getId()
{
return id;
}
public double getBalance(){
return balance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public void setId(int id){
this.id = id;
}
public void setBalance(double bal){
this.balance = bal;
}
public void setannualInterestRate(double rate)
{
this.annualInterestRate = rate;
}
public Date getDateCreated(){
return dateCreated;
}
public double getMonthlyInterestRate(){
return annualInterestRate / 12;
}
public double getMonthlyInterest(){
return (balance * annualInterestRate) / (100 * 12);
}
//A method named withdraw that withdraws a specified amount from the account.
public void withdraw(double amt) throws OverWithdrawException
{
if(balance > amt){
balance -= amt;
Transaction t = new Transaction('w', amt, this.balance, "Amount withdraw");
transaction.add(t);
}
else{
// Exception added where
throw new OverWithdrawException("Insufficient Balance");
}
}
//A method name deposit that deposits a specified amount to the account.
public void deposit(double amt){
balance = balance + amt;
Transaction t = new Transaction('d', amt, this.balance, "Amount deposit");
transaction.add(t);
}
public void print()
{
System.out.println("Name:"+name+" Interest Rate:"+annualInterestRate+" Balance:"+balance);
for(Transaction t : transaction ){
System.out.println(t.print());
}
}
}
/**
*a new data field named transactions whose type is ArrayList
*that stores that transaction for the accounts.
*Each transaction is an instance of the Transaction class.
*/
class Transaction{
private Date date;
private double balance;
private double amount;
private char type;
private String description;
public Transaction(char type,double amt,double bal,String desc){
balance = bal;
this.type = type;
this.amount = amt;
description = desc;
}
public String print()
{
return "Balance:"+balance+" Amount:"+amount+" Type:"+type+" Description:"+description;
}
}
public class OverWithdrawException extends Exception {
public OverWithdrawException(){
super();
}
public OverWithdrawException(String message) {
super(message);
}
public OverWithdrawException(String message, Throwable cause) {
super(message, cause);
}
}
import java.util.InputMismatchException;
import java.util.Scanner;
public class AccountMain {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int option;
Account account = null;
do{
System.out.println("1. Add Account");
System.out.println("2. Check Balance");
System.out.println("3. Deposit");
System.out.println("4. Withdraw");
System.out.println("5. Exit");
System.out.print("Enter Choice >> ");
option = in.nextInt();
switch (option) {
case 1:
try{
System.out.print("Enter id >> ");
int id = in.nextInt();
System.out.print("Enter amount >> ");
double bal = in.nextDouble();
account = new Account(id, bal);
}
catch (InputMismatchException e) { // Exception used
System.out.println("Invalid input, try again");
}
break;
case 2:
System.out.println(account.getBalance());
break;
case 3:
System.out.println("Enter Amount>> ");
String amount = in.next();
try{
account.deposit(Double.parseDouble(amount));
}
catch (NumberFormatException e) {
System.out.println("Only double values allowed");
}
break;
case 4:
System.out.println("Enter amount>> ");
try{
String amt = in.next();
account.withdraw(Double.parseDouble(amt));
}
catch (NumberFormatException e) {
System.out.println("Only double values allowed");
}
catch (OverWithdrawException e) {
System.out.println(e.getMessage());
}
break;
default:
System.out.println("Invalid option");
break;
}
}
while(option!=5);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.