By GokiSoft.com|
14:41 16/06/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 - C2209I
#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.c2209i.bt991;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Book {
public String bookName;
public String authorName;
public int pageNum;
public 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 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: ");
price = Float.parseFloat(scan.nextLine());
}
public void display() {
System.out.format("\nTen sach: %s, ten tac gia: %s, so trang: %d, gia: %f",
bookName, authorName, pageNum, price);
}
}
#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.c2209i.bt991;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
Book b1 = new Book();
b1.input();
Book b2 = new Book("LAP TRINH C", "TRAN VAN DIEP", 20, 200000);
b1.display();
b2.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.c2209i.bt991;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Product {
public String title;
public String manufacturer;
public float price;
public Product() {
}
public Product(String title, String manufacturer, float price) {
this.title = title;
this.manufacturer = manufacturer;
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 ten NSX: ");
manufacturer = scan.nextLine();
System.out.println("Nhap gia: ");
price = Float.parseFloat(scan.nextLine());
}
public void display() {
System.out.format("\nTen san pham: %s, NSX: %s, gia: %f\n", title, manufacturer, price);
}
}
#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.c2209i.bt991;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Test {
public static void main(String[] args) {
ArrayList<Product> productList = new ArrayList<>();
Scanner scan = new Scanner(System.in);
int choose;
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1: {
System.out.println("Nhapso san pham N = ");
int n = Integer.parseInt(scan.nextLine());
// Product p = new Product(); (1)
Product p;//(3)
for (int i = 0; i < n; i++) {
// Product p = new Product(); (2)
p = new Product();//(3)
p.input();
productList.add(p);
}
break;
}
case 2:
System.out.println("Danh sach san pham: ");
for (Product p : productList) {
p.display();
}
break;
case 3: {
Collections.sort(productList, new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
if(o1.price > o2.price) return 1;
return -1;
}
});
break;
}
case 4:
System.out.println("Thoat!!!");
break;
default:
System.out.println("Nhap sai!!!");
break;
}
} while (choose != 4);
}
public static void showMenu() {
System.out.println("1. Nhap N san pham");
System.out.println("2. Hien thi");
System.out.println("3. Sap xep");
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)