-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeal.java
More file actions
77 lines (64 loc) · 2.18 KB
/
Meal.java
File metadata and controls
77 lines (64 loc) · 2.18 KB
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
package com.mycompany.lab55;
import java.util.Arrays;
import java.util.Scanner;
public class Meal {
private int code;
public String name;
public double price;
private String description;
private static int[] usedCodes = new int[0];
Meal() {
Scanner input = new Scanner(System.in);
System.out.print("Enter meal name: ");
name = input.nextLine();
System.out.print("Enter meal price: ");
price = input.nextDouble();
while (price <= 0) {
System.out.print("Please enter a positive non-zero value for the price: ");
price = input.nextDouble();
}
description = "";
generateCode();
}
Meal(String n, double p, String d) {
name = n;
price = p;
description = d;
generateCode();
}
//لما احط الكود برايفت لازم استخدم هاي الميثود عشان ترجعلي اياه برا الكلاس الي هو فيه
public int getCode() {
return code;
}
public String getDescription() {
return description;
}
public void addToDescription(String newDesc) {
//بضيف السترينغ الي انا بدي اضيفو للدسكربشن
if (this.description.isEmpty()) {
this.description = newDesc;
} else {
this.description += ", " + newDesc;
}
}
private void generateCode() {
int code = 1000 + (int) (Math.random() * 9000);
while (Arrays.binarySearch(usedCodes, code) >= 0)
code = 1000 + (int) (Math.random() * 9000);
this.code = code;
int[] temp = new int[usedCodes.length + 1];
System.arraycopy(usedCodes, 0, temp, 0, usedCodes.length);
temp[temp.length - 1] = code;
usedCodes = temp;
Arrays.sort(usedCodes);
}
static void removeCode(int code) {
if (Arrays.binarySearch(usedCodes, code) < 0)
return;
int[] temp = new int[usedCodes.length - 1];
for (int i = 0, j = 0; i < usedCodes.length; i++)
if (usedCodes[i] != code)
temp[j++] = usedCodes[i];
usedCodes = temp;
}
}