This is what I have to do for an assignment
Design a class named Account that contains:
A private int data field named id for the account (default 0).
A private double data field named balance for the account (default 0).
A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.
A private Date data field named dateCreated that stores the date when the account was created.
A no-arg constructor that creates a default account.
A constructor that creates an account with the specified id and initial balance.
The accessor and mutator methods for id, balance, and annualInterestRate.
The accessor method for dateCreated.
A method named getMonthlyInterestRate() that returns the monthly interest rate.
A method named getMonthlyInterest() that returns the monthly interest.
A method named withdraw that withdraws a specified amount from the account.
A method named deposit that deposits a specified amount to the account.
Implement the class. (Hint: The method getMonthlyInterest() is to return monthly interest, not the interest rate. Monthly interest is balance * monthlyInterestRate. monthlyInterestRate is annualInterestRate / 12. Note that annualInterestRate is a percentage, e.g., like 4.5%. You need to divide it by 100.)
Write a test program that creates an Account object with an account ID of 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the following transactions (negative values are withdrawals, positive values are deposits). I would like to see the transactions stored in a file.
-100.00 250.00 650.00 -25.00 10.00 -50.00 -60.00 -80.00 100.00
Print the balance, the monthly interest, and the date when this account was created. Calculate the interest based upon the ending balance at the end of the month, you should include the interest in your final balance.
What I have so far is listed below. Any help would be appreciated. The error is listed after the code as comments. Below is what I have so far:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.Scanner;
//Account
class Account
{
//variables
private int id;
private double balance;
private double annualInterest;
private Date dateCreated;
//default constuctor
Account(){}
//constructor
Account(int i,double bal){
this.id=i;
this.balance=bal;
}
//set Id
public void setId(int i)
{
this.id=i;
}
//set Balance
public void setBalance(double bal)
{
this.balance=bal;
}
//set InterestRate
public void setInterestRate(double rate)
{
this.annualInterest=rate;
}
//get ID
public int getId()
{
return id;
}
//get Balance
public double getBalance()
{
return balance;
}
//get InterestRate(
public double getInterestRate()
{
return annualInterest;
}
//get Date
public Date getDate()
{
dateCreated=new Date();
return dateCreated;
}
//get MonthlyInterestRate
public double getMonthlyInterestRate()
{
double monthlyInterest=getMonthlyInterest();
return balance*monthlyInterest;
}
//get MonthlyInterest
public double getMonthlyInterest()
{
return (annualInterest/1200);
}
//withdraw
public void withdraw(double amt)
{
this.balance=balance-amt;
}
//deposit
public void deposit(double amt)
{
this.balance=balance+amt;
}
}
//main method
public class AccountDriver {
public static void main(String[] args) throws FileNotFoundException {
//set account id, balance
Account at=new Account(1122,20000);
//set InterestRate
at.setInterestRate(4.5);
System.out.println("ID: "+at.getId());
System.out.println("Balance: "+at.getBalance());
System.out.println("Interest: "+at.getInterestRate());
System.out.println("Date: "+at.getDate().toString());
//transaction
Scanner sc=new Scanner(new File("c:\\books\\trans.txt"));
while(sc.hasNext())
{
int amt=sc.nextInt();
if(amt<0)
at.withdraw(amt);
else
at.deposit(amt);
}
//print balance
System.out.println("Balance After Trasaction: ");
System.out.println("Balance: "+at.getBalance());
System.out.println("Interest Rate: "+at.getMonthlyInterestRate());
}
}
//The error I keep receiving is below.
//Exception in thread "main" java.util.InputMismatchException
//at java.base/java.util.Scanner.throwFor(Scanner.java:939)
//at java.base/java.util.Scanner.next(Scanner.java:1594)
//at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
//at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
//at Personal_Projects.AccountDriver.main(AccountDriver.java:107)
You are reading data from file "c:\books\trans.txt".
According to your code,
while(sc.hasNext()) {
int amt=sc.nextInt();
if(amt<0)
at.withdraw(amt);
else
at.deposit(amt);
}
You are only reading integers from the files, means files should be like this
1
2
3
4
And so on, no any string or comma or anything, if there is any other string or comma it will throw the exception. You can resolve the error by using sc.hasNextInt() in while loop.
** Solution**
while(sc.hasNextInt()) {
int amt=sc.nextInt();
if(amt<0)
at.withdraw(amt);
else
at.deposit(amt);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments