-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.java
43 lines (43 loc) · 1.38 KB
/
product.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
import java.util.Scanner;
public class product {
static Scanner sc = new Scanner(System.in);
int id;
float cost;
product(int i){
System.out.println("Enter the id of product " + (i+1));
id = Integer.parseInt(sc.nextLine());
System.out.println("Enter the cost of product " + (i+1));
cost = Float.parseFloat(sc.nextLine());
}
void display(){
System.out.println("Id: "+id+" Cost: "+cost);
}
void increment(){
cost = cost + 1000;
}
public static void main(String[] args) {
int n;
System.out.println("Enter the number of products you want to store");
n = Integer.parseInt(sc.nextLine());
product p[] = new product[n];
for(int i = 0; i < p.length; i++)
p[i] = new product(i);
for(int i = 0; i < p.length; i++)
p[i].display();
for(int i = 0; i < p.length; i++)
p[i].increment();
product temp;
for (int i = 1; i < p.length; i++) {
for (int j = i; j > 0; j--) {
if (p[j].cost > p[j - 1].cost) {
temp = p[j];
p[j] = p[j - 1];
p[j - 1] = temp;
}
}
}
System.out.println("\nAfter incremnting and sorting");
for(int i = 0; i < p.length; i++)
p[i].display();
}
}