-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighProductDemo.java
92 lines (74 loc) · 2.27 KB
/
HighProductDemo.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
// AppMain.java (file)
package com.exampledemo.main;
import com.exampledemo.pojo.Product;
import java.util.Scanner;
public class AppMain {
public static void main (String[] args)
{
Product[] arr = new Product[3];
Scanner sc = new Scanner(System.in);
for(int i = 0; i<arr.length; i++ ){
System.out.print(" Enter product id: ");
int id =sc.nextInt();
System.out.print(" Enter product price: ");
int prc =sc.nextInt();
System.out.print(" Enter product quantity : ");
int qty =sc.nextInt();
arr[i] = new Product(id,prc,qty);
}
int total = findTotalAmountOfProducts(arr);
System.out.println("total amount spent"+ "on all products:" + total);
int id = findProductWithHighestPrice(arr);
System.out.println("id of the product with highest"+ " price : " + id);
}
static int findTotalAmountOfProducts (Product[] products){
int totalAmount = 0;
for (Product p : products){
totalAmount = totalAmount + (p.getPrice() * p.getQuantity());
}
return totalAmount;
}
static int findProductWithHighestPrice(Product[] products){
int pid = 0;
int max = products[0].getPrice();
for(Product p : products){
if (max < p.getPrice()){
max = p.getPrice();
pid = p.getPid();
}
}
return pid;
}
}
// new file with Product name
package com.exampledemo.pojo;
public class Product {
private int pid;
private int price;
private int quantity;
public Product (){
}
public Product(int pid , int price,int quantity){
this.pid = pid;
this.price = price;
this.quantity = quantity;
}
public int getPid(){
return pid ;
}
public void setPid(int pid ){
this.pid=pid;
}
public int getPrice(){
return price ;
}
public void setPrice(int price ){
this.price=price;
}
public int getQuantity(){
return quantity ;
}
public void setQuantity(int quantity ){
this.quantity=quantity;
}
}