Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

OO design  RSS feed

 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying out a sample problem I came across online for a print software. I have this enum, but I am struggling with how to break this down in case I want to support only colour or b/w later.

I tried another enum (to hold colour and b/w values) - to be referenced within this enum but I don't know if that's a good idea or how to get that to work. Any suggestions are welcome

 
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Printing may have many parameters that affect the cost of a print job. A "print configuration" would take in all the variables and return a cost. Boil each parameter down to a variable. Use enums where possible. The variations could be hard coded for development but would allow for loading them in from some sort of file later. You could use a HashMap to store and retrieve a specific configuration.

Perhaps something along these lines:

Also note that using BigDecimal.valueOf(0.95) will give you floating point rounding errors whereas new BigDecimal("0.95") won't.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, this makes sense. I am trying to do something similar but I'm stuck with where the cost should go

You have cost as one of the parameters to print config, I know what the cost is already for each of those, but it depends on what type and what size and what ink.

How can I do that?
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, so you're saying even though I know the costs now and can hard code somewhere, receive it as an argument?
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, and I would also have something like number of BW and colour pages - should those be input arguments as well?
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, my scenario is I know the number of b/w and colour pages to print. So I'm taking those as arguments. I have this:




   
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that there is a subtlety in order for looking up the cost to work. For two PrintConfig's to be equal() you'll have to compare all the fields EXCEPT cost. Ditto for hashCode. This allows you to fill in a config without the cost and look up the cost using a Map as shown below.

You could have a class that manages all supported print configurations and allows the lookup of the cost of a particular configuration.
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasanna Raman wrote:Actually, my scenario is I know the number of b/w and colour pages to print. So I'm taking those as arguments. I have this:
   


Are you permitting a print job that changes from black&white to colour on a per-paage basis?
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. How do I handle total pages, colour pages etc. I am currently reading from file these things:

total pages
colour pages and
isTwoSided

I know the costs for each, and I have the paper type as default now.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, a single job can have 10 b/w pages and 10 colour pages.
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A PrintJob would use a particular PrintConfig and come up with a totalCost based on the PrintConfig cost times the number of pages. This assumes that you won't have a mixture of PrintConfig's for a particular PrintJob.
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasanna Raman wrote:yes, a single job can have 10 b/w pages and 10 colour pages.


Can it have 5 black&white two sided, 3 colour 1 sided, 7 colour two sided?
What is the limit of combinations allowed in a single Job?
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For now, I'm only targeting colour vs b/w being variable. So, I can do 3 colour one sided, and 5 b/w one sided.

So, my 3 variables I'm reading from file are:

Totalpages.
Totalcolourpages
Is2Sided?  - boolean

But I also want to design so I can maybe allow the combinations you asked about in the future.
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For flexibility I might consider a class like SubJob that take a PrintConfig and number of pages and can return a computed cost. Then, a PrintJob contains 1 or more SubPrintJob's and computes the total cost based on the sum of various costs of its SubPrintJob's.

I actually like the name "Joblet" instead of "SubJob". Whatever.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. So PrintJob wouldn't have cost or ink anymore in its constructor?
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A PrintJob would then contain a list of Joblets. You could either have a constructor that takes a variable number of arguments for this, or you could have an add(Joblet) method.

 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. So this is my understanding:

Have a main class (an Application? class) that reads from file these variables:
totalPages
colourPages
2sided?

That application will create Joblet objects first and pass that as argument to constructor of PrintJob.

PrintJob will keep a list of Joblets, and adding cost of those joblets will give us the cost of the print job.

The hard coded cost will go in the PrintConfigs file.

To calculate overall cost, Application class can have a list of all PrintJobs and add the cost of each?

Please let me know if I'm missing something.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and would you tell me how the PrintConfigs class look like now, with the addition of the Joblet class?
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretty much, yes.

Don't know what you want to do with totalPages? Does blackAndWhitePages = totalPages - colourPages?
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasanna Raman wrote:Oh, and would you tell me how the PrintConfigs class look like now, with the addition of the Joblet class?


PrintConfig doesn't know anything about the Joblet class. The Joblet class has-a PrintConfig instance and a numberOfPages.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know what you want to do with totalPages? Does blackAndWhitePages = totalPages - colourPages? -- yes.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, are you talking about PrintConfig (singular) or PrintConfigs(plural)?

PrintConfig doesn't know anything about the Joblet class. The Joblet class has-a PrintConfig instance and a numberOfPages. --

If it's singular, how does PrintConnfigs (plural) do loadDefaultCosts now?
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meanwhile, I am working on getting some code done. Will post soon.
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some people would prefer a name like PrintConfigManager over PrintConfigs. Choose what feels right for you.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following. I think I'm confused about how PrintConfig should calculate cost. I have a circular dependency between cost methods of PrintConfig and PrintConfigs now

PrintConfig:



PrintConfigs:

 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasanna Raman wrote:Sorry, are you talking about PrintConfig (singular) or PrintConfigs(plural)?

PrintConfig doesn't know anything about the Joblet class. The Joblet class has-a PrintConfig instance and a numberOfPages. --

If it's singular, how does PrintConnfigs (plural) do loadDefaultCosts now?


Per last post: You may prefer the name PrintConfigManager.



 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasanna Raman wrote:I have the following. I think I'm confused about how PrintConfig should calculate cost. I have a circular dependency between cost methods of PrintConfig and PrintConfigs now

PrintConfig:



PrintConfigs:


PrintConfig has-a cost which is a member variable (aka "field"). Nothing tricky here, just make a getter for it.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I think I'm understanding this a little better now So, who would call the loadDefaultCosts method then?

Would it be the Application class that reads from file?
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasanna Raman wrote:Thank you. I think I'm understanding this a little better now So, who would call the loadDefaultCosts method then?

Would it be the Application class that reads from file?


Probably "loadDefaultConfigs()". PrintConfigs includes the cost.
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're headed in the right direction. I have to take a break for a couple of hours. I'll check back later.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thank you very much!
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because I am not reading cost from file today, I have an overloaded constructor in PrintConfig to let my main method create PrintConfig without cost. Is this ok or is there a better way?
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I have about 8 classes. I know leaving them all in the default package isn't good but is there a convention to naming packages for these types of apps?

I have an Application class, 3 enums and 4 other classes (Joblet, PrintConfig, PrintJob and PrintConfigManager).
 
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find naming conventions in the Java™ Tutorials. And good to see you back
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasanna Raman wrote:Because I am not reading cost from file today, I have an overloaded constructor in PrintConfig to let my main method create PrintConfig without cost. Is this ok or is there a better way?


Yes. I was going to mention that you'd need an overloaded constructor, one that doesn't require a cost. Very good.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. More than conventions, I'd like to understand how I can organise this into packages. Please suggest
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing as how you are not a business, choosing a name is a big vague. I would have a single package with a name that suggests its purpose. A simple choice might just be "print".

Or something like (this is what I do)
<your initials>.print
 
I am displeased. You are no longer allowed to read this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!