Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class InvalidCommandException extends Exception {
- public InvalidCommandException(String message){
- super(message);
- }
- }
- public class Application {
- private VendingMachine vm = null;
- private FileManager catalogueManager;
- private FileManager purseManager;
- public Application(File catalogue, File purse, ArrayList<Double> allowedCoins) throws Exception {
- vm = new VendingMachine(allowedCoins);
- this.catalogueManager = new FileManager(catalogue);
- this.purseManager = new FileManager(purse);
- loadItems(catalogueManager.load());
- loadPurse(purseManager.load());
- vm.sort();
- }
- private void loadItems(ArrayList<String> als){
- for(int j = 0; j < als.size(); ++j){
- Item[] items = parseItem(als.get(j));
- for(int i = items.length; i > 0; --i){
- vm.add(items[i-1]);
- }
- }
- }
- private void loadPurse(ArrayList<String> als){
- for(int j = 0; j < als.size(); ++j){
- Double[] coins = parseCoin(als.get(j));
- for(int i = coins.length; i > 0; --i){
- vm.getCoins().add(coins[i-1]);
- }
- }
- }
- private ArrayList<String> saveItems(){
- ArrayList<String> als = new ArrayList<>();
- String tempOld = "";
- int counter = 0;
- for(int i = 0; i < vm.size(); ++i){
- String tempNew = vm.get(i).getName() + " " + vm.get(i).getPrice();
- if (!tempOld.equals(tempNew)){
- if (counter > 0){
- als.add(tempOld + " " + counter);
- }
- tempOld = tempNew;
- counter = 1;
- } else {
- ++counter;
- }
- }
- if (counter > 0){
- als.add(tempOld + " " + counter);
- }
- return als;
- }
- private ArrayList<String> savePurse(){
- ArrayList<String> als = new ArrayList<>();
- String tempOld = "";
- int counter = 0;
- for(int i = 0; i < vm.getCoins().size(); ++i){
- String tempNew = "" + vm.getCoins().get(i);
- if (!tempOld.equals(tempNew)){
- if (counter > 0){
- als.add(tempOld + " " + counter);
- }
- tempOld = tempNew;
- counter = 1;
- } else {
- ++counter;
- }
- }
- if (counter > 0){
- als.add(tempOld + " " + counter);
- }
- return als;
- }
- private void export() throws Exception {
- vm.sort();
- catalogueManager.save(saveItems());
- purseManager.save(savePurse());
- }
- private Item [] parseItem(String line){
- line = line.trim();
- int index1 = line.lastIndexOf(' ');
- int amount = Integer.parseInt(line.substring(index1+1));
- int index2 = line.substring(0, index1).lastIndexOf(' ');
- double price = Double.parseDouble(line.substring(index2+1, index1));
- String name = line.substring(0, index2);
- Item item = new Item(name, price);
- Item[] items = new Item[amount];
- Arrays.fill(items, item);
- return items;
- }
- private Double [] parseCoin(String line){
- line = line.trim();
- int index1 = line.lastIndexOf(' ');
- int amount = Integer.parseInt(line.substring(index1+1));
- double price = Double.parseDouble(line.substring(0, index1));
- Double[] coins = new Double[amount];
- Arrays.fill(coins, price);
- return coins;
- }
- public void execute(String command) throws Exception {
- if (command.equalsIgnoreCase("stop")){
- change();
- export();
- } else if (command.equalsIgnoreCase("view")){
- show();
- } else if (command.equalsIgnoreCase("quit")){
- System.exit(0);
- } else if (command.length() >= 4 && command.substring(0, 4).equalsIgnoreCase("add ")){
- put(Double.parseDouble(command.substring(4)));
- export();
- } else if (command.length() >= 4 && command.substring(0, 4).equalsIgnoreCase("get ")){
- sell(Integer.parseInt(command.substring(4)));
- export();
- } else {
- throw new InvalidCommandException("No such command");
- }
- }
- private void show(){
- for(int i = 0; i < vm.size(); ++i){
- System.out.println(i + " " + vm.get(i));
- }
- System.out.println("Balance is " + vm.getPurse().getBalance());
- }
- private void change() throws Exception {
- System.out.println("Your change is " + vm.getPurse().getBalance() + " = ");
- ArrayList<Double> change = vm.getPurse().getChange();
- System.out.print(change.get(0));
- for(int i = 1; i < change.size(); ++i){
- System.out.print(" + " + change.get(i));
- }
- System.out.println();
- }
- private void put(double money) throws Exception {
- if (money >= 0){
- vm.getPurse().add(money);
- System.out.println("Balance is " + vm.getPurse().getBalance());
- } else {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- }
- private void sell(int index) throws Exception {
- Item sold = vm.get(index);
- vm.sell(index);
- System.out.println("You just bought " + sold);
- System.out.println("Remaining balance is " + vm.getPurse().getBalance());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement