Java basic- Assignment - Quản lý nhà
* Xây dựng một giao diện (interface) IHouse nằm trong gói house.cm gồm có 2 phương thức:
- public void input();
- public void display();
* Xây dựng lớp House thực thi giao diện IHouse và cũng nằm trong gói house.cm, bổ sung thêm các thuộc tính sau:
private String soNha;
private String diaChi;
private String loaiNha;
Cài đặt các Constructor có tham số và không có tham số.
Cài đặt các phương thức set/get cho các thuộc tính trong lớp.
Cài đè (override) các phương thức nhập và hiển thị trong giao diện IHouse.
* Cài đặt lớp HaNoiHouse nằm trong gói house.hanoi kế thừa lớp House ở trên vào bổ sung thêm các thuộc tính:
- private String tenQuan;
Cài đặt 2 constructor, trong đó constructor có tham số phải sử dụng từ khóa super để gọi constructor của lớp House.
Cài đặt các phương thức get/set cho thuộc tính bổ sung.
Override các phương thức input(), display() trong lớp House.
* Xây dựng lớp ManagerHouse nằm trong gói house.hanoi
Tạo menu như sau và cài đặt để thực thi các công việc theo từng menu đó:
1. Nhập thông tin n ngôi nhà ở Hà Nội
2. Hiển thị thông tin của n ngôi nhà đó.
3. Sắp xếp theo trường địa chỉ và hiển thị thông tin sau khi sắp xếp.
4. Tìm kiếm nhà theo địa chỉ nhập vào.
5. Thoát.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)

Đỗ Quốc Dũng
2024-05-13 13:17:43
#House.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 house.cm;
import java.util.Scanner;
/**
*
* @author ASUS
*/
public class House implements IHouse{
private String soNha;
private String diaChi;
private String loaiNha;
public House() {
}
public House(String soNha, String diaChi, String loaiNha) {
this.soNha = soNha;
this.diaChi = diaChi;
this.loaiNha = loaiNha;
}
public String getSoNha() {
return soNha;
}
public void setSoNha(String soNha) {
this.soNha = soNha;
}
public String getDiaChi() {
return diaChi;
}
public void setDiaChi(String diaChi) {
this.diaChi = diaChi;
}
public String getLoaiNha() {
return loaiNha;
}
public void setLoaiNha(String loaiNha) {
this.loaiNha = loaiNha;
}
@Override
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.println("Nhap so nha: ");
soNha = scanner.nextLine();
System.out.println("Nhap dia chi: ");
diaChi = scanner.nextLine();
System.out.println("Loai nha: ");
loaiNha = scanner.nextLine();
}
@Override
public void display() {
System.out.println("So nha: "+soNha);
System.out.println("Dia chi: "+diaChi);
System.out.println("Loai nha: "+loaiNha);
}
}
#IHouse.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package house.cm;
/**
*
* @author ASUS
*/
public interface IHouse {
public void input();
public void display();
}
#HaNoiHouse.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 house.hanoi;
import java.util.Scanner;
import house.cm.House;
/**
*
* @author ASUS
*/
public class HaNoiHouse extends House{
private String tenQuan;
public HaNoiHouse(String soNha, String diaChi, String loaiNha) {
super(soNha, diaChi, loaiNha);
}
public HaNoiHouse() {
}
public String getTenQuan() {
return tenQuan;
}
public void setTenQuan(String tenQuan) {
this.tenQuan = tenQuan;
}
@Override
public void display() {
super.display();
System.out.println("Ten quan:" + tenQuan);
}
@Override
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.println("Nhap ten quan:");
tenQuan = scanner.nextLine();
super.input();
}
}
#ManagerHouse.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 house.hanoi;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @author ASUS
*/
public class ManagerHouse {
public static void main(String[] args) {
int choose;
Scanner scanner = new Scanner(System.in);
ArrayList<HaNoiHouse> list = new ArrayList<>();
do {
showMenu();
System.out.println("Nhap lua chon cua ban: ");
choose = Integer.parseInt(scanner.nextLine());
switch (choose) {
case 1:
System.out.println("Nhap thong tin N ngoi nha o Ha Noi: ");
int n = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < n; i++) {
System.out.println("Nhap thong tin ngoi nha so "+(i+1));
HaNoiHouse haNoiHouse = new HaNoiHouse();
haNoiHouse.input();
list.add(haNoiHouse);
}
break;
case 2:
for (int i = 0; i < list.size(); i++) {
list.get(i).display();
}
break;
case 3:
System.out.println("Sap xep theo dia chi: ");
Collections.sort(list, new Comparator<HaNoiHouse>() {
@Override
public int compare(HaNoiHouse o1, HaNoiHouse o2) {
return o1.getDiaChi().compareToIgnoreCase(o2.getDiaChi());
}
});
for (HaNoiHouse haNoiHouse : list) {
haNoiHouse.display();
}
break;
case 4:
System.out.println("Nhap dia chi nha can tim kiem: ");
String diachi = scanner.nextLine();
boolean found = false;
for (HaNoiHouse haNoiHouse : list) {
if (haNoiHouse.getDiaChi().equalsIgnoreCase(diachi)) {
System.out.println("Thong tin tim duoc dia chi nha la: ");
haNoiHouse.display();
found = true;
}
if (!found) {
System.out.println("Dia chi nha khong ton tai vui long nhap lai!!!");
}
}
break;
case 5:
System.out.println("Ban chon thoat chuong trinh");
break;
default:
System.out.println("Chon sai cu phap!!!");
break;
}
} while (choose!=5);
}
private static void showMenu() {
System.out.println("1.Nhap thong tin n ngôi nhà ở Hà Nội");
System.out.println("2. Hiển thị thông tin của n ngôi nhà đó");
System.out.println("3. Sắp xếp theo địa chỉ");
System.out.println("4. Tìm kiếm theo địa chỉ");
System.out.println("5. Thoát");
}
}

Tran Ba Quan
2024-05-13 13:16:15
#HaNoiHouse.java
package house.hanoi;
import java.util.*;
import house.cm.House;
public class HaNoiHouse extends House {
private String tenQuan;
public HaNoiHouse() { super(); }
public HaNoiHouse(String soNha, String diaChi, String loaiNha, String tenQuan) {
super(soNha, diaChi, loaiNha);
this.tenQuan = tenQuan;
}
public String getTenQuan() {
return tenQuan;
}
public void setTenQuan(String tenQuan) {
this.tenQuan = tenQuan;
}
@Override
public void nhap() {
super.nhap();
System.out.println("Nhập tên quận: ");
tenQuan = (new Scanner(System.in)).nextLine();
}
@Override
public void hienThi() {
super.hienThi();
System.out.println("Tên quận: " + tenQuan);
}
}
#ManagerHouse.java
package house.hanoi;
import java.util.*;
public class ManagerHouse {
private List<HaNoiHouse> houses = new ArrayList<>();
public void sortHouses() {
Collections.sort(houses, new Comparator<HaNoiHouse>() {
@Override
public int compare(HaNoiHouse h1, HaNoiHouse h2) {
return h1.getDiaChi().compareTo(h2.getDiaChi());
}
});
}
public int binarySearch(String diaChi) {
int left = 0, right = houses.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int res = houses.get(mid).getDiaChi().compareTo(diaChi);
if (res == 0)
return mid;
if (res < 0)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
public void menu() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Nhập thông tin n ngôi nhà ở Hà Nội");
System.out.println("2. Hiển thị thông tin của n ngôi nhà đó.");
System.out.println("3. Sắp xếp theo trường địa chỉ và hiển thị thông tin sau khi sắp xếp.");
System.out.println("4. Tìm kiếm nhà theo địa chỉ nhập vào.");
System.out.println("5. Thoát.");
System.out.print("Chọn: ");
int choice = scanner.nextInt(), n = 0;
switch (choice) {
case 1:
System.out.println("Nhập n: ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++) {
HaNoiHouse hn = new HaNoiHouse();
hn.nhap();
}
break;
case 2:
System.out.println("Thông tin n nhà: ");
for (int i = 0; i < n; i++) {
System.out.println("Nhà số " + i + " : ");
HaNoiHouse hn = houses.get(i);
hn.hienThi();
}
sortHouses();
break;
case 3:
System.out.println("Đã sắp xếp");
break;
case 4:
String s = (new Scanner(System.in)).nextLine();
int bs = binarySearch(s);
if (bs == -1)
System.out.println("Không tồn tại nhà như vậy");
else {
HaNoiHouse hn = houses.get(bs);
hn.hienThi();
}
break;
case 5:
System.out.println("Thoát.");
return;
default:
System.out.println("Lựa chọn không hợp lệ. Vui lòng chọn lại.");
}
}
}
}
#House.java
package house.cm;
import java.util.*;
public class House implements IHouse {
private String soNha;
private String diaChi;
private String loaiNha;
public House() {}
public House(String soNha, String diaChi, String loaiNha) {
this.soNha = soNha;
this.diaChi = diaChi;
this.loaiNha = loaiNha;
}
public String getSoNha() {
return soNha;
}
public void setSoNha(String soNha) {
this.soNha = soNha;
}
public String getDiaChi() {
return diaChi;
}
public void setDiaChi(String diaChi) {
this.diaChi = diaChi;
}
public String getLoaiNha() {
return loaiNha;
}
public void setLoaiNha(String loaiNha) {
this.loaiNha = loaiNha;
}
@Override
public void nhap() {
Scanner sc = new Scanner(System.in);
System.out.println("Nhập thông tin nhà: ");
System.out.println("Nhập số nhà: ");
setSoNha(sc.nextLine());
System.out.println("Nhập địa chỉ: ");
setDiaChi(sc.nextLine());
System.out.println("Nhập loại nhà: ");
setLoaiNha(sc.nextLine());
}
@Override
public void hienThi() {
System.out.println("Thông tin nhà: ");
System.out.println("Số nhà: " + getSoNha());
System.out.println("Địa chỉ: " + getDiaChi());
System.out.println("Loại nhà: " + getLoaiNha());
}
}
#IHouse.java
package house.cm;
import java.util.*;
public interface IHouse {
public void nhap();
public void hienThi();
}
![nguyễn văn huy [T1907A]](https://www.gravatar.com/avatar/b107d14d7d43c142b68c12c377262371.jpg?s=80&d=mm&r=g)
nguyễn văn huy
2020-03-17 13:57:34
/*
* 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 nhatao.hanoi;
import java.util.*;
/**
*
* @author ASUS
*/
public class main {
public static void main(String[] args) {
ArrayList<hanoihouse> hn=new ArrayList<>();
Scanner scan=new Scanner(System.in);
int choise,n;
do{
menu();
choise =Integer.parseInt(scan.nextLine());
switch(choise){
case 1:
n=Integer.parseInt(scan.nextLine());
for (int i = 0; i < 10; i++) {
hanoihouse hanoi=new hanoihouse();
hanoi.input();
hn.add(hanoi);
}
break;
case 2:
for (int i = 0; i < hn.size(); i++) {
hn.get(i).display();
}
break;
case 3:
System.out.println("sap sep theo dia chi");
Collections.sort(hn, new Comparator<hanoihouse>() {
@Override
public int compare(hanoihouse o1, hanoihouse o2) {
return o1.getDiaChi().compareTo(o2.getDiaChi()); //To change body of generated methods, choose Tools | Templates.
}
});
break;
case 4:
System.out.println("nhap dia chi can tim");
String diachi=scan.nextLine();
for (int i = 0; i < hn.size(); i++) {
if(hn.get(i).getDiaChi().equalsIgnoreCase(diachi)){
hn.get(i).display();
}
}
break;
case 5:
System.out.println("thoat");
break;
default:
System.out.println("Sai!!!!");
break;
}
}while(choise!=5);
}
public static void menu(){
System.out.println("1. Nhập thông tin n ngôi nhà ở Hà Nội");
System.out.println("2. Hiển thị thông tin của n ngôi nhà đó.");
System.out.println("3. Sắp xếp theo trường địa chỉ và hiển thị thông tin sau khi sắp xếp.");
System.out.println("4. Tìm kiếm nhà theo địa chỉ nhập vào");
System.out.println("5.Thoat");
}
}
///////
/*
* 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 nhatao.hanoi;
import nhatao.cm.house;
import java.util.*;
/**
*
* @author ASUS
*/
public class hanoihouse extends house{
private String tenQuan;
public hanoihouse() {
}
public hanoihouse(String tenQuan){
this.tenQuan=tenQuan;
}
public hanoihouse(String tenQuan,String soNha,String diaChi,String loaiNha) {
super(soNha,diaChi,loaiNha);
this.tenQuan = tenQuan;
}
public String getTenQuan() {
return tenQuan;
}
public void setTenQuan(String tenQuan) {
this.tenQuan = tenQuan;
}
@Override
public void input(){
Scanner scan=new Scanner(System.in);
System.out.println("nhap ten quan");
tenQuan=scan.nextLine();
}
@Override
public void display(){
super.display();
System.out.println(toString());
}
@Override
public String toString() {
return "hanoihouse{" + "tenQuan=" + tenQuan + '}';
}
}
/////
/*
* 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.
*/
/*
* 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 nhatao.cm;
import java.util.*;
/**
*
* @author ASUS
*/
public class house implements inter {
private String soNha;
private String diaChi;
private String loaiNha;
public house() {
}
public house(String soNha, String diaChi, String loaiNha) {
this.soNha = soNha;
this.diaChi = diaChi;
this.loaiNha = loaiNha;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("nhap so nha");
soNha = scan.nextLine();
System.out.println("nhap dia chi");
diaChi = scan.nextLine();
System.out.println("nha loai nha");
loaiNha = scan.nextLine();
}
public void display() {
System.out.println(toString());
}
@Override
public String toString() {
return "house{" + "soNha=" + soNha + ", diaChi=" + diaChi + ", loaiNha=" + loaiNha + '}';
}
public String getSoNha() {
return soNha;
}
public void setSoNha(String soNha) {
this.soNha = soNha;
}
public String getDiaChi() {
return diaChi;
}
public void setDiaChi(String diaChi) {
this.diaChi = diaChi;
}
public String getLoaiNha() {
return loaiNha;
}
public void setLoaiNha(String loaiNha) {
this.loaiNha = loaiNha;
}
}
/////
/*
* 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 nhatao.cm;
/**
*
* @author ASUS
*/
public interface inter {
public void input();
public void display();
}
![NguyenHuuThanh [T1907A]](https://www.gravatar.com/avatar/035e4f4fed661b8e1c3e066e43cd5e41.jpg?s=80&d=mm&r=g)
NguyenHuuThanh
2020-03-15 03:46:34
/*
* 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 house.cm;
import java.util.Scanner;
/**
*
* @author Minh
*/
public class House implements IHouse {
private String soNha;
private String diaChi;
private String loaiNha;
public House()
{
}
public House(String soNha, String diaChi, String loaiNha) {
this.soNha = soNha;
this.diaChi = diaChi;
this.loaiNha = loaiNha;
}
public String getSoNha() {
return soNha;
}
public void setSoNha(String soNha) {
this.soNha = soNha;
}
public String getDiaChi() {
return diaChi;
}
public void setDiaChi(String diaChi) {
this.diaChi = diaChi;
}
public String getLoaiNha() {
return loaiNha;
}
public void setLoaiNha(String loaiNha) {
this.loaiNha = loaiNha;
}
@Override
public void input()
{
Scanner scan = new Scanner(System.in);
soNha = scan.nextLine();
diaChi = scan.nextLine();
loaiNha = scan.nextLine();
}
@Override
public void display()
{
System.out.println(toString());
}
@Override
public String toString() {
return "House{" + "soNha=" + soNha + ", diaChi=" + diaChi + ", loaiNha=" + loaiNha + '}';
}
}
/*
* 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 house.cm;
/**
*
* @author Minh
*/
public interface IHouse {
public void input();
public void 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 house.hanoi;
import java.util.Scanner;
import house.cm.House;
/**
*
* @author Minh
*/
public class HanoiHouse extends House {
String tenQuan;
public HanoiHouse()
{
}
public HanoiHouse(String tenQuan) {
this.tenQuan = tenQuan;
}
public HanoiHouse(String tenQuan, String soNha, String diaChi, String loaiNha) {
super(soNha, diaChi, loaiNha);
this.tenQuan = tenQuan;
}
public String getTenQuan() {
return tenQuan;
}
public void setTenQuan(String tenQuan) {
this.tenQuan = tenQuan;
}
@Override
public void input()
{
Scanner scan = new Scanner(System.in);
super.input();
tenQuan = scan.nextLine();
}
@Override
public void display()
{
super.display();
System.out.println(toString());
}
@Override
public String toString() {
return "HanoiHouse{" + "tenQuan=" + tenQuan + '}';
}
}
/*
* 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 house.hanoi;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
*
* @author Minh
*/
public class Managerhouse {
public static void main(String[] args)
{
int choice;
Scanner scan = new Scanner(System.in);
ArrayList<HanoiHouse> hnhouse = new ArrayList<>();
do{
showMenu();
System.out.println("Nhap lua chon");
choice =Integer.parseInt(scan.nextLine());
switch(choice){
case 1 :
int n = Integer.parseInt(scan.nextLine());
for(int i = 0 ; i < n; i++)
{
HanoiHouse h = new HanoiHouse();
h.input();
hnhouse.add(h);
}
break;
case 2 :
for(int i = 0 ; i < hnhouse.size() ; i++)
{
hnhouse.get(i).display();
}
break;
case 3 :
System.out.println("Sap xep theo dia chi");
Collections.sort(hnhouse, new Comparator<HanoiHouse>(){
@Override
public int compare (HanoiHouse o1 , HanoiHouse o2){
return o1.getDiaChi().compareTo(o2.getDiaChi());
}
});
break;
case 4 :
System.out.println("Nhap dia chi nha");
String dc = scan.nextLine();
for ( int i = 0 ; i < hnhouse.size(); i++)
{
if(hnhouse.get(i).getDiaChi().equalsIgnoreCase(dc))
{
hnhouse.get(i).display();
}
}
break;
case 5 :
System.out.println("Thoat");
break;
default :
System.out.println("Moi ban nhap lai");
break;
}
}while(choice!=5);
}
public static void showMenu()
{
System.out.println("1.Nhap thong tin n ngôi nhà ở Hà Nội");
System.out.println("2. Hiển thị thông tin của n ngôi nhà đó");
System.out.println("3. Sắp xếp theo địa chỉ");
System.out.println("4. Tìm kiếm theo địa chỉ");
System.out.println("5. Thoát");
}
}
![Minh Nghia [T1907A]](https://www.gravatar.com/avatar/ecca255d725eed36a872105205af1b8e.jpg?s=80&d=mm&r=g)
Minh Nghia
2020-03-14 14:46:52
/*
* 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 House.cm;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class House {
private String soNha;
private String diaChi;
private String loaiNha;
public House() {
}
public House(String soNha, String diaChi, String loaiNha) {
this.soNha = soNha;
this.diaChi = diaChi;
this.loaiNha = loaiNha;
}
public String getSoNha() {
return soNha;
}
public void setSoNha(String soNha) {
this.soNha = soNha;
}
public String getDiaChi() {
return diaChi;
}
public void setDiaChi(String diaChi) {
this.diaChi = diaChi;
}
public String getLoaiNha() {
return loaiNha;
}
public void setLoaiNha(String loaiNha) {
this.loaiNha = loaiNha;
}
public void input(){
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so nha:");
soNha = scan.nextLine();
System.out.println("Nhap dia chi:");
diaChi = scan.nextLine();
System.out.println("Nhap loai nha:");
loaiNha = scan.nextLine();
}
public void display(){
System.out.println("So nha:" + soNha);
System.out.println("Dia chi:" + diaChi);
System.out.println("Loai nha:" + loaiNha);
}
}
/*
* 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 House.cm;
/**
*
* @author Administrator
*/
public interface IHouse {
public void input();
public void 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 House.hanoi;
import House.cm.House;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class HaNoiHouse extends House{
private String tenQuan;
public HaNoiHouse() {
}
public HaNoiHouse(String soNha, String diaChi, String loaiNha) {
super(soNha, diaChi, loaiNha);
}
public String getTenQuan() {
return tenQuan;
}
public void setTenQuan(String tenQuan) {
this.tenQuan = tenQuan;
}
@Override
public void input(){
Scanner scan = new Scanner(System.in);
super.input();
System.out.println("Nhap ten quan:");
tenQuan = scan.nextLine();
}
@Override
public void display(){
super.display();
System.out.println("Ten quan:" + tenQuan);
}
}
/*
* 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 House.hanoi;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class ManagerHouse {
public static void main(String[] args) {
int choose;
ArrayList<HaNoiHouse> list = new ArrayList<>();
Scanner scan = new Scanner(System.in);
do{
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch(choose){
case 1:
System.out.println("Nhap thong tin N ngoi nha o Han Noi:");
int n = Integer.parseInt(scan.nextLine());
for(int i = 0; i < n; i++){
HaNoiHouse haNoiHouse = new HaNoiHouse();
haNoiHouse.input();
list.add(haNoiHouse);
}
break;
case 2:
for(int i = 0 ; i < list.size(); i++ ){
list.get(i).display();
}
break;
case 3:
Collections.sort(list, new Comparator<HaNoiHouse>() {
@Override
public int compare(HaNoiHouse o1, HaNoiHouse o2) {
if(o1.getDiaChi().equalsIgnoreCase(o2.getDiaChi())){
return 1;
}
return -1;
}
});
for (int i = 0; i < list.size(); i++) {
list.get(i).display();
}
break;
case 4:
String diaChi;
System.out.println("Nhap dia chi can tim:");
diaChi = scan.nextLine();
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getDiaChi().equalsIgnoreCase(diaChi)){
list.get(i).display();
}
}
break;
case 5:
System.out.println("Thoat.");
break;
default:
System.out.println("Chon lai:");
break;
}
}while(choose != 5);
}
public static void showMenu(){
System.out.println("1. Nhập thông tin n ngôi nhà ở Hà Nội");
System.out.println("2. Hiển thị thông tin của n ngôi nhà đó.");
System.out.println("3. Sắp xếp theo trường địa chỉ và hiển thị thông tin sau khi sắp xếp.");
System.out.println("4. Tìm kiếm nhà theo địa chỉ nhập vào.");
System.out.println("5. Thoat.");
System.out.println("Choose:");
}
}
![Phí Văn Long [T1907A]](https://www.gravatar.com/avatar/5db166b7b74443c5c221e4c0068d6da9.jpg?s=80&d=mm&r=g)
Phí Văn Long
2020-03-14 07:45:01
main :
/*
* 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 QuanLiNha;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int choose;
ArrayList<HaNoiHouse> list = new ArrayList<>();
do {
showMenu();
System.out.print("Nhap lua chon: ");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
System.out.print("Nhap n ngoi nha: ");
int n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
HaNoiHouse hn = new HaNoiHouse();
System.out.println("Nhap thong tin ngoi nha thu " + (i + 1) + " :");
hn.input();
list.add(hn);
}
break;
case 2:
for (int j = 0; j < list.size(); j++) {
System.out.println("Thong tin ngoi nha thu " + (j + 1) + " la: ");
list.get(j).display();
}
break;
case 3:
Collections.sort(list, new Comparator<HaNoiHouse>() {
@Override
public int compare(HaNoiHouse o1, HaNoiHouse o2) {
if (o1.getDiaChi().equalsIgnoreCase(o2.getDiaChi())) {
return 1;
}
return -1;
}
});
for (int i = 0; i < list.size(); i++) {
list.get(i).display();
}
break;
case 4:
System.out.println("Nhap ten dia chi nha can tim: ");
String diachi = scan.nextLine();
System.out.println("Thong tin ngoi nha can tim la: ");
for (int k = 0; k < list.size(); k++) {
if (list.get(k).getDiaChi().equalsIgnoreCase(diachi)) {
list.get(k).display();
} else {
System.out.println("Khong tim thay thong tin ngoi nha co dia chi: " + diachi);
}
}
break;
case 5:
System.out.println("Thoat.");
break;
default:
System.err.println("Nhap sai !!!");
break;
}
} while (choose != 5);
}
public static void showMenu() {
System.out.println("*==================*");
System.out.println("1. Nhap thong tin so ngoi nha o Ha Noi.");
System.out.println("2. Hien thi thong tin cua so ngoi nha do.");
System.out.println("3. Sap xep theo truong dia chi va hien thi thong tin sau khi sap xep.");
System.out.println("4. Tim kiem nha theo dia chi nhap vao.");
System.out.println("5. Thoat.");
System.out.println("*==================*");
}
}
HaNoiHouse:
/*
* 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 QuanLiNha;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class HaNoiHouse extends House {
private String tenQuan;
public HaNoiHouse() {
}
public HaNoiHouse(String soNha, String diaChi, String loaiNha, String tenQuan) {
super(soNha, diaChi, loaiNha);
this.tenQuan = tenQuan;
}
public String getTenQuan() {
return tenQuan;
}
public void setTenQuan(String tenQuan) {
this.tenQuan = tenQuan;
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
super.input();
System.out.println("Nhap ten quan: ");
tenQuan = scan.nextLine();
System.out.println("");
}
@Override
public void display() {
super.display();
System.out.println("Ten quan la: " + tenQuan);
System.out.println("");
}
}
iHouse:/*
* 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 QuanLiNha;
/**
*
* @author Admin
*/
public interface IHouse {
public void input();
public void display();
}
House:
/*
* 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 QuanLiNha;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class House implements IHouse {
private String soNha;
private String diaChi;
private String loaiNha;
public House() {
}
public House(String soNha, String diaChi, String loaiNha) {
this.soNha = soNha;
this.diaChi = diaChi;
this.loaiNha = loaiNha;
}
public String getSoNha() {
return soNha;
}
public void setSoNha(String soNha) {
this.soNha = soNha;
}
public String getDiaChi() {
return diaChi;
}
public void setDiaChi(String diaChi) {
this.diaChi = diaChi;
}
public String getLoaiNha() {
return loaiNha;
}
public void setLoaiNha(String loaiNha) {
this.loaiNha = loaiNha;
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so nha: ");
soNha = scan.nextLine();
System.out.println("Nhap dia chi: ");
diaChi = scan.nextLine();
System.out.println("Nhap loai nha: ");
loaiNha = scan.nextLine();
}
@Override
public void display() {
System.out.println("So nha la: " + soNha);
System.out.println("Dia chi nha la: " + diaChi);
System.out.println("Loai nha la: " + loaiNha);
}
}
![Phan Bạch Tùng Dương [T1907A]](https://www.gravatar.com/avatar/e74e3ec62fe3a191929e12eecbe01edf.jpg?s=80&d=mm&r=g)
Phan Bạch Tùng Dương
2020-03-14 07:43:31
/*
* 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 QLNha;
/**
*
* @author Admin
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int choose;
ArrayList<HaNoiHouse> list = new ArrayList<>();
do {
showMenu();
System.out.print("Chon : ");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
System.out.print("Nhap n ngoi nha: ");
int n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
HaNoiHouse hn = new HaNoiHouse();
System.out.println("Nhap thong tin ngoi nha thu " + (i + 1) + " :");
hn.input();
list.add(hn);
}
break;
case 2:
for (int j = 0; j < list.size(); j++) {
System.out.println("Thong tin ngoi nha thu " + (j + 1) + " la: ");
list.get(j).display();
}
break;
case 3:
Collections.sort(list, new Comparator<HaNoiHouse>() {
@Override
public int compare(HaNoiHouse o1, HaNoiHouse o2) {
if (o1.getDiaChi().equalsIgnoreCase(o2.getDiaChi())) {
return 1;
}
return -1;
}
});
for (int i = 0; i < list.size(); i++) {
list.get(i).display();
}
break;
case 4:
System.out.println("Nhap ten dia chi nha can tim : ");
String diachi = scan.nextLine();
System.out.println("Thong tin ngoi nha can tim : ");
for (int k = 0; k < list.size(); k++) {
if (list.get(k).getDiaChi().equalsIgnoreCase(diachi)) {
list.get(k).display();
} else {
System.out.println("Khong tim thay thong tin ngoi nha co dia chi: " + diachi);
}
}
break;
case 5:
System.out.println("Thoat.");
break;
default:
System.err.println("!!!Error!!!");
break;
}
} while (choose != 5);
}
public static void showMenu() {
System.out.println("1. Nhap thon tin n ngoi nha o Ha Noi : ");
System.out.println("2. Hien thi thong tin cua n ngoi nha do : ");
System.out.println("3. Sap xep theo truong dia chi va hien thi thong tin sau khi sap xep : ");
System.out.println("4. Tim kiem nha theo dia chi : ");
System.out.println("5. Thoát.");
}
}
/*
* 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 QLNha;
/**
*
* @author Admin
*/
public interface IHouse {
public void input();
public void 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 QLNha;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class House implements IHouse {
private String soNha;
private String diaChi;
private String loaiNha;
public House() {
}
public House(String soNha, String diaChi, String loaiNha) {
this.soNha = soNha;
this.diaChi = diaChi;
this.loaiNha = loaiNha;
}
public String getSoNha() {
return soNha;
}
public void setSoNha(String soNha) {
this.soNha = soNha;
}
public String getDiaChi() {
return diaChi;
}
public void setDiaChi(String diaChi) {
this.diaChi = diaChi;
}
public String getLoaiNha() {
return loaiNha;
}
public void setLoaiNha(String loaiNha) {
this.loaiNha = loaiNha;
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so nha : ");
soNha = scan.nextLine();
System.out.println("Nhap dia chi : ");
diaChi = scan.nextLine();
System.out.println("Nhap loai nha : ");
loaiNha = scan.nextLine();
}
@Override
public void display() {
System.out.println("So nha la : " + soNha);
System.out.println("Dia chi nha la : " + diaChi);
System.out.println("Loai nha la : " + loaiNha);
}
}
/*
* 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 QLNha;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class HaNoiHouse extends House {
private String tenQuan;
public HaNoiHouse() {
}
public HaNoiHouse(String soNha, String diaChi, String loaiNha, String tenQuan) {
super(soNha, diaChi, loaiNha);
this.tenQuan = tenQuan;
}
public String getTenQuan() {
return tenQuan;
}
public void setTenQuan(String tenQuan) {
this.tenQuan = tenQuan;
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
super.input();
System.out.println("Nhap ten quan: ");
tenQuan = scan.nextLine();
System.out.println("");
}
@Override
public void display() {
super.display();
System.out.println("Ten quan la: " + tenQuan);
System.out.println("");
}
}
![thienphu [T1907A]](https://www.gravatar.com/avatar/c4573ea65e411176c1852fd8584f1ab1.jpg?s=80&d=mm&r=g)
thienphu
2020-03-13 15:11:17
/*
* 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 house.cm;
/**
*
* @author Thien Phu
*/
public interface IHouse {
void input();
void 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 house.cm;
import java.util.Scanner;
/**
*
* @author Thien Phu
*/
public class House implements IHouse {
private String soNha;
private String diaChi;
private String loaiNha;
public House() {
}
public House(String soNha, String diaChi, String loaiNha) {
this.soNha = soNha;
this.diaChi = diaChi;
this.loaiNha = loaiNha;
}
public String getSoNha() {
return soNha;
}
public void setSoNha(String soNha) {
this.soNha = soNha;
}
public String getDiaChi() {
return diaChi;
}
public void setDiaChi(String diaChi) {
this.diaChi = diaChi;
}
public String getLoaiNha() {
return loaiNha;
}
public void setLoaiNha(String loaiNha) {
this.loaiNha = loaiNha;
}
@Override
public void input() {
Scanner sc = new Scanner(System.in);
System.out.println("Nhap sonha:");
soNha = sc.nextLine();
System.out.println("Nhap diachi: ");
diaChi = sc.nextLine();
System.out.println("Nhap loaiNha");
loaiNha = sc.nextLine();
}
@Override
public void display() {
System.out.print(toString());
}
@Override
public String toString() {
return "House:" + "soNha=" + soNha + ", diaChi=" + diaChi + ", loaiNha=" + loaiNha;
}
}
/*
* 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 house.cm;
import java.util.Scanner;
/**
*
* @author Thien Phu
*/
public class HaNoiHouse extends House {
private String tenQuan;
public HaNoiHouse() {
}
public HaNoiHouse(String tenQuan, String soNha, String diaChi, String loaiNha) {
super(soNha, diaChi, loaiNha);
this.tenQuan = tenQuan;
}
public void setTenQuan(String tenQuan) {
this.tenQuan = tenQuan;
}
public String getTenQuan() {
return tenQuan;
}
@Override
public void display() {
super.display(); //To change body of generated methods, choose Tools | Templates.
System.out.print(" TenQuan: " + tenQuan);
}
@Override
public void input() {
super.input();
Scanner sc = new Scanner(System.in);
System.out.println("Nhap ten Quan:");
tenQuan = sc.nextLine();
}
}
/*
* 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 house.cm;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Thien Phu
*/
public class ListHouse {
private List<HaNoiHouse> houselist = new ArrayList<>();
private static Scanner sc = new Scanner(System.in);
public ListHouse() {
}
public void inputN() {
System.out.println("Nhap thong tin N ngoi nha o Ha Noi");
int n = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n; i++) {
HaNoiHouse hn = new HaNoiHouse();
hn.input();
houselist.add(hn);
}
}
public void display() {
for (HaNoiHouse houselist1 : houselist) {
houselist1.display();
System.out.println("");
}
}
public void findAddress() {
int a = -1;
System.out.println("Nhap dia chi can tim:");
String address = sc.nextLine();
for (int i = 0; i < houselist.size(); i++) {
if (houselist.get(i).getDiaChi().equalsIgnoreCase(address)) {
a = i;
}
}
if (a != -1) {
System.out.println("Thong tin can tim la: " + address);
houselist.get(a).display();
} else {
System.err.println("Dia chi nay khong ton tai");
}
}
//sap xep theo dia chi
public void sapxep() {
Collections.sort(houselist, new Comparator<HaNoiHouse>() {
@Override
public int compare(HaNoiHouse o1, HaNoiHouse o2) {
return o1.getDiaChi().compareToIgnoreCase(o2.getDiaChi());
}
}
);
System.out.println("danh sap nha sap xep theo dia chi:");
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 house.cm;
import java.util.Scanner;
/**
*
* @author Thien Phu
*/
public class ManagerHouse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ListHouse listHouse = new ListHouse();
int choose;
do {
showmenu();
choose = Integer.parseInt(sc.nextLine());
switch (choose) {
case 1:
listHouse.inputN();
break;
case 2:
listHouse.display();
break;
case 3:
listHouse.sapxep();
break;
case 4:
listHouse.findAddress();
break;
case 5:
System.out.println("Exit thanh cong");
break;
default:
System.err.println("Nhap sai roi");
break;
}
} while (choose != 5);
}
public static void showmenu() {
System.out.println("1. Nhập thông tin n ngôi nhà ở Hà Nội");
System.out.println("2. Hiển thị thông tin của n ngôi nhà đó.");
System.out.println("3. Sắp xếp theo trường địa chỉ và hiển thị thông tin sau khi sắp xếp.");
System.out.println("4. Tìm kiếm nhà theo địa chỉ nhập vào.");
System.out.println("5: Thoat");
System.out.println("Choose: ");
}
}
![Trương Công Vinh [T1907A]](https://www.gravatar.com/avatar/223a7e3a46f4a747f81b921fe023fcc4.jpg?s=80&d=mm&r=g)
Trương Công Vinh
2020-03-13 10:57:05
/*
* 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 house.hanoi;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
*
* @author DELL
*/
public class ManagerHouse {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<HaNoiHouse> houseHN = new ArrayList<>();
int c;
do{
menu();
c= Integer.parseInt(scan.nextLine());
switch(c){
case 1:
int n;
System.out.println("Nhap so ngoi nha can quan ly : ");
n=Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
HaNoiHouse h = new HaNoiHouse();
h.input();
houseHN.add(h);
}
break;
case 2:
System.out.println("Show InHouse : ");
for (int i = 0; i < houseHN.size(); i++) {
houseHN.get(i).display();
}
break;
case 3:
Collections.sort(houseHN, new Comparator<HaNoiHouse>() {
@Override
public int compare(HaNoiHouse o1, HaNoiHouse o2) {
if(o1.getDiaChi().equalsIgnoreCase(o2.getDiaChi())){
return 1;
}return -1;
}
});
for (int i = 0; i < houseHN.size(); i++) {
houseHN.get(i).display();
}
break;
case 4:
String dc ;
System.out.println("Nhap dia Chi can tim : ");
dc = scan.nextLine();
for (int i = 0; i < houseHN.size(); i++) {
if(houseHN.get(i).getDiaChi().equalsIgnoreCase(dc))
{
houseHN.get(i).display();
}
}
break;
case 5:
System.out.println("End!!");
break;
default :
break;
}
}while(c!=5);
}
public static void menu() {
System.out.println("1. Nhập thông tin n ngôi nhà ở Hà Nội");
System.out.println("2. Hiển thị thông tin của n ngôi nhà đó.");
System.out.println("3. Sắp xếp theo trường địa chỉ và hiển thị thông tin sau khi sắp xếp.");
System.out.println("4. Tìm kiếm nhà theo địa chỉ nhập vào.");
System.out.println("5. Thoát.");
System.out.println("Chon : ");
}
}
/*
* 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 house.hanoi;
import house.cm.House;
import java.util.Scanner;
/**
*
* @author DELL
*/
public class HaNoiHouse extends House {
Scanner scan = new Scanner(System.in);
private String tenQuan;
public HaNoiHouse() {
}
public HaNoiHouse(String tenQuan, String soNha, String diaChi, String loaiNha) {
super(soNha, diaChi, loaiNha);
this.tenQuan = tenQuan;
}
public String getTenQuan() {
return tenQuan;
}
public void setTenQuan(String tenQuan) {
this.tenQuan = tenQuan;
}
@Override
public void input() {
super.input();
System.out.println("Nhap ten quan : ");
tenQuan = scan.nextLine();
}
@Override
public void display() {
super.display();
System.out.println("Ten quan : " + tenQuan);
}
}
/*
* 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 house.cm;
import java.util.Scanner;
/**
*
* @author DELL
*/
public class House {
private String soNha;
private String diaChi;
private String loaiNha;
public House() {
}
public House(String soNha, String diaChi, String loaiNha) {
this.soNha = soNha;
this.diaChi = diaChi;
this.loaiNha = loaiNha;
}
public String getSoNha() {
return soNha;
}
public void setSoNha(String soNha) {
this.soNha = soNha;
}
public String getDiaChi() {
return diaChi;
}
public void setDiaChi(String diaChi) {
this.diaChi = diaChi;
}
public String getLoaiNha() {
return loaiNha;
}
public void setLoaiNha(String loaiNha) {
this.loaiNha = loaiNha;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("So nha : ");
soNha = scan.nextLine();
System.out.println("Dia chi : ");
diaChi = scan.nextLine();
System.out.println("Loai nha : ");
loaiNha = scan.nextLine();
}
public void display() {
System.out.println("So nha : "+soNha);
System.out.println("Dia chi : "+diaChi);
System.out.println("Loai nha : "+loaiNha);
}
}
/*
* 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 house.cm;
/**
*
* @author DELL
*/
public interface IHouse {
public void input();
public void display();
}
![Đỗ Văn Huấn [T1907A]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ Văn Huấn
2020-03-11 15:17:27
package ASSIGNMENT.assignment1.house.cm; |
package ASSIGNMENT.assignment1.house.cm; |
package ASSIGNMENT.assignment1.house.hanoi; |
package ASSIGNMENT.assignment1.house.hanoi; |