Help me minimize errors import java.io.*; import java.util.*; public class TheBa
ID: 3701558 • Letter: H
Question
Help me minimize errors
import java.io.*;
import java.util.*;
public class TheBank{
public static void main (String [] args) throws FileNotFoundException {
//Program Introduction
bankIntro();
//Load Accounts from file
BankAccount [] theBranch = loadAccounts();
// Manage transactions
transactions(theBranch);
// Sort array of accounts
sort(theBranch);
//Output updated accounts to file
outprint(theBranch);
// Sort array of accounts
newSort();
// Output accounts to screen
System.out.println("C L I E N T A C C O U N T S");
for (int i = 0; i < theBranch.length; i++){
System.out.print(theBranch[i].getOwnerName() + " " + theBranch[i].getBalance() + " ");
}
}
public static void bankIntro() {
System.out.println("T R A N S A C T I O N U P D A T I N G");
System.out.println(" ");
}
public static BankAccount[] loadAccounts() throws FileNotFoundException {
//read File
Scanner clientIn = new Scanner(new File("AccountBackup.dat"));
//make an array as large as largest possible number of clients
BankAccount [] clientData = new BankAccount[1000];
// input all accounts from file & count the number
int number = 0;
while (clientIn.hasNextLine()) {
clientData[number] = new BankAccount();
clientData[number].setOwnerName(clientIn.next());
clientData[number].setAccountNumber(clientIn.nextInt());
clientData[number].setBalance(clientIn.nextDouble());
System.out.println(clientData[number]);
number++;
}
// copy the client data to an array that is the right size, called returningClients
BankAccount[] returningClients = new BankAccount[number];
for (int i = 0; i < number; i++) {
returningClients[i] = clientData[i];
}
return returningClients;
}
public static BankAccount[] transactions(BankAccount [] theBranch)
throws FileNotFoundException{
Scanner inFile = new Scanner (new File("TransactionsJanuary.dat"));
//read the file line by line
while(inFile.hasNextLine()){
//assign each value to each attribute
String name = inFile.next();
int accountMatch = inFile.nextDouble();
boolean isFound = false;
//adjust the array based on the info in the file
for(int i = 0; i < theBranch.length;i++){
//match the right account
if (accountMatch == theBranch[i].getAccountNumber() && name.equals(theBranch[i].getOwnerName())){
//check if it should be + or -
isFound = true;
if(change.equals("charge") || change.equals("withdrawal")){
theBranch[i].withdrawal(numberChange);
}else if (change.equals("interest")){
theBranch[i].deposit(numberChange/100*theBranch[i],getBalance());
}else{
theBranch[i].deposit(numberChange);
}
}
}
//check error and print into a file
if(!isFound) {
PrintStream out = new PrintStream(new File("error.dat"));
out.println("ERROR: Account/name mismatch: "+ name +" "+ accountMatch);
out.close();
}
}
return theBranch;
}
public static BankAccount[] sort(BankAccount [] theBranch){
for (int i = 0; i < theBranch.length; i++) {
//first element is assumed (initially) to be minimum
int locationMin = i;
int min = theBranch[i].getAccountNumber();
//now find actual minimum
for(int j = i ; j < theBranch.length; j++){
if (theBranch[j].getAccountNumber() < min) {
locationMin = j;
min = theBranch[j].getAccountNumber();
}
}
if (locationMin !=i) {
//swap
BankAccount swap = theBranch[i];
theBranch[i] = theBranch[locationMin];
theBranch[locationMin] = swap;
}
}
return theBranch;
}
public static void outprint(BankAccount [] theBranch)throws FileNotFoundException{
// create a file
PrintStream out = new PrintStream(new File("TransactionFebruary.dat"));
//out put the bank array
for (int i = 0; i < theBranch.length; i++) {
out.println(theBranch[i].toString());
}
}
}
Explanation / Answer
// Bankaccount.java : containing BankAccount class
public class BankAccount {
private String ownerName;
private int accountNumber;
private double balance;
public BankAccount()
{
ownerName = "";
accountNumber=0;
balance=0;
}
public BankAccount(String owner, int account, double initial_balance)
{
ownerName = owner;
accountNumber = account;
balance = initial_balance;
}
public void setOwnerName(String owner)
{
ownerName = owner;
}
public void setAccountNumber(int account)
{
accountNumber = account;
}
public void setBalance(double balance)
{
this.balance = balance;
}
public String getOwnerName()
{
return ownerName;
}
public int getAccountNumber()
{
return accountNumber;
}
public double getBalance()
{
return balance;
}
public void withdrawal(double amount)
{
if(amount <= balance)
balance = balance - amount;
else
System.out.println("Insufficient funds");
}
public void deposit(double amount)
{
if(amount > 0)
balance = balance + amount;
else
System.out.println("Deposit amount cannot be negative");
}
public String toString()
{
return(" Owner name : "+ownerName+" Account number : "+accountNumber+" Balance : "+balance);
}
}
//end of BankAccount
// TheBank.java : containing TheBank class and using BankAccount class
import java.io.*;
import java.util.*;
public class TheBank {
public static void main(String[] args) throws FileNotFoundException {
//Program Introduction
bankIntro();
//Load Accounts from file
BankAccount [] theBranch = loadAccounts();
// Manage transactions
transactions(theBranch);
// Sort array of accounts
sort(theBranch);
//Output updated accounts to file
outprint(theBranch);
// Sort array of accounts
//newSort();
sort(theBranch);
// Output accounts to screen
System.out.println("C L I E N T A C C O U N T S");
for (int i = 0; i < theBranch.length; i++){
System.out.print(theBranch[i].getOwnerName() + " " + theBranch[i].getBalance() + " ");
}
}
public static void bankIntro() {
System.out.println("T R A N S A C T I O N U P D A T I N G");
System.out.println(" ");
}
public static BankAccount[] loadAccounts() throws FileNotFoundException {
//read File
Scanner clientIn = new Scanner(new File("AccountBackup.dat"));
//make an array as large as largest possible number of clients
BankAccount [] clientData = new BankAccount[1000];
// input all accounts from file & count the number
int number = 0;
while (clientIn.hasNextLine()) {
clientData[number] = new BankAccount();
clientData[number].setOwnerName(clientIn.next());
clientData[number].setAccountNumber(clientIn.nextInt());
clientData[number].setBalance(clientIn.nextDouble());
System.out.println(clientData[number]);
number++;
}
// copy the client data to an array that is the right size, called returningClients
BankAccount[] returningClients = new BankAccount[number];
for (int i = 0; i < number; i++) {
returningClients[i] = clientData[i];
}
return returningClients;
}
public static BankAccount[] transactions(BankAccount [] theBranch)
throws FileNotFoundException{
Scanner inFile = new Scanner (new File("TransactionsJanuary.dat"));
//read the file line by line
while(inFile.hasNextLine()){
//assign each value to each attribute
String name = inFile.next();
int accountMatch = inFile.nextInt();
boolean isFound = false;
//adjust the array based on the info in the file
for(int i = 0; i < theBranch.length;i++){
//match the right account
if (accountMatch == theBranch[i].getAccountNumber() && name.equals(theBranch[i].getOwnerName())){
//check if it should be + or -
String change = inFile.next();
double numberChange = inFile.nextDouble();
isFound = true;
if(change.equalsIgnoreCase("charge") || change.equalsIgnoreCase("withdrawal")){
theBranch[i].withdrawal(numberChange);
}else if (change.equalsIgnoreCase("interest")){
theBranch[i].deposit((numberChange/100)*theBranch[i].getBalance());
}else{
theBranch[i].deposit(numberChange);
}
}
}
//check error and print into a file
if(!isFound) {
PrintStream out = new PrintStream(new File("error.dat"));
out.println("ERROR: Account/name mismatch: "+ name +" "+ accountMatch);
out.close();
}
}
return theBranch;
}
public static BankAccount[] sort(BankAccount [] theBranch){
for (int i = 0; i < theBranch.length; i++) {
//first element is assumed (initially) to be minimum
int locationMin = i;
int min = theBranch[i].getAccountNumber();
//now find actual minimum
for(int j = i ; j < theBranch.length; j++){
if (theBranch[j].getAccountNumber() < min) {
locationMin = j;
min = theBranch[j].getAccountNumber();
}
}
if (locationMin !=i) {
//swap
BankAccount swap = theBranch[i];
theBranch[i] = theBranch[locationMin];
theBranch[locationMin] = swap;
}
}
return theBranch;
}
public static void outprint(BankAccount [] theBranch)throws FileNotFoundException{
// create a file
PrintStream out = new PrintStream(new File("TransactionFebruary.dat"));
//out put the bank array
for (int i = 0; i < theBranch.length; i++) {
out.println(theBranch[i].toString());
}
}
}
//end of TheBank class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.