Advertisement
irmantas_radavicius

VendingMachine.java

Mar 27th, 2022
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class InsufficientFundsException extends Exception {
  5.     public InsufficientFundsException(String message){
  6.         super(message);
  7.     }
  8. }
  9.  
  10. public class VendingMachine {
  11.     private ArrayList<Item> list = new ArrayList<>();
  12.     private Purse purse = new Purse();
  13.    
  14.     public VendingMachine(){
  15.     }
  16.     public VendingMachine(ArrayList<Double> allowedValues){
  17.         purse = new Purse(allowedValues);
  18.     }
  19.  
  20.     public Item get(int i){
  21.         return list.get(i);
  22.     }  
  23.     public int size(){
  24.         return list.size();
  25.     }
  26.    
  27.     public void sort(){
  28.         Collections.sort(list);
  29.         Collections.sort(purse.getCoins());
  30.     }
  31.    
  32.     public void add(Item item){
  33.         list.add(new Item(item));
  34.     }
  35.     public void add(int i, Item item){
  36.         list.add(i, new Item(item));
  37.     }      
  38.     public void del(int i){
  39.         list.remove(i);
  40.     }
  41.    
  42.     public Purse getPurse(){
  43.         return purse;
  44.     }
  45.     public ArrayList<Double> getCoins(){
  46.         return purse.getCoins();
  47.     }
  48.    
  49.     void sell(int i) throws Exception {
  50.         double price = get(i).getPrice();
  51.         double balance = getPurse().getBalance();
  52.         if(price <= balance){ // can sell
  53.             this.del(i);
  54.             getPurse().remove(price);
  55.         } else { // cannot sell
  56.             throw new InsufficientFundsException("Not enough money!");
  57.         }
  58.        
  59.        
  60.         //System.out.println(price + " " + balance);
  61.     }
  62.     /*
  63.     public static void main(String args[]) {       
  64.         try {                      
  65.             Item item1 = new Item("saldainis", 1.1);
  66.             Item item2 = new Item("saldainis", 2.1);
  67.             Item item3 = new Item("sultys", 3.1);
  68.            
  69.             VendingMachine vm = new VendingMachine();
  70.             vm.add(item1);
  71.             vm.add(item2);
  72.             vm.add(item3);
  73.             vm.del(1);
  74.             vm.del(1);
  75.             vm.add(item1);
  76.             item1.setPrice(4.1);
  77.             vm.add(item1);
  78.                        
  79.             for(int i = 0; i < vm.size(); ++i){
  80.                 System.out.println(vm.get(i));
  81.             }          
  82.         } catch(Exception e){
  83.             System.out.println(e);         
  84.             System.out.println("Unexpected error, sorry!");
  85.         }          
  86.     }
  87.     */ 
  88.    
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement