After I compile the following and try to run it, it prints out the wrong values for the customer variable and cost variable.
/** @author CCochran */
/** @version Java Version 6 Update 30 */
/** Creates Viewer class which represents a customer who purchased a pay-per-view movie */
public class Viewer
{
/** Instance field for customer */
private String Customer;
/** Instance field for moviecode */
private String moviecode;
/** Instance field for cost */
private double cost;
/** Default constructor for Viewer class */
public Viewer()
{
Customer = " ";
moviecode = " ";
cost = 0;
}
/** Paramterized constructor for viewer class @param acustomer mcode */
public Viewer( String Customer, String moviecode)
{
this.Customer = this.Customer;
this.moviecode = moviecode;
this.cost = cost;
computeCost();
}
/** Method that returns the name of the customer
@return customer name */
public String getCustomer()
{
return Customer;
}
/**Method that returns the moviecode
@return movie code */
public String getMovieCode()
{
return moviecode;
}
/**Method that returns the cost of the movie
@return the cost */
public double getCost()
{
return cost;
}
/** Method that computes the cost of the movie */
private double computeCost()
{
double mCost = 3.95;
char mc = ' ';
char mCode = ' ';
mCode = moviecode.charAt(0);
mc = moviecode.charAt(0);
switch(mc)
{
case 'A' : cost = mCost * 1.50; break;
case 'B' : cost = mCost * 1.36; break;
case 'C' : cost = mCost * 1.26; break;
case 'D' : cost = mCost * 1.06 ; break ;
case 'F' : cost = mCost * 1.00; break;
default : System.out.println("Invalid code ");
cost = 9999.99; }
if( mCode == 'N')
{
cost = mCost + 2.50;
}
else
{
cost = 9999.99;
}
if(mCode == 'R')
{
cost = mCost + 1.00;
}
else
{
cost = 9999.99;
}
return cost;
}
/** Method that takes in a new moviecode and updates the cost and moviecode variables
@param newmoviecode */
public void updateMovieCode(String newmoviecode)
{
newmoviecode = moviecode;
computeCost();
}
/** Method that returns a string with the customer, moviecode, and cost variables
@return string */
public String toString()
{
return (Customer + " " + moviecode + " " + cost) ;
}
}
Tester class : import javax.swing.JOptionPane;
/** Class that tests the methods of the viewer class */
public class ViewerTest
{
/** Test methods of viewer class */
public static void main(String[] args)
{
Viewer view1 = new Viewer("John Smith", "A");
System.out.println(view1.toString());
Viewer view2 = new Viewer("Chris", "B");
System.out.println(view2.toString());
Viewer view3 = new Viewer(" Darius", "C");
System.out.println(view3.toString());
Viewer view4 = new Viewer("Cheryl", "D");
System.out.println(view4.toString());
Viewer view5 = new Viewer("Christine","AN");
System.out.println(view5.toString());
Viewer view6 = new Viewer("Mark", "AR");
System.out.println(view6.toString());
Viewer view7 = new Viewer("Michelle"," BN");
System.out.println(view7.toString());
Viewer view8 = new Viewer("Cameron", "BR");
System.out.println(view8.toString());
Viewer view9 = new Viewer("Christina", "CN");
System.out.println(view9.toString());
Viewer view10 = new Viewer("James", "CR");
System.out.println(view10.toString());
Viewer view11 = new Viewer("CarMichael", "DR");
System.out.println(view11.toString());
Viewer view12 = new Viewer("Haley", "DN");
System.out.println(view12.toString());
Viewer view13 = new Viewer("Lewis", "FR");
System.out.println(view13.toString());
}
}