By GokiSoft.com| 15:31 14/06/2023|
Java Basic

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

Bài tập 1:

Tạo class Book có các thuộc tính: Tên sách, tác giả số trang, giá tiền.

-       Tạo 2 constructor (1 có tham số và một không có tham số).

-       Tạo phương thức nhập và hiển thị.

Tạo class BookTest và khai báo hàm main.

-       Tạo 2 đối tượng của lớp Book. Một đối tượng được khởi tạo bằng constructor không có tham số và sau đó gọi hàm nhập.

-       Đối tượng còn lại khởi tạo bằng constructor có tham số.

Gọi phương thức hiển thị của 2 đối tượng này.

 

Bài tập 2:

Tạo class Product gồm các thuộc tính:

-       Tên hàng hóa

-       Nhà sản xuất

-       Giá bán

+ Tạo 2 constructor cho lớp này.

+ Cài đặt hàm nhập và hiển thị.

 

Tạo class ProductMenu, khai báo hàm main và tạo menu sau:

1.    Nhập thông tin cho n sản phẩm

2.    Hiển thị thông tin vừa nhập

3.    Sắp xếp thông tin giảm dần theo giá và hiển thị

4.    Thoát.

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

5

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

Lê Xuân Dũng [JavaFree]
Lê Xuân Dũng

2020-03-22 10:18:52



//Bai 2:


package productmenu;

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

public class ProductMenu {

    public static void main(String[] args) {
        int n, choose;
        Scanner sc = new Scanner(System.in);
        ArrayList<Product> productList = new ArrayList<>();
        do{
            
            System.out.println("1. Nhap thong tin N san pham.");
            System.out.println("2. Hien thi thong tin vua nhap.");
            System.out.println("3. Sap xem thong tin giam dan theo gia va hien thi");
            System.out.println("4. Thoat");
            System.out.println("==============================");
            choose = Integer.parseInt(sc.nextLine());
            
            switch(choose){
                case 1:
                    System.out.println("Nhap thong tin cho n san pham:");
                    System.out.println("Nhap n= ");
                    n = Integer.parseInt(sc.nextLine());
                    
                    for(int i = 0; i < n; i++){
                        Product product = new Product();
                        product.nhapHang();
                        productList.add(product);
                    }
                    break;
                case 2:
                    for(int i = 0; i < productList.size(); i++){
                        productList.get(i).hienthiHang();
                    }
                    break;
                case 3:
                    Collections.sort(productList, new Comparator<Product>() {//anonymous class
                        @Override
                        public int compare(Product o1, Product o2) {
                            if(o1.getGia() < o2.getGia()){
                                return 1;
                            } else if(o1.getGia() > o2.getGia()){
                                return -1;
                            } else {
                                return 0;
                            }
                            
                    }                        
                    });
                    
                    for(int i = 0; i < productList.size(); i++){
                        productList.get(i).hienthiHang();
                    }
                case 4:
                    System.out.println("Thoat");
                    break;
                default:
                    System.out.println("Nhap sai! Moi nhap lai!");
                    break;
            }
        } while (choose!=4);
    }
    
}



package productmenu;

import java.util.Scanner;

public class Product {
    String ten;
    String NSX;
    int gia;

    public Product() {
    }

    public Product(String ten, String NSX, int gia) {
        this.ten = ten;
        this.NSX = NSX;
        this.gia = gia;
    }

    public String getTen() {
        return ten;
    }

    public void setTen(String ten) {
        this.ten = ten;
    }

    public String getNSX() {
        return NSX;
    }

    public void setNSX(String NSX) {
        this.NSX = NSX;
    }

    public int getGia() {
        return gia;
    }

    public void setGia(int gia) {
        this.gia = gia;
    }
    
    
    
    public void nhapHang(){
        System.out.println("Nhap thong tin hang hoa");
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Nhap ten hang hoa: ");
        this.ten = sc.nextLine();
        
        System.out.println("Nhap nha san xuat: ");
        this.NSX = sc.nextLine();
        
        System.out.println("Nhap gia: ");
        this.gia = Integer.parseInt(sc.nextLine());
        System.out.println("==============================");
    }
    
    public void hienthiHang() {
        
        System.out.println("Ten hang: "+this.ten);
        System.out.println("Nha san xuat: "+this.NSX);
        System.out.println("Gia: "+this.gia);
        System.out.println("==============================");
    }
    
}



Phạm Kim Anh [JavaFree]
Phạm Kim Anh

2020-03-21 14:21:51



package gokisoft.com;

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

public class ProductMenu {

	public static void main(String[] args) {
		int choose;
		boolean exit = false;
		Scanner sc = new Scanner(System.in);
		ArrayList<Product>listP = new ArrayList<Product>();
		
		do {
			menu();
			System.out.println("Moi ban nhap lua chon: ");
			choose = Integer.parseInt(sc.nextLine());
			switch(choose) {
			case 1:
				System.out.println("Nhap so luong san pham ban muon them: ");
				int n = Integer.parseInt(sc.nextLine());
				for(int i = 0; i < n; i++) {
					Product p = new Product();
					p.nhap(listP);
					listP.add(p);
				}
				break;
			case 2:
				for(Product p : listP) {
					p.xuat();
				}
				break;
			case 3:
				Collections.sort(listP, new Comparator<Product>() {

					@Override
					public int compare(Product o1, Product o2) {
						// TODO Auto-generated method stub
						return o1.getGiaBan() >= o2.getGiaBan() ? -1:1;
					}
					
				});
				
				for(int i = 0; i < listP.size(); i++) {
					listP.get(i).xuat();
				}
				break;
			case 4:
				System.out.println("Cam on ban da su dung tien ich! ");
				exit = true;
				break;
			default:
				System.out.println("Moi ban nhap lua chon!");
				break;
			}
			
		}while(choose != 4);
		
	}
	
	static void menu() {
		System.out.println("=======================================================");
		System.out.println("1. Nhap thong tin cho n san pham.");
		System.out.println("2. Hien thi thong tin vua nhap.");
		System.out.println("3. Sap xep thong tin giam dan theo gia va hien thi.");
		System.out.println("4. Thoat.");
		System.out.println("=======================================================");
	}
}



Phạm Kim Anh [JavaFree]
Phạm Kim Anh

2020-03-21 14:21:28



package gokisoft.com;

import java.util.ArrayList;
import java.util.Scanner;

public class Product {

	String tenHangHoa;
	String nhaSanXuat;
	double giaBan;
	
	public Product() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param tenHangHoa
	 * @param nhaSanXuat
	 * @param giaBan
	 */
	public Product(String tenHangHoa, String nhaSanXuat, double giaBan) {
		super();
		this.tenHangHoa = tenHangHoa;
		this.nhaSanXuat = nhaSanXuat;
		this.giaBan = giaBan;
	}
	
	public void nhap() {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Moi ban nhap ten hang hoa: ");
		tenHangHoa = sc.nextLine();
		
		System.out.println("Moi ban nhap nha san xuat: ");
		nhaSanXuat = sc.nextLine();
		
		System.out.println("Moi ban nhap gia ban: ");
		giaBan = Double.parseDouble(sc.nextLine());
		
	}
	
	public void nhap(ArrayList<Product>listP) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Moi ban nhap ten hang hoa: ");
		tenHangHoa = sc.nextLine();
		
		System.out.println("Moi ban nhap nha san xuat: ");
		nhaSanXuat = sc.nextLine();
		
		System.out.println("Moi ban nhap gia ban: ");
		giaBan = Double.parseDouble(sc.nextLine());
		
	}
	
	public void xuat() {
		System.out.println("Ten hang hoa: " + tenHangHoa);
		System.out.println("Nha san xuat: "+nhaSanXuat);
		System.out.println("Gia ban: "+giaBan);
		System.out.println();
	}

	public String getTenHangHoa() {
		return tenHangHoa;
	}

	public void setTenHangHoa(String tenHangHoa) {
		this.tenHangHoa = tenHangHoa;
	}

	public String getNhaSanXuat() {
		return nhaSanXuat;
	}

	public void setNhaSanXuat(String nhaSanXuat) {
		this.nhaSanXuat = nhaSanXuat;
	}

	public double getGiaBan() {
		return giaBan;
	}

	public void setGiaBan(double giaBan) {
		this.giaBan = giaBan;
	}
	
	

}




Phạm Kim Anh [JavaFree]
Phạm Kim Anh

2020-03-21 14:21:08



package gokisoft.com;

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

public class ProductMenu {

	public static void main(String[] args) {
		int choose;
		boolean exit = false;
		Scanner sc = new Scanner(System.in);
		ArrayList<Product>listP = new ArrayList<Product>();
		
		do {
			menu();
			System.out.println("Moi ban nhap lua chon: ");
			choose = Integer.parseInt(sc.nextLine());
			switch(choose) {
			case 1:
				System.out.println("Nhap so luong san pham ban muon them: ");
				int n = Integer.parseInt(sc.nextLine());
				for(int i = 0; i < n; i++) {
					Product p = new Product();
					p.nhap(listP);
					listP.add(p);
				}
				break;
			case 2:
				for(Product p : listP) {
					p.xuat();
				}
				break;
			case 3:
				Collections.sort(listP, new Comparator<Product>() {

					@Override
					public int compare(Product o1, Product o2) {
						// TODO Auto-generated method stub
						return o1.getGiaBan() >= o2.getGiaBan() ? -1:1;
					}
					
				});
				
				for(int i = 0; i < listP.size(); i++) {
					listP.get(i).xuat();
				}
				break;
			case 4:
				System.out.println("Cam on ban da su dung tien ich! ");
				exit = true;
				break;
			default:
				System.out.println("Moi ban nhap lua chon!");
				break;
			}
			
		}while(choose != 4);
		
	}
	
	static void menu() {
		System.out.println("=======================================================");
		System.out.println("1. Nhap thong tin cho n san pham.");
		System.out.println("2. Hien thi thong tin vua nhap.");
		System.out.println("3. Sap xep thong tin giam dan theo gia va hien thi.");
		System.out.println("4. Thoat.");
		System.out.println("=======================================================");
	}
}


package gokisoft.com;

import java.util.ArrayList;
import java.util.Scanner;

public class Product {

	String tenHangHoa;
	String nhaSanXuat;
	double giaBan;
	
	public Product() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param tenHangHoa
	 * @param nhaSanXuat
	 * @param giaBan
	 */
	public Product(String tenHangHoa, String nhaSanXuat, double giaBan) {
		super();
		this.tenHangHoa = tenHangHoa;
		this.nhaSanXuat = nhaSanXuat;
		this.giaBan = giaBan;
	}
	
	public void nhap() {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Moi ban nhap ten hang hoa: ");
		tenHangHoa = sc.nextLine();
		
		System.out.println("Moi ban nhap nha san xuat: ");
		nhaSanXuat = sc.nextLine();
		
		System.out.println("Moi ban nhap gia ban: ");
		giaBan = Double.parseDouble(sc.nextLine());
		
	}
	
	public void nhap(ArrayList<Product>listP) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Moi ban nhap ten hang hoa: ");
		tenHangHoa = sc.nextLine();
		
		System.out.println("Moi ban nhap nha san xuat: ");
		nhaSanXuat = sc.nextLine();
		
		System.out.println("Moi ban nhap gia ban: ");
		giaBan = Double.parseDouble(sc.nextLine());
		
	}
	
	public void xuat() {
		System.out.println("Ten hang hoa: " + tenHangHoa);
		System.out.println("Nha san xuat: "+nhaSanXuat);
		System.out.println("Gia ban: "+giaBan);
		System.out.println();
	}

	public String getTenHangHoa() {
		return tenHangHoa;
	}

	public void setTenHangHoa(String tenHangHoa) {
		this.tenHangHoa = tenHangHoa;
	}

	public String getNhaSanXuat() {
		return nhaSanXuat;
	}

	public void setNhaSanXuat(String nhaSanXuat) {
		this.nhaSanXuat = nhaSanXuat;
	}

	public double getGiaBan() {
		return giaBan;
	}

	public void setGiaBan(double giaBan) {
		this.giaBan = giaBan;
	}
	
	

}



Phạm Kim Anh [JavaFree]
Phạm Kim Anh

2020-03-21 14:21:08



package gokisoft.com;

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

public class ProductMenu {

	public static void main(String[] args) {
		int choose;
		boolean exit = false;
		Scanner sc = new Scanner(System.in);
		ArrayList<Product>listP = new ArrayList<Product>();
		
		do {
			menu();
			System.out.println("Moi ban nhap lua chon: ");
			choose = Integer.parseInt(sc.nextLine());
			switch(choose) {
			case 1:
				System.out.println("Nhap so luong san pham ban muon them: ");
				int n = Integer.parseInt(sc.nextLine());
				for(int i = 0; i < n; i++) {
					Product p = new Product();
					p.nhap(listP);
					listP.add(p);
				}
				break;
			case 2:
				for(Product p : listP) {
					p.xuat();
				}
				break;
			case 3:
				Collections.sort(listP, new Comparator<Product>() {

					@Override
					public int compare(Product o1, Product o2) {
						// TODO Auto-generated method stub
						return o1.getGiaBan() >= o2.getGiaBan() ? -1:1;
					}
					
				});
				
				for(int i = 0; i < listP.size(); i++) {
					listP.get(i).xuat();
				}
				break;
			case 4:
				System.out.println("Cam on ban da su dung tien ich! ");
				exit = true;
				break;
			default:
				System.out.println("Moi ban nhap lua chon!");
				break;
			}
			
		}while(choose != 4);
		
	}
	
	static void menu() {
		System.out.println("=======================================================");
		System.out.println("1. Nhap thong tin cho n san pham.");
		System.out.println("2. Hien thi thong tin vua nhap.");
		System.out.println("3. Sap xep thong tin giam dan theo gia va hien thi.");
		System.out.println("4. Thoat.");
		System.out.println("=======================================================");
	}
}


package gokisoft.com;

import java.util.ArrayList;
import java.util.Scanner;

public class Product {

	String tenHangHoa;
	String nhaSanXuat;
	double giaBan;
	
	public Product() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param tenHangHoa
	 * @param nhaSanXuat
	 * @param giaBan
	 */
	public Product(String tenHangHoa, String nhaSanXuat, double giaBan) {
		super();
		this.tenHangHoa = tenHangHoa;
		this.nhaSanXuat = nhaSanXuat;
		this.giaBan = giaBan;
	}
	
	public void nhap() {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Moi ban nhap ten hang hoa: ");
		tenHangHoa = sc.nextLine();
		
		System.out.println("Moi ban nhap nha san xuat: ");
		nhaSanXuat = sc.nextLine();
		
		System.out.println("Moi ban nhap gia ban: ");
		giaBan = Double.parseDouble(sc.nextLine());
		
	}
	
	public void nhap(ArrayList<Product>listP) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Moi ban nhap ten hang hoa: ");
		tenHangHoa = sc.nextLine();
		
		System.out.println("Moi ban nhap nha san xuat: ");
		nhaSanXuat = sc.nextLine();
		
		System.out.println("Moi ban nhap gia ban: ");
		giaBan = Double.parseDouble(sc.nextLine());
		
	}
	
	public void xuat() {
		System.out.println("Ten hang hoa: " + tenHangHoa);
		System.out.println("Nha san xuat: "+nhaSanXuat);
		System.out.println("Gia ban: "+giaBan);
		System.out.println();
	}

	public String getTenHangHoa() {
		return tenHangHoa;
	}

	public void setTenHangHoa(String tenHangHoa) {
		this.tenHangHoa = tenHangHoa;
	}

	public String getNhaSanXuat() {
		return nhaSanXuat;
	}

	public void setNhaSanXuat(String nhaSanXuat) {
		this.nhaSanXuat = nhaSanXuat;
	}

	public double getGiaBan() {
		return giaBan;
	}

	public void setGiaBan(double giaBan) {
		this.giaBan = giaBan;
	}
	
	

}



Phạm Kim Anh [JavaFree]
Phạm Kim Anh

2020-03-21 13:51:26

package gokisoft.com;


import java.util.Scanner;


public class Book {


String tenSach;

String tacGia;

int soTrang;

double giaTien;

public Book() {

// TODO Auto-generated constructor stub

}


/**

* @param tenSach

* @param tacGia

* @param soTrang

* @param giaTien

*/

public Book(String tenSach, String tacGia, int soTrang, double giaTien) {

super();

this.tenSach = tenSach;

this.tacGia = tacGia;

this.soTrang = soTrang;

this.giaTien = giaTien;

}


public String getTenSach() {

return tenSach;

}


public void setTenSach(String tenSach) {

this.tenSach = tenSach;

}


public String getTacGia() {

return tacGia;

}


public void setTacGia(String tacGia) {

this.tacGia = tacGia;

}


public int getSoTrang() {

return soTrang;

}


public void setSoTrang(int soTrang) {

this.soTrang = soTrang;

}


public double getGiaTien() {

return giaTien;

}


public void setGiaTien(double giaTien) {

this.giaTien = giaTien;

}

public void nhap() {

Scanner sc = new Scanner(System.in);

System.out.println("Moi ban nhap ten sach: ");

tenSach = sc.nextLine();

System.out.println("Moi ban nhap tac gia: ");

tacGia = sc.nextLine();

System.out.println("Moi ban nhap so trang: ");

soTrang = Integer.parseInt(sc.nextLine());

System.out.println("Moi ban nhap gia tien: ");

giaTien = Double.parseDouble(sc.nextLine());

}

public void xuat() {

System.out.println(toString());

System.out.println();

}


@Override

public String toString() {

return " tenSach=" + tenSach + "\n tacGia=" + tacGia + "\n soTrang=" + soTrang + "\n giaTien=" + giaTien;

}

}



package gokisoft.com;

public class BookTest {

public static void main(String[] args) {
//tao doi tuong book 1
Book b1 = new Book();
//tao doi tuong book 2
Book b2 = new Book("Nha gia kim", "Paulo Coelho" , 228, 69.000f);
b1.nhap();
b1.xuat();
b2.xuat();
}
}


package gokisoft.com;

public class BookTest {

	public static void main(String[] args) {
		//tao doi tuong book 1
		Book b1 = new Book();
		//tao doi tuong book 2
		Book b2 = new Book("Nha gia kim", "Paulo Coelho" , 228, 69.000f);
		
		b1.nhap();
		
		b1.xuat();
		b2.xuat();
	}
}



package gokisoft.com;

import java.util.Scanner;

public class Book {

	String tenSach;
	String tacGia;
	int soTrang;
	double giaTien;
	
	public Book() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param tenSach
	 * @param tacGia
	 * @param soTrang
	 * @param giaTien
	 */
	public Book(String tenSach, String tacGia, int soTrang, double giaTien) {
		super();
		this.tenSach = tenSach;
		this.tacGia = tacGia;
		this.soTrang = soTrang;
		this.giaTien = giaTien;
	}

	public String getTenSach() {
		return tenSach;
	}

	public void setTenSach(String tenSach) {
		this.tenSach = tenSach;
	}

	public String getTacGia() {
		return tacGia;
	}

	public void setTacGia(String tacGia) {
		this.tacGia = tacGia;
	}

	public int getSoTrang() {
		return soTrang;
	}

	public void setSoTrang(int soTrang) {
		this.soTrang = soTrang;
	}

	public double getGiaTien() {
		return giaTien;
	}

	public void setGiaTien(double giaTien) {
		this.giaTien = giaTien;
	}
	
	public void nhap() {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Moi ban nhap ten sach: ");
		tenSach = sc.nextLine();
		
		System.out.println("Moi ban nhap tac gia: ");
		tacGia = sc.nextLine();
		
		System.out.println("Moi ban nhap so trang: ");
		soTrang = Integer.parseInt(sc.nextLine());
		
		System.out.println("Moi ban nhap gia tien: ");
		giaTien = Double.parseDouble(sc.nextLine());
		
	}
	
	public void xuat() {
		System.out.println(toString());
		System.out.println();
	}

	@Override
	public String toString() {
		return " tenSach=" + tenSach + "\n tacGia=" + tacGia + "\n soTrang=" + soTrang + "\n giaTien=" + giaTien;
	}
	
	
}



package gokisoft.com;

import java.util.Scanner;

public class Book {

	String tenSach;
	String tacGia;
	int soTrang;
	double giaTien;
	
	public Book() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param tenSach
	 * @param tacGia
	 * @param soTrang
	 * @param giaTien
	 */
	public Book(String tenSach, String tacGia, int soTrang, double giaTien) {
		super();
		this.tenSach = tenSach;
		this.tacGia = tacGia;
		this.soTrang = soTrang;
		this.giaTien = giaTien;
	}

	public String getTenSach() {
		return tenSach;
	}

	public void setTenSach(String tenSach) {
		this.tenSach = tenSach;
	}

	public String getTacGia() {
		return tacGia;
	}

	public void setTacGia(String tacGia) {
		this.tacGia = tacGia;
	}

	public int getSoTrang() {
		return soTrang;
	}

	public void setSoTrang(int soTrang) {
		this.soTrang = soTrang;
	}

	public double getGiaTien() {
		return giaTien;
	}

	public void setGiaTien(double giaTien) {
		this.giaTien = giaTien;
	}
	
	public void nhap() {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Moi ban nhap ten sach: ");
		tenSach = sc.nextLine();
		
		System.out.println("Moi ban nhap tac gia: ");
		tacGia = sc.nextLine();
		
		System.out.println("Moi ban nhap so trang: ");
		soTrang = Integer.parseInt(sc.nextLine());
		
		System.out.println("Moi ban nhap gia tien: ");
		giaTien = Double.parseDouble(sc.nextLine());
		
	}
	
	public void xuat() {
		System.out.println(toString());
		System.out.println();
	}

	@Override
	public String toString() {
		return " tenSach=" + tenSach + "\n tacGia=" + tacGia + "\n soTrang=" + soTrang + "\n giaTien=" + giaTien;
	}
	
	
}



Phạm Kim Anh [JavaFree]
Phạm Kim Anh

2020-03-21 13:50:48

package gokisoft.com;


import java.util.Scanner;


public class Book {


String tenSach;

String tacGia;

int soTrang;

double giaTien;

public Book() {

// TODO Auto-generated constructor stub

}


/**

* @param tenSach

* @param tacGia

* @param soTrang

* @param giaTien

*/

public Book(String tenSach, String tacGia, int soTrang, double giaTien) {

super();

this.tenSach = tenSach;

this.tacGia = tacGia;

this.soTrang = soTrang;

this.giaTien = giaTien;

}


public String getTenSach() {

return tenSach;

}


public void setTenSach(String tenSach) {

this.tenSach = tenSach;

}


public String getTacGia() {

return tacGia;

}


public void setTacGia(String tacGia) {

this.tacGia = tacGia;

}


public int getSoTrang() {

return soTrang;

}


public void setSoTrang(int soTrang) {

this.soTrang = soTrang;

}


public double getGiaTien() {

return giaTien;

}


public void setGiaTien(double giaTien) {

this.giaTien = giaTien;

}

public void nhap() {

Scanner sc = new Scanner(System.in);

System.out.println("Moi ban nhap ten sach: ");

tenSach = sc.nextLine();

System.out.println("Moi ban nhap tac gia: ");

tacGia = sc.nextLine();

System.out.println("Moi ban nhap so trang: ");

soTrang = Integer.parseInt(sc.nextLine());

System.out.println("Moi ban nhap gia tien: ");

giaTien = Double.parseDouble(sc.nextLine());

}

public void xuat() {

System.out.println(toString());

System.out.println();

}


@Override

public String toString() {

return " tenSach=" + tenSach + "\n tacGia=" + tacGia + "\n soTrang=" + soTrang + "\n giaTien=" + giaTien;

}

}



package gokisoft.com;

public class BookTest {

public static void main(String[] args) {
//tao doi tuong book 1
Book b1 = new Book();
//tao doi tuong book 2
Book b2 = new Book("Nha gia kim", "Paulo Coelho" , 228, 69.000f);
b1.nhap();
b1.xuat();
b2.xuat();
}
}


package gokisoft.com;

public class BookTest {

	public static void main(String[] args) {
		//tao doi tuong book 1
		Book b1 = new Book();
		//tao doi tuong book 2
		Book b2 = new Book("Nha gia kim", "Paulo Coelho" , 228, 69.000f);
		
		b1.nhap();
		
		b1.xuat();
		b2.xuat();
	}
}



Phạm Kim Anh [JavaFree]
Phạm Kim Anh

2020-03-21 13:50:33

package gokisoft.com;


import java.util.Scanner;


public class Book {


String tenSach;

String tacGia;

int soTrang;

double giaTien;

public Book() {

// TODO Auto-generated constructor stub

}


/**

* @param tenSach

* @param tacGia

* @param soTrang

* @param giaTien

*/

public Book(String tenSach, String tacGia, int soTrang, double giaTien) {

super();

this.tenSach = tenSach;

this.tacGia = tacGia;

this.soTrang = soTrang;

this.giaTien = giaTien;

}


public String getTenSach() {

return tenSach;

}


public void setTenSach(String tenSach) {

this.tenSach = tenSach;

}


public String getTacGia() {

return tacGia;

}


public void setTacGia(String tacGia) {

this.tacGia = tacGia;

}


public int getSoTrang() {

return soTrang;

}


public void setSoTrang(int soTrang) {

this.soTrang = soTrang;

}


public double getGiaTien() {

return giaTien;

}


public void setGiaTien(double giaTien) {

this.giaTien = giaTien;

}

public void nhap() {

Scanner sc = new Scanner(System.in);

System.out.println("Moi ban nhap ten sach: ");

tenSach = sc.nextLine();

System.out.println("Moi ban nhap tac gia: ");

tacGia = sc.nextLine();

System.out.println("Moi ban nhap so trang: ");

soTrang = Integer.parseInt(sc.nextLine());

System.out.println("Moi ban nhap gia tien: ");

giaTien = Double.parseDouble(sc.nextLine());

}

public void xuat() {

System.out.println(toString());

System.out.println();

}


@Override

public String toString() {

return " tenSach=" + tenSach + "\n tacGia=" + tacGia + "\n soTrang=" + soTrang + "\n giaTien=" + giaTien;

}

}



package gokisoft.com;

public class BookTest {

public static void main(String[] args) {
//tao doi tuong book 1
Book b1 = new Book();
//tao doi tuong book 2
Book b2 = new Book("Nha gia kim", "Paulo Coelho" , 228, 69.000f);
b1.nhap();
b1.xuat();
b2.xuat();
}
}


Lê Xuân Dũng [JavaFree]
Lê Xuân Dũng

2020-03-21 09:21:18



//Bai 1:

package booktest;

import java.util.Scanner;

public class Book {
    String bookName;
    String Author;
    int pageNumber;
    int price;

    public Book() {
    }

    public Book(String bookName, String Author, int pageNumber, int price) {
        this.bookName = bookName;
        this.Author = Author;
        this.pageNumber = pageNumber;
        this.price = price;
    }
    
    public void addBook(){
        System.out.println("Nhap thong tin sach: ");
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Nhap ten sach: ");
        this.bookName = sc.nextLine();
        System.out.println("Nhap ten tac gia: ");
        this.Author = sc.nextLine();
        System.out.println("Nhap so trang: ");
        this.pageNumber = Integer.parseInt(sc.nextLine());
        System.out.println("Nhap gia tien: ");
        this.price = Integer.parseInt(sc.nextLine());
    }
    
    public void showBook(){
        System.out.println("==============================");
        System.out.println("Ten sach: "+this.bookName);
        System.out.println("Tac gia: "+this.Author);
        System.out.println("So trang: "+this.pageNumber);
        System.out.println("Gia tien: "+this.price);
        System.out.println("==============================");
    }
}


package booktest;
public class BookTest {
    public static void main(String[] args) {
        Book book1 = new Book();
        book1.addBook();
        Book book2 = new Book("Thien Long Bat Bo", "Kim Dung", 2123, 686000);
        
        book1.showBook();
        book2.showBook();
    }
    
}



Minh Nghia [T1907A]
Minh Nghia

2020-03-19 06:43:19



/*
 * 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 Bt44;

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class Book {
    String nameBook;
    String Author;
    int numberOfPage;
    int price;

    public Book() {
    }

    public Book(String nameBook, String Author, int numberOfPage, int price) {
        this.nameBook = nameBook;
        this.Author = Author;
        this.numberOfPage = numberOfPage;
        this.price = price;
    }
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ten sach:");
        nameBook = scan.nextLine();
        
        System.out.println("Nhap ten tac gia:");
        Author = scan.nextLine();
        
        System.out.println("Nhap so trang:");
        numberOfPage = Integer.parseInt(scan.nextLine());
        
        System.out.println("Nhap gia ban:");
        price = Integer.parseInt(scan.nextLine());
        
    
        
    }
    public void display(){
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "/nBook{" + "nameBook=" + nameBook + ", Author=" + Author + ", numberOfPage=" + numberOfPage + ", price=" + price + '}';
    }
    
    
}




/*
 * 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 Bt44;

/**
 *
 * @author Administrator
 */
public class bookTest {
    public static void main(String[] args) {
        Book book = new Book();
        
        book.input();
        
        Book book1 = new Book(book.nameBook,book.Author,book.numberOfPage,book.price);
        book.display();
        book1.display();
        
        
        
        
        
    }

   
    
    
}



/*
 * 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 Bt44;


import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class Product {
    String goodsName;
    String producer;
    int price;

    public Product() {
    }

    public Product(String goodsName, String producer, int price) {
        this.goodsName = goodsName;
        this.producer = producer;
        this.price = price;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public String getProducer() {
        return producer;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
    
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ten hang hoa:");
        goodsName = scan.nextLine();
        System.out.println("Nhap ten nsx:");
        producer = scan.nextLine();
        System.out.println("Nhap gia ban:");
        price = Integer.parseInt(scan.nextLine());
    }
    public void display(){
        System.out.println("Ten hang san xuat:" + goodsName);
        System.out.println("Ten nha sx:" + producer);
        System.out.println("Gia ban:" + price);
    }
}



/*
 * 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 Bt44;



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

/**
 *
 * @author Administrator
 */
public class productMenu {
    public static void main(String[] args) {
       int choose;
       Scanner scan = new Scanner(System.in);
       ArrayList<Product> list = new ArrayList();
       
       do{
           showMenu();
           choose = Integer.parseInt(scan.nextLine());
           
           switch(choose){
                case 1:
                    System.out.println("Nhap thong tin cho N sp:");
                    int n = Integer.parseInt(scan.nextLine());
                    
                    for(int i = 0; i < n; i++){
                        Product product = new Product();
                        product.input();
                        
                        list.add(product);
                    }
                   break;
                case 2:
                    for(int i = 0; i < list.size(); i++){
                        list.get(i).display();
                    }
                   break;
                case 3:
                    Collections.sort(list, new Comparator<Product>() {
                        @Override
                        public int compare(Product o1, Product o2) {
                            if(o1.getPrice() < o2.getPrice()){
                                return 1;
                            }
                            return -1;
                    }                        
                    });
                    for (int i = 0; i < list.size(); i++){
                        list.get(i).display();
                    }
                   break;
                case 4:
                    System.out.println("Thoat");
                   break;
                default:
                    System.out.println("Chon lai");
                    break;
           }
       }while(choose != 4);
       
    }
    public static void showMenu(){
        System.out.println("1. Nhap thong tin N san pham.");
        System.out.println("2. Hien thi thong tin vua nhap.");
        System.out.println("3. Sap xem thong tin giam dan theo gia va hien thi");
        System.out.println("4. Thoat");
        System.out.println("Chon lai:");
    }
}