hi pple
I have one more request and it goes like this
in both of my assignment I and assignment II, the last part of the instruction states that
The balance should reflect all changes on one execution of the program.
Example if id entered was 2 and a withdrawal of 30 was made then a deposit of 100, at the end
of the program if balance was checked, it should reflect 120.
hoiw can i get my calculation to do just as what it says in the statement above
how can i accomplish that.
here are my codes.
her is the main part of the program, this is assignment I and will have to do the same for assignment II
import java.text.DecimalFormat;
import java.util.Scanner;
public class ATM {
DecimalFormat df = new DecimalFormat("#.00");
public static void main(String[] args) {
createAccount();
}
public static void createAccount()
{
Scanner input = new Scanner(System.in);
int i = 0;
double bal= 50.0;
//creation of the accounts
Account[] acc = new Account[10];
Person[] p = new Person[i++];
//gathering the user information
for(i=0; i<acc.length; i++){
System.out.println("Enter first name");
String fn = input.next();
System.out.println("Enter last name");
String ln = input.next();
System.out.println("Enter address");
String ad = input.next();
System.out.println("Enter contact");
String c = input.next();
//this object hold the information for each user
Person person = new Person(fn, ln, ad, c);
acc[i]= new Account(i, 50, person);//creating a new account for all of the account
}
while(true)
{
//asking the user to enter their id
System.out.println("Enter your id");
int id = input.nextInt();
//this will break out of the loop once the user enter the last transaction
if(id == -1)
{
break;
}
while(id < 0 || id > 9)
{
//letting the user know that the wrong id was entered
System.out.println("id is incorect, try again");
id = input.nextInt();
}
while(true)
{
displayMenu();
if(!useMenu(acc[id],input))
{
System.out.println();
break;
}
System.out.println("\n");
}
}
}
//construction that holds the account and a sccanner input instead of importing another scanner methods to read input
public static boolean useMenu(Account acc, Scanner input)
{
int userchoice = input.nextInt();
double amt=0;
//this is to let the user know if they enter choice 5 it would return a false value
if(userchoice == 5)
{
return false;
}
else if(userchoice == 1)
{
//user indicating that they are accessing to view their balance
System.out.println("The account balance is: "+ acc.getbalance());
return true;
}
else if(userchoice == 2)
{
//user asked for the amount to withdraw
System.out.println("Enter the amount to withdraw");
amt = input.nextDouble();//this hold the amount the user enter
// if the amt in the account is greater than what it has
if(amt > acc.getbalance())
{
//acknowledging that there is insufficient amount and getting the balance
System.out.println("Insufficient amount "+ " Balance is: "+acc.getbalance());
}
else
{
//letting the user know that there is enough to be withdrawn
acc.withdraw(amt);
System.out.println("Balance is: "+ acc.getbalance());
}
}
else if(userchoice == 3)
{
//asking the user what amt to be deposited
System.out.println("Enter the amount to deposit");
acc.deposit(input.nextDouble());//hold the amt that was deposited
acc.deposit(amt);
System.out.println("Balance is "+ acc.getbalance());//printing out the balance after deposited the amt
return true;
}
else if(userchoice == 4)
{
//gathering the owner information after all transaction taken place
System.out.println(acc.getowner().toString());
}
else
{
//let the user that they entered an invalid choice
System.out.println("invalid choice");
}
return true;
}
public static void displayMenu()
{
//giving the user the option to choose from
System.out.println("Welcome to the Automated Teller Machine!\n");
System.out.println("Select from the following menu option below:\n");
System.out.println("===================================");
System.out.println("| Main Menu |");
System.out.println("===================================");
System.out.println("| [1] Check Balance |");
System.out.println("| [2] Withdraw |");
System.out.println("| [3] Deposit |");
System.out.println("| [4] Display User Information |");
System.out.println("| [5] Exist |");
System.out.println("===================================");
System.out.println("Please select your choice now!!");
}
}
this is the account class
public class Account
{
private int ID;// this is the id variable
private double balance;
private Person owner;
public Account()
{
//no arg constructor
}
//constructor method with
public Account(int ID, double balance, Person owner)
{
this.ID = ID;
this.balance = balance;
this.owner = owner;
}
public int getID()
{
return ID;
}
public double getbalance()
{
return balance;
}
public Person getowner()
{
return owner;
}
public void setID(int ID)
{
this.ID = ID;
}
public void setbalance(double balance)
{
this.balance = balance;
}
public void setowner(Person owner)
{
this.owner = owner;
}
public void deposit(double amt)
{
balance = balance + amt;
}
public void withdraw(double amt)
{
balance = balance - amt;
}
@Override
public String toString()
{
String getdetails =
"/nID: "+ID+" "
+ "\nBalance: "+balance;
return getdetails;
}
}
here is the person class
public class Person {
private String FirstName;
private String LastName;
private String address;
private String contact;
public Person(String f, String l, String a, String c) {
FirstName = f;
LastName = l;
address = a;
contact = c;
}
//get first name
public String getFirstName() {
return FirstName;
}
//get last name
public String getLastName() {
return LastName;
}
//get address
public String getaddress() {
return address;
}
//get telephone number
public String getcontact() {
return contact;
}
//accessor method with the person name and with parameters
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
//accessor method with the person last name with parameters
public void setLastName(String LastName) {
this.LastName = LastName;
}
//accessor method the person address with parameters
public void setaddress(String address) {
this.address = address;
}
//accessor method that take one parameter
public void setcontact(String contact) {
this.contact = contact;
}
@Override
public String toString() {
String getdetails =
"\nFirst Name " + FirstName + " "
+ "\nLast Name " + LastName + " "
+ "\nAddress " + address + " "
+ "\nContact " + contact;
return getdetails;
}
}


