Have Some JAVA Problems. First Time User
/** * Banks. * A bank is a collection of BankAccounts indexed by account numbers. * */ public class Bank { // your fields go here private string Bank; /** * Constructor for objects of class Bank.
This topic was started by ShockaR,/**
* Banks.
* A bank is a collection of BankAccounts indexed by account numbers.
*
*/
public class Bank
{
// your fields go here
private string Bank;
/**
* Constructor for objects of class Bank.
* Newly created banks contain no accounts.
*/
public Bank()
{
// to be implemented
// initialise your fields here
}
/**
* Open a new bank account and return its account number.
* Account numbers are assigned to new accounts automatically:
* the first account created has account number "1", the second
* has account number "2", and so on.
*/
public String openNewAccount(String name, int initialBalance, int overdraftLimit)
{
// to be implemented
return ""; // replace this by something sensible
}
/**
* If the account exists and has a zero balance,
* remove it from the bank. If it does not exist, or
* does exist but has a non-zero balance, do nothing.
* Note that removing an account has NO effect on the
* account numbers of other accounts, or on the
* automatically allocated account numbers for new
* accounts.
*
* @param accountNumber the account number to remove
*/
public void closeAccount(String accountNumber)
{
// to be implemented
}
/**
* The bank account with the given account number, or null
* if no such account exists.
*
* @param accountNumber the account number
*/
public BankAccount getAccount(String accountNumber)
{
// to be implemented
return null; // replace this by something sensible
}
/**
* The value of the total debt of all overdrawn accounts.
* For example, when the bank contains four accounts, with balances
* of 99, -20, -230, 0 then this method returns 250.
*/
public int totalDebt()
{
// to be implemented
return -1; // replace this by something sensible
}
/**
* Print a list of the account numbers and account details
* for all accounts.
*/
public void printAccounts()
{
// not assessed
// but maybe useful for debugging purposes
}
/**
* The total number of accounts in this bank.
*/
public int size()
{
// to be implemented
return -1; // replace this by something sensible
}
}
Hi all. It's basically my first time i'm working on Java and I am already stuck! I have to implement methods & decide on an appropriate type of collection from the java.util package in, which individual bank accounts will be stored. I also have to declare a field to refer my collection.
I also have to create a field to keep track of the next available account number.
So far I know that all field in the classes should be Private and any method I add to the Bank class must also be private.
I've created my other classes so there fine, it's just this i'm stuck on.
Thanks in advance and don't worry it isn't a piece of coursework so i'm not cheating or anything ridiculous like that because at the end of the day I won't learn anything!
It's a project my cousin has sent me from his previous year at uni. I'm still at college and want to do Computer Science so I want to learn before I do it next year if you know what I mean.
Thank you again.
Participate on our website and join the conversation
This topic is archived. New comments cannot be posted and votes cannot be cast.
Responses to this topic
could you give an example what you want to do
and no .... methods do not have to be private ... the main goal of object oriented programming is information hiding ... means all data inside an object should be private and can only be modified by members of the class/object
now methods MUST be public in order to be able to communicate with the outside world
with your methods you can define how anyone can access the data inside an object
e.g. when you got 10$ on your account and you have something like
mike.getmoney(20);
you can use the public method getmoney to check the account and refusing to give out 20$ since mike only got 10$ left
and no .... methods do not have to be private ... the main goal of object oriented programming is information hiding ... means all data inside an object should be private and can only be modified by members of the class/object
now methods MUST be public in order to be able to communicate with the outside world
with your methods you can define how anyone can access the data inside an object
e.g. when you got 10$ on your account and you have something like
mike.getmoney(20);
you can use the public method getmoney to check the account and refusing to give out 20$ since mike only got 10$ left
Well this is what I have so far but i'm getting errors during compilation:
Bank Class
BankAccount Class
CashMachine Class
Bank Class
import java.util.Vector;
/**
* Banks.
* A bank is a collection of BankAccounts indexed by account numbers.
*
*/
public class Bank
{
private String bankName;
private Vector bankAccounts = new Vector ();
/**
* Constructor for objects of class Bank.
* Newly created banks contain no accounts.
*/
public Bank(String bankName)
{
this.bankName = bankName;
}
public String getBankName()
{
return bankName;
}
/**
* Open a new bank account and return its account number.
* Account numbers are assigned to new accounts automatically:
* the first account created has account number "1", the second
* has account number "2", and so on.
*/
public String openNewAccount(String ownerName, double initialBalance, double overdraftLimit)
{
Account newAccount = new Account(ownerName, initialBalance, overdraftLimit);
bankAccounts.add(newAccount);
return newAccount.getAccountNumber();
}
/**
* If the account exists and has a zero balance,
* remove it from the bank. If it does not exist, or
* does exist but has a non-zero balance, do nothing.
* Note that removing an account has NO effect on the
* account numbers of other accounts, or on the
* automatically allocated account numbers for new
* accounts.
*
* @param accountNumber the account number to remove
*/
public void closeAccount(String accountNumber)
{
for(int i = 0; i < bankAccounts.size(); i++)
{
if(bankAccounts.get(i).getAccountNumber().equals(accountNumber))
{
bankAccounts.remove(i);
break;
}
}
}
/**
* The bank account with the given account number, or null
* if no such account exists.
*
* @param accountNumber the account number
*/
public BankAccount getAccount(String accountNumber)
{
for(int i = 0; i < bankAccounts.size(); i++)
{
if(bankAccounts.get(i).getAccountNumber().equals(accountNumber))
return bankAccounts.get(i);
}
return null;
}
/**
* The value of the total debt of all overdrawn accounts.
* For example, when the bank contains four accounts, with balances
* of 99, -20, -230, 0 then this method returns 250.
*/
public double totalDebt()
{
double debt = 0;
for(int i = 0; i < bankAccounts.size(); i++)
{
if(bankAccounts.get(i).getBalance() < 0)
debt += bankAccounts.get(i).getBalance();
}
return Math.abs(debt);
}
/**
* Print a list of the account numbers and account details
* for all accounts.
*/
public void printAccounts()
{
for(int i = 0; i < bankAccounts.size(); i++)
{
System.out.println("Account Number: " + bankAccounts.get(i).getAccountNumber());
System.out.println("Owner: " + bankAccounts.get(i).getOwnerName());
System.out.println("Balance: " + bankAccounts.get(i).getBalance());
System.out.println("Overdraft Limit: " + bankAccounts.get(i).getOverdraftLimit());
System.out.println("............................................");
}
}
/**
* The total number of accounts in this bank.
*/
public int size()
{
return bankAccounts.size();
}
}]
BankAccount Class
/**
* Bank accounts with a current balance and an overdraft limit.
*
* For simplicity, both the balance and the overdraft limit are
* always a whole number of pounds. The balance is allowed to
* become negative.
*
*/
public class BankAccount
{
private static int accountCounter = 0;
private String ownerName;
private String accountNumber;
private double accountBalance;
private double overdraftLimit;
public String getOwnerName()
{
return ownerName;
}
public String getAccountNumber()
{
return accountNumber;
}
public double getBalance()
{
return accountBalance;
}
public double getOverdraftLimit()
{
return overdraftLimit;
}
Account(String ownerName, double accountBalance, double overdraftLimit)
{
this.ownerName = ownerName;
this.accountBalance = accountBalance;
this.overdraftLimit = overdraftLimit;
accountCounter++;
this.accountNumber = "" + accountCounter;
}
}
CashMachine Class
**
* Cash machines.
* Cash machines which can dispense twenty pound notes,
* ten pound notes and five pound notes.
*
*/
public class CashMachine
{
/** The bank. */
private Bank bank;
/** Number of twenty pound notes in machine. */
private int twenties;
/** Number of ten pound notes in machine. */
private int tens;
/** Number of five pound notes in machine. */
private int fives;
/** Was the last request for cash successful? */
private boolean lastRequestOK;
/**
* Constructor for objects of class CashMachine.
* Newly created machines contain no bank notes.
*
* @param bank the bank to which this machine is connected
*/
public CashMachine(Bank bank)
{
this.bank = bank;
twenties = 0;
tens = 0;
fives = 0;
lastRequestOK = true;
}
/**
* Add some twenty pound notes to the machine's reserves.
*
* @param n the number of notes to add
*/
public void addTwenties(int n)
{
twenties += n;
}
/**
* Add some ten pound notes to the machine's reserves.
*
* @param n the number of notes to add
*/
public void addTens(int n)
{
tens += n;
}
/**
* Add some five pound notes to the machine's reserves.
*
* @param n the number of notes to add
*/
public void addFives(int n)
{
fives += n;
}
/**
* If the given account exists, return its current balance,
* otherwise return 0.
*
* @param accountNumber the account number
*/
public int getUserBalance(String accountNumber)
{
BankAccount account = bank.getAccount(accountNumber);
// YOUR CODE GOES HERE
return -1; // replace this with something sensible
}
/**
* Request some cash.
* Only dispense cash if the account exists and contains
* sufficient funds (taking into account its overdraft limit)
* and if the exact amount requested is possible
* using the machine's current reserves of bank notes.
*
* Set lastRequestOK to indicate if the request was met.
*
* Print messages to standard output saying what notes have
* been dispensed or explaining why the request cannot be met.
*
* Update the bank note reserves and account balance accordingly.
*
* @param accountNumber the account number for this transaction
* @param amount the amount of cash requested
*/
public void request(String accountNumber, int amount)
{
lastRequestOK = false;
BankAccount account = bank.getAccount(accountNumber);
// check if bank account exists; print a message and return
// without dispensing any cash if it does not exist
// YOUR CODE GOES HERE
int availableFunds = -1; // replace this by something sensible
if (availableFunds < amount) {
System.out.println("You have insufficient funds.");
} else if (reserves() == 0) {
System.out.println("This machine is empty.");
} else if (reserves() < amount) {
System.out.println("This machine only contains " + reserves() + " pounds.");
} else {
int fivesToDispense = 0;
int tensToDispense = 0;
int twentiesToDispense = 0;
int remainder = amount;
twentiesToDispense = Math.min(twenties, remainder / 20);
remainder = remainder - twentiesToDispense * 20;
tensToDispense = Math.min(tens, remainder / 10);
remainder = remainder - tensToDispense * 10;
fivesToDispense = Math.min(fives, remainder / 5);
remainder = remainder - fivesToDispense * 5;
if (remainder == 0) {
// YOUR CODE GOES HERE
// withdraw the requested amount from the bank account
twenties = twenties - twentiesToDispense;
tens = tens - tensToDispense;
fives = fives - fivesToDispense;
System.out.print("Please take your money: ");
System.out.print(fivesToDispense + " fives, ");
System.out.print(tensToDispense + " tens, ");
System.out.print(twentiesToDispense + " twenties.");
System.out.println();
lastRequestOK = true;
} else {
System.out.println("This machine cannot dispense that amount with its current stock of banknotes.");
}
}
}
/**
* Was the last request for cash successful?
*/
public boolean getLastRequestOK()
{
return lastRequestOK;
}
/**
* The total value of bank notes in the machine.
*/
private int reserves()
{
return fives * 5 + tens * 10 + twenties * 20;
}
/**
* The number of twenty pound notes the machine currently contains.
*/
public int getTwentiesCount()
{
return twenties;
}
/**
* The number of ten pound notes the machine currently contains.
*/
public int getTensCount()
{
return tens;
}
/**
* The number of five pound notes the machine currently contains.
*/
public int getFivesCount()
{
return fives;
}
}