**Rico**
Mr & Ms Pac-Man
Class Product with properties are ID – int, Name – String, quantity – int, Price – float
Methods are:
- void createProduct() : enter the information of product from the keyboard
- void displayProduct() : display product’s information as format:
id name quantity price cost
- float getCost() : return the cost for a product (price * quantity)
- void sortProduct(Product p[]) : sort product in array p base on cost (price * quantity) in acsending
- Product findProduct(int quantity, Product p[]) : return the first product that has the quantity is greater than the number you have entered.
- In main:
+ Create an array product with n elements, n is get from the keyboard
+ Enter the information for all elements of array p
+ Display all products in ascending of cost
+ Enter a whole number from the keyboard, display the information of the first product that has the quantity is greater than this number.
Methods are:
- void createProduct() : enter the information of product from the keyboard
- void displayProduct() : display product’s information as format:
id name quantity price cost
- float getCost() : return the cost for a product (price * quantity)
- void sortProduct(Product p[]) : sort product in array p base on cost (price * quantity) in acsending
- Product findProduct(int quantity, Product p[]) : return the first product that has the quantity is greater than the number you have entered.
- In main:
+ Create an array product with n elements, n is get from the keyboard
+ Enter the information for all elements of array p
+ Display all products in ascending of cost
+ Enter a whole number from the keyboard, display the information of the first product that has the quantity is greater than this number.
PHP:
import java.util.Scanner;
public class Product {
int pID;
String pName;
int pQuanty;
float pPrice;
// Nhap thong tin cho san pham
void createProduct(){
Scanner sc = new Scanner(System.in);
System.out.println("Id: ");
pID = sc.nextInt();
System.out.println("Name: ");
pName = sc.next();
System.out.println("Quantity: ");
pQuanty = sc.nextInt();
System.out.println("Price: ");
pPrice = sc.nextFloat();
}
// Hien thi san pham
void displayProduct(){
System.out.println(pID + "\t" + pName + "\t" + pQuanty + "\t" + pPrice + "\t" + getCost());
}
// Tra ve chi phi cua san pham
float getCost(){
return pQuanty * pPrice;
}
// Sap xep san pham theo chi phi
void sortProduct(Product p[]){
for(int i = 0; i < p.length - 1; i++){
for(int j = i + 1; j < p.length; j++){
if(p[j].getCost() < p[i].getCost()){
Product temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
}
// Tra ve san pham dau tien co so luong lon hon so nhap vao
Product findProduct(int quanty, Product p[]){
for(int i = 0; i < p.length; i++){
if(p[i].pQuanty > quanty){
return p[i];
}
}
return null;
}
public static void main(String[] args) {
Product pro = new Product();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the number elements of the array: ");
int n = sc.nextInt();
Product[] p = new Product[n];
// Nhap thong tin cho cac phan tu mang
for(int i = 0; i < n; i++){
p[i] = new Product();
System.out.println("Please enter the information of the product " + (i + 1) + ":");
p[i].createProduct();
}
// Sap xep tat ca san pham theo thu tu tang dan cua chi phi
pro.sortProduct(p);
// Hien thi tat ca san pham theo thu tu tang dan cua chi phi
System.out.println("Id\tName\tQuanty\tPrice\tCost");
for(int i = 0; i < n; i++){
p[i].displayProduct();
}
// Hien thi thong tin san pham dau tien co so luong lon hon so nhap vao
System.out.println("Please enter any number to find quantity: ");
int quanty = sc.nextInt();
System.out.println("The first product have quantity more than " + quanty + " is:");
pro.findProduct(quanty, p).displayProduct();
}
}