By GokiSoft.com|
19:15 08/05/2024|
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 đó.
Tags:
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]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ Văn Huấn
2020-03-20 02:39:44
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe; |
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe; |
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe; |
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe; |
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe; |
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe; |
package BaiTapNgay16_3_2020.Quan_li_Bai_Do_Xe; |
![hoangduyminh [T1907A]](https://www.gravatar.com/avatar/33675cc9fc3762fd323389a179aa047f.jpg?s=80&d=mm&r=g)
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 : ");
}
}