Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class InsufficientFundsException extends Exception {
- public InsufficientFundsException(String message){
- super(message);
- }
- }
- public class VendingMachine {
- private ArrayList<Item> list = new ArrayList<>();
- private Purse purse = new Purse();
- public VendingMachine(){
- }
- public VendingMachine(ArrayList<Double> allowedValues){
- purse = new Purse(allowedValues);
- }
- public Item get(int i){
- return list.get(i);
- }
- public int size(){
- return list.size();
- }
- public void sort(){
- Collections.sort(list);
- Collections.sort(purse.getCoins());
- }
- public void add(Item item){
- list.add(new Item(item));
- }
- public void add(int i, Item item){
- list.add(i, new Item(item));
- }
- public void del(int i){
- list.remove(i);
- }
- public Purse getPurse(){
- return purse;
- }
- public ArrayList<Double> getCoins(){
- return purse.getCoins();
- }
- void sell(int i) throws Exception {
- double price = get(i).getPrice();
- double balance = getPurse().getBalance();
- if(price <= balance){ // can sell
- this.del(i);
- getPurse().remove(price);
- } else { // cannot sell
- throw new InsufficientFundsException("Not enough money!");
- }
- //System.out.println(price + " " + balance);
- }
- /*
- public static void main(String args[]) {
- try {
- Item item1 = new Item("saldainis", 1.1);
- Item item2 = new Item("saldainis", 2.1);
- Item item3 = new Item("sultys", 3.1);
- VendingMachine vm = new VendingMachine();
- vm.add(item1);
- vm.add(item2);
- vm.add(item3);
- vm.del(1);
- vm.del(1);
- vm.add(item1);
- item1.setPrice(4.1);
- vm.add(item1);
- for(int i = 0; i < vm.size(); ++i){
- System.out.println(vm.get(i));
- }
- } catch(Exception e){
- System.out.println(e);
- System.out.println("Unexpected error, sorry!");
- }
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement