By GokiSoft.com| 19:37 27/06/2022|
Java Basic

[Source 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 - C2108L

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


#Book.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt991;

import java.util.Scanner;

/**
 *
 * @author std
 */
public class Book {
    String name;
    String authorName;
    int pageNum;
    int price;

    public Book() {
    }

    public Book(String name, String authorName, int pageNum, int price) {
        this.name = name;
        this.authorName = authorName;
        this.pageNum = pageNum;
        this.price = price;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten sach: ");
        name = scan.nextLine();
        
        System.out.println("Nhap tac gia: ");
        authorName = scan.nextLine();
        
        System.out.println("Nhap so trang: ");
        pageNum = Integer.parseInt(scan.nextLine());
        
        System.out.println("Nhap gia: ");
        price = Integer.parseInt(scan.nextLine());
    }
    
    public void display() {
        System.out.format("\nTen sach: %s, tac gia: %s, so trang: %d, gia: %d", name, authorName, pageNum, price);
    }
}


#Product.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt991;

import java.util.Scanner;

/**
 *
 * @author std
 */
public class Product {
    String title;
    String manufacturerName;
    float price;

    public Product() {
    }

    public Product(String title, String manufacturerName, float price) {
        this.title = title;
        this.manufacturerName = manufacturerName;
        this.price = price;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap tieu de: ");
        title = scan.nextLine();
        
        System.out.println("Nhap nsx: ");
        manufacturerName = scan.nextLine();
        
        System.out.println("Nhap gia: ");
        price = Integer.parseInt(scan.nextLine());
    }
    
    public void display() {
        System.out.format("\nTieu de: %s, nsx: %s, gia: %f\n", title, manufacturerName, price);
    }
}


#Test.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt991;

/**
 *
 * @author std
 */
public class Test {
    public static void main(String[] args) {
        Book b1 = new Book();
        b1.input();
        
        b1.display();
        
        Book b2 = new Book("LAP TRINH C", "TRAN VAN DIEP", 20, 200000);
        b2.display();
    }
}


#Test2.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt991;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 *
 * @author std
 */
public class Test2 {
    public static void main(String[] args) {
        ArrayList<Product> products = new ArrayList<>();
        
        Scanner scan = new Scanner(System.in);
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose) {
                case 1:
                    System.out.println("Nhap N san pham can them = ");
                    int N = Integer.parseInt(scan.nextLine());
                    
                    for (int i = 0; i < N; i++) {
                        Product p = new Product();
                        p.input();
                        
                        products.add(p);
                    }
                    break;
                case 2:
                    System.out.println("Thong tin N san pham: ");
                    for (Product product : products) {
                        product.display();
                    }
                    break;
                case 3:
                    Collections.sort(products, new Comparator<Product>() {
                        @Override
                        public int compare(Product o1, Product o2) {
                            if(o1.price > o2.price) {
                                //Sap xep dugn
                                return -1;
                            }
                            return 1;
                        }
                    });
                    System.out.println("Thong tin N san pham sau sap xep: ");
                    for (Product product : products) {
                        product.display();
                    }
                    break;
                case 4:
                    System.out.println("Thoat!!!");
                    break;
            }
        } while(choose != 4);
    }
    
    static void showMenu() {
        System.out.println("1. Nhap thong tin N san pham");
        System.out.println("2. Hien thi");
        System.out.println("3. Sap xep theo gia giam dan");
        System.out.println("4. Thoat");
        System.out.println("Chon: ");
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)