By GokiSoft.com| 19:59 13/07/2021|
Java Basic

Java basic- Overview - Chương trình quản lý bãi đỗ xe java - nâng cao tay nghề lập trình

Quản lý quản lý bãi đỗ xe.

1. Tạo 1 lớp People chứa các thông tin : id, tên, tuổi, địa chỉ, giới tính, năm sinh

Viết các phương thức

- input() : nhập thông tin người dùng

- showInfo() : Hiển thị thông tin người dùng.

2. Tạo 1 lớp đối được Vehicle chứa các thông tin (nhà sản xuất, biển số xe, năm sản xuất, id của chủ sở hữu, chiều dài, chiều rộng) và gồm các phương thức

- input() : nhập thông tin xe

-showInfo() : Hiển thị thông tin xe

3. Tạo các lớp Car, Container biết rằng : 

- Car có chiều rộng = 2 và chiều dài = 3

- Container : có chiều rộng = 3 và chiều dài = 5

4. Tạo 1 lớp Area (nơi quản lý từng khu vực của xe) gồm các thông tin : vị trí của khu vực đó, chiều dài và chiều rộng và danh sách các xe cộ đang đậu trên khu vự này

- Tự viết các phương thức cần thiết


5. Xây dựng lớp Zone : chứ danh sách các khu vực Area. 

- Tự viết các phương thức cần thiết

6. Xây dựng lớp quản lý People.

- Chứa danh sách people và phương thức cần thiết


Xây dựng menu chương trình thực hiện các yêu cầu sau

1. Nhập thông tin N people.

2. Nhập thông tin N Area -> Lưu vào Zone

3. Nhập thông tin N vehicle và tự động tìm kiếm khu vự phù hợp để lưu chữ.

4. Hiển thị thông tin các xe được sở hữu bởi 1 người theo Tên

5. Dùng giải thuật Quick Sort để sắp xếp theo thứ tự giảm dần theo diện tích của xe trong từng khu vực.

6. Sử dụng kết quả 5 hiển thị thông tin xe cộ trong Zone đó.

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

5

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

Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-02-25 14:24:49


#Area.java


/*
 * 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 lesson5.QuanLiBaiDoXe;

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

/**
 *
 * @author MyPC
 */
public class Area{
    String site;
    int width, length;
    ArrayList<Vehicle> vehicleList = new ArrayList<>();

    public Area() {
    }
    
    public void addVehicle(Vehicle vehicle){
        vehicleList.add(vehicle);
    }
    
    public void sort(){
        Collections.sort(vehicleList, new Comparator<Vehicle>() {
            @Override
            public int compare(Vehicle o1, Vehicle o2) {
                if(o1.getS() < o2.getS()){
                    return -1;
                }
                return 1;
            }
        });
    }
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Vi tri: ");
        site = scan.nextLine();
        System.out.println("Chieu rong: ");
        width = scan.nextInt();
        System.out.println("Chieu dai: ");
        length = scan.nextInt();
    }
    
    public boolean checkFree(Vehicle vehicle){
        //xep cheo.
        int usedWidth = 0;
        int usedLength = 0;
        for (Vehicle v : vehicleList) {
            usedWidth += v.getWidth();
            usedLength += v.getLength();
        }
        
        int freeWidth = width - usedWidth;
        int freeLength = length - usedLength;
        if( freeWidth >= vehicle.getWidth() && freeLength >= vehicle.getLength() ) return true;
        return false;
    }
    
    public void showInfor(){
        for (Vehicle vehicle : vehicleList) {
            vehicle.showInfo();
        }
    }
}


#Car.java


/*
 * 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 lesson5.QuanLiBaiDoXe;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Car extends Vehicle{

    public Car() {
        super(2, 3);
    }

    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nha san xuat:");
        manu = scan.nextLine();
        System.out.println("Bien so xe:");
        carNum = scan.nextLine();
        System.out.println("Nam san xuat:");
        year = scan.nextInt();
        System.out.println("Id cua chu so huu:");
        idOwner = scan.nextInt();
    }
    
    
}


#Container.java


/*
 * 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 lesson5.QuanLiBaiDoXe;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Container extends Vehicle{

    public Container() {
        super(3, 5);
    }
    
    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nha san xuat:");
        manu = scan.nextLine();
        System.out.println("Bien so xe:");
        carNum = scan.nextLine();
        System.out.println("Nam san xuat:");
        year = scan.nextInt();
        System.out.println("Id cua chu so huu:");
        idOwner = scan.nextInt();
    }
}


#People.java


/*
 * 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 lesson5.QuanLiBaiDoXe;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class People {
    String name,  address, gender;
    int year, id, age;
    
    public People(){
    }

    public People(String name, String address, String gender, int year, int id, int age) {
        this.name = name;
        this.address = address;
        this.gender = gender;
        this.year = year;
        this.id = id;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Id:");
        id = Integer.parseInt(scan.nextLine());
        System.out.println("Ten:");
        name = scan.nextLine();
        System.out.println("Tuoi:");
        age = Integer.parseInt(scan.nextLine());
        System.out.println("Dia chi:");
        address = scan.nextLine();
        System.out.println("Gioi tinh:");
        gender = scan.nextLine();
        System.out.println("Nam sinh:");
        year = Integer.parseInt(scan.nextLine());
    }
    
    public void showInfo(){
        System.out.println("Id: " + id);
        
        System.out.println("Ten: " + name);
        
        System.out.println("Tuoi: " + age);
        
        System.out.println("Dia chi: " + address);
        
        System.out.println("Gioi tinh: " + gender);
        
        System.out.println("Nam sinh: " + year);
        
    }
}


#PeopleList.java


 /*
 * 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 lesson5.QuanLiBaiDoXe;

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

/**
 *
 * @author MyPC
 */
public class PeopleList{
   ArrayList<People> peopleList = new ArrayList<>();
   
   public void input(){
       Scanner scan = new Scanner(System.in);
       System.out.println("Nhap so chu xe:");
       int n = Integer.parseInt(scan.nextLine());
       for (int i = 0; i < n; i++) {
           People people = new People();
           people.input();
           peopleList.add(people);
       }
   }
    
}


#Test.java


/*
 * 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 lesson5.QuanLiBaiDoXe;

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

/**
 *
 * @author MyPC
 */
public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        PeopleList peopleList = new PeopleList();
        Zone zoneList = new Zone();
        int choose, n;
        do{
            Menu();
            System.out.println("Lua chon chuong trinh:");
            choose = Integer.parseInt(scan.nextLine());
            switch(choose){
                case 1:
                    peopleList.input();
                break;
                
                case 2:
                    zoneList.input();
                break;
                
                case 3:
                    zoneList.inputVehicle();
                break;
                    
                case 4:
                    zoneList.sort();
                break;
                
                case 5:
                    zoneList.showInfor();
                break;
                
                case 6:
                    System.out.println("Tam biet!!");
                break;
                
                default:
                    System.err.println("Nhap sai!!!");
                    System.out.println("");
                break;
            }
        }while(choose != 6);
    }
    
    static void Menu(){
        System.out.println("1. Nhập thông tin N people.");
        System.out.println("2. Nhập thông tin N Area -> Lưu vào Zone");
        System.out.println("3. Nhập thông tin N vehicle và tự động tìm kiếm khu vự phù hợp để lưu chữ.");
        System.out.println("4. Hiển thị thông tin các xe được sở hữu bởi 1 người theo Tên");
        System.out.println("5. Dùng giải thuật Quick Sort để sắp xếp theo thứ tự giảm dần theo diện tích của xe trong từng khu vực.");
        System.out.println("6. Sử dụng kết quả 5 hiển thị thông tin xe cộ trong Zone đó.");
    }
}


#Vehicle.java


/*
 * 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 lesson5.QuanLiBaiDoXe;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Vehicle{
    String manu, carNum;
    int year, idOwner, width, length;
    
    public Vehicle(){
    }

    public Vehicle(int width, int length) {
        this.width = width;
        this.length = length;
    }
    

    public Vehicle(String manu, String carNum, int year, int idOwner, int width, int length) {
        this.manu = manu;
        this.carNum = carNum;
        this.year = year;
        this.idOwner = idOwner;
        this.width = width;
        this.length = length;
    }

    public String getManu() {
        return manu;
    }

    public void setManu(String manu) {
        this.manu = manu;
    }

    public String getCarNum() {
        return carNum;
    }

    public void setCarNum(String carNum) {
        this.carNum = carNum;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getIdOwner() {
        return idOwner;
    }

    public void setIdOwner(int idOwner) {
        this.idOwner = idOwner;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
    
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nha san xuat:");
        manu = scan.nextLine();
        System.out.println("Bien so xe:");
        carNum = scan.nextLine();
        System.out.println("Nam san xuat:");
        year = scan.nextInt();
        System.out.println("Id cua chu so huu:");
        idOwner = scan.nextInt();
        System.out.println("Chieu dai:");
        length = scan.nextInt();
        System.out.println("Chieu rong:");
        width = scan.nextInt();
    }
    
    public void showInfo(){
        System.out.println("Nha san xuat: " + manu);
        
        System.out.println("Bien so xe: " + carNum);
        
        System.out.println("Nam san xuat: " + year);
        
        System.out.println("Id cua chu so huu: " + idOwner);
        
        System.out.println("Chieu dai: " + length);
        
        System.out.println("Chieu rong: " + width);
    }
    
    public int getS(){
        return length*width;
    }
}


#Zone.java


/*
 * 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 lesson5.QuanLiBaiDoXe;

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

/**
 *
 * @author MyPC
 */
public class Zone {
    ArrayList<Area> areaList = new ArrayList<>();
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap so khu vuc cho zone:");
        int n = scan.nextInt();
        for (int i = 0; i < n; i++) {
            Area area = new Area();
            area.input();
            areaList.add(area);
        }
   }
    
    
    
    public void inputVehicle(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap so xe can them:");
        int n = scan.nextInt();
        int choose;
        for (int i = 0; i < n; i++) {
            showOptions();
            choose = scan.nextInt();
            Vehicle vehicle;
            if( choose == 1 ){
                vehicle = new Car();
            }else{
                vehicle = new Container();
            }
            vehicle.input();
            //tim khu vuc luu xe co
            for( int j = 0 ; j < areaList.size() ; j++ ){
                if(areaList.get(i).checkFree(vehicle)){
                    areaList.get(i).addVehicle(vehicle);
                    break;
                }
            }
        }
    }
    
    public void showOptions(){
        System.out.println("1.Nhap Car");
        System.out.println("2.Nhap Container");
        System.out.println("Lua chon:");
    }
    
    public void sort(){
        for (Area area : areaList) {
            area.sort();
        }
    }
    
    public void showInfor(){
        for (Area area : areaList) {
            area.showInfor();
        }
    }
    
}



Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-03-20 02:39:44


package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe;

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

public class Area {
String location;
int width, height;
ArrayList<Vehicle> listVehicle = new ArrayList<>();

public Area() {
}

public Area(String location, int width, int height) {
this.location = location;
this.width = width;
this.height = height;
}

public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap vi tri:");
location = scan.nextLine();
System.out.println("Nhap chieu rong:");
width = Integer.parseInt(scan.nextLine());
System.out.println("Nhap chieu dai:");
height = Integer.parseInt(scan.nextLine());
}

public void showInfo() {
System.out.println(toString());
}

@Override
public String toString() {
return "Area{" +
"location='" + location + '\'' +
", width=" + width +
", height=" + height +
", listVehicle=" + listVehicle +
'}';
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

}
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe;

public class Car extends Vehicle {


public Car() {
width = 2;
height = 3;
}

@Override
public void input() {
super.input();
System.out.println("chieu rong: "+width);
System.out.println("chieu dai: "+height);

}

@Override
public void showInfo() {
super.showInfo();
System.out.println("chieu rong: "+width);
System.out.println("chieu dai: "+height);
}
}
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe;

public class Container extends Vehicle {
public Container() {
width=3;
height=5;
}

@Override
public void input() {
super.input();
System.out.println("chieu rong: "+width);
System.out.println("chieu dai: "+height);
}

@Override
public void showInfo() {
super.showInfo();
System.out.println("chieu rong: "+width);
System.out.println("chieu dai: "+height);
}
}
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
People people = new People();
Zone zone = new Zone();
int choose;

do {
showMenu();
System.out.print("Nhap vao lua chon cua ban: ");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
people.InputPeople();
break;
case 2:
zone.inputArea();
break;
case 3:
zone.inputVehicle();
break;
case 4:
zone.showVehicle();
break;
case 5:
zone.sort();
break;
case 6:
zone.output_Vehicle_DienTich();
break;
case 7:
System.out.println("Thoat.");
break;
default:
System.err.println("Nhap loi !!!");
break;
}
} while (choose != 7);

}

static void showMenu() {
System.out.println("1. Nhap thong tin cho n chu so huu.");
System.out.println("2. Nhap thong tin cho n khu vuc. ");
System.out.println("3. Nhap thong tin cho n xe va tu dong tim kiem khu vuc phu hop de luu tru.");
System.out.println("4. Hien thi thong tin cac xe duoc so huu boi mot ten.");
System.out.println("5. Dung giai thuat Quick Sort de xap xep thu tu giam dan theo dien tich cua xe trong tung khu vuc.");
System.out.println("6. Su dung ket 5 de hien thong tin xe co trong khu vuc do.");
System.out.println("7. Thoat.");
}
}
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe;

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

public class People {
int id, age;
String name, addRess, sex, birthDay;
Scanner scan = new Scanner(System.in);
ArrayList<People> listPeople = new ArrayList<>();

public People() {
}

public People(int id, int age, String name, String addRess, String sex, String birthDay) {
this.id = id;
this.age = age;
this.name = name;
this.addRess = addRess;
this.sex = sex;
this.birthDay = birthDay;
}

public void input() {
System.out.println("Nhap id: ");
id = Integer.parseInt(scan.nextLine());
System.out.println("Nhap ten: ");
name = scan.nextLine();
System.out.println("Nhap tuoi: ");
age = Integer.parseInt(scan.nextLine());
System.out.println("Nhap dia chi: ");
addRess = scan.nextLine();
System.out.println("Nhap gioi tinh: ");
sex = scan.nextLine();
System.out.println("Nhap nam sinh: ");
birthDay = scan.nextLine();
System.out.println("");
}

public void showInfo() {
System.out.println(toString());
}

@Override
public String toString() {
return "People{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
", addRess='" + addRess + '\'' +
", sex='" + sex + '\'' +
", birthDay='" + birthDay + '\'' +
'}';
}


public void InputPeople() {
System.out.println("Nhap so chu so huu can them thong tin: ");
int n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
System.out.println("Nhap chu so huu thu " + (i + 1) + " :");
People people = new People();
people.input();
listPeople.add(people);
}
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

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

public String getAddRess() {
return addRess;
}

public void setAddRess(String addRess) {
this.addRess = addRess;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getBirthDay() {
return birthDay;
}

public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
}
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe;

import java.util.ArrayList;

import java.util.Scanner;

public class Vehicle {
String producer, bienSoXe, namSanXuat;
int id_chuSoHuu, width, height;
public Vehicle() {
}

public Vehicle(String producer, String bienSoXe, String namSanXuat, int id_chuSoHuu, int width, int height) {
this.producer = producer;
this.bienSoXe = bienSoXe;
this.namSanXuat = namSanXuat;
this.id_chuSoHuu = id_chuSoHuu;
this.width = width;
this.height = height;
}

public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap nha san xuat:");
producer = scan.nextLine();
System.out.println("Nhap bien so xe:");
bienSoXe = scan.nextLine();
System.out.println("Nhap nam san xuat: ");
namSanXuat = scan.nextLine();
System.out.println("Nhap id chu so huu: ");
id_chuSoHuu = Integer.parseInt(scan.nextLine());
}

public void showInfo() {
System.out.println(toString());
}

@Override
public String toString() {
return "Vehicle{" +
"producer='" + producer + '\'' +
", bienSoXe='" + bienSoXe + '\'' +
", namSanXuat='" + namSanXuat + '\'' +
", id_chuSoHuu=" + id_chuSoHuu +
", width=" + width +
", height=" + height +
'}';
}

float dienTich() {
return (float) (width * height);
}


public String getProducer() {
return producer;
}

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

public String getBienSoXe() {
return bienSoXe;
}

public void setBienSoXe(String bienSoXe) {
this.bienSoXe = bienSoXe;
}

public String getNamSanXuat() {
return namSanXuat;
}

public void setNamSanXuat(String namSanXuat) {
this.namSanXuat = namSanXuat;
}

public int getId_chuSoHuu() {
return id_chuSoHuu;
}

public void setId_chuSoHuu(int id_chuSoHuu) {
this.id_chuSoHuu = id_chuSoHuu;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}
}
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe;

import java.util.*;

public class Zone extends Area {
ArrayList<Vehicle> listVehicle = new ArrayList<>();
ArrayList<Area> listArea = new ArrayList<>();
ArrayList<People> listPeople = new ArrayList<>();
Scanner scan = new Scanner(System.in);

public void inputArea() {
System.out.print("Nhap so khu vuc can them thong tin: ");
int n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
Area area = new Area();
area.input();
listArea.add(area);
}
}

public void inputVehicle() {
System.out.println("Nhap so xe can them thong tin:");
int n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
System.out.println("1. nhap thong tin xe Car.");
System.out.println("2. nhap thong tin xe Container.");
System.out.print("Chon loai xe: ");
int a = Integer.parseInt(scan.nextLine());
Vehicle vehicle; // tinh da hinh
if (a == 1) { // neu chon 1 thi tao ra 1 xe loai Car
vehicle = new Car();
} else { // neu ko chon 1 thi tao ra 1 xem loai Container
vehicle = new Container();
}
vehicle.input(); // nhap thong tin cho xe
listVehicle.add(vehicle); // va add vao mang
}
}

public void showVehicle() {
System.out.println("Nhap ten cua chu xe: ");
String name = scan.nextLine();
for (int i = 0; i < listPeople.size(); i++) {
if (listPeople.get(i).name.equalsIgnoreCase(name)) { //tim nguoi co ten giong ten nhap vao
for (int j = 0; j < listVehicle.size(); j++) { // vog for chay qua cac phan tu cua lop xe
if (listPeople.get(i).id == listVehicle.get(j).id_chuSoHuu) {
// neu id ten nhap trung voi id_chusohuu thi hien thi thong tin xe
System.out.println("thong tin xe cua chu so huu cos ten la " + name + " :");
listVehicle.get(j).showInfo();
}
}
}
}
}

public void sapXepTheoDienTich() { //sap xep theo dien tich xe giam dan
Collections.sort(listVehicle, new Comparator<Vehicle>() {
@Override
public int compare(Vehicle vehicle, Vehicle t1) {
if (vehicle.dienTich() > t1.dienTich()) {
return 1;
}
return -1;
}
});
}


public void sort() { //trong tung khu vuc thi sap xep giam dan theo dien tich cua xe
for (int i = 0; i < listArea.size(); i++) {
sapXepTheoDienTich();
}
}


public void output_Vehicle_DienTich() { // hien thi thong tin xe cua khu vuc theo thu tu giam dan
for (int i = 0; i < listArea.size(); i++) { // vong for chay qua tung khu vuc
listVehicle.get(i).showInfo(); // hien thi thong tin xe cua khu vuc do
}
}
}



hoangduyminh [T1907A]
hoangduyminh

2020-03-17 16:22:29



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

import java.util.Scanner;

/**
 *
 * @author Minh
 */
public class People {
    int old;
    String id, fullname;

    public People() {
    }

    public People(int old, String id, String fullname) {
        this.old = old;
        this.id = id;
        this.fullname = fullname;
    }

    public int getOld() {
        return old;
    }

    public void setOld(int old) {
        this.old = old;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }
    
    
    public void input() {
      Scanner input = new Scanner(System.in);
      
        System.out.println("Nhap id:");
        id = input.nextLine();
        System.out.println("Nhap ten:");
        fullname = input.nextLine();
        System.out.println("Nhap vao tuoi :");
        old = Integer.parseInt(input.nextLine());
    
    }

    @Override
    public String toString() {
        return "People{" + "old=" + old + ", id=" + id + ", fullname=" + fullname + '}';
    }
    
   public void showInfor() {
       System.out.println(toString());
   
   }
}



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

import java.util.Scanner;

/**
 *
 * @author Minh
 */
public class Vehicle {
    String manufaturer,No,year,owner;
    int width,height;

    public Vehicle() {
    }

    public Vehicle(int width, int height) {
        this.width = width;
        this.height = height;
    }
    
    
    
    
    public String getManufaturer() {
        return manufaturer;
    }

    public void setManufaturer(String manufaturer) {
        this.manufaturer = manufaturer;
    }

    public String getNo() {
        return No;
    }

    public void setNo(String No) {
        this.No = No;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
    
    public void input() {
      Scanner input = new Scanner(System.in);
      
        System.out.println("Nhap Nhà sản xuất:");
        manufaturer = input.nextLine();
        System.out.println("Nhap biển số xe:");
        No = input.nextLine();
        System.out.println("Nhap năm sản xuất :");
        year = input.nextLine();
        System.out.println("Nhap chủ sở hữu");
        owner = input.nextLine();
         System.out.println("Nhap chiều rộng");
         width = Integer.parseInt(input.nextLine());
         System.out.println("Nhập chiều dài");
         height = Integer.parseInt(input.nextLine());
    
    }

    @Override
    public String toString() {
        return "Vehicle{" + "manufaturer=" + manufaturer + ", No=" + No + ", year=" + year + ", owner=" + owner + ", width=" + width + ", height=" + height + '}';
    }
    
    
    public void showInfor() {
        System.out.println(toString());
    
    }
    
    public int  getS() {
      return width * height;
      
     
    }
}



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

/**
 *
 * @author Minh
 */
public class Car extends Vehicle{

    public Car() {
        super(2, 3);
    }
    
}



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

import java.util.Scanner;

/**
 *
 * @author Minh
 */
public class Container extends Vehicle{

    public Container() {
        super(3, 5);
    }
    @Override
      public void input() {
      Scanner input = new Scanner(System.in);
      
        System.out.println("Nhap Nhà sản xuất:");
        manufaturer = input.nextLine();
        System.out.println("Nhap biển số xe:");
        No = input.nextLine();
        System.out.println("Nhap năm sản xuất :");
        year = input.nextLine();
        System.out.println("Nhap chủ sở hữu");
        owner = input.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 Car;

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

/**
 *
 * @author Minh
 */
public class Area {
    String address;
    int width,height;
    ArrayList<Vehicle> vehicleList  = new ArrayList<>();

    public Area() {
    }
    public void addVehicle(Vehicle vehicle){
      vehicleList.add(vehicle); 
    }
    
    public void sort() {
        Collections.sort(vehicleList, new Comparator<Vehicle>() {
            @Override
            public int compare(Vehicle o1, Vehicle o2) {
                   if(o1.getS() < o2.getS()) {
                      return -1;
                   }
                   return 1;
            }
        });
    }
    
    public void input() {
      Scanner input = new Scanner(System.in);
        System.out.println("Vi trí :");
        address = input.nextLine();
        System.out.println("Nhap chiều rộng :");
        width = Integer.parseInt(input.nextLine());
        System.out.println("Nhập chiều dài :");
        height = Integer.parseInt(input.nextLine());
    }
    public boolean checkAvaiable(Vehicle vehicle){
      int usedWidth = 0;
      int usedHeight = 0;
      
        for (int i = 0; i < vehicleList.size(); i++) {
            usedWidth += vehicleList.get(i).getWidth();
            usedHeight += vehicleList.get(i).getHeight();
            
        }
        int avaialbeWidth = width  - usedWidth;
        int avaialbeHeight = height - usedHeight;
        
        if(avaialbeWidth >= vehicle.getWidth() && avaialbeHeight >= vehicle.getHeight()){
          return true;
           
        }
        return false;
    }
    public void showInfor() {
        for (int i = 0; i < vehicleList.size(); i++) {
            vehicleList.get(i).showInfor();
        }
    
    }

}
 



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

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

/**
 *
 * @author Minh
 */
public class Zone {
    ArrayList<Area> areaList  = new ArrayList<>();
    
    public void input() {
     Scanner input = new Scanner(System.in);
     
        System.out.println("Nhap so khu vuc cho zone:");
        int n = input.nextInt();
        for (int i = 0; i < n; i++) {
            Area area = new Area();
            area.input();
            
            areaList.add(area);
        }
    }
    
    public void inputVehicle() {
      Scanner input = new Scanner(System.in);
        System.out.println("Nhap số xe cần thêm :");
        int n  = input.nextInt();
        int choose;
        for (int i = 0; i < n; i++) {
            showOptions();
            choose = input.nextInt();
            
            Vehicle vehicle = new Vehicle();
            
            if(choose == 1){
                vehicle = new Car();
            
            }else{
            
               vehicle = new Container();
            }
            vehicle.input();
            for (int j = 0; j < areaList.size(); j++) {
                if(areaList.get(i).checkAvaiable(vehicle)){
                   areaList.get(i).addVehicle(vehicle);
                   break;
                }
            }
        }
    
    }
    private void showOptions() {
        System.out.println("1. Nhập  car:");
        System.out.println("2.Nhap container");
        System.out.println("Lựa chọn");
    
    }
    
    public void sort() {
        for (int i = 0; i < areaList.size(); i++) {
            areaList.get(i).sort();
        }
    }
    
    public void showInfor() {
         for (int i = 0; i < areaList.size(); i++) {
            areaList.get(i).showInfor();
        }
    }

    
}



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

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

/**
 *
 * @author Minh
 */
public class PeopleMgr {
    ArrayList<People> peopleList = new ArrayList<>();
     
    public void input() {
     Scanner input = new Scanner(System.in);
        System.out.println("Nhap so chu xe:");
       int n  = input.nextInt();
         for (int i = 0; i < n; i++) {
               People people = new People();
            people.input();
            
            peopleList.add(people);
        }
    }
}



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

import java.util.Scanner;

/**
 *
 * @author Minh
 */
public class Main {
    public static void main(String[] args) {
        PeopleMgr peopleMgr = new PeopleMgr();
        Zone zone = new Zone();
        int choose = 0;
        Scanner input = new Scanner(System.in);
        
        
        do{
            showMenu();
            choose = input.nextInt();
            
            switch(choose){
                case 1:
                    peopleMgr.input();
                    break;
                case 2:
                    zone.input();
                    break;
                case 3:
                    zone.inputVehicle();
                    break;
                case 4:
                    zone.sort();
                    break;
                case 5:
                    zone.showInfor();    
                    break;
                case 6:
                    System.out.println("Thoat:");
                    break;
                default:
                    System.out.println("Error!!!");
                    break;
            
            }
        
        }while(choose !=6);
    }
    public static void showMenu() {
        System.out.println("1. Nhap thông tin N people.");
        System.out.println("2.Nhâp thông tin N Area lưu vào zone");
        System.out.println("3.Nhập thông tin N vehicle và tự động tìm kiếm khu vự phù hợp để lưu chữ.");
        System.out.println("4. Hiển thị thông tin các xe được sở hữu bởi 1 người theo Tên");
        System.out.println("5.Dùng giải thuật Quick Sort để sắp xếp theo thứ tự giảm dần theo diện tích của xe trong từng khu vực.");
        System.out.println("6.Sử dụng kết quả 5 hiển thị thông tin xe cộ trong Zone đó.");
        System.out.println("Choose : ");
    }
}