By GokiSoft.com| 20:03 03/02/2023|
Java Basic

[Source Code] Tìm hiểu OOP - Đóng gói & kế thừa - overloading & Exception Java - 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();
    }
}

#People.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 People {
    String name;
    String cccd;
    String address;

    public People() {
    }

    public People(String name, String cccd, String address) {
        this.name = name;
        this.cccd = cccd;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCccd() {
        return cccd;
    }

    public void setCccd(String cccd) {
        this.cccd = cccd;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten: ");
        name = scan.nextLine();
        
        System.out.println("Nhap cccd: ");
        cccd = scan.nextLine();
        
        System.out.println("Nhap dia chi: ");
        address = scan.nextLine();
    }
    
    public void display() {
        System.out.println(name + ", " + cccd + ", " + address);
    }
    
    public void test1() {
        System.out.println("Hello");
    }
    
    public void test2() {
        System.out.println("Hello > " + name);
    }
}

#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 {
    protected String name;
    private String manufacturerName;
    public float price;
    float discount;

    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);
    }
    
    public void display(String msg) {
        System.out.println("Thong diep > " + msg);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getManufacturerName() {
        return manufacturerName;
    }

    public void setManufacturerName(String manufacturerName) {
        this.manufacturerName = manufacturerName;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        if(price < 0) {
            System.err.println("Yeu cau price >= 0");
        } else {
            this.price = price;
        }
    }
    
    
}

#Student.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 Student extends People{
    String email;
    String rollno;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRollno() {
        return rollno;
    }

    public void setRollno(String rollno) {
        this.rollno = rollno;
    }
    
    @Override
    public void input() {
        super.input();
        
        Scanner scan = new Scanner(System.in);
        
//        System.out.println("Nhap ten: ");
//        name = scan.nextLine();
//        
//        System.out.println("Nhap cccd: ");
//        cccd = scan.nextLine();
//        
//        System.out.println("Nhap dia chi: ");
//        address = scan.nextLine();
        
        System.out.println("Nhap email: ");
        email = scan.nextLine();
        
        System.out.println("Nhap msv: ");
        rollno = scan.nextLine();
    }
    
    @Override
    public void display() {
        System.out.println(name + ", " + cccd + ", " + address + ", " + email + ", " + rollno);
    }
    
    public void learning() {
        System.out.println("Hoc ...");
    }
}

#Test.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;

/**
 *
 * @author diepvan
 */
public class Test {
    public static void main(String[] args) {
        Product p = new Product();
        
        p.price = -50;
//        p.setPrice(50);
        p.name = "Xin chao";
//        p.manufacturerName = "ABC";
        
        System.out.println(p.getPrice());
    }
}

#Test3.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;

/**
 *
 * @author diepvan
 */
public class Test3 {
    public static void main(String[] args) {
        People people = new People();
//        people.input();
        people.name = "A";
        
        Student std2 = new Student();
        std2.name = "C";
        
        Student std = new Student();
//        std.input();
        std.name = "B";
        
        System.out.println(people.name);
        System.out.println(std.name);
        System.out.println(std2.name);
        
        people.test1();
        std.test1();
        std2.test1();
        
        people.test2();
        std.test2();
        std2.test2();
        
        std.learning();
        
        people.display();
        
        std.display();
        
        
        people.input();
        std.input();
    }
}

#Test4.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;

/**
 *
 * @author diepvan
 */
public class Test4 {
    public static void main(String[] args) {
        //Exception
        //logic
        int x = 6;
        int y = 0;
        try {
            int z = x / y;
            int k = 10;
            System.out.println(z);
        } catch(ArithmeticException e) {
            e.printStackTrace();
        }
        
        int t[] = {1, 10};
        System.out.println(t[0]);
        
//        Product p = null;
        Product p = new Product();
        p.name = "ABC";
        System.out.println(p.name);
        //Thu vien ben thu 3
        //System
        
        try {
            Product p2 = null;
            p2.name = "123";
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("Xu ly cuoi cung");
        }
        
        System.out.println("AAAAa");
    }
}

#Test2.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.sub;

import com.gokisoft.lesson03.Product;

/**
 *
 * @author diepvan
 */
public class Test2 {
    public static void main(String[] args) {
        Product p = new Product();
        
        p.price = -50;
//        p.name = "Xin chao";
//        p.manufacturerName = "ABC";
        
        System.out.println(p.getPrice());
    }
}
Tags:

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

5

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