Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the Customer class, create a method called hasMoreMoneyThan(Customer c) that

ID: 3878342 • Letter: I

Question

In the Customer class, create a method called hasMoreMoneyThan(Customer c) that returns a boolean value of true if the customer has more money than the customer passed in as parameter c and false otherwise. Add the following two lines of code to the bottom of your CustomerSpendingTestProgram to make sure that the answers are false and true, respectively:

Now we will modify our test code to make use of our computeFee() method to gain admission into an event (i.e., such as a circus or movie). Below is the test code that we would like to have working. Create that test program, but it will not yet compile. Notice that there is a new method called payAdmission() which simulates a person paying admission to the circus.

Write the payAdmission() method. It should have a void return type. Your method should be one line long ... making use of the computeFee() and spend() methods that you wrote earlier. Compile your code, test it by running the CustomerAdmissionTestProgram and then examine the output to make sure that it is correct.

We will now adjust our Customer object so that we can keep track of who has paid for their admission and who has not. Add a boolean attribute (i.e., instance variable) to the Customer class called admitted. Set this attribute to false in ALL of your constructors. Modify your payAdmission() method so that this variable is set to true when the customer has successfully paid the proper fee amount. Adjust your CustomerAdmissionTestProgram code to display this new attribute after each call to payAdmission() so that you can see if it is set properly. Here is the portion of code to change:

Now we will adjust how a Customer object appears when displayed. We saw earlier that a Customer object appears like this when displayed: Customer@140e19d . We change the "look" of the customer by defining the String that will be returned when the customer is displayed. We do this through the toString() method. Write a toString() method so that customers appear in this format when displayed:

Test your code by adding these two lines to the bottom of your CustomerAdmissionTestProgram:

Create a static/class variable in the Customer class called CUSTOMER_NUMBER. Give it an initial value of 1. Add an appropriate instance variable to the Customer class called id. Change each constructor so that the id is properly set to be a unique number each time a Customer object is created. Alter the CustomerConstructorTestProgram so that it displays the id attribute for each Customer object as well. Test your code to make sure that each Customer gets a unique id, starting with 1.  

Here are my codes:

Explanation / Answer

find the java class

//customer class

public class Customer {

String name;

int age;

char gender;

float money;

boolean admitted;

static int CUSTOMER_NUMBER = 0;

  

public Customer(String n) {

Customer.CUSTOMER_NUMBER += 1;

this.name = n;

this.age = 0;

this.gender = 'M';

this.money = 0.0f;

this.admitted = false;

}

  

public boolean hasMoreMoneyThan(Customer c) {

if(this.money > c.money )

return true;

else

return false;

}

public Customer(String n, int a, char g) {

Customer.CUSTOMER_NUMBER += 1;

this.name = n;

this.age = a;

this.gender = g;

this.money = 0.0f;

this.admitted = false;

}

public Customer(String n, int a, char g, float m) {

Customer.CUSTOMER_NUMBER += 1;

this.name = n;

this.age = a;

this.gender = g;

this.money = m;

this.admitted = false;

}

public Customer() {

Customer.CUSTOMER_NUMBER += 1;

}

// 4

public float computeFee() {

if ((this.age >= 18) && (this.age < 65)) {

return 12.75f;

}

if (this.age <= 3) {

return 0;

}

if (this.age >= 65) {

return 6.375f;

}

if ((this.age >= 4) && (this.age <= 17)) {

return 8.50f;

} else {

return 0;

}

}

public boolean spend(float amount) {

if (this.money > amount && amount > 0) {

this.money = this.money - amount;

return true;

}

return false;

}

  

public void payAdmission() {

float money = this.computeFee();

this.admitted = this.spend(money);

}

  

public String toString() {

String str = "";

String gender="";

if(this.gender == 'M')

gender = "Male";

else

gender = "Female";

String isadmitted = "";

if(this.admitted)

isadmitted = " who has been admitted.";

else

isadmitted = " who has not been admitted";

str = "Customer "+this.name+": a "+this.age+" year old "+gender +" with $"+this.money+" "+isadmitted;

return str;

}

}

//customerconstructortestprogram.java

public static void main(String args[]) {

Customer c1, c2, c3, c4;

c1 = new Customer("Bob", 17, 'M');
c2 = new Customer("Dottie", 3, 'F', 10f);
c3 = new Customer("Jane");
c4 = new Customer();

c2.spend(3);
System.out.println("Dottie's money remaining is $" + c2.money);

c2.spend(-80);
System.out.println("Dottie's money remaining is $" + c2.money);

System.out.println("Bob looks like this: "+c1.CUSTOMER_NUMBER + c1.name + ", " + c1.age + ", " + c1.gender + ", " + c1.money);
System.out.println("Dottie looks like this: "+c2.CUSTOMER_NUMBER + c2.name + ", " + c2.age + ", " + c2.gender + ", " + c2.money);
System.out.println("Jane looks like this: "+c3.CUSTOMER_NUMBER + c3.name + ", " + c3.age + ", " + c3.gender + ", " + c3.money);
System.out.println("Customer 4 looks like this: "+c4.CUSTOMER_NUMBER + c4.name + ", " + c4.age + ", " + c4.gender + ", " + c4.money);

System.out.println("Bob's fee is $" + c1.computeFee());
System.out.println("Dottie's fee is $" + c2.computeFee());
c3.age = 23;
System.out.println("Jane's fee is $" + c3.computeFee());
c4.age = 67;
System.out.println("No Name's fee is $" + c4.computeFee());
}
}

//customer spending test program

public class CustomerSpendingTestProgram {

public static void main(String args[]) {

Customer c1;

Customer c2, c3;

c2 = new Customer("Dottie", 26, 'M', 51.00f);

c3 = new Customer("Jane", 27, 'F', 45.00f);

c1 = new Customer("Bob", 25, 'M', 50.00f);

System.out.println(c1.money);

if (!c1.spend(10.00f))

System.out.println("Unable to spend $10");

if (!c1.spend(4.75f))

System.out.println("Unable to spend $4.75");

if (!c1.spend(15.25f))

System.out.println("Unable to spend $15.25");

if (!c1.spend(100.00f))

System.out.println("Unable to spend $100");

System.out.println(c1.money);

System.out.println("Bob has more money than Dottie: " +c1.hasMoreMoneyThan(c2));

System.out.println("Dottie has more money than Jane: " + c2.hasMoreMoneyThan(c3));

}

}

//customer test program

public class CustomerTestProgram {

public static void main(String args[]) {

Customer c;
c = new Customer("Bob");
c.name = "Bob";
System.out.println(c.name);
System.out.println(c.age);
System.out.println(c.money);
System.out.println(c.gender);

}
}

//customer admission test program

public class CustomerAdmissionTestProgram {
public static void main(String args[]) {
Customer c1, c2, c3, c4;
c1 = new Customer("Bob", 17, 'M', 100);
c2 = new Customer("Dottie", 3, 'F', 10);
c3 = new Customer("Jane", 24, 'F', 40);
c4 = new Customer("Sam", 72, 'M', 5);
System.out.println("Here is the money before going into the circus:");
System.out.println(" Bob has $" + c1.money);
System.out.println(" Dottie has $" + c2.money);
System.out.println(" Jane has $" + c3.money);
System.out.println(" Sam has $" + c4.money);
// Simulate people going into the circus
c1.payAdmission();
c2.payAdmission();
c3.payAdmission();
c4.payAdmission();
System.out.println("Here is the money after going into the circus:");
System.out.println(" Bob has $" + c1.money);
System.out.println(" Dottie has $" + c2.money);
System.out.println(" Jane has $" + c3.money);
System.out.println(" Sam has $" + c4.money);
System.out.println("Here is the money after going into the circus:");
System.out.println(" Bob has $" + c1.money);
System.out.println(" Dottie has $" + c2.money);
System.out.println(" Jane has $" + c3.money);
System.out.println(" Sam has $" + c4.money);
  
  
}
}