• Post Reply Bookmark Topic Watch Topic
  • New Topic

structuring my business application  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys i have a few classes for my business application that keeps track of company payments

i have  a class Program: which really just holds main




then i have the Company Class




a Function class



and an import utility





im battling to get my input class to return a value in the mainMenu where i can have the user select from the menu
im pretty much just lost in stringing this all together,some suggestions would be greatly appreciated

thanks

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
used code tags honestly dont know why it didnt work on all...sorry again
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:used code tags honestly dont know why it didnt work on all...sorry again


You only used code tags on two out of the four code snippets. Anyway, I went and added it for you.

Henry
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this line of code

What happens to the return value from getNumber()? You call the function but you don't store the value anywhere.
You'll want to store this return value in a variable, and then check the value of the variable to see which command you should use.
Then you can put this inside of a loop - for example, while the number is not equal to the exit code, keep asking for a command and then running it.


How is the user going to enter "Q" when the getNumber() method only returns ints?
Maybe quit should be 0.

Some other feedback:


This scanner should be local unless there's a particular reason why it should be class visible. That way you wouldn't have to do the hasNextInt() check.
But at least, this should be private.

There are issues with your function and class names. For example this:

The name getNumber tells me nothing. In fact, I can see that it will return a number by the return value alone. What number is it returning? What will that number be used for? That should be related for the name.

Same thing here. What is this String for?


Function is probably not a good class name. It should be descriptive of what the class is going to do.

There's no error checking in your program yet. If the user enters "a", it will cause an exception.
Since you're using an external util class to read numbers, this would be a perfect place to do error checking. Use a loop and keep asking for a number until a correct one is given.
You can even check if the option is between the minimum option and maximum here.


What's this class for? Why does a Company have a list of companies (which are Strings)? And should it be private?
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi zachery thanks for your elaborate answer

In this line of code

InputUtility.getNumber();

What happens to the return value from getNumber()? You call the function but you don't store the value anywhere.
You'll want to store this return value in a variable, and then check the value of the variable to see which command you should use.
Then you can put this inside of a loop - for example, while the number is not equal to the exit code, keep asking for a command and then running it.

[
System.out.println("Q) Quit");

How is the user going to enter "Q" when the getNumber() method only returns ints?
Maybe quit should be 0.

color=red]yes i agree 100% wasnt thinking there just yped the menu up 0 would be fine to quit [/color]

Some other feedback:


static Scanner userInput = new Scanner(System.in);

This scanner should be local unless there's a particular reason why it should be class visible. That way you wouldn't have to do the hasNextInt() check.
But at least, this should be private.

i was advised to have the scanner static as i was told by other members it needs to be available to the whole program in case you want to add a user interface,also its not good to have multiple scanners in different classes 

There are issues with your function and class names. For example this:

public static int getNumber()

The name getNumber tells me nothing. In fact, I can see that it will return a number by the return value alone. What number is it returning? What will that number be used for? That should be related for the name.

public static String getString()

Same thing here. What is this String for?

ok fair enough,it just needs to be more descriptive,like getUserNumber? or menuNumber? something like that?
public class Function

Function is probably not a good class name. It should be descriptive of what the class is going to do.

There's no error checking in your program yet. If the user enters "a", it will cause an exception.
Since you're using an external util class to read numbers, this would be a perfect place to do error checking. Use a loop and keep asking for a number until a correct one is given.
You can even check if the option is between the minimum option and maximum here.
in the InputUtil class there isnt exception handling like try catch i was advised to use class methods instead like hasnextint


public class Company {
    private String companyName;
    private int companyTele;
 
    ArrayList<String> companyList = new ArrayList<>();

What's this class for? Why does a Company have a list of companies (which are Strings)? And should it be private?
it was just an example i was trying to do...do you mind if i PM you?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the utility class is a better means of getting keyboard input than having a Scanner in each object. But I agree, that Scanner reference shou‍ld be private and final. It is quite important not to allow access to it from outwith the keyboard inputs class. Agree however that the method names could do with improvement. Why not nextInt?

The use of nextLine on its own like that will not work; if you don't check carefully, that method will often return the empty String and subsequent input can be lost.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja, have a cow for a good indentation (well done) and quite good formatting (this still could be improved further by removing some extra unused whitespace lines).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!