CS2514 Lab 2
Exercise
You will be writing a class definition called ATM
, which represents simplified
ATMs (automated teller machines, i.e. hole-in-the-wall cash dispensers) for an international bank.
Machines in different countries will be loaded with banknotes of different denominations depending on the
currency of the country.
Here's some 'client' code:
int[] euroDenoms = {5, 10, 20, 50}; // denominations, i.e. what kinds of notes we use ATM atm1 = new ATM("euro", euroDenoms); // create an ATM int[] kronurDenoms = {1000, 5000, 500, 2000, 10000}; // note: don't assume the notes are in a particular order ATM atm2 = new ATM("Icelandic kronur", kronurDenoms); // create another ATM for a different currency atm1.load(1, 5); // load it with 1 x 5 euro notes atm1.load(3, 20); // load it with 3 x 20 euro notes atm1.load(2, 50); // load it with 2 x 50 euro notes System.out.println(atm1.getQty(20)); // print how many 20 euro notes the ATM contains System.out.println(atm1); // display the contents atm1.dispense(1, 50); // dispense 1 x 50 euro note to a customer System.out.println(atm1.getTotal() + " " + atm1.getCurrency()); // print total and currency
From the above you will realise we need one constructor and six instance methods. A few words about some of the less obvious methods:
public String toString()
ThisString
that this method returns shows how many of each note the ATM contains, e.g.1 x euro5; 1 x euro10; 0 x euro20; 1 x euro50;
There's no need to ensure that the denominations are in ascending order, even though they are here.public int getQty(int denom)
If the parameter is an illegal denomination, then the return value should be zero.public void load(int qty, int denom)
Simply assume that the first parameter is a positive integer. (Don't bother to check it.)
But, if the first parameter is an illegal denomination, then no update should occur. (Just don't update: no need to print an error message.)public void dispense(int qty, int denom)
Simply assume that the first parameter is a positive integer. (Don't bother to check it.)
But, if the first parameter is an illegal denomination, or if there are not enough notes of the right denomination, then no update should occur. (Just don't update: no need to print an error message.)
You'll want to write a main
method, e.g. in a file called ATMTester.java
,
to test your ATM
class. And, if you've any
sense at all, you'll use it to try out more than the few things in the 'client' code shown above.
Submission
Deadline: 4pm, Friday 5th February 2016.
Put both ATM.java
and ATMTester.java
into a directory called lab2
.
To submit:
- Open the Dolphn File Manager. Right-click on your
lab2
directory. Choose Compress from the menu and As Zip File from the sub-menu. You should see a new icon appear, namedlab2.zip
. - Open a console; use the cd command to move to the directory that contains
lab2.zip
and type:submit-2514 lab2.zip
Challenge exercise
Remember that challenge exercises are always optional. They do not form part of your year's work and they are not worth any marks. They are designed for those students who finish the main exercise quickly and easily, and wish to explore further. You may need to use things not covered so far in lectures.
When you are convinced that your ATM program is superb (correct, efficient, elegant), make a copy
of it in ATMChallenge.java
. Modify this copy by adding an
additional dispense
method (yes, you can have two with the same name), as
follows:
public int[] dispense(int amount)
This method is used to dispense a specified amount of money from this ATM, if possible, using a 'small' number of notes.
If it can dispense the requested amount of money exactly, it updates the ATM and returns an array: each value in this array is the number of notes of the corresponding denomination that is being dispensed.
If the given amount cannot be exactly dispensed using the notes that are currently in this ATM, the contents of the ATM are not changed, and the array that is returned contains all zeros.
One way to get a 'small' number of notes is to use as many higher denomination notes as possible before using lower denomination notes. This is probably what you should implement.
But this does not give the minimum number of notes. You might look-up minimum change algorithms as a way of thinking more deeply about all this.