Hi im coding a project in JAVA and need some help with a few areas in my project
ID: 3692692 • Letter: H
Question
Hi im coding a project in JAVA and need some help with a few areas in my project. Ive commented out the areas that are unfinished so it should make sense. I've already finished the first class but I will import it for reference. Here is the first class:
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-=minus;
}
public int getNum() {
return num;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public String toString() {
return num + ", " + name + ", $" + balance;
}
}// end Account class
Here is the class that needs some work:
import java.util.*;
import java.io.*;
public class AccountApp {
public static void main(String[] args) throws IOException{
int k = 0;
String n = "";
double d = 0.0;
ArrayList<Account> list = new ArrayList<Account>();
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
Scanner file = new Scanner(new File (filename));
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': // print - find and display and account object
System.out.print("Enter account number: ");
int num = Integer.parseInt(s.nextLine());
Account a = find(list, num);
System.out.println(a);
break;
case 'd': // deposit money into an account
System.out.print("Enter account number: ");
num = Integer.parseInt(s.nextLine());
Account b = find(list, num);//Call 'find' method below to retrieve the account with entered account number
System.out.print("Enter deposit amount: ");
double dep = Double.parseDouble(s.nextLine());
b.deposit(dep);
break;
case 'w': // withdraw money from an account
System.out.print("Enter account number: ");
num = Integer.parseInt(s.nextLine());
Account c = find(list, num);
System.out.print("Enter withdrawal amount: ");
double with = Double.parseDouble(s.nextLine());
c.withdrawal(with);
break;
case 'l': // lookup - find an account and display using account name
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 object's name matches the name entered by user (ignore case)
//if found, print the number for that account
}
}
break;
} // end switch
} while (option != 'q');
} // end main
public static Account find(ArrayList<Account> accounts, int num) {
//YOU DO THIS
//loop through accounts, calling get to get each account (or using an Iterator)
//for each account, see if object's account number matches the number entered by user
//if found, return that account
//leave this at the very end of the method in case the account isn't found
return null;
} // end find
}// end AccountApp class
Explanation / Answer
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 -= minus;
}
public int getNum() {
return num;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public String toString() {
return num + ", " + name + ", $" + balance;
}
}// end Account class
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class AccountApp {
public static void main(String[] args) throws IOException {
int k = 0;
String n = "";
double d = 0.0;
ArrayList<Account> list = new ArrayList<Account>();
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
Scanner file = new Scanner(new File(filename));
while (file.hasNext()) {
String line = file.nextLine();
Scanner lineScan = new Scanner(line);
Account account = new Account(lineScan.nextInt(), lineScan.next(),
lineScan.nextDouble());
list.add(account);
}
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': // print - find and display and account object
System.out.print("Enter account number: ");
int num = Integer.parseInt(s.nextLine());
Account a = find(list, num);
System.out.println(a);
break;
case 'd': // deposit money into an account
System.out.print("Enter account number: ");
num = Integer.parseInt(s.nextLine());
Account b = find(list, num);// Call 'find' method below to
// retrieve the account with entered
// account number
System.out.print("Enter deposit amount: ");
double dep = Double.parseDouble(s.nextLine());
b.deposit(dep);
break;
case 'w': // withdraw money from an account
System.out.print("Enter account number: ");
num = Integer.parseInt(s.nextLine());
Account c = find(list, num);
System.out.print("Enter withdrawal amount: ");
double with = Double.parseDouble(s.nextLine());
c.withdrawal(with);
break;
case 'l': // lookup - find an account and display using account name
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 object's name matches the name
// entered by user (ignore case)
// if found, print the number for that account
Iterator<Account> iterator = list.iterator();
boolean flag = true;
while (iterator.hasNext()) {
Account account = iterator.next();
if (account.getName().equalsIgnoreCase(name)) {
System.out.println("Account Number :"
+ account.getNum());
flag = false;
break;
}
}
if (flag) {
System.out
.println("Account Number is not found for that name");
}
break;
}
} while (option != 'q');
} // end main
public static Account find(ArrayList<Account> accounts, int num) {
// YOU DO THIS
// loop through accounts, calling get to get each account (or using an
// Iterator)
// for each account, see if object's account number matches the number
// entered by user
// if found, return that account
// leave this at the very end of the method in case the account isn't
// found
Iterator<Account> iterator = accounts.iterator();
while (iterator.hasNext()) {
Account account = iterator.next();
if (account.getNum() == num)
return account;
}
return null;
} // end find
}// end AccountApp class
accounts.txt
123 srinivas 2345.54
124 rajesh 23456.45
125 pavan 32345.34
126 kishore 23415.43
127 rajesh 123124.32
128 anil 12312.43
129 srinu 12342.21
130 ravi 12123.56
OUTPUT:
Enter the name of the input file: accounts.txt
Enter (p)rint, (d)eposit, (w)ithdrawal, (l)ookup, or (q)uit: p
Enter account number: 123
123, srinivas, $2345.54
Enter (p)rint, (d)eposit, (w)ithdrawal, (l)ookup, or (q)uit: d
Enter account number: 123
Enter deposit amount: 435
Enter (p)rint, (d)eposit, (w)ithdrawal, (l)ookup, or (q)uit: w
Enter account number: 123
Enter withdrawal amount: 56
Enter (p)rint, (d)eposit, (w)ithdrawal, (l)ookup, or (q)uit: l
Enter name: srinu
Account Number :129
Enter (p)rint, (d)eposit, (w)ithdrawal, (l)ookup, or (q)uit: l
Enter name: chakri
Account Number is not found for that name
Enter (p)rint, (d)eposit, (w)ithdrawal, (l)ookup, or (q)uit: q
NOTE:
i cannot enhanced the deposit and withdraw functionalities, because of not commented
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.