Advertisement
irmantas_radavicius

Application.java

Mar 27th, 2022
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class InvalidCommandException extends Exception {
  5.     public InvalidCommandException(String message){
  6.         super(message);
  7.     }
  8. }
  9.  
  10.  
  11. public class Application {
  12.     private VendingMachine vm = null;  
  13.     private FileManager catalogueManager;
  14.     private FileManager purseManager;
  15.    
  16.     public Application(File catalogue, File purse, ArrayList<Double> allowedCoins) throws Exception {  
  17.         vm = new VendingMachine(allowedCoins);
  18.         this.catalogueManager = new FileManager(catalogue);
  19.         this.purseManager = new FileManager(purse);        
  20.        
  21.         loadItems(catalogueManager.load());    
  22.         loadPurse(purseManager.load());
  23.         vm.sort();     
  24.     }
  25.    
  26.     private void loadItems(ArrayList<String> als){
  27.         for(int j = 0; j < als.size(); ++j){
  28.             Item[] items = parseItem(als.get(j));
  29.             for(int i = items.length; i > 0; --i){                 
  30.                 vm.add(items[i-1]);
  31.             }
  32.         }          
  33.        
  34.     }
  35.    
  36.     private void loadPurse(ArrayList<String> als){
  37.         for(int j = 0; j < als.size(); ++j){
  38.             Double[] coins = parseCoin(als.get(j));
  39.             for(int i = coins.length; i > 0; --i){                 
  40.                 vm.getCoins().add(coins[i-1]);
  41.             }
  42.         }              
  43.     }
  44.    
  45.     private ArrayList<String> saveItems(){             
  46.         ArrayList<String> als = new ArrayList<>();         
  47.         String tempOld = "";
  48.         int counter = 0;
  49.         for(int i = 0; i < vm.size(); ++i){
  50.             String tempNew = vm.get(i).getName() + " " + vm.get(i).getPrice();
  51.             if (!tempOld.equals(tempNew)){             
  52.                 if (counter > 0){
  53.                     als.add(tempOld + " " + counter);
  54.                 }              
  55.                 tempOld = tempNew;
  56.                 counter = 1;
  57.             } else {
  58.                 ++counter;
  59.             }          
  60.         }
  61.         if (counter > 0){
  62.             als.add(tempOld + " " + counter);
  63.         }          
  64.         return als;
  65.     }
  66.    
  67.     private ArrayList<String> savePurse(){     
  68.         ArrayList<String> als = new ArrayList<>();         
  69.         String tempOld = "";
  70.         int counter = 0;
  71.         for(int i = 0; i < vm.getCoins().size(); ++i){
  72.             String tempNew = "" + vm.getCoins().get(i);
  73.             if (!tempOld.equals(tempNew)){             
  74.                 if (counter > 0){
  75.                     als.add(tempOld + " " + counter);
  76.                 }              
  77.                 tempOld = tempNew;
  78.                 counter = 1;
  79.             } else {
  80.                 ++counter;
  81.             }          
  82.         }
  83.         if (counter > 0){
  84.             als.add(tempOld + " " + counter);
  85.         }          
  86.         return als;
  87.     }
  88.    
  89.     private void export() throws Exception {       
  90.         vm.sort();
  91.         catalogueManager.save(saveItems());
  92.         purseManager.save(savePurse());
  93.     }
  94.    
  95.     private Item [] parseItem(String line){
  96.         line = line.trim();
  97.         int index1 = line.lastIndexOf(' ');                
  98.         int amount = Integer.parseInt(line.substring(index1+1));
  99.         int index2 = line.substring(0, index1).lastIndexOf(' ');
  100.         double price = Double.parseDouble(line.substring(index2+1, index1));
  101.         String name = line.substring(0, index2);           
  102.         Item item = new Item(name, price);
  103.         Item[] items = new Item[amount];
  104.         Arrays.fill(items, item);
  105.         return items;
  106.     }
  107.    
  108.     private Double [] parseCoin(String line){
  109.         line = line.trim();
  110.         int index1 = line.lastIndexOf(' ');                
  111.         int amount = Integer.parseInt(line.substring(index1+1));       
  112.         double price = Double.parseDouble(line.substring(0, index1));      
  113.         Double[] coins = new Double[amount];
  114.         Arrays.fill(coins, price);
  115.         return coins;
  116.     }
  117.    
  118.     public void execute(String command) throws Exception {
  119.         if (command.equalsIgnoreCase("stop")){         
  120.             change();
  121.             export();
  122.         } else if (command.equalsIgnoreCase("view")){
  123.             show();
  124.         } else if (command.equalsIgnoreCase("quit")){
  125.             System.exit(0);
  126.         } else if (command.length() >= 4 && command.substring(0, 4).equalsIgnoreCase("add ")){
  127.             put(Double.parseDouble(command.substring(4)));
  128.             export();
  129.         } else if (command.length() >= 4 && command.substring(0, 4).equalsIgnoreCase("get ")){
  130.             sell(Integer.parseInt(command.substring(4)));          
  131.             export();
  132.         } else {
  133.             throw new InvalidCommandException("No such command");
  134.         }
  135.     }
  136.    
  137.     private void show(){
  138.         for(int i = 0; i < vm.size(); ++i){
  139.             System.out.println(i + " " + vm.get(i));
  140.         }
  141.         System.out.println("Balance is " + vm.getPurse().getBalance());
  142.     }
  143.     private void change() throws Exception {
  144.         System.out.println("Your change is " + vm.getPurse().getBalance() + " = ");
  145.         ArrayList<Double> change = vm.getPurse().getChange();
  146.         System.out.print(change.get(0));
  147.         for(int i = 1; i < change.size(); ++i){
  148.             System.out.print(" + " + change.get(i));
  149.         }
  150.         System.out.println();
  151.        
  152.     }
  153.     private void put(double money) throws Exception {      
  154.         if (money >= 0){
  155.             vm.getPurse().add(money);
  156.             System.out.println("Balance is " + vm.getPurse().getBalance());
  157.         } else {
  158.             throw new IllegalArgumentException("Money cannot be negative");
  159.         }
  160.     }
  161.     private void sell(int index) throws Exception {    
  162.         Item sold = vm.get(index);
  163.         vm.sell(index);
  164.         System.out.println("You just bought " + sold);     
  165.         System.out.println("Remaining balance is " + vm.getPurse().getBalance());
  166.     }
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement