I am implemeting a circular queue in java which will accept objects (Employee) as entries. Now I have a method to edit the surname of the specific object, but somehow I cannot access the surname setter method found in the Employee class from the CQueue class even though I am importing all the required packages. Here is the code:
//PACKAGES IMPORTED
package linearstructures;
import dataobjects.*;
import linearnodes.*;
public class CQueue{
Node front, rear, temp;
boolean full = false;
String key;
public AnyClass searchKey(String key)
{
temp = rear.next; // first node
do
{
if (temp.obj.getKey().equals(key))
return temp.obj;
temp = temp.next;
} while (temp != rear.next);
return null;
}
public AnyClass editObject(String key){
int choice, newSeqNo;
double newPay;
boolean exit = false;
Employee etemp = new Employee(); //INCLUDED THIS AFTER EDIT
searchKey(key);
if(searchKey(key) != null){
temp.obj.getData();
System.out.println();
System.out.println("------------------------");
System.out.print("Enter new Salary: ");
newPay = sc.nextDouble();
System.out.println("------------------------");
System.out.println();
etemp.setPay(newPay); //INCLUDED THIS AFTER EDIT
else
System.out.println("NO OBJECT WAS FOUND!");
return null;
}
}
Employee class:
package dataobjects;
public class Employee extends AnyClass
{
public String surname;
public double pay;
public Employee(){}
public Employee(int seqNo, String surname, double pay)
{
super(seqNo);
this.surname = surname;
this.pay = pay;
}
public double getSalary()
{
return pay;
}
public void setPay(double newPay)
{
pay = newPay;
}
public String getData()
{
return super.getData() + ", Surname: " + surname + ", Pay: " + pay;
}
public String getKey()
{
return surname;
}
}
AnyClass class:
package dataobjects;
public class AnyClass
{
public int seqNo;
public AnyClass(){}
public AnyClass(int seqNo)
{
this.seqNo = seqNo;
}
public int getseqNo()
{
return seqNo;
}
public void setseqNo(int seqNo) {
this.seqNo = seqNo;
}
public String getData()
{
return "Sequential Number - " + seqNo;
}
public String getKey()
{
return Integer.toString(seqNo);
}
public void edit(){}
}
Node Class
package linearnodes;
import dataobjects.*;
public class Node{
public AnyClass obj;
public Node next;
public Node(AnyClass obj){
next = null;
this.obj = obj;
}
}
Your editobject method could be something like this:
public AnyClass editObject(String key){
// ...
// Store your search result to avoid performing searchKey twice
AnyClass searchResult = searchKey(key);
if( searchResult != null ){
// Check if searchResult is an Employee object
if( searchResult instanceof Employee )
// Cast AnyClass to Employee
Employee empl = (Employee) searchResult;
// Your logic here...
// Set properties of the Employee object
empl.setPay(newPay);
// ...
return empl;
}
// Add 'else if' here, if you need to manage other Object types
// otherwise you can join previous if conditions
}
else
System.out.println("NO OBJECT WAS FOUND!");
return null;
}
Your code creates a new local Employee instance that dies when the method ends, its value will be lost because no object points to it.
for include pay "etemp.setPay(newPay);" you will change return object to Employee.
public Employee editObject(String key){
Employee etemp = new Employee(); //INCLUDED THIS AFTER EDIT
....
....
etemp.setPay(newPay); //INCLUDED THIS AFTER EDIT
return etemp;
}
because AnyClass hasn't "public double pay;"
Related
Getting stuck with my Travail System Project, Confusing a little bit about understanding that if there Classes called Bookable, Hotel and BookingSystem.
Hotel class is implements Bookable. Furthermore, BookingSystem Class is composition from Bookable, So, I need to create method at BookingSystem class which called addHotel.
what I must do about it to make a relationship between Hotel Class and BookingSystem Class.
Thanks In Advance.
Israa
Hotal Class:
public class Hotel implements Bookable {
private String name, location;
private int noOfRooms;
private double roomPrice;
private Date bookingDate;
private ArrayList<Integer> bookedRooms = new ArrayList<Integer>();
private ArrayList<Integer> numberOfrooms = new ArrayList<Integer>();
public Hotel() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getNoOfRooms() {
return noOfRooms;
}
public void setNoOfRooms(int noOfRooms) {
this.noOfRooms = noOfRooms;
}
public double getRoomPrice() {
return roomPrice;
}
public void setRoomPrice(double roomPrice) {
this.roomPrice = roomPrice;
}
public Date getBookingDate() {
return bookingDate;
}
public void setBookingDate(Date bookingDate) {
this.bookingDate = bookingDate;
}
public ArrayList<Integer> getBookedRooms() {
return bookedRooms;
}
public void setBookedRooms(ArrayList<Integer> bookedRooms) {
this.bookedRooms = bookedRooms;
}
public String Book() {
if ( numberOfrooms.size() != (bookedRooms.size())) {
for (int i = 0; i < bookedRooms.size(); i++) {
int oldVal = bookedRooms.get(i);
int newVal = oldVal + 1;
bookedRooms.add(bookedRooms.set(i, newVal));
}
}
return null;
}
}
Bookable class:
public interface Bookable {
public String Book();
}
BookingSytsem Class:
public class BookingSystem {
private ArrayList<Customer> customer = new ArrayList<Customer>();
private ArrayList<Bookable> bookable = new ArrayList<Bookable>();
private ArrayList<Operation> operation = new ArrayList<Operation>();
public BookingSystem() {
}
// **
public void addCustomer(String name, int id) {
Customer customers = new Customer(id, name);
customer.add(customers);
System.out.println("new customer " + customers.getName() + " added");
}
// **
public void deleteCustomer(String name, int id) {
Customer customers = new Customer(id, name);
if (customer.contains(name)) {
customer.remove(name);
}
System.out.println("Customer " + customers.getName() + " deleted");
}
public Customer findCustomer(int id) {
for (Customer c : customer) {
if (c.getId() == id) {
return c;
}
}
return null;
}
public void addHotel() {
Hotel H1 = new Hotel();
Scanner name = new Scanner(System.in);
System.out.println("Please Enter the name of Hotel: ");
String n1 = name.nextLine();
bookable.add(H1);
System.out.println("The Hotel " + name + "added");
}
public void makeABooking(Customer c, Bookable b) {
Scanner input =new Scanner(System.in);
System.out.println("Please Enter your name: ");
String name = input.nextLine();
System.out.println("Please Enter your ID: ");
int ID = input.nextInt();
while(true) {
if(ID == -1 && ID == 0) {
System.out.println("Invalid ID. Enter again: ");
name = input.nextLine();
System.out.println("Please Enter your ID: ");
ID = input.nextInt();
}
}
}
}
(Your question is more suitable to https://softwareengineering.stackexchange.com/ - recommend asking it there...)
General speaking it wouldn't make sense to have a Hotel without a name, location or number of rooms so I'd recommend adding a constructor with minimal required information:
public Hotel (String name, String location, int rooms) {
this.name = name;
this.location = location;
this.noOfRooms = rooms;
}
bookingDate makes no sense as a single property of a hotel but rather a property of each booked room so you have a design issue - this is not addressed here.
Again, roomPrice usually varies by room so in a robust solution this would be a property of a room not a hotel - not addressed here.
Why is there a noOfRooms and a numberOfRooms list. In fact, the numberOfRooms list doesn't make sense as a list. I'd just keep the noOfRooms and get rid of numberOfRooms.
An implied property, nbrOfAvailableRooms can be derived from noOfRooms - bookedRooms.size();
I would assume your bookedRooms is a list of room numbers which are booked but that's not possible to tell from your implementation. You should focus on what you want Book to do.
The Book interface method is not documented but it looks like it should simply take an available asset (room) and consider it booked. It should return a boolean success not a String - especially not null.
I recommend writing (in pseudo code) what you want a Book implementation to do - include that in question. That is the core issue you are having.
This is my Car class:
public class Car {
private int FGNr;
private String name;
private String type;
private Owner owner;
private static ArrayList<Integer> allCarIds = new ArrayList<>();
public Car(int FGNr, String name, String type, Owner o) throws Exception {
setFGNr(FGNr);
setName(name);
setType(type);
setOwner(o);
}
public int getFGNr() {
return FGNr;
}
public void setFGNr(int FGNr) throws Exception{
this.FGNr = FGNr;
if(allCarIds.contains(this.FGNr))
throw new Exception("FGNr already excists!! ");
allCarIds.add(this.FGNr);}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Owner getOwner() {
return owner;
}
public void setOwner(Owner owner) throws Exception{
owner.addCar(this);
this.owner = owner;
}
#Override
public int hashCode() {
int hash = 7;
hash = 73 * hash + this.FGNr;
return hash;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Car other = (Car) obj;
if (this.FGNr != other.FGNr) {
return false;
}
return true;
}
#Override
public String toString() {
return "Car{" + "FGNr=" + FGNr + ", name=" + name + ", type=" + type + ", owner=" + owner + '}';
}
}
And this is my Owner class:
public class Owner {
private String SVNr;
private String name;
HashSet<Car> allCars = new HashSet<>();
private static ArrayList<String> allOwnerSVNs = new ArrayList<>();
public Owner(String SVNr, String name) throws Exception{
setSVNr(SVNr);
setName(name);
}
public void addCar(Car c) throws Exception{
if(allCars.contains(c))
throw new Exception("this user has already this car");
if(c.getOwner()!=null)
throw new Exception("this car belongs to other owner");
c.setOwner(this);
allCars.add(c);
}
public String getSVNr() {
return SVNr;
}
public void setSVNr(String SVNr) throws Exception{
this.SVNr = SVNr;
if(allOwnerSVNs.contains(this.SVNr))
throw new Exception("SVNg already excists!! ");
allOwnerSVNs.add(this.SVNr);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HashSet<Car> getAllCars() {
return allCars;
}
#Override
public int hashCode() {
int hash = 5;
hash = 41 * hash + Objects.hashCode(this.SVNr);
return hash;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Owner other = (Owner) obj;
if (!Objects.equals(this.SVNr, other.SVNr)) {
return false;
}
return true;
}
#Override
public String toString() {
return "Owner{" + "SVNr=" + SVNr + ", name=" + name + ", allCars=" + allCars + '}';
}
}
And this is my main:
try {
Owner o1 = new Owner("0001","Owner1");
Owner o2 = new Owner("0002","Owner2");
Car c1 = new Car(1,"Model S", "Tesla",o1);
Car c2 = new Car(2,"Model 3", "Tesla",o2);
Car c3 = new Car(3,"TT", "Audi",o2);
} catch (Exception ex) {
System.out.println("error:"+ex.getMessage());
}
So when trying to create a new Car I get this error:
Exception in thread "main" java.lang.StackOverflowError
at java.util.HashMap.containsKey(HashMap.java:595)
at java.util.HashSet.contains(HashSet.java:203)
at pkgData.Owner.addCar(Owner.java:28)
at pkgData.Car.setOwner(Car.java:63)
...........
It is a recursion error, but I don't know how to fix it. If I create a new car obviously I have to add the Car to the owner arrayList of cars. and if I call the addCar function the function calls the getOwner function. It's an endless circle of calling.
How I can make sure that when creating a new car that the collection of the owner will also be changed. It would not make any sense that a car has an owner but the owner of the car does not the car in his collection.
These two functions fall an infinite loop as you see.
In Car class
public void setOwner(Owner owner) throws Exception{
owner.addCar(this);
this.owner = owner;
}
And in Owner Class
public void addCar(Car c) throws Exception{
if(allCars.contains(c))
throw new Exception("this user has already this car");
if(c.getOwner()!=this && c.getOwner()!=null)
throw new Exception("this car belongs to other owner");
c.setOwner(this);
allCars.add(c);
}
The car sets its owner and sends itself to the Owner class' addCar() method, thats OK. However, why the Owner class' addCar() method sets the owner as itself again ?
I think there is a logical mistake. If you remove c.setOwner(this) line, it works fine.
Usually adding element to a container should be done in the container itself.
In you example Owner is the container and Car is the element.
As an example see java.awt.Container and java.awt.Component.
Don't call owner.addCar(this); from the Car.setOwner.
Let the Owner add the car to the list (which you already do) and set itself as owner to the Car.
public void setOwner(Owner owner) throws Exception{
owner.addCar(this); //remove this line
this.owner = owner;
}
I need to print out the content of my ArrayList however it doesn't print out correctly. More specifically it is the getAccountSaldo in CustomerRegister that is not returning the correct value (if I wanna print out for example a different account).
Here is my code:
Class Account
public class Account {
private Customer customer;
private String nbr;
private double saldo;
public void setNbr(String nbr){
this.nbr = nbr;
}
public String getNbr(){
return nbr;
}
public void setSaldo(double saldo){
this.saldo = saldo;
}
public double getSaldo(){
return saldo;
}
public void setCustomer(Customer customer){
this.customer = customer;
}
public Customer getCustomer(){
return customer;
}
public void withdraw(double amount){
saldo -= amount;
}
public void deposit(double amount){
saldo += amount;
}
}
Class Customer
public class Customer {
private String nbr;
private String namn;
private ArrayList<Account> accounts = new ArrayList<Account>();
public void setNbr(String nbr){
this.nbr = nbr;
}
public String getNbr(){
return nbr;
}
public void setNamn(String namn){
this.namn = namn;
}
public String getNamn(){
return namn;
}
public void setAccounts(ArrayList<Account> accounts){
this.accounts = accounts;
}
public ArrayList<Account> getAccounts(){
return accounts;
}
public void add(Account account){
accounts.add(account);
}
public Account find(String nbr){
for(Account a : accounts){
if(nbr == a.getNbr()){
return a;
}
}
return null;
}
}
Class CustomerRegister
public class CustomerRegister {
private ArrayList<Customer> customers = new ArrayList<Customer>();
public void setCustomers(ArrayList<Customer> customers){
this.customers = customers;
}
public ArrayList<Customer> getCustomers(){
return customers;
}
public void add(Customer k){
customers.add(k);
}
public Customer find(String nr){
for (Customer b : customers){
if (nr == b.getNbr()){
return b;
}
}
return null;
}
public ArrayList<Account> printAccounts(String customerNbr){
Customer l = find(customerNbr);
if (l != null){
return l.getAccounts();
}
return null;
}
// CAN'T SOLVE THIS
public Double getAccountSaldo(String customerNbr, String accountNbr) {
double balance = 0;
Customer custNbr = find(customerNbr);
// Shouldn't it be "Account acctNbr = find(accountNbr);" here?
// Problem is I can't access that method in this class...
if(custNbr != null){
for (Account x : printAccounts(customerNbr)) {
if (accountNbr == x.getNbr()) {
balance += x.getSaldo();
}
return balance;
}
}
return null;
}
}
Class demo:
public class Demo {
public static void main(String[] args){
CustomerRegister r1 = new CustomerRegister();
ArrayList<Account> listOfAccounts = new ArrayList<Account>();
ArrayList<Customer> listOfCustomers = new ArrayList<Customer>();
Customer c = new Customer();
c.setNbr("1");
c.setNamn("Adam Schinn");
Account a = new Account();
a.setNbr("Konto 1");
a.setSaldo(2200);
a.setCustomer(c);
Account b = new Account();
b.setNbr("Konto 2");
b.setSaldo(2000);
b.setCustomer(c);
listOfCustomers.add(c);
listOfAccounts.add(a);
listOfAccounts.add(b);
c.setAccounts(listOfAccounts);
r1.add(c);
r1.setCustomers(listOfCustomers);
for(Account temp : r1.printAccounts("1")){
System.out.println(temp.getNbr() + " Balance: " + temp.getSaldo() + "kr ");
}
for(Customer temp : r1.getCustomers()){
System.out.println("Nr: " + temp.getNbr() + " Namn: " + temp.getNamn());
}
// CAN'T SOLVE THIS
// If I write "Konto 2" instead of "Konto 1" it prints "0.0"
System.out.println(r1.getAccountSaldo("1", "Konto 1"));
}
}
Print out
Konto 1 Balance: 2200.0kr
Konto 2 Balance: 2000.0kr
Nr: 1 Namn: Adam Schinn
2200.0
You can't compare strings using == operator, this will not check equality for content instead it check the address of two strings.
Also the return statement was not at correct position.
I have made few changes to your function, have a try with that :
public Double getAccountSaldo(String customerNbr, String accountNbr) {
double balance = 0;
Customer custNbr = find(customerNbr);
// Shouldn't it be "Account acctNbr = find(accountNbr);" here?
// Problem is I can't access that method in this class...
if(custNbr != null){
for (Account x : printAccounts(customerNbr)) {
if (accountNbr.equals(x.getNbr())) {
balance += x.getSaldo();
}
}
}
return balance;
}
I'm new to Java. Please help me understand Java.
I have trouble understanding some my teacher's code about "stacks."
The code bellow is for my class Person. My question is what does private Person next; mean? Why this variable such with class name ? What is this class doing?
public class Person {
private String _name, _address;
private int _id;
private Person next; // what is this mean and do
public Person(String a, String b, int c){
this._name = a;
this._address = b;
this._id = c;
}
public Person(){
}
public String getname(){
return this._name;
}
public String getaddress(){
return this._address;
}
public int getid(){
return this._id;
}
public person getnext(){
return this.next;
}
public void setname(String a){
this._name = a;
}
public void setaddress(String a){
this._address = a;
}
public void setid(int a){
this._id = a;
}
public void setnext(person a){
this.next = a;
}
#Override
public String toString(){
return "Person{"+ "Name = " + _name +", Address" + _address +", Id = " + _id +'}';
}
}
And this is my code for main class. In this main class I can't understand what private static Person HeadStack contains and what it will do. Why is HeadStack keyword always used in method "pop", "push", and "print"?
public class mainmahasisswa {
private static Person HeadStack; // what is this mean & do
public static void main(String[] args) {
push();
push();
push();
print();
pop();
print();
pop();
print();
push();
print();
}
private static Person setdata(){
person pr = new person();
String name, address;
int id;
name = JOptionPane.showInputDialog("Name : ");
address = JOptionPane.showInputDialog("Address : ");
id = Integer.parseInt(JOptionPane.showInputDialog("id : "));
pr.setname(name);
pr.setaddress(address);
pr.setid(id);
pr.setnext(null);
return pr;
}
private static void pop(){
if (HeadStack != null){
person aa ;
aa = HeadStack.getnext();
HeadStack = aa;
}
else{
System.out.println();
}
}
private static void push(){
person x = setdata();
if(HeadStack != null){
x.setnext(HeadStack);
HeadStack = x;
}
else{
HeadStack = x;
}
}
private static void print(){
if(HeadStack != null ){
person y = HeadStack;
while(y != null){
System.out.println(y.toString());
y = y.getnext();
}
System.out.println();
}
else{
System.out.println();
}
}
}
Think of it as a link in the chain private person next provides a link to the "next" person object in this chain or null if it does not exist.
private person next; // what is this mean and do
This a declaration that says next is a type of person, which is initially assigned null
This...
private static person HeadStack; // what is this mean & do
is simply a declaration that says HeadStack is type of person which is static, meaning that it doesn't rely on particular instance of any class to be accessed.
Take a look at Language Basics: Variables and Understanding Class Members
why this HeadStack keyword always used in method "pop","push","print"
The object represents the first link in the chain
I have a Query I have developed a pojo ..
public class Customer {
int Age;
public Customer(int age, String surname, String forename) {
super();
Age = age;
Surname = surname;
Forename = forename;
}
String Surname,Forename;
public int getAge() {
// TODO Auto-generated method stub
return Age;
}
public String getSurname() {
// TODO Auto-generated method stub
return Surname;
}
public String getForename() {
// TODO Auto-generated method stub
return Surname;
}
public void display()
{
// System.out.println(Forename+"\t"+Surname+"\t"+Age);
System.out.println(Age+"\t"+Forename+"\t"+Surname);
}
}
and here is my collection class ..
class testCustomerComparator
{
public static void main(String... a)
{
Customer customerFirst = new Customer(46,"Alabama", "Christonson");
Customer customerSecond = new Customer(21, "Anna", "Sobek");
Customer customerThird = new Customer(27, "Rafael", "Sobek");
List<Customer> list = new ArrayList<Customer>();
list.add(customerThird);
list.add(customerSecond);
list.add(customerFirst);
}
}
please advise me How to make comprator for this class , I want to make comparator so that a list of customers get sorted by age and second by surname. After that you want to sort by forename. please advise I have nesting condition inside comparator
lOGIC MUST BE SOMETHING LIKE...
public class CustomerComparator implements Comparator<Customer> {
#Override
public int compare(Customer c1, Customer c2) {
if (c1.getAge() == c2.getAge()) {
if (c1.getSurname().compareTo(c2.getSurname()) == 0) {
return c1.getForename().compareTo(c2.getForename()) {
} else {
return c1.getSurname().compareTo(c2.getSurname());
}
} else if (c1.getAge() > b2.getAge()) {
return -1;
} else {
return 1;
}
}
but it is not working please advise
Seems much like homework. I can give you some hints in where to look at.
You have two choices:
make the POJO class extend Comparable<Customer>
define a custom external comparator as a Comparator<Customer>.
Assuming the second choice, in which you have two explicit customers, you'll have to define a method similar to this one:
#Override
public int compare(Customer c1, Customer c2)
{
// this method should return 0 if c1.equals(c2),
// should instead return 1 if c1 should come first than c2 and -1 otherwise
}
public class CustomerComparator implements Comparator<Customer> {
public int compare(Customer c1, Customer c2) {
.... here you have c1 and c2. compare returns -1 if c1 should go before c2,
0 if they are found to be equal, and 1 if c2 should go before c1.
You add the logic to compare c1 and c2 fields as you stated and return the result.
}
}
Then you use Collections.sort to sort that list using this comparator.
You can help of the below code.
import java.util.*;
class Customer {
private int age;
private String name;
private String forename;
public Customer(int age, String surname, String forename) {
super();
this.age = age;
this.name = surname;
this.forename = forename;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setForename(String forename) {
this.forename = forename;
}
public String getForename() {
return forename;
}
}
class AgeComparator implements Comparator {
public int compare(Object emp1, Object emp2) {
int emp1Age = ((Customer) emp1).getAge();
int emp2Age = ((Customer) emp2).getAge();
if (emp1Age > emp2Age)
return 1;
else if (emp1Age < emp2Age)
return -1;
else
return 0;
}
}
/*
* The below given comparator compares employees on the basis of their name.
*/
class NameComparator implements Comparator {
public int compare(Object emp1, Object emp2) {
// parameter are of type Object, so we have to downcast it to Employee
// objects
int emp1Age = ((Customer) emp1).getAge();
int emp2Age = ((Customer) emp2).getAge();
if (emp1Age > emp2Age) {
return 1;
} else if (emp1Age < emp2Age) {
String emp1Name = ((Customer) emp1).getName();
String emp2Name = ((Customer) emp2).getName();
// uses compareTo method of String class to compare names of the
// employee
return emp1Name.compareTo(emp2Name);
} else {
return 0;
}
}
}
class CustomerComparator implements Comparator {
public int compare(Object emp1, Object emp2) {
// parameter are of type Object, so we have to downcast it to Employee
// objects
String emp1Name = ((Customer) emp1).getName();
String emp2Name = ((Customer) emp2).getName();
// uses compareTo method of String class to compare names of the
// employee
return emp1Name.compareTo(emp2Name);
}
}
public class JavaComparatorExample {
public static void main(String args[]) {
// Employee array which will hold employees
Customer employee[] = new Customer[3];
// set different attributes of the individual employee.
employee[0] = new Customer(46, "Alabama", "Christonson");
employee[1] = new Customer(21, "Anna", "Sobek");
employee[2] = new Customer(27, "Rafael", "Sobek");
System.out.println("Order of employee before sorting is");
// print array as is.
for (int i = 0; i < employee.length; i++) {
System.out.println("Employee " + (i + 1) + " name :: "
+ employee[i].getName() + ", Age :: "
+ employee[i].getAge());
}
Arrays.sort(employee, new AgeComparator());
System.out
.println("\n\nOrder of employee after sorting by employee age is");
for (int i = 0; i < employee.length; i++) {
System.out.println("Employee " + (i + 1) + " name :: "
+ employee[i].getName() + ", Age :: "
+ employee[i].getAge());
}
// Sorting array on the basis of employee Name by passing NameComparator
Arrays.sort(employee, new NameComparator());
System.out
.println("\n\nOrder of employee after sorting by employee name is");
for (int i = 0; i < employee.length; i++) {
System.out.println("Employee " + (i + 1) + " name :: "
+ employee[i].getName() + ", Age :: "
+ employee[i].getAge());
}
}
}
Hope this will help you.
EDIT
Look at the CustomerComparator class.
#Override
public int compare(Customer c1, Customer c2) {
int r = Integer.valueOf(c1.getAge()).compareTo(c2.getAge());
if (r != 0) return r;
r = c1.getSurname().compareTo(c2.getSurname());
if (r != 0) return r;
return c1.getForename().compareTo(c2.getForename());
}