repl.it
@imigames/

Vending nou 100

Java

No description

fork
loading
Files
  • Main.java
  • Change.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
import java.util.Scanner;
import java.text.DecimalFormat;
/**
 *
 * @author Mircea
 */
public class Main {


private static double calctotal(int fivep, int tenp, int twentyp, int fiftyp, int onepound) {
    return ((0.05 * fivep) + (0.10 * tenp) + (0.20 * twentyp) + (0.50 * fiftyp)+ (1 * onepound));
}
    static double totalIncome = 0;
    //Change pool
   static int c_five = 20;
   static int c_ten = 20;
   static int c_twenty = 20;
   static int c_fifty = 20;
   static int c_pounds = 10;
   //Change
   static int five = 20;
   static int ten = 20;
   static int twenty = 20;
   static int fifty = 20;
   static int pounds = 10;
    //Decimal
     static DecimalFormat fmt = new DecimalFormat("#,###.##");

    static Items marsObject = new Items("Mars Bar", 0.75, 10);
    static Items colaObject = new Items("Coca Cola", 1, 10);
    static Items eugeniaObject = new Items("Eugenia", 0.85, 10);
    static Items waterObject = new Items("Water", 0.75, 10);
    static Items crispsObject = new Items("Crisps", 0.75, 10);
    static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) {

        double amount = 0;

        //         while (keyboard.hasNext()) {
        //      if (keyboard.hasNextDouble())
        //      amount = amount + keyboard.nextDouble();
        //        else if (keyboard.next().equals("*"))
        //        break;
        //    }
        //   if (coin != 0.05 && coin != 0.10 && coin != 0.20 && coin != 0.50 && coin != 1) {
        //      amount = coin + keyboard.nextFloat();
        //       keyboard.nextLine();
        //       System.out.println("Incorect Coin, please insert £0.05 , £0.10 , £0.20 , £0.50 , £1");
        //   }
        System.out.println("Hi! Which item do you like to buy?");
        String itemName = keyboard.nextLine();

        if (itemName.equalsIgnoreCase("exit")) {

        }
        System.out.println("Insert amount: ");
  
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your coins:");
    System.out.print("£ 1:");
    System.out.flush();
    int onepound = input.nextInt();
    c_pounds += onepound;
    System.out.print("£ 50p:");
    System.out.flush();
    int fiftyp = input.nextInt();
    c_fifty += fiftyp;
    System.out.print("£ 20p:");
    System.out.flush();
    int twentyp = input.nextInt();
    c_twenty += twentyp;
    System.out.print("£ 10p:");
    System.out.flush();
    int tenp = input.nextInt();
    c_ten += tenp;
    System.out.print("£ 5p:");
    System.out.flush();
    int fivep = input.nextInt();
    c_five += fivep;
    System.out.println("Credit: £"+fmt.format(calctotal(fivep, tenp, twentyp,fiftyp,onepound)));
    amount =calctotal(fivep, tenp, twentyp,fiftyp,onepound);
 /*   int quarters = 0 ;
      int dimes = 0;
      while(true){
        if (keyboard.nextLine().equals("1")){
        quarters ++ ;
        keyboard.nextLine();
        System.out.println("You have Quarters: "+quarters);}
        else if (keyboard.nextLine().equals("2")){
        dimes ++ ;
       keyboard.nextLine();
        System.out.println("You have Dimes: "+dimes);
      }
        else if (keyboard.nextLine().equals("*")){
               break;
      }
        else{System.out.println("Please add Coins");
        keyboard.nextLine();}
      }
      */
        if (itemName.equalsIgnoreCase("Water")) {
            buyItem(waterObject, amount);
        
        } else if (itemName.equalsIgnoreCase("Mars Bar")) {
            buyItem(marsObject, amount);

        } else if (itemName.equalsIgnoreCase("Crisps")) {
            buyItem(crispsObject, amount);
        } else if (itemName.equalsIgnoreCase("Eugenia")) {
            buyItem(eugeniaObject, amount);
        } else if (itemName.equalsIgnoreCase("Coca Cola")) {
            buyItem(colaObject, amount);
        } else {
            System.out.println("Wrong selection!!!");
        }
        System.out.println();

        reporting();
    }






    public static double buyItem(Items item, double amount) {

        double change = 0;
        String name = item.getName();
        if (amount >= item.getPrice()) {
            item.decreaseQuantity();
            change = amount - item.getPrice();
            totalIncome += item.getPrice();
            System.out.println("Enjoy your: " + item.getName());
        } else {
            System.out.println("Sorry. Not enough money!!!! The price for  " + item.getName() + " is " + item.getPrice());
            change = amount;
        }
        change = change *100;
        int change1 = (int)change;
        System.out.println("Please take your change: £" + fmt.format(change/100));
        if(change1 >= 100)
        {
            pounds = change1 / 100;
            if (c_pounds >= pounds){
            c_pounds -= pounds;}
            else{
            change1 = change1 + ((pounds - c_pounds)*100);
            c_pounds = 0;}
            change1 = change1 % 100;
            
        }
            //less then 100
            fifty = change1 / 50;
            if (c_fifty >= fifty){
            c_fifty -= fifty;}
            else{
              
              change1 = change1 +((fifty - c_fifty)*100);
              c_fifty = 0;}
              change1 = change1 % 50;
            
            twenty = change1 / 20;
            if (c_twenty >= twenty){
            c_twenty -= twenty;}
            else{
              
              change1 = change1 +((twenty - c_twenty)*100);
              c_twenty = 0;}
              change1 = change1 % 20;
            
            ten = change1 / 10;
            if(c_ten >= ten){
            c_ten -= ten;}
            else{
              
              change1 = change1 +((ten - c_ten)*100);
              c_ten = 0 ;}
            change1 = change1 % 10;
            five = change1 / 5;
            if (c_five >= five){
            c_five -= five;}
            else{c_five = 0;}
            change1 = change1 % 5;        


        //result
        System.out.println("Pounds: " + pounds + ", 50p: " + fifty + ", 20p: " + twenty + ", 10p: " + ten + ", 5p: " + five);
        
        //System.out.println("Please take your change: " + fmt.format(change));
        return change;

    }

    public static void reporting() {
        System.out.println("******************");
        System.out.println("******Report******");
        System.out.println("Total collected money:" + totalIncome);
        System.out.println("Remaining Water: " + waterObject.getQuantity());
        System.out.println("Remaining Coca Cola: " + colaObject.getQuantity());
        System.out.println("Remaining Eugenia: " + eugeniaObject.getQuantity());
        System.out.println("Remaining Crisps: " + crispsObject.getQuantity());
        System.out.println("Remaining Mars Bar: " + marsObject.getQuantity());
        System.out.println("******************");
        System.out.println(c_pounds);
        System.out.println(c_fifty);
        System.out.println(c_twenty);
        System.out.println(c_ten);
        System.out.println(c_five);
    }

}