We are building a bank program, but the professor only gave us a small powerpoin
ID: 3673966 • Letter: W
Question
We are building a bank program, but the professor only gave us a small powerpoint, and i have been unable to learn it online. Specifically i need help with the Delete Account method right now, as i have Add method working and i can work on the other stuff later. This program just takes an array and adds or deletes to the text file im pretty sure. Thanks in advance for any help!
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
public class Richard1155318
{
String acctName;
int acctNum, acctBal;
private ArrayList<Accounts> bankAccounts = new ArrayList<Accounts>();
//CHECK account balance
public void balance()
{
}
//DEPOSIT money
public void deposit()
{
}
//WITHDRAW money
public void withdraw()
{
}
//ADD a new account
public void add()
{
//Gather Info
acctName = JOptionPane.showInputDialog("Please enter the name for the account:");
acctNum = Integer.parseInt(JOptionPane.showInputDialog("Please enter the number for the account:"));
acctBal = Integer.parseInt(JOptionPane.showInputDialog("Please enter the initial deposit for the account:"));
//Append to file
try
{
FileWriter fw = new FileWriter("bank.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(acctName + "," + acctNum + "," + acctBal + " ");
bw.close();
JOptionPane.showMessageDialog(null, "Account has been added.", "Success!", JOptionPane.PLAIN_MESSAGE);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
//DELETE account
public void delete()
{
//Gather account# to be deleted
acctNum = Integer.parseInt(JOptionPane.showInputDialog("Please enter your account#:"));
if(acct found)
{
//Rewrite file
try
{
FileWriter fw = new FileWriter("bank.txt", false);
BufferedWriter bw = new BufferedWriter(fw);
for (Accounts rewriteFile : bankAccounts)
{
bw.write(rewriteFile.getName() + "," + rewriteFile.getNum() + "," + rewriteFile.getBal() + " ");
System.out.println(rewriteFile);
}
bw.close();
JOptionPane.showMessageDialog(null, "Your account has been deleted.", "Success!", JOptionPane.PLAIN_MESSAG);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
)
else
{
JOptionPane.showMessageDialog(null, "Account not found.", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
public static void main (String[] args)
{
boolean var = false;
String choice;
do
{
Richard1155318 account = new Richard1155318();
//Choose transaction type
choice = JOptionPane.showInputDialog("Please choose an option: 1. Check Balance 2. Deposit 3. Withdraw 4. Add Account 5. Delete Account #. Quit");
if(choice.equals("1"))
{
account.balance();
}
else if(choice.equals("2"))
{
account.deposit();
}
else if(choice.equals("3"))
{
account.withdraw();
}
else if(choice.equals("4"))
{
account.add();
}
else if(choice.equals("5"))
{
account.delete();
}
else if(choice.equals("#"))
{
var = true;
JOptionPane.showMessageDialog(null, "Have a nice day!", "Thank You", JOptionPane.PLAIN_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "There was an error, please try again.", "Error", JOptionPane.ERROR_MESSAGE);
}
} while(var == false);
}
}
Explanation / Answer
hi, I have written your delete method. You can replace your delete method with this
public void delete()
{
//Gather account# to be deleted
acctNum = Integer.parseInt(JOptionPane.showInputDialog("Please enter your account#:")); //Rewrite file
try
{
// declaring input output files
File inputFile = new File("bank.txt");
File tempFile = new File("myTempFile.txt");
BufferedReader reader = new BufferedReader(new FileReader());
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = String.valueOf(acctNum); // converting account into String
String currentLine;
boolean flag = false; // to check account was in file or not
while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if(trimmedLine.contains(lineToRemove)) {// if current line contains account to be deleted
continue; // then do not write to temp file
flag = true ;
}
writer.write(currentLine);
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(inputFile); // renaming temp file to "bank.txt"
if(flag == false){// did not found account
JOptionPane.showMessageDialog(null, "Account not found.", "ERROR", JOptionPane.ERROR_MESSAGE);
}else // acount found
JOptionPane.showMessageDialog(null, "Your account has been deleted.", "Success!", JOptionPane.PLAIN_MESSAG);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.