نمونه کلاس آژانس تلفنی در جاوا

در این سورس کد شما ابتدا کلاس Person را برای مدل کردن اشخاص پیاده می کنید. سپس، کلاس Car را برای پیاده کردن ماشین‌هایی که در آژانس کار می کنند پیاده می کنید. در نهایت کلاس Agency را پیاده و بطور مناسب تست می کنید.
• کلاس Person شامل اسم، شماره ی ملی، جنسیت، سن و شماره ی گواهینامه‌ی رانندگی است.
• کلاس Car دارای اسم، سال ساخت، تعداد سرنشین و راننده است.
• کلاس Agency شامل مدیر آژانس، شماره ی تماس، آدرس و تعدادی ماشین است.
• متدهای کلاسهای Person و Car مثل اجزا سازنده ی پیش‌فرض، کامل و کپی، متدهای set و get ، equals و متد toString را پیاده کنید.
• متدهای کلاس Agency شامل اجزاء سازنده ی کپی، پیش‌فرض و کامل هستند. هم چنین، متدهای equals و toString را پیاده کنید. این کلاس دارای متدهای addCar و removeCar برای اضافه کردن ماشین‌های جدید و حذف ماشین‌های قبلی هستند.
• در کلاس تست، ابتدا نه نفر ایجاد کنید. سپس، دو آژانس ایجاد کنید و به هر کدام به ترتیب سه و چهار ماشین اضافه کنید. از نه نفر ایجاد شده دو نفر به عنوان مدیر آژانس‌ها و هفت نفر به عنوان راننده‌ی ماشین‌ها استفاده می شوند. سپس متدهای toString و equals را تست کنید. سپس، از آژانس دوم یک کپی ایجاد کنید و با استفاده از آن صحت عملکرد متد equals را هنگام مقایسه ی آژانس کپی و آژانس کپی شده استفاده کنید.
(تست کلاس بر عهده‌ی شما)

//Person Class
//Java Programming
//By: Sina Moradi
//Website: Samiantec.ir

class Person
{

    public enum Gender
	{
        Male, Female, Unknown;
    }

	private String name,nativeCode;
	private Gender gender;
    private int age, licenseNum;

    public Person(String name, String nativeCode, Gender gender, int age, int licenseNum)
	{
		setName(name);
		setNativeCode(nativeCode);
		setGender(gender);
        setAge(age);
		setLicenseNum(licenseNum);
    }

    public Person()
	{
        this("Person", "0123456789", Gender.Male, 19, 94);
    }

    public Person(Object otherObject)
	{
        if (otherObject == null)
		{
            System.out.print("Object was null.");
            System.exit(0);
        }
		if (getClass() != otherObject.getClass())
		{
            System.out.print("Object isnot valid.");
            System.exit(0);
        }
		Person otherPerson=(Person)otherObject;
        setName(otherPerson.name);
		setNativeCode(otherPerson.nativeCode);
		setGender(otherPerson.gender);
        setAge(otherPerson.age);
		setLicenseNum(otherPerson.licenseNum);
    }

    public boolean equals(Object otherObject)
	{
        if (otherObject == null)
			return false;
		if (getClass() != otherObject.getClass())
			return false;
		Person otherPerson=(Person)otherObject;
        if (name == otherPerson.name && nativeCode == otherPerson.nativeCode && gender == otherPerson.gender && age == otherPerson.age && licenseNum == otherPerson.licenseNum)
		{
            return true;
        }
        return false;
    }

    public String toString()
	{
        return "{Name=" + name + " ; NativeCode=" + nativeCode + " ; Gender=" + gender.toString() + " ; Age=" + age + " ; LicenseNum=" + licenseNum+"}";
    }

	public String getName()
	{
        return name;
    }

    public void setName(String name)
	{
        if (name.trim().length() <= 0)
		{
            System.out.println("The name is invalid..");
            System.exit(0);
        }
        this.name = name;
    }

	public String getNativeCode()
	{
        return nativeCode;
    }

    public void setNativeCode(String nativeCode)
	{
        if (!isValidNativeNum(nativeCode))
		{
            System.out.println("The native code is invalid..");
            System.exit(0);
        }
        this.nativeCode = nativeCode.trim() ;
    }

	private boolean isValidNativeNum(String nativeCode)
	{
		if (nativeCode.trim().length() <= 0)
			return false;
        for (int i=0;i < nativeCode.length();i++)
			if (!Character.isDigit(nativeCode.charAt(i)))
				return false;
		return true;
    }

    public int getAge()
	{
        return age;
    }

    public void setAge(int age)
	{
        if (age <= 0)
		{
            System.out.println("Age was negative.");
            System.exit(0);
        }
        this.age = age;
    }

    public int getLicenseNum()
	{
        return licenseNum;
    }

    public void setLicenseNum(int licenseNum)
	{
        if (licenseNum <= 0)
		{
            System.out.println("License Num was negative.");
            System.exit(0);
        }
        this.licenseNum = licenseNum;
    }

    public Gender getGender()
	{
        return gender;
    }

    public void setGender(Gender gender)
	{
        this.gender = gender;
    }
}
//Car Class
//Java Programming
//By: Sina Moradi
//Website: Samiantec.ir

class Car
{
    private String name;
	private int year;
    private int size;
	private Person driver;

    public Car(String name, int year, int size, Person driver)
	{
		setName(name);
		setYear(year);
		setSize(size);
		setDriver(driver);
    }

    public Car()
	{
        this("Lancer", 94, 5, new Person());
    }

    public Car(Object otherObject)
	{
        if (otherObject == null)
		{
            System.out.print("Object was null.");
            System.exit(0);
        }
		if (getClass() != otherObject.getClass())
		{
            System.out.print("Object isnot valid.");
            System.exit(0);
        }
		Car otherCar=(Car)otherObject;
        setName(otherCar.name);
		setYear(otherCar.year);
		setSize(otherCar.size);
		setDriver(otherCar.driver);
    }

    public boolean equals(Object otherObject)
	{
        if (otherObject == null)
			return false;
		if (getClass() != otherObject.getClass())
			return false;
		Car otherCar=(Car)otherObject;
        if (name == otherCar.name && year == otherCar.year && size == otherCar.size && driver.equals(otherCar.driver))
            return true;
        return false;
    }

    public String toString()
	{
        return "{Name=" + name + " ; Year=" + year + " ; Size=" + size + " ; Driver=" + driver + "}";
    }

	public String getName()
	{
        return name;
    }

    public void setName(String name)
	{
        if (name.trim().length() <= 0)
		{
            System.out.println("The name is invalid..");
            System.exit(0);
        }
        this.name = name;
    }

	public int getYear()
	{
        return year;
    }

    public void setYear(int year)
	{
		if (year <= 0)
		{
            System.out.print("Year cannot be negative.");
            System.exit(0);
        }
		this.year = year;
    }

	public int getSize()
	{
        return size;
    }

    public void setSize(int size)
	{
        if (size <= 0)
		{
            System.out.println("The size is invalid..");
            System.exit(0);
        }
        this.size = size ;
    }

	public Person getDriver()
	{
        return new Person(driver);
    }

    public void setDriver(Person driver)
	{
		if (driver == null)
		{
            System.out.print("Object was null.");
            System.exit(0);
        }
		this.driver = new Person(driver);
    }
}
//Agency Class
//Java Programming
//By: Sina Moradi
//Website: Samiantec.ir

class Agency
{
    private Person admin;
	private String tel;
	private String addr;
	private Car[] cars=new Car[0];

    public Agency(Person admin, String tel, String addr, Car[] cars)
	{
		setAdmin(admin);
		setTel(tel);
		setAddr(addr);
		setCars(cars);
    }

    public Agency()
	{
		Car sampleCars[]=new Car[1];
		sampleCars[0] = new Car();
		setAdmin(new Person());
		setTel("09180000000");
		setAddr("ilam-farhangian");
		setCars(sampleCars);
    }

    public Agency(Object otherObject)
	{
        if (otherObject == null)
		{
            System.out.print("Object was null.");
            System.exit(0);
        }
		if (getClass() != otherObject.getClass())
		{
            System.out.print("Object isnot valid.");
            System.exit(0);
        }
		Agency otherAgency=(Agency)otherObject;
        setAdmin(otherAgency.admin);
		setTel(otherAgency.tel);
		setAddr(otherAgency.addr);
		setCars(otherAgency.cars);
    }

    public boolean equals(Object otherObject)
	{
        if (otherObject == null)
			return false;
		if (getClass() != otherObject.getClass())
			return false;
		Agency otherAgency=(Agency)otherObject;
		boolean f=false;
        if (admin.equals(otherAgency.admin) && tel == otherAgency.tel && addr == otherAgency.addr && cars.length == otherAgency.cars.length)
		{
			for (int i=0;i < cars.length;i++)
			{
				f = false;
				for (int j=0;j < cars.length;j++)
				{
					if (cars[i].equals(otherAgency.cars[j]))
					{
						f = true;
						break;
					}
				}
				if (f == false)
					return false;
			}
		}
		if (f) return true; else
			return false;
    }

    public String toString()
	{
		String res="Admin= " + admin + " ; Tel=" + tel + " ; Address=" + addr + " ; Cars={";
        if (cars != null)
			for (int i=0;i < cars.length;i++)
				res += cars[i].toString();
		return res + "}";
    }

	public Person getAdmin()
	{
        return new Person(admin);
    }

    public void setAdmin(Person admin)
	{
		if (admin == null)
		{
            System.out.print("Object was null.");
            System.exit(0);
        }
		this.admin = new Person(admin);
    }

	public String getTel()
	{
        return tel;
    }

    public void setTel(String tel)
	{
        if (tel.trim().length() <= 0)
		{
            System.out.println("The tel num is invalid..");
            System.exit(0);
        }
        this.tel = tel;
    }

	public String getAddr()
	{
        return addr;
    }

    public void setAddr(String addr)
	{
        if (addr.trim().length() <= 0)
		{
            System.out.println("The addr is invalid.");
            System.exit(0);
        }
        this.addr = addr;
    }

	public Car[] getCars()
	{
		Car[] temp=new Car[cars.length];
		for (int i=0;i < cars.length;i++)
			temp[i] = new Car(cars[i]);
        return temp;
    }

    public void setCars(Car[] cars)
	{
		if (cars == null) return;
		for (int i=0;i < cars.length;i++)
			if (cars[i] == null)
			{
				System.out.println("at least one of the cars is invalid.");
				System.exit(0);
			}
		Car[] temp=new Car[cars.length];
		for (int i=0;i < cars.length;i++)
			temp[i] = new Car(cars[i]);
        this.cars = temp ;
    }

	public void addCar(Car car)
	{
		if (car == null)
		{
			System.out.println("The object is null.");
			System.exit(0);
		}
		Car[] temp=new Car[cars.length + 1 ];
		for (int i=0;i < cars.length;i++)
			temp[i] = new Car(cars[i]);
		temp[cars.length] = new Car(car);
		cars = temp;
	}

	public void removeCar(int carIndex)
	{
		if (carIndex < 0 || carIndex >= cars.length || cars.length == 0)
		{
			System.out.println("Remove car error.");
			System.exit(0);
		}
		int k=0;
		Car[] temp=new Car[cars.length - 1];
		for (int i=0;i < cars.length;i++)
			if (i == carIndex)
				k = 1;
			else
				temp[i - k] = new Car(cars[i]);
		cars = temp;
	}
}
//Agency Test Class
//Java Programming
//By: Sina Moradi
//Website: Samiantec.ir

public class Test
{
	public static void main(String[] args)
	{
		Car[] ca=new Car[2];
		ca[0] = new Car("p1", 94, 5, new Person());
		ca[1] = new Car("p2", 94, 5, new Person());
		Agency a=new Agency(new Person("admin1", "0123456789", Person.Gender.Male, 19, 94), "0918", "ilam", ca);

		Car[] cb=new Car[2];
		cb[1] = new Car("p1", 94, 5, new Person());
		cb[0] = new Car("p2", 94, 5, new Person());
		Agency b=new Agency(new Person("admin1", "0123456789", Person.Gender.Male, 19, 94), "0918", "ilam", cb);

		System.out.println(a);
		System.out.println(b);
		System.out.println(a.equals(b));
		
	}
}

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *




Enter Captcha Here :