The Artima Developer Community
Sponsored Link

Java Answers Forum
can't get the output to come up in class of a java program

2 replies on 1 page. Most recent reply: Feb 27, 2006 3:13 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Michelle Loh

Posts: 10
Nickname: modernage
Registered: Feb, 2006

can't get the output to come up in class of a java program Posted: Feb 24, 2006 11:15 PM
Reply to this message Reply
Advertisement
hi i am trying to get the actual price to show up when i run another program to test my class, but it just shows up as $0.00, can anyone please tell me what my error is if possible?

public class HotelRoom
{
final String singleRoom = "single";
final String doubleRoom = "double";
final String businessSuite = "business";


// define your fields here
private String guestname,roomtype;
private int days,reservationlength;
private double estimatedprice,actualprice;



public HotelRoom(String guestname, String roomtype, int days)
{
// complete this method
this.guestname=guestname;
this.roomtype=roomtype;
this.days=days;
}

public String getGuestName()
{
return guestname;

}

public String getRoomType()
{
return roomtype;
}

public int getReservationLength()
{
return days;
}

public double getEstimatedPrice()
{

if (roomtype==singleRoom && days>7)
{
estimatedprice=(60.00*days);
estimatedprice=(estimatedprice-(estimatedprice*.1));
}
else if (roomtype==singleRoom && days>0 && days<7)
{
estimatedprice=(60.00*days);
}
else if (roomtype==doubleRoom && days>7)
{
estimatedprice=(80.00*days);
estimatedprice=(estimatedprice-(estimatedprice*.1));
}
else if (roomtype==doubleRoom && days>0 && days<7)
{
estimatedprice=(80.00*days);
}
else if (roomtype==businessSuite && days>7)
{
estimatedprice=(150.00*days);
estimatedprice=(estimatedprice-(estimatedprice*.1));
}
else if (roomtype==businessSuite && days>0 && days<7)
{
estimatedprice=(150.00*days);
}
else
{
estimatedprice=(80.00);
}
return estimatedprice;

}

public void setRoomType(String roomtype)
{
roomtype=roomtype;
}

public void setReservationLength(int days)
{
days=reservationlength;
}

public double getActualPrice(int daysStayed)
{
if (roomtype==singleRoom)
{
if (daysStayed==days && days<7 && days>0)
{
actualprice=(estimatedprice);
}
else if (daysStayed>7 && daysStayed==days)
{
actualprice=estimatedprice;
actualprice=(actualprice-(actualprice*.1));
}
else if (daysStayed<days)
{
days=(days-daysStayed);
actualprice=(estimatedprice-(days*60.00)+(5.00*days));
}
else if (daysStayed>days && daysStayed>7)
{

actualprice=(daysStayed*60.00);
actualprice=(actualprice-(actualprice*.1));
}
else
{
actualprice=(60.00);
}
}
if (roomtype==doubleRoom)
{
if (daysStayed==days && days<7 && days>0)
{
actualprice=(estimatedprice);
}
else if (daysStayed>7 && daysStayed==days)
{
actualprice=(daysStayed*80.00);
actualprice=(actualprice-(actualprice*.1));
}
else if (daysStayed<days)
{
days=(days-daysStayed);
actualprice=(estimatedprice-(days*80.00)+(5.00*days));
}
else if (daysStayed>days && daysStayed>7)
{

actualprice=(daysStayed*80.00);
actualprice=(actualprice-(actualprice*.1));
}
else
{
actualprice=80.00;

}
}

if (roomtype==businessSuite)
{
if (daysStayed==days && days<7 && days>0)
{
actualprice=(estimatedprice);
}
else if (daysStayed>7 && daysStayed==days)
{

actualprice=(daysStayed*150.00);
actualprice=(actualprice-(actualprice*.1));
}
else if (daysStayed>days && daysStayed>7)
{

actualprice=(daysStayed*150.00);
actualprice=(actualprice-(actualprice*.1));
}
else
{
actualprice=150.00;
}
}

return actualprice;


}



}


varanasi R eswar

Posts: 3
Nickname: eswar
Registered: Nov, 2003

Re: can't get the output to come up in class of a java program Posted: Feb 27, 2006 2:41 AM
Reply to this message Reply
HI, i worked on ur java code: its working fine and put these lines in main function (Publis static void main(args))
ok::::....

public static void main(String args[])
{
HotelRoom hr = new HotelRoom("Eswar","double",8);
System.out.println("Estimate Price : "+hr.getEstimatedPrice());
System.out.println("Actual Price : "+hr.getActualPrice(6));
}

ok by the way indentation is quite confusing and u Didnt put code check for 7th day stay at the hotel, so better put =7 in the check cases of ur days variable ...days<=7 && days>0 ok




i changed the code to the folowing :


************************************************************


public class HotelRoom
{
final String singleRoom = "single";
final String doubleRoom = "double";
final String businessSuite = "business";


// define your fields here
private String guestname,roomtype;
private int days,reservationlength;
private double estimatedprice,actualprice;

public HotelRoom(String guestname, String roomtype, int days)
{
// complete this method
this.guestname=guestname;
this.roomtype=roomtype;
this.days=days;
}

public String getGuestName()
{
return guestname;
}

public String getRoomType()
{
return roomtype;
}

public int getReservationLength()
{
return days;
}

public double getEstimatedPrice()
{
if (roomtype==singleRoom && days>7)
{
estimatedprice=(60.00*days);
estimatedprice=(estimatedprice-(estimatedprice*.1));
}
else if (roomtype==singleRoom && days>0 && days<7)
{
estimatedprice=(60.00*days);
}
else if (roomtype==doubleRoom && days>7)
{
estimatedprice=(80.00*days);
estimatedprice=(estimatedprice-(estimatedprice*.1));
}
else if (roomtype==doubleRoom && days>0 && days<7)
{
estimatedprice=(80.00*days);
}
else if (roomtype==businessSuite && days>7)
{
estimatedprice=(150.00*days);
estimatedprice=(estimatedprice-(estimatedprice*.1));
}
else if (roomtype==businessSuite && days>0 && days<7)
{
estimatedprice=(150.00*days);
}
else
{
estimatedprice=(80.00);
}
return estimatedprice;
}

public void setRoomType(String roomtype)
{
roomtype=roomtype;
}

public void setReservationLength(int days)
{
days=reservationlength;
}

public double getActualPrice(int daysStayed)
{
if (roomtype==singleRoom)
{
if (daysStayed==days && days<7 && days>0)
{
actualprice=(estimatedprice);
}
else if (daysStayed>7 && daysStayed==days)
{
actualprice=estimatedprice;
actualprice=(actualprice-(actualprice*.1));
}
else if (daysStayed<days)
{
days=(days-daysStayed);
actualprice=(estimatedprice-(days*60.00)+(5.00*days));
}
else if (daysStayed>days && daysStayed>7)
{

actualprice=(daysStayed*60.00);
actualprice=(actualprice-(actualprice*.1));
}
else
{
actualprice=(60.00);
}
}
if (roomtype==doubleRoom)
{
if (daysStayed==days && days<=7 && days>0)
{
actualprice=(estimatedprice);
}
else if (daysStayed>7 && daysStayed==days)
{
actualprice=(daysStayed*80.00);
actualprice=(actualprice-(actualprice*.1));
}
else if (daysStayed<days)
{
System.out.println("in daysstayed<days in actual price...:");
days=(days-daysStayed);
System.out.println("days : "+days+"Estimated Price "+ estimatedprice);
actualprice=(estimatedprice-(days*80.00)+(5.00*days));
}
else if (daysStayed>days && daysStayed>7)
{

actualprice=(daysStayed*80.00);
actualprice=(actualprice-(actualprice*.1));
}
else
{
actualprice=80.00;
}
}

if (roomtype==businessSuite)
{
if (daysStayed==days && days<7 && days>0)
{
actualprice=(estimatedprice);
}
else if (daysStayed>7 && daysStayed==days)
{

actualprice=(daysStayed*150.00);
actualprice=(actualprice-(actualprice*.1));
}
else if (daysStayed>days && daysStayed>7)
{

actualprice=(daysStayed*150.00);
actualprice=(actualprice-(actualprice*.1));
}
else
{
actualprice=150.00;
}
}

return actualprice;
}

public static void main(String args[])
{
HotelRoom hr = new HotelRoom("Eswar","double",8);
System.out.println("Estimate Price : "+hr.getEstimatedPrice());
System.out.println("Actual Price : "+hr.getActualPrice(6));
}
}

************************************************************

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: can't get the output to come up in class of a java program Posted: Feb 27, 2006 3:13 AM
Reply to this message Reply
To oth of you: Please use the Java tags as shown on the right of the input text field to post code.

Flat View: This topic has 2 replies on 1 page
Topic: error:stringindexoutofbounds, please help Previous Topic   Next Topic Topic: xml parsing innovation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use