• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with calculations  RSS feed

 
Noah McGee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I am new to java and I need help with these calculations that wont work correctly. I am creating code for a school project and it is based off the text file I have added to this post. Here is my code:



And this is the result that is showing up:

-- Seat Discount System --
Would you wish to specify a custom discount rate? (Y|N)
Y
Enter a valid Discount Amount
88

Seat Type: 1ST(Table)   Seat Price: £48.50   Booking Number: 2   Income: £97.00   Discount Amount: £85.36

Price of each ticket after the discount has been applied: £5.82   Income after set discount has been applied: £-36.86   Seat Type: 1ST   Seat Price: £44.50   Booking Number: 3   Income: £133.50   Discount Amount: £117.48

Price of each ticket after the discount has been applied: £5.34   Income after set discount has been applied: £-72.98   Seat Type: STD(Table)   Seat Price: £28.50   Booking Number: 3   Income: £85.50   Discount Amount: £75.24

Price of each ticket after the discount has been applied: £3.42   Income after set discount has been applied: £-46.74   Seat Type: STD   Seat Price: £24.50   Booking Number: 5   Income: £122.50   Discount Amount: £107.80

Price of each ticket after the discount has been applied: £2.94   Income after set discount has been applied: £-83.30  


Text File Image: 



   
 
Noah McGee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the text file:

1ST(Table)
48.50
2

1ST
44.50
3

STD(Table)
28.50
3

STD
24.50
5
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the calculations supposed to do that they're not doing? You showed us the input file, which is great. Now we need to see a comparison between the output you're getting vs. the output you want to see.
 
Noah McGee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:What are the calculations supposed to do that they're not doing? You showed us the input file, which is great. Now we need to see a comparison between the output you're getting vs. the output you want to see.



This is what I get when the file is run:

"-- Seat Discount System --
Would you wish to specify a custom discount rate? (Y|N)
Y
Enter a valid Discount Amount
20

Seat Type: 1ST(Table)   Seat Price: £48.50   Booking Number: 2   Income: £97.00   Discount Amount: £19.40

Price of each ticket after the discount has been applied: £38.80   Income after set discount has been applied: £77.60   Seat Type: 1ST   Seat Price: £44.50   Booking Number: 3   Income: £133.50   Discount Amount: £26.70

Price of each ticket after the discount has been applied: £35.60   Income after set discount has been applied: £106.80   Seat Type: STD(Table)   Seat Price: £28.50   Booking Number: 3   Income: £85.50   Discount Amount: £17.10

Price of each ticket after the discount has been applied: £22.80   Income after set discount has been applied: £68.40   Seat Type: STD   Seat Price: £24.50   Booking Number: 5   Income: £122.50   Discount Amount: £24.50

Price of each ticket after the discount has been applied: £19.60   Income after set discount has been applied: £98.00   "


And this is the outcome I am looking to achieve"

- - Seat Discount System - -

Specify Custom Discount Rate [Y|N] : Y
Assuming Discount Rate = 20.0%

Seat Type : 1ST(Table), Seat Price : £48.50, Bookings : 2, Discount : £19.40, Income : £77.60
Seat Type : 1ST, Seat Price : £44.50, Bookings : 3, Discount : £26.70, Income : £106.80
Seat Type : STD(Table), Seat Price : £28.50, Bookings : 3, Discount : £17.10, Income : £68.40
Seat Type : STD, Seat Price : £24.50, Bookings : 5, Discount : £24.50, Income : £98.00

Total Income : £350.80
Total Discount : £87.70
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formatting suggests that you have a brace (}) in the wrong place.  Do you really want a close brace at the end of line 54?  Where would you put it instead?
 
Noah McGee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:The formatting suggests that you have a brace (}) in the wrong place.  Do you really want a close brace at the end of line 54?  Where would you put it instead?


I really do not know I am struggling here, only starting out on java
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about what you want to repeat and what you don't want to repeat.  Move the brace from line 54 to the last line you want repeating because of the while.
 
Noah McGee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Think about what you want to repeat and what you don't want to repeat.  Move the brace from line 54 to the last line you want repeating because of the while.


Sorry I really dont understand what you mean, when I try and move the brace I get a lot of error messages.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand where your Income: amount is supposed to come from. Nothing in your calculations comes close to the results you are expecting.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move the brace to just after the lines you want to repeat and just before the lines you don't (the totals).
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The indentation of your code suggests that you want to execute lines 35-47 as part of a while-loop.  However, Java doesn't go by your indentation. It will include statements in the while-loop based on what it finds between the {} that defines the body of the while loop.

Java will see that your while-loop body starts at the end of line 34, with an opening brace, {.  The matching closing brace is at the end of line 54. Knute was asking whether you really wanted to while-loop body to include those other lines up to line 54 or did you really want to end the while-loop at line 47, as your indentation suggests. 

If you only intended to include lines 35-47 in the while loop, then put the ending brace, }, in the proper place so that the Java compiler does what you actually intended.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you move the closing brace to match your intent for the while loop, you will get other errors. These errors don't mean that you were wrong to move the closing brace to where it should be. If you look at the error messages closely, they will probably tell you that there are undefined tokens or something like that.  This is because you have other kinds of mistakes in your code that have to do with scope of variables.  Your variables need to be declared within the scope that they are used. Variables that you declare inside the body of a while-loop can only be used inside the while-loop body. You can't reference them anymore once you exit the while-loop. If you need to reference them outside the while loop, then you need to declare them outside the while loop.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I don't understand where your Income: amount is supposed to come from. Nothing in your calculations comes close to the results you are expecting.

Ah, it's income-discTotal.
 
Noah McGee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I don't understand where your Income: amount is supposed to come from. Nothing in your calculations comes close to the results you are expecting.


My calculations are all over the place I need help trying to fix them.

This is the code I have at the minute:
       while (file.hasNext()) {
String SeatType = file.next();
double price = file.nextDouble();
int bookingNumb = file.nextInt();
double income = price * bookingNumb;
totalInc = totalInc + income;
totalDisc = totalDisc + discountAmount;
double discTotal = income * discountAmount / 100;
double discTotalPrice = price * discountAmount / 100;

System.out.printf("Seat Type: %s   Seat Price: £%.2f   Booking Number: %d   Income: £%.2f   Discount Amount: £%.2f %n", SeatType, price, bookingNumb, income, discTotal); }

And this is my result:
Seat Type: 1ST(Table)   Seat Price: £48.50   Booking Number: 2   Income: £97.00   Discount Amount: £19.40
Seat Type: 1ST   Seat Price: £44.50   Booking Number: 3   Income: £133.50   Discount Amount: £26.70
Seat Type: STD(Table)   Seat Price: £28.50   Booking Number: 3   Income: £85.50   Discount Amount: £17.10
Seat Type: STD   Seat Price: £24.50   Booking Number: 5   Income: £122.50   Discount Amount: £24.50


everything is working except for the Income and I dont know how to fix it
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This formatting is closer to what you are expecting.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Carey Brown wrote:I don't understand where your Income: amount is supposed to come from. Nothing in your calculations comes close to the results you are expecting.

Ah, it's income-discTotal.
 
Noah McGee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Carey Brown wrote:
Carey Brown wrote:I don't understand where your Income: amount is supposed to come from. Nothing in your calculations comes close to the results you are expecting.

Ah, it's income-discTotal.


Yess! Thank you very much sorted all of the code there now!

One more thing though, how do I get the Total Income and the Total Discount (Just all of the incomes in each table and all of the discounts in each table added together)
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your formulas are a bit of a mess. The names don't clearly describe what they are. For instance

Don't be afraid to type more, it may make it clearer to you.
 
Noah McGee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Your formulas are a bit of a mess. The names don't clearly describe what they are. For instance

Don't be afraid to type more, it may make it clearer to you.


This is what shows up when I type in that code:

-- Seat Discount System --
Would you wish to specify a custom discount rate? (Y|N)
N
Default Discount Amount will be applied.

Seat Type: 1ST(Table)   Seat Price: £48.50   Booking Number: 2   Income: £77.60   Discount Amount: £19.40
Total Income: £174.60Total Discount: £39.40Seat Type: 1ST   Seat Price: £44.50   Booking Number: 3   Income: £106.80   Discount Amount: £26.70
Total Income: £414.90Total Discount: £86.10Seat Type: STD(Table)   Seat Price: £28.50   Booking Number: 3   Income: £68.40   Discount Amount: £17.10
Total Income: £568.80Total Discount: £123.20Seat Type: STD   Seat Price: £24.50   Booking Number: 5   Income: £98.00   Discount Amount: £24.50
Total Income: £789.30Total Discount: £167.70


But I only need 1 total income as a whole that adds to 350.80 and 1 total discount which adds to 87.70



This is the code I put in now:



 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Income is not price * bookingNumb, that is the pricePerBooking and you want incomePerBooking. As I mentioned, more descriptive names make things clearer, something like this.

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your closing brace AFTER printing the totals means that the print takes place inside the while() loop and will be printed every iteration of bookings, instead of just printing once at the end.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!