Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Java Help

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-25-2007, 04:40 AM
WingKalimdor WingKalimdor is offline
Newbie
 
Join Date: Jul 2007
Posts: 13
Rep Power: 0
WingKalimdor is on a distinguished road
Default Keep getting error!!

Hi everyone, I having a minor difficulty in sorting the menu.

Quote:
Create a class for service offered by the hair styling salon. Data fields include a String to hold the service description (for example, “Cut”, “Shampoo”, or “Manicure”), a double to hold the price and an integer to hold the average minutes it takes to perform the service. The class should include the following:-
  1. Constructor
  2. Mutator
  3. Accesor
  4. toString method

Write a program that contains an array to hold six services as below:-



Your program should include a menu that view the report based on service, price or time depend on the user’s input. Sort your report in ascending order.

Example:

Here is my coding .... please help me... thanks..

Code:
import java.io.*;

class TestService{
	public static void main(String args []){
		
		int selection;
	
		
		Service array[]=new Service[6];
		
		
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("View The Report By \n");
		System.out.println("1. Service");
		System.out.println("2. Price");
		System.out.println("3. Time");
		System.out.println("4. Exit");
		
		try{
		
		do{
			
		
		System.out.print("Enter selection :");
		selection =Integer.parseInt(br.readLine());
		
		
		if(selection ==1){
		
				
			array[0]=new Service("Cut",8.00,15);
			array[1]=new Service("Shampoo",4.00,10);
			array[2]=new Service("Manicure",18.00,30);
			array[3]=new Service("Style",48.00,55);
			array[4]=new Service("Permanent",18.00,35);
			array[5]=new Service("Trim",6.00,5);
			
		}else if(selection ==2){
			array[0]=new Service("Shampoo",4.00,10);
			array[1]=new Service("Trim",6.00,5);
			array[2]=new Service("Cut",8.00,15);
			array[3]=new Service("Manicure",18.00,30);
			array[4]=new Service("Permanent",18.00,35);
			array[5]=new Service("Style",48.00,55);
		
			
		}else if(selection ==3){
			array[0]=new Service("Trim",6.00,5);
			array[1]=new Service("Shampoo",4.00,10);
			array[2]=new Service("Cut",8.00,15);
			array[3]=new Service("Manicure",18.00,30);
			array[4]=new Service("Permanent",18.00,35);
			array[5]=new Service("Style",48.00,55);}
		
		}while(!=4);
		
		System.out.println();
		System.out.println("Service"+"\t\t"+"Price<$>" +"\t" + "Time<Minutes>");
		System.out.println("=======" + "\t\t" +"========" +"\t" + "=============");

		
		for(int a=0;a<6;a++)
		System.out.println(array[a].toString());
		}
		

		catch(NumberFormatException ex){		
		System.out.println("You can only enter number");
		}
		catch(IOException ex){
			System.out.println("Input error");
		}
	}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-25-2007, 06:49 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,746
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default

From the looks of it, your data is stored in an array. You can use the java.util.Arrays.sort() method to sort the data of a primitive type in ascending order.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-25-2007, 08:32 AM
WingKalimdor WingKalimdor is offline
Newbie
 
Join Date: Jul 2007
Posts: 13
Rep Power: 0
WingKalimdor is on a distinguished road
Default

java.util.Arrays.sort() only can support 1 character, I having some difficulty to modify it. Here is a new program but it won't work after I change it back to array form. Please help me ....

Code:
class Report{
	
	String service;
	double price;
	int time;
	
	public Report (){
	}
	
	public Report (String s,double d,int i){
		service=s;
		price=d;
		time=i;
	}
	
	public void setService(String s){
		service=s;
	}
	public String getService(){
		return service;
	}
	
	public void setPrice(double d){
		price=d;
	}
	public double getPrice(){
		return price;
	}
	
	public void setTime(int i){
		time=i;
	}
	public int getTime(){
		return time;
	}
	
	public String toString(){
		return getService() + "\t   " + getPrice() + "\t\t\t" + getTime();
	}

	
}
Code:
import java.io.*;

class TestReport{
	public static void main (String args[]) throws IOException{
		
		int x;
		BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
		
		Report salon1 = new Report("Cut       ", 8.00, 15);
		Report salon2 = new Report("Shampo    ", 4.00, 10);
		Report salon3 = new Report("Manicure  ", 18.00, 30);
		Report salon4 = new Report("Style     ", 48.00, 55);
		Report salon5 = new Report("Permanent ", 18.00, 35);
		Report salon6 = new Report("Trim      ", 6.00, 5);
	
	do{System.out.println("View The Report By : \n");
	System.out.println("1. Service\n2. Price\n3. Time\n4. Exit\n");
	System.out.print("Enter Selection : ");
	x=Integer.parseInt(br.readLine());
	
	System.out.println("\n\n\n");
	
	if(x==4){
	System.out.println("Thanks for using this program!\n\n");
	System.exit(-1);
	}
			
	System.out.println("Service   \tPrice($)\tTime(minutes)");
	System.out.println("==========\t========\t=============\n");
	
	if(x==1){
	System.out.println(salon1);
	System.out.println(salon3);
	System.out.println(salon5);
	System.out.println(salon2);
	System.out.println(salon4);
	System.out.println(salon6);
	}else if(x==2){
		System.out.println(salon2);
		System.out.println(salon6);
		System.out.println(salon1);
		System.out.println(salon3);
		System.out.println(salon5);
		System.out.println(salon4);
	}else if(x==3){
		System.out.println(salon6);
		System.out.println(salon2);
		System.out.println(salon1);
		System.out.println(salon3);
		System.out.println(salon5);
		System.out.println(salon4);
	}
	else if(x==4){
		System.out.println("Thanks for using this program!\n\n");
		System.exit(-1);
	}else{
		System.out.println("\t<Data is not available because of wrong entry>\n\n");
	}
	
	System.out.println("\n\n\n\n");
	}while(x!=4);
	}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-25-2007, 03:57 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,746
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default

First, make an array of Reports, it is more difficult to sort random data than an array of data.

And what do you mean sort() only supports one character?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-26-2007, 06:00 AM
WingKalimdor WingKalimdor is offline
Newbie
 
Join Date: Jul 2007
Posts: 13
Rep Power: 0
WingKalimdor is on a distinguished road
Default

Here is the result when I try to align, please help me see whether got any place to be improve or not?

Code:
import java.io.*;
class runsalon
{

	public static void main(String args[])
	{
		salon type[]= new salon[6];
		salon choose=new salon();
		
		type[0]= new salon(leftAlign.left("Cut",10),8.00,15);
		type[1]= new salon(leftAlign.left("Shampoo",10),4.00,10);
		type[2]= new salon(leftAlign.left("Manicure",10),18.00,30);
		type[3]= new salon(leftAlign.left("Style",10),48.00,55);
		type[4]= new salon(leftAlign.left("Permanent",10),18.00,35);
		type[5]= new salon(leftAlign.left("Trim",10),6.00,5);
		
		do
		{
			try
			{
	
				System.out.println("View The Report By: ");
				System.out.println("1. Service");
				System.out.println("2. Price");
				System.out.println("3. Time");
				System.out.println("4. Exit");
	
				BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
				
				System.out.println("Enter selection: ");
				choose.setSelect(Integer.parseInt(br.readLine()));
				System.out.println("\n");
				
				if(choose.getSelect()==1)
				{
					serviceType(type);
				}
				else if(choose.getSelect()==2)
				{
					priceType(type);
				}
				else if(choose.getSelect()==3)
				{
					minuteType(type);
				}
			}
			catch(selectException ex)
			{
				System.out.println("Error" +ex.getMessage());
			}
			catch(IOException io)
			{
				System.out.println("Error: Invalid Selection");
			}
				System.out.println("\n");
		}while(choose.getSelect()!=4);
			
		
		
	}
	public static void serviceType(salon type[])
	{
		int charCheck;
		int charCompare;
		int serviceIndex[]=new int[6];
		int serviceChar[]=new int[6];
		int temp=0;
		
		for(int w=0;w<type.length;w++)
		{
			serviceChar[w]=type[w].getService().charAt(0);
			serviceIndex[w]=w;
		}
		for(int x=0;x<type.length;x++)
		{
			for(int y=x+1;y<type.length;y++)
			{
				if(serviceChar[serviceIndex[x]]>serviceChar[serviceIndex[y]])
				{
					temp=serviceIndex[x];
					serviceIndex[x]=serviceIndex[y];
					serviceIndex[y]=temp;
				}
				else if(serviceChar[serviceIndex[x]]==serviceChar[serviceIndex[y]])
				{
					charCheck=type[serviceIndex[x]].getService().charAt(1);
					charCompare=type[serviceIndex[y]].getService().charAt(1);
					if(charCheck>charCompare)
					{
						temp=serviceIndex[x];
						serviceIndex[x]=serviceIndex[y];
						serviceIndex[y]=temp;	
					}
				}
			}			
		}
		
		display(serviceIndex,type);
	}
	public static void priceType(salon type[])
	{
		int lowPriceIndex[]=new int[6];
		int temp=0;
		int charCheck;
		int charCompare;
		
		for(int w=0;w<type.length;w++)
		{
			lowPriceIndex[w]=w;
		}
		for(int x=0;x<lowPriceIndex.length;x++)
		{
			for(int y=x+1;y<type.length;y++)
			{
				if(type[lowPriceIndex[x]].getPrice()>type[lowPriceIndex[y]].getPrice())
				{
					temp=lowPriceIndex[x];
					lowPriceIndex[x]=lowPriceIndex[y];
					lowPriceIndex[y]=temp;
				}
				else if(type[lowPriceIndex[x]].getPrice()==type[lowPriceIndex[y]].getPrice())
				{
					charCheck=type[lowPriceIndex[x]].getService().charAt(0);
					charCompare=type[lowPriceIndex[y]].getService().charAt(0);
					if(charCheck>charCompare)
					{
						temp=lowPriceIndex[x];
						lowPriceIndex[x]=lowPriceIndex[y];
						lowPriceIndex[y]=temp;	
					}
				}
			}
			
		}	
			display(lowPriceIndex,type);	
	}
	public static void minuteType(salon type[])
	{
		int minuteIndex[]=new int[6];
		int temp=0;
		
		for(int w=0;w<type.length;w++)
		{
			minuteIndex[w]=w;
		}
		for(int x=0;x<minuteIndex.length;x++)
		{
			for(int y=x+1;y<type.length;y++)
			{
				if(type[minuteIndex[x]].getMinute()>type[minuteIndex[y]].getMinute())
				{
					temp=minuteIndex[x];
					minuteIndex[x]=minuteIndex[y];
					minuteIndex[y]=temp;
				}
			}		
		}	
			display(minuteIndex,type);
	}
	public static void display(int displayIndex[],salon type[])
	{
		System.out.println("Service\t       Price($)\t    Time(minutes)");
		System.out.println("=======\t       ========\t    =============");
		
		for(int z=0;z<displayIndex.length;z++)
		{
			System.out.println(type[displayIndex[z]].toString());
		}
		
	}
}
Code:
class salon
{
	private String service;
	private double price;
	private int minute;
	private int select;
	
	public salon()
	{
	
	}
	public salon(String s,double p,int m)
	{
		setService(s);
		setPrice(p);
		setMinute(m);
	}
	
	public void setService(String s)
	{
		service=s;
	}
	public String getService()
	{
		return service;
	}
	public void setPrice(double p)
	{
		price=p;
	}
	public double getPrice()
	{
		return price;
	}
	public void setMinute(int m)
	{
		minute=m;
	}
	public int getMinute()
	{
		return minute;
	}
	public String toString()
	{
		return getService() +"\t" +getPrice() +"\t\t" +getMinute();
	}
	public void setSelect(int c)throws selectException
	{
		if(c>4||c<1)
		{
			throw (new selectException("Invalid Selection"));
		}
		select=c;
	}
	public int getSelect()
	{
		return select;
	}	
}
Code:
class selectException extends Exception
{
	public selectException(String x)
	{
		super(x);
		
	}
}
Code:
class leftAlign
{
	public static String left(String s, int size)
	{
		String temp=s;
		for(int x=0;x<size-s.length();x++)
		{
			temp = temp +" ";
		}
		return temp;
	}
	
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-26-2007, 06:04 AM
WingKalimdor WingKalimdor is offline
Newbie
 
Join Date: Jul 2007
Posts: 13
Rep Power: 0
WingKalimdor is on a distinguished road
Default

I guess the code is getting more complicated than before, I prefer simple and sexy.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Php error handling Jaan PHP Tutorials 2 Today 07:32 AM
VB6.0:Tutorial, Error handling TcM VB Tutorials 9 06-06-2008 01:31 PM
Download problem, internal Server Error j3cubcapt ionFiles 1 07-02-2007 09:45 AM
Php - CGI Error thesquirrel16 General Programming 1 05-19-2007 05:09 PM
can someone help me with my c librarys? bobwrit C and C++ 4 04-27-2007 06:19 PM


All times are GMT -5. The time now is 09:45 AM.

Contest Stats

GoogleKeyw ........ 20.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads