Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java program for beginner

 
Diana Wales
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first off I would like to apologize for this being my first post. But in my searches this was the only forum I could find that seems to have a semi-decent, to say the least, member base that might be able to help me. i am new to java, really need for your help =S My question is

Excellent fertilizer wholesaler sdn bhd stocks manure in 100kg, 20kg, 10kg and 5kg sacks. write java program that will tell the wholesaler how many sacks of each weight are required to make up a retailer's order. To reduce handling problems the objectives is to use the minimum total number of sacks.
If retailer having retailer-code ‘A’ or ‘a’, they cannot accept 100kg sacks.
If retailer having retailer-code ‘B’ or ‘b’, they cannot accept 100kg sacks or 20kg sacks.
If retailer having retailer-code ‘C’ or ‘c’, they can only accept 5kg sacks.
If retailer having retailer-code ‘D’ or ‘d’, they can accept any sizes sacks.
Retailers are charged $10.50 per order for handling and delivery plus :
a. $5.50 per 100kg sack
b. $2.35 per 20kg sack
c. $1.20 per 10kg sack
d. $0.80 per 5kg sack
If an order does not satisfy a multiple of 5kg then the order will be filled up to the nearest multiple of 5kg beneath the amount ordered.

how sholud i do the coding for this case statement and using if..else statement?

example output:-
Order quantity 154kg
100kg Sacks 1
20kg Sacks 2
10kg Sacks 1
Total number of sacks : 4kg
Invoice : $21.90


i tried case A, and its not working T__T anyway here my answer

 
Kristjan Toots
Ranch Hand
Posts: 59
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Diana Wales, welcome to the Javaranch.
You really should edit your post and format it a bit - I really struggled to get your point (which i didn't succeed).
It would help alot to use code tags aswell (to improve readability) How to use Code tags
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Diana Wales wrote:i tried case A, and its not working T__T anyway here my answer

What exactly does "it is not working" mean? Can you compile the program? Can you run it? Do you get an error message? If yes, then what exactly is the error message? Does the program run OK, but produce other output than you expected? If yes, then how does the actual output differ from what you expected?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code shows many missed original requirements... first you can avoid writing all the logic in the main method. You can have a separate class with instance variables used to store the cost of each sack like below. You could initialize them using a constructor.



You can have another class with the main() method to get user input and call the InvoiceCalculator class methods to compute the invoice.

The method to calculate the invoice shall be written in the InvoiceCalculator class ( You have the cost of each sack and the delivery charge information assigned using its constructor).

Below shall be the calculator method in the InvoiceCalculator class. You have to pass the user input of number of orders and the order code.



 
Diana Wales
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it! thanks for helps guys i really appreciate it
 
Campbell Ritchie
Sheriff
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:. . . You can have a separate class with instance variables used to store the cost of each sack like below. . . .
Surely you don't want instance fields?Then you can call them by Prices.PRICE_FOR_75 or similar.

You ought to avoid float arithmetic unless you can specifically tolerate its imprecision. You ought to avoid double arithmetic for money, using BigDecimal arithmetic instead. Most beginners use doubles for simplicity, however.
 
Campbell Ritchie
Sheriff
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much arithmetic have you done with the / and % operators?
Why are you importing java.util.*? You don't appear to be doing anything with it.
 
Campbell Ritchie
Sheriff
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you create an array with the bag sizes in? Have you done arrays yet?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
John Jai wrote:. . . You can have a separate class with instance variables used to store the cost of each sack like below. . . .
Surely you don't want instance fields?Then you can call them by Prices.PRICE_FOR_75 or similar.


I intend to make the prices variable... say if they decide to change the cost of a pack then it can be changed by passing the value to the constructor and not in code. correct me if am wrong.
 
Diana Wales
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i haven't learned how to use about array yet, the / operator state about quantity order, example order 154kg divide by 100kg = 1 sack remainder 54 then divide by 20kg = 2 sacks remainder 14 then divide by 10 kg = 1 sack, therefore total number of sacks : 4(1+2+1). The % operator is not working but according to my lecturer its for the short order(remainder) i should ask my lecturer about it.
 
Campbell Ritchie
Sheriff
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can you not get the % operator to work.
154 / 100 gives you 1.54, which you cast to an int. so 154 % 100 should give you 54. It is a lot easier if you convert everything to ints and use integer arithmetic. You will have to do a lot of casting to int, to get the bags into whole numbers. Casting to an int will also give you the rounding-down specified for fractions of a bag (as long as you only have positive numbers).
campbell@???:~/java> java RemainderDemo 154.0 100.0
d % dd = 54.0
(int)d % (int)dd = 54
(int)(d % dd) = 54
d / dd = 1.540
(int)d / (int)dd = 1
(int)(d / dd) = 1
 
Diana Wales
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get it, its working thanks
 
Campbell Ritchie
Sheriff
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done and "You're welcome"
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic