Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Chapter 3 of Deitel’s textbook “Java: How to Program 10th Edition”, the examp

ID: 3668067 • Letter: I

Question

In Chapter 3 of Deitel’s textbook “Java: How to Program 10th Edition”, the example program in Fig 3.5 and Fig. 3.6 implements a simple Bank Account program. Using Deitel’s code as a starting point, add a withdraw(amount) method to the Account class that lets users withdraw money from their account. Add additional methods to the classes in your program to facilitate an interactive dialog with the user. To make your program easier for the user to run, output a menu-type dialog where you can interact with the user as follows: Select an account #: Choose from the options below: 1. Print account balance 2. Make a deposit 3. Make a withdrawal 4. Exit Choice: Obviously, when 1 is selected the account balance is printed. If 2 or 3 is selected, a second prompt must be given to the user to enter the amount. Enter amount: Create a unique account identifier (ID) for each account instance that uniquely identifies the account. In class we found that a static integer data member in the class was useful for storing the account number for the next instance. (Note, we also found it helpful to create methods to process deposits and withdrawals using methods of the class rather than hard code them into the main method as Deitel does in his book. Build your program by adding methods to the Account class that the main() method can call to do the work. Likewise, add “helper” methods to the AccountTest class to make the work of writing the program more “modular” and thus resuable.) Since users should be able to perform multiple transactions in one session, you’ll need to incorporate a loop in your program (much like you did for the last phase of the Quadratic programming assignment) to allow the user to make several transactions on several accounts. Use an Array to store five (5) account objects that are to be loaded from a text file on startup. You’ll need to save each object’s data to the file when the program exits. File I/O will be covered in class in order to demonstrate how data can be maintained in between executions of your program.

This is what I have so far.

Account class
public class Account {
    private int accNumber;
    private double balance;

    public Account(double initialBalance, int accNo) {
        balance = initialBalance;
        accNumber = accNo;
    }

    public void deposit(double amount) {
        balance += amount;
    }

    public double withdraw(double amount) {
        balance -= amount;
        return amount;
    }

    public double getBalance() {
        return balance;
    }

    public int getAccNumber() {
        return accNumber;
    }
}

AccountTest
import java.io.*;
import java.util.Scanner;

public class AccountT
{
static Account[] account = new Account[5];

public static void main (String[] args)
{
/* account[0] = new Account("Jane Green", 50.00);
   account[1] = new Account("John Blue", 12.00);
   account[2] = new Account("George Smith",100.00);
   account[3] = new Account("Janet Paulson", 134.00);
   account[4] = new Account("Justin Wilson", 10.95);
   */
   loadAccounts();
   printAccounts();
   userDialog();
   saveAccounts();
}

static void loadAccounts()
{
   Scanner file = new Scanner(new FileInputStream("accounts.txt"));
}

static void printAccounts()
{
   for (int i = 0; i < account.length; ++i)
      System.out.println("ID = " + account[i].getID() +
                        " Owner = " + account[i].getName() +
                        "Balance = " + account[i].getBalance());
   }
  
   static void userDialog()
   {
      Scanner input = new Scanner(System.in);
      int index = 1, choice = 1;
      while (index !=0)
      {
         System.out.println("Select Acct# (1-%d): ", account.length);
         index = input.nextInt();
         while (1 <= choice && choice <= 3)
         { choice = Account.printMenu(input);
               switch (choice)
               {
               case 1: account[index-1].printBalance();
                  break;
               case 2: account[index-1].processDeposit();
                  break;
               case 3: account[index-1].processWithdrawl();
               }
         }
                  choice = 1;
       }
           
          
    }
     static void saveAccounts()throws FileNotFoundException
      {
         PrintWriter file = new PrintWriter(new fileOutputStream("accounts.txt"));
         file.println(account.length);
         for (int i = 0; i < account.lenght; ++i)
         file.write(account[i].getID() + " " + account[i].getName() + account[i].getBalance() + " ");
         file.close();
      }
   }

Explanation / Answer

Class Account

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package AccountTest;

/**
*
* @author Pelusa
*/
public class Account {
private int accNumber;
private double balance;
private String owner; //the person that create the account
  
public Account(double initialBalance, int accNo,String person) {
balance = initialBalance;
accNumber = accNo;
owner = person;
}
public void deposit(double amount) {
balance += amount;
}
public double withdraw(double amount) {
balance -= amount;
return amount;
}
public double getBalance() {
return balance;
}
public int getAccNumber() {
return accNumber;
}

public String getOwner() {
return owner;
}
  
public String printMenu (int index){
  
String choice = null;
  
  
  
return choice;
  
}
}

Class AccountT

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package AccountTest;

/**
*
* @author Pelusa
*/
import java.io.*;
import java.util.Scanner;
public class AccountT
{
static Account[] account = new Account[5];

public static void main (String[] args) throws FileNotFoundException, IOException
{
account[0] = new Account(225.5, 50,"Linda Freshman");
account[1] = new Account(200.89, 12,"John Blue");
account[2] = new Account(900.2, 1486,"Stephen King");
account[3] = new Account(32512.9, 45624,"Paul Jefferson");
account[4] = new Account(3452.6, 566624,"Nicolas Kardashian");
// loadAccounts();
printAccounts();
userDialog();
saveAccounts();
}

static void loadAccounts() throws FileNotFoundException
{
Scanner file = new Scanner(new File("C:\Users\Pelusa\Desktop\accounts.txt"));

}

static void printAccounts()
{
int i=0;
while (i<account.length){
System.out.println("ID = " + account[i].getAccNumber() +
" Owner = " + account[i].getOwner() +
"Balance = " + account[i].getBalance());
i = i+1;
}

}
  
  
static void userDialog() throws IOException
{
Scanner input = new Scanner(System.in);
int index = 1;
double amount;
while (index !=0)
{
System.out.println("Select an account");
index = input.nextInt();
System.out.println("Your account number is:");
System.out.println(account[index].getAccNumber());
System.out.println("Select Acct");
System.out.println("1. Verify your balance");
System.out.println("2. Make a deposit");
System.out.println("3. Make a withdraw ");
System.out.println("4. Make a transfer ");
  
int choice;
choice= input.nextInt();
switch (choice)
{
case 1: System.out.println("Your balance is: ");
System.out.println(account[index].getBalance());
break;
case 2: System.out.println("Enter the amount to deposit ");
amount=input.nextDouble();
account[index].deposit(amount);
System.out.println("Your transaction has been realized ");
break;
case 3: System.out.println("Enter the amount to withdraw ");
amount=input.nextDouble();
if ((amount) >=(account[index].getBalance())){
System.out.println("Sorry, you have insuficents founds ");
}else{
account[index].deposit(amount);account[index].withdraw(amount);
System.out.println("Your transaction has been realized ");
}
break;
case 4:
System.out.println("Enter the amount to transfer ");
amount=input.nextDouble();
;account[index].withdraw(amount);
System.out.println(account[index].getBalance());
System.out.println("Select an account to transfer");
printAccounts();
index = input.nextInt();
account[index].deposit(amount);
System.out.println(account[index].getBalance());

System.out.println("Your transaction has been realized ");
break;
}


}



}
static void saveAccounts()throws FileNotFoundException
{
try
{
PrintWriter pr = new PrintWriter("C:\Users\Pelusa\Desktop\accounts.txt");

for (int i=0; i<account.length ; i++)
{
pr.println(account[i].getAccNumber());
pr.println(account[i].getBalance());
pr.println(account[i].getOwner());
}
pr.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("No such file exists.");
}
}
}

The project works fine but i have some problems with the load of the file. I'm going to try solve this problem today.

If you have a problem or a comment leave me a message

PD: You have to edit the paths of files and you have that create the file then the program will edit the file