10 Replies - 1355 Views - Last Post: 25 July 2010 - 03:26 PM Rate Topic: -----

#1 Guest_Kathleen*


Reputation:

Using parallel arrays in classes

Posted 24 July 2010 - 04:44 PM

Hi all,
I have a question. I'm parallel array in my program. One is for keeping track of customer name and the other is for their tip.
So do I store the Customer name into an array that I am able to recall from Salon to Stylist and be able to call back?
package labs;

public class Stylist
{
	private String name;
	private double wage, hours, totalTip;
	private int numCustomers;
	private final int MINWAGE = 11, MAXWAGE = 25, MINHOURS = 1, MAXHOURS = 60;
	private final double MINTIP = 0.1, MAXTIP =100;
	private String [] cusName = new String[100];
	private double [] tips = new double[cusName.length];
	public Stylist ()
	{
		numCustomers = 0;
		wage = MINWAGE;
	}
	public Stylist (String n)
	{
		this();
		name = n;
	}
	public Stylist (String n, double w)
	{
		this(n);
		wage = w;
	}
	public boolean addCustomer(double t)
	{
		if (!valid(t,MINTIP, MAXTIP))
			return false;
		numCustomers++;
		totalTip += t;
		return true;
	}
	public boolean setHours (double h)
	{
		if (!valid(h, MINHOURS, MAXHOURS))
		return false;
		hours = h;
		return true;
	}
	public boolean setWage(double w)
	{
		if (!valid (w, MINWAGE,MAXWAGE))
		return false;
		wage = w;
		return true;
	}
	public boolean valid(double value, double min, double max)
	{
		return (value >= min && value <= max);
	}
	public void setName(String n)
	{
		name = n;
	}
	public void setCusName(String cn){
		int i;
		cusName[i] = cn;
		i++;
	}
	public void setCusTip(double [] t){
		tips = t;
	}
	public double salary() {return hours*wage;}
	public double getWage()	{return wage;	}
	public double getTotalTip()	{return totalTip;}
	public String getName(){return name;}
	public int getNumCustomer(){return numCustomers;	}
	public double getHours()	{return hours;	}
	public String[] getCusName(){ return cusName;}
	public double[] getCusTip(){return tips;}
	public String toString()
	{
		return "\nStylist:  "+name + " earned a salary of: $"+salary()+" and tips $" + totalTip + "\nTotal Pay:  $" + (salary()+totalTip) + "\nAverage tip: $" + totalTip/numCustomers ;

	}

}





package labs;

import javax.swing.JOptionPane;
public class Salon
{
	public static void main (String [] args)
	{
		String summary = "Information from all Stylists: \n";
		int numStylists=0;
		double totalSalary=0, allTips=0;
		Stylist oneStylist;
		do {
			oneStylist = fillStylist();
			totalSalary+=oneStylist.salary();
			processStylist(oneStylist);
			allTips += oneStylist.getTotalTip();
			summary += oneStylist.toString();
			numStylists++;
			JOptionPane.showMessageDialog(null, oneStylist.toString());
		}while (JOptionPane.showConfirmDialog(null, "Another Stylist?")==JOptionPane.YES_OPTION);
		display(summary + "\n\n Total Salary paid:  $"+totalSalary+"\nTotal Tips earned:  $"+allTips);
	}
	public static void display(String allInfo)
	{
		JOptionPane.showMessageDialog(null,allInfo);
	}
	public static Stylist fillStylist()
	{
		Stylist oneStylist = new Stylist(JOptionPane.showInputDialog("Enter stylist's name:"));
		double wage, hours;
		do {
			wage = Double.parseDouble(JOptionPane.showInputDialog("What is "+oneStylist.getName()+"'s hourly wage?"));
		} while (!oneStylist.setWage(wage));
		do {
			hours = Double.parseDouble(JOptionPane.showInputDialog("How many hours did "+oneStylist.getName()+" work this week?"));
		} while (!oneStylist.setHours(hours));
		return oneStylist;
	}
	public static void customer(Stylist one)
	{
		// I am planning to pass three arguments in this method. One will be the String array(customer name)
		// The other will be the customer array and the third will the index of where to do the customer.
		String cName;
		double tip;
		
		
		cName = JOptionPane.showInputDialog("Enter name of customer:");
		

		tip = Double.parseDouble(JOptionPane.showInputDialog("What was the tip amount left by "+cName));
		if (!one.addCustomer(tip))
			JOptionPane.showMessageDialog(null, "No tip!!!");
	}

	public static void processStylist(Stylist one)
	{
			do {
				customer(one);
			}while (JOptionPane.showConfirmDialog(null, "Did "+one.getName()+" have another customer?")==JOptionPane.YES_OPTION);

	}

}



Is This A Good Question/Topic? 0

Replies To: Using parallel arrays in classes

#2 Guest_Kathleen*


Reputation:

Re: Using parallel arrays in classes

Posted 24 July 2010 - 04:46 PM

correction. I am *USING parallel arrays. :stupid: lol
Was This Post Helpful? 0

#3 Guest_Kathleen*


Reputation:

Re: Using parallel arrays in classes

Posted 24 July 2010 - 04:50 PM

Correction to customer method in Salon class.

public static void customer(Stylist one)
	{
		// I am planning to pass three arguments in this method. One will be the String array  
		// The other will be the customer name array and the third will the index of where to put the customer.
		String cName;
		double tip;
		
		
		cName = JOptionPane.showInputDialog("Enter name of customer:");
		

		tip = Double.parseDouble(JOptionPane.showInputDialog("What was the tip amount left by "+cName));
		if (!one.addCustomer(tip))
			JOptionPane.showMessageDialog(null, "No tip!!!");
	}


Was This Post Helpful? 0

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,479
  • Joined: 29-May 08

Re: Using parallel arrays in classes

Posted 24 July 2010 - 04:56 PM

Some Reading Material
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10646
  • View blog
  • Posts: 39,536
  • Joined: 27-December 08

Re: Using parallel arrays in classes

Posted 24 July 2010 - 06:07 PM

View PostAdamSpeight2008, on 24 July 2010 - 07:56 PM, said:


It's a very well-written tutorial, if I do say so myself. :D
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Kathleen*


Reputation:

Re: Using parallel arrays in classes

Posted 24 July 2010 - 07:23 PM

I did look at it. thanks. I still have a problem. The customer's name and tip is printing TWICE. I don't know where the problem is.
package labs;

public class Stylist
{
	private String name;
	private double wage, hours, totalTip;
	public int numCustomers;
	private final int MINWAGE = 11, MAXWAGE = 25, MINHOURS = 1, MAXHOURS = 60;
	private final double MINTIP = 0.1, MAXTIP =100;
	public String[] cusName = new String[20];
	public double[] tips = new double[20];
	
	
	public Stylist ()
	{
		numCustomers = 0;
		wage = MINWAGE;
	}
	public Stylist (String n)
	{
		this();
		name = n;
	}
	public Stylist (String n, double w)
	{
		this(n);
		wage = w;
	}
	public boolean addCustomer(String a, double t)
	{
		if (!valid(t,MINTIP, MAXTIP))
			return false;
	
		cusName[numCustomers] = a;
		tips[numCustomers] = t;
		
		numCustomers++;
		
		return true;
	}
	public double totalTips (double[] a)
	{ for(int x =0;x < a.length; x++)
		totalTip += a[x];
	return totalTip;
	}
	public boolean setHours (double h)
	{
		if (!valid(h, MINHOURS, MAXHOURS))
		return false;
		hours = h;
		return true;
	}
	public boolean setWage(double w)
	{
		if (!valid (w, MINWAGE,MAXWAGE))
		return false;
		wage = w;
		return true;
	}
	public boolean valid(double value, double min, double max)
	{
		return (value >= min && value <= max);
	}
	public void setName(String n)
	{
		name = n;
	}
	
	public double salary() {return hours*wage;}
	public double getWage()	{return wage;	}
	public double getTotalTip()	{return totalTip;}
	public String getName(){return name;}
	public int getNumCustomer(){return numCustomers;	}
	public double getHours()	{return hours;	}
	public String toString()
	{ String displayCust ="";
		for(int i = 0; i < numCustomers; i++)
		{ if(cusName[i] == null) break;
		displayCust += "\n" + cusName[i] + "\t" + tips[i];}
		

				return "\nStylist:  "+name + " earned a salary of: $"+salary()+ displayCust + " and tips $" + totalTip + "\nTotal Pay:  $" + (salary()+totalTip) + "\nAverage tip: $" + totalTip/numCustomers ;
	}

}



package labs;

import javax.swing.JOptionPane;
public class Salon
{
	public static void main (String [] args)
	{
		String summary = "Information from all Stylists: \n";
		String displayCust = "";
		int numStylists=0;
		double totalSalary=0, allTips=0;
		Stylist oneStylist;
	
		
		
		
		do {
			oneStylist = fillStylist();
			totalSalary+=oneStylist.salary();
			processStylist(oneStylist);
			allTips += oneStylist.getTotalTip();
			summary += oneStylist.toString();
			numStylists++;			
			
			JOptionPane.showMessageDialog(null, oneStylist.toString()+ displayCust);
			
		}while (JOptionPane.showConfirmDialog(null, "Another Stylist?")==JOptionPane.YES_OPTION);
		display(summary + "\n\n Total Salary paid:  $"+totalSalary+"\nTotal Tips earned:  $"+allTips);
		
	}
	public static void display(String allInfo)
	{
		JOptionPane.showMessageDialog(null,allInfo);
	}
	public static Stylist fillStylist()
	{
		Stylist oneStylist = new Stylist(JOptionPane.showInputDialog("Enter stylist's name:"));
		double wage, hours;
		do {
			wage = Double.parseDouble(JOptionPane.showInputDialog("What is "+oneStylist.getName()+"'s hourly wage?"));
		} while (!oneStylist.setWage(wage));
		do {
			hours = Double.parseDouble(JOptionPane.showInputDialog("How many hours did "+oneStylist.getName()+" work this week?"));
		} while (!oneStylist.setHours(hours));
		return oneStylist;
	}
	public static void customer(Stylist one)
	{
		String cName;
		double tip;
		
		cName = JOptionPane.showInputDialog("Enter name of customer:");
		tip = Double.parseDouble(JOptionPane.showInputDialog("What was the tip amount left by "+cName));
		
		one.addCustomer(cName, tip);
		
		if (!one.addCustomer(cName, tip))
			JOptionPane.showMessageDialog(null, "No tip!!!");
	}

	public static void processStylist(Stylist one)
	{
			do {
				customer(one);
			}while (JOptionPane.showConfirmDialog(null, "Did "+one.getName()+" have another customer?")==JOptionPane.YES_OPTION);

	}

}


Was This Post Helpful? 0

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10646
  • View blog
  • Posts: 39,536
  • Joined: 27-December 08

Re: Using parallel arrays in classes

Posted 24 July 2010 - 07:59 PM

The whole point of classes and Objects is to not use parallel arrays at all. So rather than having 2 arrays for customer name and tip, create a Customer class to encapsulate those attributes, and store a Customer[] in your Stylist class.

As for your problem, you invoke addCustomer() twice, which places the customer in the array twice. No need for the first addCustomer() invocation if you have one in the if statement.
one.addCustomer(cName, tip);

if (!one.addCustomer(cName, tip))
      JOptionPane.showMessageDialog(null, "No tip!!!");


Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Using parallel arrays in classes

Posted 24 July 2010 - 08:32 PM

Look at macosxnerd101 tutorial. It is well written and you won't need to use parallel arrays anymore
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Kathllen*


Reputation:

Re: Using parallel arrays in classes

Posted 24 July 2010 - 09:36 PM

I HAVE to use parallel arrays. Its part of the requirments...I want to create a method of "totalTips" for each stylist. Because right now the average tips and total pay is not giving the right answer.
package labs;

public class Stylist
{
	private String name;
	private double wage, hours, totalTip;
	public int numCustomers;
	private final int MINWAGE = 11, MAXWAGE = 25, MINHOURS = 1, MAXHOURS = 60;
	private final double MINTIP = 0.1, MAXTIP =100;

	public String[] cusName = new String[20];
	public double[] tips = new double[20];


	public Stylist ()
	{
		numCustomers = 0;
		wage = MINWAGE;
	}
	public Stylist (String n)
	{
		this();
		name = n;
	}
	public Stylist (String n, double w)
	{
		this(n);
		wage = w;
	}
	public boolean addCustomer(String a, double t)
	{
		if (!valid(t,MINTIP, MAXTIP))
			return false;

		cusName[numCustomers] = a;
		tips[numCustomers] = t;

		numCustomers++;

		return true;
	}

	//public double totalTips ()
	//for each stylist.
	//}
	public double addToTips ()
	{ for(int x =0;x < tips.length; x++)
		totalTip += tips[x];
	return totalTip;
	}
	public boolean setHours (double h)
	{
		if (!valid(h, MINHOURS, MAXHOURS))
		return false;
		hours = h;
		return true;
	}
	public boolean setWage(double w)
	{
		if (!valid (w, MINWAGE,MAXWAGE))
		return false;
		wage = w;
		return true;
	}
	public boolean valid(double value, double min, double max)
	{
		return (value >= min && value <= max);
	}
	public void setName(String n)
	{
		name = n;
	}

	public double salary() {return hours*wage;}
	public double getWage()	{return wage;	}


	public double getTotalTips()	{return totalTip;}
	public String getName(){return name;}
	public int getNumCustomer(){return numCustomers;	}
	public double getHours()	{return hours;	}
	public String toString()
	{
		String displayCust ="";
		for(int i = 0; i < numCustomers; i++)
		{ if(cusName[i] == null) break;
		displayCust += "\nCustomer: " + cusName[i] + "\nTip :" + tips[i];}


				return "\nStylist:  "+name + " earned a salary of $"+salary()+ displayCust +
				"\nTotal Pay:  $" + (salary()+ totalTips()) + "\nAverage tip: $" + (totalTips()/numCustomers) ;
	}

}


Was This Post Helpful? 0

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: Using parallel arrays in classes

Posted 25 July 2010 - 11:00 AM

View PostKathllen, on 24 July 2010 - 10:36 PM, said:

I HAVE to use parallel arrays. Its part of the requirments...


Pity.

This is your big error: "for(int x =0;x < tips.length; x++)".

Also, you need to be careful about initializing and checking your vars. Be careful they don't have zero customers on the toString. Given your unfortunate array requirement, I'd do it like this:
public class Stylist {
	private static final int MAX_CUSTOMERS = 20;
	private final int MINWAGE = 11, MAXWAGE = 25, MINHOURS = 1, MAXHOURS = 60;
	private final double MINTIP = 0.1, MAXTIP =100;
	private String name;
	//private double totalTip;
	private double hours, wage;
	
	private int numCustomers;
	private String[] cusName = new String[MAX_CUSTOMERS];
	private double[] tips = new double[MAX_CUSTOMERS];

	public Stylist (String name, double wage) {
		this.numCustomers = 0;
		setName(name);
		this.wage = MINWAGE;
		setWage(wage);
	}

	public Stylist () { this(null, MINWAGE); }
	
	public Stylist (String name) { this(name, MINWAGE); }
	
	private boolean valid(double value, double min, double max) {
		return (value >= min && value <= max);
	}
	
	
	public boolean addCustomer(String customerName, double tip) {
		if (!valid(tip,MINTIP, MAXTIP)) { return false; }
		if (customerName==null) { return false; }
		cusName[numCustomers] = customerName;
		tips[numCustomers] = tip;
		numCustomers++;
		return true;
	}
	


	public boolean setHours (double hours) {
		if (!valid(hours, MINHOURS, MAXHOURS)) { return false; }
		this.hours = hours;
		return true;
	}
	
	public boolean setWage(double wage) {
		if (!valid (w, MINWAGE,MAXWAGE)) { return false; }
		this.wage = wage;
		return true;
	}
	
	public void setName(String name) { this.name = name==null ? "Uknown" : name; }

	public double getSalary() { return hours*wage; }
	
	public double getWage() { return wage; }
	
	public int getTotalTips() {
		int totalTip = 0;
		for(int x =0; x < numCustomers.length; x++) {
			totalTip += tips[x];
		}
		return totalTip;
	}
	
	public String getName(){ return name; }
	
	public int getNumCustomer(){ return numCustomers; }
	
	public double getHours() { return hours; }
	
	public String toString() {
		double salary = getSalary();
		double tips = getTotalTips();
		String s = "Stylist:  "+ name + " earned a salary of $" + salary;
		if (numCustomers>0)
			for(int i = 0; i < numCustomers; i++) { 
				// this is a bad check, you shouldn't let them enter a null name to start with
				// if(cusName[i] == null) { break; }
				s += "\nCustomer: " + cusName[i] + "\nTip :" + tips[i];
			}
			s += "\nTotal Pay:  $" + (salary + tips) + "\nAverage tip: $" + (tips/numCustomers);
		}
		return s;
	}
}


This post has been edited by baavgai: 25 July 2010 - 11:01 AM

Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Using parallel arrays in classes

Posted 25 July 2010 - 03:26 PM

View PostKathllen, on 24 July 2010 - 10:36 PM, said:

I HAVE to use parallel arrays.

If it is for school assignment and you haven't seen Objects yet
sins forgiven
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1