/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson03;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Main {
static ArrayList<Product> products = new ArrayList<>();
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int choose;
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
input();
break;
case 2:
display();
break;
case 3:
sort();
break;
case 4:
System.out.println("Thoat!!!");
break;
default:
System.out.println("Nhap sai!!!");
}
} while(choose != 4);
}
static void showMenu() {
System.out.println("1. Nhap N san pham");
System.out.println("2. Hien thi thong tin");
System.out.println("3. Sap xep theo gia & hien thi");
System.out.println("4. Thoat");
System.out.println("Chon: ");
}
private static void input() {
System.out.println("Nhap so san pham can them: ");
int n = Integer.parseInt(scan.nextLine());
// Product p = new Product(); Cach 2
Product p; //Cach 3
for (int i = 0; i < n; i++) {
// Product p = new Product(); -> cach 1
p = new Product();
p.input();
products.add(p);
}
// Product p = new Product();
// p.name = "A";
// p.display();
// Product p3 = p;
//
// Product p2 = new Product();
// p2.display();
//
// p = new Product();
// p.name = "C";
// p.display();
//
// System.out.println(p.name + ", " + p3.name);
}
private static void display() {
System.out.println("Danh sach san pham");
for (int i = 0; i < products.size(); i++) {
products.get(i).display();
}
System.out.println("Danh sach san pham");
for (Product product : products) {
product.display();
}
}
private static void sort() {
Collections.sort(products, new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
if(o1.price > o2.price) {
return 1;
}
return -1;
}
});
display();
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson03;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Product {
String name;
String manufacturerName;
float price;
public Product() {
}
public Product(String name, String manufacturerName, float price) {
this.name = name;
this.manufacturerName = manufacturerName;
this.price = price;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten: ");
name = scan.nextLine();
System.out.println("Nhap nha san xuat: ");
manufacturerName = scan.nextLine();
System.out.println("Nhap gia ban: ");
price = Float.parseFloat(scan.nextLine());
}
public void display() {
System.out.println("Ten = " + name + ", nha san xuat = " + manufacturerName + ", gia = " + price);
}
}
Ứng Dụng Học
Theo dõi cập nhật nội dung học trên Youtube & Facebook
Thông Tin Liên Hệ
Công Ty Cổ Phần Công nghệ ZicZac Việt Nam.
Website: https://gozic.vn
SĐT: 096 - 70 25 996
Email: ziczacgroup@gmail.com
Thiết kế webiste chuyên nghiệp
Thiết kế phần mềm quản trị
Thiết kế ứng dụng Android
Thiết kế ứng dụng IOS
Thiết kế Web App
Hỗ trợ Digital Marketing
Hỗ trợ quảng cáo Google Ads
Hỗ trợ quảng cáo Facebook Ads
Hỗ trợ SEO Website