By GokiSoft.com|
14:40 14/09/2022|
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 - C2109I
#Book.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.lesson04.Main;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Book {
String bookName, authorName;
int pageNum;
float price;
public Book() {
}
public Book(String bookName, String authorName, int pageNum, float price) {
this.bookName = bookName;
this.authorName = authorName;
this.pageNum = pageNum;
this.price = price;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten sach: ");
bookName = 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 tien: ");
price = Float.parseFloat(scan.nextLine());
}
public void display() {
System.out.println(toString());
// System.out.println(this);
}
@Override
public String toString() {
return "bookName=" + bookName + ", authorName=" + authorName + ", pageNum=" + pageNum + ", price=" + price;
}
}
#BT991.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.lesson04.Main;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class BT991 {
public static void main(String[] args) {
//Bai 1:
// Book b1 = new Book();
// b1.input();
//
// b1.display();
//
// Scanner scan = new Scanner(System.in);
//
// System.out.println("Nhap ten sach: ");
// String bookName = scan.nextLine();
// System.out.println("Nhap tac gia: ");
// String authorName = scan.nextLine();
// System.out.println("Nhap so trang: ");
// int pageNum = Integer.parseInt(scan.nextLine());
// System.out.println("Nhap gia tien: ");
// float price = Float.parseFloat(scan.nextLine());
//
// Book b2 = new Book(bookName, authorName, pageNum, price);
// b2.display();
//Bai 2:
ArrayList<Product> productList = new ArrayList<>();
int choose;
Scanner scan = new Scanner(System.in);
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1: {
System.out.println("Nhap so san pham can them N = ");
int N = Integer.parseInt(scan.nextLine());
// Product p = new Product();
Product p;
for (int i = 0; i < N; i++) {
// Product p = new Product();
p = new Product();
p.input();
productList.add(p);
}
break;
}
case 2:
System.out.println("Thong tin san pham");
for (Product product : productList) {
product.display();
}
break;
case 3:
Collections.sort(productList, new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
if(o1.getPrice() >= o2.getPrice()) return -1;
return 1;
}
});
System.out.println("Thong tin san pham");
for (Product product : productList) {
product.display();
}
break;
case 4:
System.out.println("Thoat!!!");
break;
default:
System.out.println("Nhap sai!!!");
break;
}
} while (choose != 4);
}
static void showMenu() {
System.out.println("1. Nhap 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: ");
}
}
#C2109L.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
*/
package com.gokisoft.lesson04.Main;
/**
*
* @author diepvan
*/
public class C2109L {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
#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.lesson04.Main;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Product {
String title, nsx;
float price;
public Product() {
}
public Product(String title, String nsx, float price) {
this.title = title;
this.nsx = nsx;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNsx() {
return nsx;
}
public void setNsx(String nsx) {
this.nsx = nsx;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten san pham: ");
title = scan.nextLine();
System.out.println("Nhap nsx: ");
nsx = scan.nextLine();
System.out.println("Nhap gia ban: ");
price = Float.parseFloat(scan.nextLine());
}
public void display() {
System.out.println(toString());
}
@Override
public String toString() {
return "title=" + title + ", nsx=" + nsx + ", price=" + price;
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)