Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.*; public class Account { private int num; private String name; private double balance; public Account(int n, String na, double b) { num =

import java.util.*;

public class Account { private int num; private String name; private double balance; public Account(int n, String na, double b) { num = n; name = na; balance = b; } public void deposit(double add) { balance+=add; } public void withdrawal(double minus) { balance-=add; } public int getNum() { return num; } public String getName() { return name; } public double getBalance() { return balance; } public String toString() { return num + ", " + name + ", $" + balance; } }

--------_--------_---------- --_--------------_-----------

Account .java

import java.util.*; import java.io.*;

public class Lab10 { public static void main(String[] args) { ArrayList list = new ArrayList(); Scanner s = new Scanner(System.in); System.out.print("Enter the name of the input file: "); String filename = s.nextLine(); //YOU DO THIS: //Open the file in the filename variable //Parse each line, create an Account object, and add it to the list char option; do { System.out.print("Enter (p)rint, (d)eposit, (w)ithdrawal, (l)ookup, or (q)uit: "); option = (s.nextLine()).charAt(0); switch (option) { case 'p': System.out.print("Enter acount number: "); int num = Integer.parseInt(s.nextLine()); Account a = find(list, num); System.out.println(a); break; case 'd': System.out.print("Enter account number: "); num = Integer.parseInt(s.nextLine()); //YOU DO THIS: //Call find to get the account with that number System.out.print("Enter deposit amount: "); double dep = Double.parseDouble(s.nextLine()); //YOU DO THIS: //Call deposit on the account you got back to deposit dep break; case 'w': System.out.print("Enter account number: "); num = Integer.parseInt(s.nextLine()); //YOU DO THIS: //Call find to get the account with that number System.out.print("Enter withdrawal amount: "); double with = Double.parseDouble(s.nextLine()); //YOU DO THIS: //Call deposit on the account you got back to withdraw with break; case 'l': System.out.print("Enter name: "); String name = s.nextLine(); //YOU DO THIS: //Loop through each account, calling get to get each one (or using an Iterator) //for each account, see if its name matches the name from the user //if so, print the number for that account break; } } while (option != 'q'); } public static Account find(ArrayList accounts, int num) { //YOU DO THIS //loop through accounts, calling get to get each account (or using an Iterator) //for each account, see if its number matches num //if so, return that account //leave this at the very end of the method in case the account isn't found return null; } }

--------_--------_---------- --_--------------_-----------

write a program that handles bank accounts. Each bank account will have an account number, name, and balance. You will support deposits and withdrawals to accounts. To make things easy for the lab, you will allow the balance to go negative.

Download the input file bank.txtimage text in transcribedimage text in transcribed. Each line has the information for a single account (account number, name, balance)

Download Lab10.zipimage text in transcribedimage text in transcribed, which contains two classes: Account and Lab10. If you are using Eclipse, create a new project with two new classes (Account and Lab10), and copy the text from the downloaded files into the new classes.

The Account class is already finished. Read through it to see what it does.

The Lab10 class is partially complete. You still need to:

Finish the find method to look up an account by number. There are comments in this method telling you what to do.

At the top of the main method, read the bank.txt file. Create a new Account object for each line in the file, and add it to the ArrayList.

Finish the deposit, withdrawal, and lookup functionality (in the switch in main).

Test your program. What happens if you give the program bad input (say, a string when it is expecting an integer)? What happens if you try to read from the file "banks.txt" (not the right name) instead?

Add try/catch blocks to your program to handle the following:

If the file the user provides does not exist, your program should print an error and exit

If the user provides bad input for the account number (i.e., not an int), amount to deposit, or amount to withdraw (not a double), your program should print an error and NOT perform that command (but keep looping and ask for the next menu option).

You may assume that there won't be any other errors in the program (such as the input file having the wrong format), but if you want to handle other problems then that's great.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions

Question

In what ways do lenders and investors lend legitimacy to a firm?

Answered: 1 week ago