 |
| JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 7th, 2004, 01:20 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Please Help Create Java Program!!
Hello, I am having trouble creating this program and do not even know where to start. Any help would be greatly appreciated thank you very much!
Your assignment is to create a file called Customer.java containing a class Customer (there is no main method in this class). A Customer has a first name (a String), last name (String), ID number (integer), number of matinee tickets (integer), number of normal tickets (integer), and total cost (double). Each matinee ticket costs $5.00 and each normal ticket costs $7.50. You need to compute the total cost based on the number of matinee tickets and normal tickets.
The class Customer must include the following constructors and methods. (If your class does not contain any of the following methods, points will be deducted.)
Method
Description of the Method
public Customer( )
Default constructor sets the first name and last name to "???â, customer ID, the number of matinee tickets, and the number of normal tickets to 0, and the total cost to 0.0.
public Customer (String lname, String fname, int id, int matineeTickets, int normalTickets)
Constructs a Customer object given the last name, first name, customer id, the number of matinee tickets, the number of normal tickets.
public Customer (String customerInfo)
Constructs a Customer object using the string containing customer's info. Use the StringTokenizer to extract first name, last name, id, the number of matinee tickets, and the number of normal tickets. An example of the input string is:
David Johnson/666888999/16/5
public String getFirstName( )
Returns the first name.
public String getLastName()
Returns the last name.
public int getCustomerID()
Return the customer ID.
public int getMatineeTickets( )
Returns the number of matinee tickets
public int getNormalTickets( )
Returns the number of normal tickets.
public double getTotalCost()
Returns the total cost.
public void setFirstName (String fname)
Sets the first name.
public void setLastName (String fname)
Sets the last name.
public void setCustomerID(int id)
Sets the id.
public void setMatineeTickets(int matineeNum)
Sets the number of matinee tickets.
public void setNormalTickets(int normalNum)
Sets the number of normal tickets.
public boolean equals (Customer other)
Returns true if the ids and names are the same, return false otherwise.
private void computeTotalCost( )
This is a helper(service) method that computes the total cost based on the number of matinee tickets and normal tickets (Each matinee ticket costs $5.00 and each normal ticket costs $7.50). This method will be called by other methods inside of the class Customer anytime the number of matinee tickets or normal tickets is updated.
public Customer hasMore (Customer other)
Compares the total cost and returns the Customer who has more total cost. If the total cost is same, the first customer is returned.
public String toString( )
Returns a String with information on each Customer using the following format:
First name: Bill
Last name: Gate
Customer ID: 888777888
Number of Matinee Ticket(s): 0
Number of Normal Ticket(s): 50
Total cost: $375.00
Make use of the NumberFormat class to format the total cost.
Save the Customer class in a file called Customer.java and use the following program stored in Assignment6.java, which has the main method to create new Customer objects and to test your class. A sample output is shown below.
Important Notes: Your class should have exactly the method headers that are described or otherwise your class will not work with the test driver program (Assignment6.java) that is provided. You should never change the test driver program if the test driver is provided but instead make changes to Customer class to make it work.
Helpful hints for doing this assignment:
· work on it in steps â write one method, test it with a test driver and make sure it works before going on to the next method
· always make sure your code compiles before you add another method
· your methods should be able to be called in any order
After compiling Customer.java file and Assignment6.java file, you need to execute Assignment6 class.
Sample Output: (the inputs entered by a user are shown in bold)
C:\MyJava\applications>java Assignment6
******** Print the default value of customer's info: **********
First name: ???
Last name: ???
Customer ID: 0
Number of Matinee Ticket(s): 0
Number of Normal Ticket(s): 0
Total cost: $0.00
******** Print the first Customer's info: **********
First name: Bill
Last name: Gate
Customer ID: 888777888
Number of Matinee Ticket(s): 0
Number of Normal Ticket(s): 50
Total cost: $375.00
******** Print the second Customer's info: **********
First name: David
Last name: Beckham
Customer ID: 555666777
Number of Matinee Ticket(s): 4
Number of Normal Ticket(s): 5
Total cost: $57.50
Enter customer's info:
First Name:
George
Last Name:
Bush
ID #:
100000000
Number of Matinee Tickets:
234
Number of Normal Tickets:
4
******** Print the third Customer's info using get methods:********
customer3's first name: George
customer3's last name: Bush
customer3's customer ID: 100000000
customer3's matinee ticket(s): 234
customer3's normal ticekt(s): 4
customer3's total cost: 1200.0
******** Print the third Customer's info: **********
First name: George
Last name: Bush
Customer ID: 100000000
Number of Matinee Ticket(s): 234
Number of Normal Ticket(s): 4
Total cost: $1,200.00
********Print the customer who has more total cost:*****
First name: George
Last name: Bush
Customer ID: 100000000
Number of Matinee Ticket(s): 234
Number of Normal Ticket(s): 4
Total cost: $1,200.00
********First comparison******
customer2 and customer3 are different.
********Second comparison******
customer2 and customer3 are the same customer.
|
|

April 17th, 2004, 02:59 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Let me state that I have just started learning Java myself at the beginning of the year (I have spent the last 4 years working with COBOL and a 4GL called VisualAge Generator). It looks like you are trying to learn from a book which is good, however, if the assignments are arranged by chapter and at chapter 6 (this is assignment 6) you don't know where to commence, I would recommend you re-read the previous 5 chapters.
This code took me about 2 hours to write, and I'm sure it could be improved upon (like I said I am still learning myself). When I have more time I will write a test class for it and post the final product. I have not tested the code, but it should give you an idea of what is needed. Good luck and let me know how it goes.
P.S I am using the eclipse IDE to develop my Java classes I highly recommend it, it can be downloaded for free at http://www.eclipse.org/downloads/index.php.
Code:
/*
* Created on Apr 17, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.wrox.p2p;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.StringTokenizer;
/**
* @author Alvin
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Customer {
private String firstName;
private String lastName;
private int idNumber;
private int matineeTickets;
private int normalTickets;
private double totalCost;
private static final int MATINEE_TICKET_PRICE = 5;
private static final double NORMAL_TICKET_PRICE = 7.5;
public Customer() {
firstName = "???";
lastName = "???";
idNumber = 0;
matineeTickets = 0;
normalTickets = 0;
totalCost = 0.0;
}
public Customer(
String aFirstName,
String aLastName,
int anIdNumber,
int aMatineeTicket,
int aNormalTicket) {
}
public Customer(String aTokenizedCustomer) {
StringTokenizer st = new StringTokenizer(aTokenizedCustomer, "/");
String firstLastName = st.nextToken();
Integer idNumberObject = new Integer(st.nextToken());
this.idNumber = idNumberObject.intValue();
Integer matineeTicketInteger = new Integer(st.nextToken());
this.matineeTickets = matineeTicketInteger.intValue();
Integer normalTicketsInteger = new Integer(st.nextToken());
this.normalTickets = normalTicketsInteger.intValue();
StringTokenizer st2 = new StringTokenizer(firstLastName);
this.firstName = st2.nextToken();
this.lastName = st2.nextToken();
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String aFirstName) {
firstName = aFirstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String aLastName) {
lastName = aLastName;
}
public int getIdNumber() {
return idNumber;
}
public void setIdNumber(int anIdNumber) {
idNumber = anIdNumber;
}
public int getMatineeTickets() {
return matineeTickets;
}
public void setMatineeTickets(int aMatineeTickets) {
matineeTickets = aMatineeTickets;
}
public int getNormalTickets() {
return normalTickets;
}
public void setNormalTickets(int aNormalTickets) {
normalTickets = aNormalTickets;
}
public double getTotalCost() {
double yourTotalCost = 0.0;
double tempWorkingArea = 0.0;
if (!(matineeTickets == 0)) {
yourTotalCost += (matineeTickets * MATINEE_TICKET_PRICE);
// yourTotalCost = tempWorkingArea;
}
if (!(normalTickets == 0)) {
yourTotalCost += (normalTickets * NORMAL_TICKET_PRICE);
// yourTotalCost += tempWorkingArea;
}
NumberFormat formatTotalCost = NumberFormat.getInstance(Locale.US);
formatTotalCost.format(yourTotalCost);
return yourTotalCost;
}
public boolean equals(Customer aCustomer) {
boolean isEqual = true;
if (!(this.firstName.equalsIgnoreCase(aCustomer.firstName))) {
isEqual = false;
} else if (!(this.lastName.equalsIgnoreCase(aCustomer.lastName))) {
isEqual = false;
} else if (!(this.idNumber == aCustomer.idNumber)) {
isEqual = false;
}
return isEqual;
}
public String toString() {
String customerString = null;
String myFirstName = "First Name: " + this.getFirstName() + "/n";
String myLastName = "Last Name: " + this.getLastName() + "/n";
String myCustomerId = "Customer ID: " + this.getIdNumber() + "/n";
String myMatineeTickets = "Matinee Ticket(s): " + this.getMatineeTickets() + "/n";
String myNormalTickets = "Normal Ticket(s)" + this.getNormalTickets() + "/n";
String myTotalCost = "Total Cost: " + this.totalCost + "/n";
customerString = myFirstName + myLastName + myCustomerId + myMatineeTickets + myNormalTickets
+ myTotalCost;
return customerString;
}
public Customer hasMore(Customer other) {
Customer hasMoreCustomer;
if (this.totalCost > other.getTotalCost()) {
hasMoreCustomer = this;
} else {
hasMoreCustomer = other;
}
return (hasMoreCustomer);
}
}
|
|

April 18th, 2004, 11:24 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
After writing a test class, I had to make some changes to the hasMore() and toString() methods. The only requirement that this class does not address is the formatting of the totalCost field to a format of $xxx.xx. I will post my test class afterwards.
Code:
/*
* Created on Apr 17, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.wrox.p2p;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.StringTokenizer;
/**
* @author Alvin
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Customer {
private String firstName;
private String lastName;
private int idNumber;
private int matineeTickets;
private int normalTickets;
private double totalCost;
private static final int MATINEE_TICKET_PRICE = 5;
private static final double NORMAL_TICKET_PRICE = 7.5;
public Customer() {
firstName = "???";
lastName = "???";
idNumber = 0;
matineeTickets = 0;
normalTickets = 0;
totalCost = 0.0;
}
public Customer(
String aFirstName,
String aLastName,
int anIdNumber,
int aMatineeTicket,
int aNormalTicket) {
}
public Customer(String aTokenizedCustomer) {
StringTokenizer st = new StringTokenizer(aTokenizedCustomer, "/");
String firstLastName = st.nextToken();
Integer idNumberObject = new Integer(st.nextToken());
this.idNumber = idNumberObject.intValue();
Integer matineeTicketInteger = new Integer(st.nextToken());
this.matineeTickets = matineeTicketInteger.intValue();
Integer normalTicketsInteger = new Integer(st.nextToken());
this.normalTickets = normalTicketsInteger.intValue();
StringTokenizer st2 = new StringTokenizer(firstLastName);
this.firstName = st2.nextToken();
this.lastName = st2.nextToken();
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String aFirstName) {
firstName = aFirstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String aLastName) {
lastName = aLastName;
}
public int getIdNumber() {
return idNumber;
}
public void setIdNumber(int anIdNumber) {
idNumber = anIdNumber;
}
public int getMatineeTickets() {
return matineeTickets;
}
public void setMatineeTickets(int aMatineeTickets) {
matineeTickets = aMatineeTickets;
}
public int getNormalTickets() {
return normalTickets;
}
public void setNormalTickets(int aNormalTickets) {
normalTickets = aNormalTickets;
}
public double getTotalCost() {
totalCost = 0.0;
double tempWorkingArea = 0.0;
if (!(matineeTickets == 0)) {
tempWorkingArea = (matineeTickets * MATINEE_TICKET_PRICE);
totalCost += tempWorkingArea;
}
if (!(normalTickets == 0)) {
tempWorkingArea = (normalTickets * NORMAL_TICKET_PRICE);
totalCost += tempWorkingArea;
}
return totalCost;
}
public boolean equals(Customer aCustomer) {
boolean isEqual = true;
if (!(this.firstName.equalsIgnoreCase(aCustomer.firstName))) {
isEqual = false;
} else if (!(this.lastName.equalsIgnoreCase(aCustomer.lastName))) {
isEqual = false;
} else if (!(this.idNumber == aCustomer.idNumber)) {
isEqual = false;
}
return isEqual;
}
public String toString() {
String customerString = null;
String myFirstName = "First Name: " + this.getFirstName() + "\n";
String myLastName = "Last Name: " + this.getLastName() + "\n";
String myCustomerId = "Customer ID: " + this.getIdNumber() + "\n";
String myMatineeTickets =
"Matinee Ticket(s): " + this.getMatineeTickets() + "\n";
String myNormalTickets =
"Normal Ticket(s)" + this.getNormalTickets() + "\n";
NumberFormat formatTotalCost = NumberFormat.getNumberInstance(Locale.US);
formatTotalCost.format(totalCost);
String myTotalCost = "Total Cost: " + this.totalCost + "\n";
customerString =
myFirstName
+ myLastName
+ myCustomerId
+ myMatineeTickets
+ myNormalTickets
+ myTotalCost;
return customerString;
}
public Customer hasMore(Customer one) {
Customer hasMoreCustomer;
if (this.getTotalCost() > one.getTotalCost()) {
hasMoreCustomer = this;
} else {
hasMoreCustomer = one;
}
return (hasMoreCustomer);
}
}
|
|

April 18th, 2004, 11:26 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
As promised, here is the test class I used to test the Customer class.
Code:
/*
* Created on Apr 17, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.wrox.p2p;
/**
* @author
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class CustomerMain {
public static void main(String[] args) {
testTokenizedConstructor();
Customer cust = testTotalCost();
testCustomerHasMore();
}
public static void testTokenizedConstructor() {
String TokenizedCustomer = "Joe Smith/340763230/10/5";
Customer newCustomer = new Customer(TokenizedCustomer);
System.out.println(newCustomer.getFirstName());
System.out.println(newCustomer.getLastName());
System.out.println(newCustomer.getIdNumber());
System.out.println(newCustomer.getMatineeTickets());
System.out.println(newCustomer.getNormalTickets());
}
public static Customer testTotalCost() {
Customer newCustomer = new Customer();
newCustomer.setFirstName("First");
newCustomer.setLastName("FirstToLast");
newCustomer.setIdNumber(33300333);
newCustomer.setMatineeTickets(234);
newCustomer.setNormalTickets(4);
System.out.println("First Customer Total Cost = " + newCustomer.getTotalCost());
return newCustomer;
}
public static void testCustomerHasMore() {
Customer firstCustomer = testTotalCost();
Customer secondCustomer = new Customer();
secondCustomer.setFirstName("Second");
secondCustomer.setLastName("SecondToLast");
secondCustomer.setIdNumber(44400444);
secondCustomer.setMatineeTickets(2);
secondCustomer.setNormalTickets(3);
secondCustomer.hasMore(firstCustomer);
Customer anotherCustomer = secondCustomer.hasMore(firstCustomer);
System.out.println("The Customer = " + anotherCustomer.getFirstName() + " " + anotherCustomer.getLastName() + " with a total cost of " + anotherCustomer.getTotalCost());
System.out.println(secondCustomer.toString());
}
}
|
|

April 18th, 2004, 09:21 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I figured out how to return a formatted currency string. I have updated the toString() method with my results. What you have know is the complete solution to this assignment, as I understand the requirements. Hope this helps.
Code:
public String toString() {
String customerString = null;
String myFirstName = "First Name: " + this.getFirstName() + "\n";
String myLastName = "Last Name: " + this.getLastName() + "\n";
String myCustomerId = "Customer ID: " + this.getIdNumber() + "\n";
String myMatineeTickets =
"Matinee Ticket(s): " + this.getMatineeTickets() + "\n";
String myNormalTickets =
"Normal Ticket(s)" + this.getNormalTickets() + "\n";
Locale defaultLocale = new Locale("us", "US");
NumberFormat formatTotalCost = NumberFormat.getNumberInstance(Locale.US);
NumberFormat nf = NumberFormat.getCurrencyInstance(defaultLocale);
String formattedValue = nf.format(totalCost);
String myTotalCost = "Total Cost: " + formattedValue + "\n";
customerString =
myFirstName
+ myLastName
+ myCustomerId
+ myMatineeTickets
+ myNormalTickets
+ myTotalCost;
return customerString;
}
|
|
 |