By GokiSoft.com|
19:15 03/02/2023|
Java Basic
[Share Code] Java basic- OOP - căn bản - Tổng hợp ví dụ lập trính hướng đổi tượng trong java - mới bắt đầu với OOP - C2206L
#Main.java
/*
* 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();
}
}
#Product.java
/*
* 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);
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)