repl.it
@imigames/

Vending.....................................

Java

No description

fork
loading
Files
  • Main.java
  • Coins.java
  • Items.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import java.util.Scanner;
import java.text.DecimalFormat;

public class Main {
  //This is the variable to use for quantity inputet by user to buy 
  static int input_qty;
  //This is the variable for total money colected
    static double moneyCollected;
   //this is to print out what individual coin user recives
   static int five;
   static int ten;
   static int twenty;
   static int fifty;
   static int pounds;

   //This is used to calculate the money inserted into machine/Credit
    public static int totalMoneyIn(int fivep, int tenp, int twentyp, int fiftyp, int onepound) {
    return ((5 * fivep) + (10 * tenp) + (20 * twentyp) + (50 * fiftyp)+ (100 * onepound));}
    static int fivep;
    static int tenp;
    static int twentyp;
    static int fiftyp;
    static int onepound;
    //Decimal is to only show 2 decimals when printing some value
     static DecimalFormat fmt = new DecimalFormat("#,###.##");
    //Here are the Coins objects
    static Coins fivepObject = new Coins("£ 5p", 5, 20);
    static Coins tenpObject = new Coins("£ 10p", 10, 20);
    static Coins twentypObject = new Coins("£ 20p", 20, 20);
    static Coins fiftypObject = new Coins("£ 50p", 50, 20);
    static Coins onepoundObject = new Coins("£ 1", 100, 10);

    //Items from Vending machine
    static Items marsObject = new Items("Mars Bar", 75, 10);
    static Items colaObject = new Items("Coca Cola", 100, 10);
    static Items eugeniaObject = new Items("Eugenia", 85, 10);
    static Items waterObject = new Items("Water", 75, 10);
    static Items crispsObject = new Items("Crisps", 75, 10);
   // this is the scanner for user input
    static Scanner input = new Scanner(System.in);
       //Initial total item quantity in the machine
  static int initial_total_qty = marsObject.getQuantity()+colaObject.getQuantity()+eugeniaObject.getQuantity()+waterObject.getQuantity()+crispsObject.getQuantity();
    public static void main(String[] args) {
      //there are 2 loops so when i use break to start from the begining
      while(true){
      while(true){  

    //This is so taht the money inserted will be reseted after the loop
    int fivep=0;
    int tenp=0;
    int twentyp=0;
    int fiftyp=0;
    int onepound=0;
     //This will show the option menu , Prices and quantity
      menu();

        System.out.println("Hi! Which item do you like to buy?");
        String itemName = input.next();
        if (itemName.equalsIgnoreCase("1")) {}
        else if (itemName.equalsIgnoreCase("2")) {}
        else if (itemName.equalsIgnoreCase("3")) {}
        else if (itemName.equalsIgnoreCase("4")) {}
        else if (itemName.equalsIgnoreCase("5")) {}
        else if (itemName.equalsIgnoreCase("8")) {
            reporting();
            break;}
        else {
            System.out.println("Wrong selection!!!");
            break;}
      //Inserting qty and check if avaible
      
      System.out.println("Insert Qty you want to buy");
       input_qty = input.nextInt();
       if (itemName.equalsIgnoreCase("1") && input_qty > crispsObject.getQuantity()){
         System.out.println("There is no enough stock");
         break;
         }
         else if(itemName.equalsIgnoreCase("2") && input_qty > marsObject.getQuantity()){
         System.out.println("There is no enough stock");
         break;
         }
         else if(itemName.equalsIgnoreCase("3") && input_qty > colaObject.getQuantity()){
         System.out.println("There is no enough stock");
         break;
         }
         else if(itemName.equalsIgnoreCase("4") && input_qty > eugeniaObject.getQuantity()){
         System.out.println("There is no enough stock");
         break;
         }
         else if(itemName.equalsIgnoreCase("5") && input_qty > waterObject.getQuantity()){
         System.out.println("There is no enough stock");
         break;
         }


    //This prints coins options
    System.out.println(" ______________________________________ ");
    System.out.println("|Coins    £5p   £10p   £20p   £50p   £1|");
    System.out.println("|Option    1     2      3      4      5|");
    System.out.println("|______________________________________|");
    System.out.println("Enter your coins:");
    //this I use for if statements when user add coins
    int sc;
    //The loop for iserting coins one by one
    while(true){
       if (input.hasNextInt()){
             sc = input.nextInt();
        if (sc == 1){
        fivep ++ ;
        fivepObject.increaseQuantity();
        int credit = totalMoneyIn(fivep, tenp, twentyp,fiftyp,onepound);
        System.out.println("Credit:  £"+fmt.format((double)credit/100) );
        System.out.println("Insert more coins or press option 9 to compleate transaction");
        input.nextLine();}
        else if (sc == 2){
        tenp ++ ;
        tenpObject.increaseQuantity();
        int credit = totalMoneyIn(fivep, tenp, twentyp,fiftyp,onepound);
        System.out.println("Credit:  £"+fmt.format((double)credit/100) );
        System.out.println("Insert more coins or press option 9 to compleate transaction");
        input.nextLine();}
        else if (sc == 3){
        twentyp ++ ;
        twentypObject.increaseQuantity();
        int credit = totalMoneyIn(fivep, tenp, twentyp,fiftyp,onepound);
        System.out.println("Credit:  £"+fmt.format((double)credit/100) );
        System.out.println("Insert more coins or press option 9 to compleate transaction");
        input.nextLine();}
        else if (sc == 4){
        fiftyp ++ ;
        fiftypObject.increaseQuantity();
        int credit = totalMoneyIn(fivep, tenp, twentyp,fiftyp,onepound);
        System.out.println("Credit:  £"+fmt.format((double)credit/100) );
        System.out.println("Insert more coins or press option 9 to compleate transaction");
        input.nextLine();}
        else if (sc == 5){
        onepound ++ ;
        onepoundObject.increaseQuantity();
        int credit = totalMoneyIn(fivep, tenp, twentyp,fiftyp,onepound);
        System.out.println("Credit:  £"+fmt.format((double)credit/100) );
        System.out.println("Insert more coins or press option 9 to compleate transaction");
        }                
        else if (sc == 9){
              break;}
        else{System.out.println("Please add Coins");
        }
      }
      }
      //this will calculate the coins inserted and setting the total money inserted
      
      int amount =totalMoneyIn(fivep, tenp, twentyp,fiftyp,onepound);
      System.out.println("Credit: £"+fmt.format((double)amount/100));
        //Buy Item
        if (itemName.equalsIgnoreCase("5")) {
            buyItem(waterObject, amount);
        
        } else if (itemName.equalsIgnoreCase("2")) {
            buyItem(marsObject, amount);

        } else if (itemName.equalsIgnoreCase("1")) {
            buyItem(crispsObject, amount);
        } else if (itemName.equalsIgnoreCase("4")) {
            buyItem(eugeniaObject, amount);
        } else if (itemName.equalsIgnoreCase("3")) {
            buyItem(colaObject, amount);
        } else {
            System.out.println("Wrong selection!!!");
        }
        System.out.println();

      }
      }
    }
        //Buy Item method and change
    public static int buyItem(Items item, int amount) {

        int change = 0;
        String name = item.getName();
        if (amount >= item.getPrice()*input_qty) {
            item.decreaseQuantity();
            change = amount - input_qty * item.getPrice();
            moneyCollected += item.getPrice()*input_qty;
            System.out.println("Enjoy your: " + item.getName());

        } else {
            System.out.println("Sorry. Not enough money!!!! The price for "+input_qty+" " + item.getName() + " is £" + fmt.format((double) (input_qty*item.getPrice())/100));
            change = amount;

        }   
        //this is used for calculating and giving change
          int five=0;
          int ten=0;
          int twenty=0;
          int fifty=0;
          int pounds=0;
        System.out.println("Your change is: £"+fmt.format((double)change/100));
          while(onepoundObject.getQty() > 0)
        {
            if (change>=100){
            onepoundObject.decreaseQuantity();
            pounds ++;
            change -=100;
            }else{break;}
        }
          while(fiftypObject.getQty() > 0)
        {
            if (change>=50){
            fiftypObject.decreaseQuantity();
            fifty ++;
            change -=50;
            }else{break;}
        }
          while(twentypObject.getQty() > 0)
        {
            if (change>=20){
            twentypObject.decreaseQuantity();
            twenty ++;
            change-= 20;
            }else{break;}
        }
          while(tenpObject.getQty() > 0){
           if(change >=10){
        
            tenpObject.decreaseQuantity();
            ten ++;
            change-=10;
                    
        }else{break;}
        }
        while(fivepObject.getQty() >0)
        {
            if (change>=5){
            fivepObject.decreaseQuantity();
            five ++;
            change-=5;
            }else{break;}
            
        }          
        //This is a print of change for every coin 
        System.out.println("Pounds: " + pounds + ", 50p: " + fifty + ", 20p: " + twenty + ", 10p: " + ten + ", 5p: " + five );


        return change;
    }

    //This is method for showing menu
    public static void menu() {
    System.out.println(" ___________________________________________ ");
    System.out.println("|*******************Product*****************");
    System.out.println("|Option   Item           Price         Qty");
    System.out.println("|  1      Crisps         £0.75          "+ crispsObject.getQuantity());
    System.out.println("|  2      Mars Bar       £0.70          "+marsObject.getQuantity());
    System.out.println("|  3      Coca Cola      £1             "+colaObject.getQuantity());
    System.out.println("|  4      Eugenia        £0.50          "+eugeniaObject.getQuantity());
    System.out.println("|  5      Water          £0.85          "+waterObject.getQuantity());
    System.out.println("|___________________________________________");
    }
  
    //This is the report for administrator
    public static void reporting() {
        System.out.println(" __________________________________________ ");
        System.out.println("|Total collected money: £" + fmt.format(moneyCollected/100));
        int actual_qty = marsObject.getQuantity()+colaObject.getQuantity()+eugeniaObject.getQuantity()+waterObject.getQuantity()+crispsObject.getQuantity();
        System.out.println("|Profit/Loss: £"+ fmt.format(((moneyCollected-(initial_total_qty - actual_qty)*45)/100)));        
        System.out.println("|*******Items left in machine*******");
        System.out.println("|Remaining Crisps: " + crispsObject.getQuantity());
        System.out.println("|Remaining Mars Bar: " + marsObject.getQuantity());
        System.out.println("|Remaining Coca Cola: " + colaObject.getQuantity());
        System.out.println("|Remaining Eugenia: " + eugeniaObject.getQuantity());
        System.out.println("|Remaining Water: " + waterObject.getQuantity());
        System.out.println("|**********Money in machine**********");
        System.out.println("|             £  1: "+onepoundObject.getQty());
        System.out.println("|             £50p: "+fiftypObject.getQty());
        System.out.println("|             £20p: "+twentypObject.getQty());
        System.out.println("|             £10p: "+tenpObject.getQty());
        System.out.println("|             £ 5p: "+fivepObject.getQty());
       System.out.println("|___________________________________________ ");
    }
    }