For my CS course I have to program a hotel checkin/checkout system. The program must be able to check people in and out. The programm assigns the first available room to a guest upon checkin. If no room is free, it will say so as well. The Hotel has four rooms.
In the assignment it says there need to be 4 classes: Assignment5, Hotel, Room, & Guest.
Assignment5 class is for the interaction with the user.
Hotel class has four rooms and all methods for operating the rooms.
Room class has 1 guest. If the rooms is empty a guest can check in. If the guest is leaving the room needs to be emptied.
Guest class: the guest has a firstname and a last name.
In the menu there need to be 4 options:
1 show status of rooms available and occupation.
2 check in option
3 check out option
4 end program option.
OK, so I know I should make my assignment for myself. However, I don't know what it is, but starting with assignments I have great problems cutting the thing up in smaller pieces. Also this assignment is to learn working with different classes and I don't really understand which sequence of steps i should take in this case.
Can someone help me getting started by giving some tips? Have been staring at my screen for hours now and just thought I could use some little insights to get me started. Any help is greatly apprecieted!
OK thnks for the help so far.
**First of all, MANY thanks for all your help, you guys are great! Have been at it for 7 hours straight now and still stuck. My problem now is that it doesn't compile. It says:
Java:28: checkIn(Gast) in Hotel cannot be applied to (java.lang.String)
hotel.checkIn("Guido");
^
1 error
And maybe, can someone look if this way i put it now is a little bit on the right path? I do thank JavaGeek for his program, but i want to learn it by doing myself.
up until now I have the following:
import java.util.Scanner;
public class bopgave5 {
public static void main(String[] args) {
boolean opnieuw = false;
do {
int invoer = menu();
if (invoer == 2){
Hotel hotel = new Hotel();
hotel.checkIn("Guido");
opnieuw = true;
}
else if (invoer == 4)
opnieuw = false;
else
opnieuw = true;
}
while (opnieuw == true);
}
public static int menu (){
Scanner sc = new Scanner(System.in);
System.out.println("MENU: [1] Statusoverzicht");
System.out.println(" [2] Check-in");
System.out.println(" [3] Check-out");
System.out.println(" [4] Einde");
System.out.print("Uw invoer: ");
int invoer = sc.nextInt();
return invoer;
}
}
class Hotel {
Kamer kamer1, kamer2, kamer3, kamer4;
Hotel (){
kamer1 = new Kamer();
kamer2 = new Kamer();
kamer3 = new Kamer();
kamer4 = new Kamer();
}
void checkIn(Gast nieuweGast) {
if (kamer1.gast == null) {
kamer1.gast = nieuweGast;
System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 1");
return;
}
else if (kamer2.gast == null) {
kamer2.gast = nieuweGast;
System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 2");
return;
}
else if (kamer3.gast == null) {
kamer3.gast = nieuweGast;
System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 3");
return;
}
else if (kamer4.gast == null) {
kamer4.gast = nieuweGast;
System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 5");
return;
}
else {
System.out.println("hotel is vol");
return;
}
}
}
class Kamer {
Gast gast;
Kamer() {
gast = null;
}
}
class Gast {
String naam;
Gast(String nieuweNaam) {
naam = nieuweNaam;
}
}
So you have 4 classes.
In the assignment it says there need
to be 4 classes: Assignment5, Hotel,
Room, & Guest.
With the division of responsibility as such:
Assignment5 class is for the
interaction with the user.
Hotel class has four rooms and all
methods for operating the rooms. (extra emphasis: "rooms" is plural)
Room class has 1 guest. If the rooms
is empty a guest can check in. If the
guest is leaving the room needs to be
emptied. (or in other word, operating a single room)
Guest class: the guest has a firstname
and a last name.
First, you'd probably want to identify the "state" that each object would have. IOW, you need to determine the attributes/instance fields that each object have. Let's start with an example: Guest class: the guest has a firstname and a last name.. Do the same for all the other classes.
Next, you want to identify the methods that will be needed. Let's start with another example: Room class has 1 guest. If the rooms is empty a guest can check in. If the guest is leaving the room needs to be emptied.. On top of that, you'll need some constructors for each class.
Next, for each method, you want to find out the arguments that the method needs, and their return values, if they need to return a value. For example, for a check in method, you'll need the Room and a Guest; and you'll need to check whether the room is empty before you can check in.
UPDATE:
My problem now is: how can i make it work that there are 4 rooms and that after checking in 1 person, checking in a second person will put it in a different room?
Your teacher has a good advice, break it into pieces.
Basically, you have the problem: "Checking in the second person should put him in a different room".
So, how do we break this down? First, we need to find an empty room, so we need a method for that (say findEmptyRoom()), and after we find a room that's available, we need to check in the guest into that room, so we need another method (say checkIn()). So, we find an empty room, then we checked the guest into that room, then we're done.
Next step, we break findEmptyRoom(). Our hotel has 4 rooms (let's say we store room1, room2, room3, and room4 as member variables, alternatively, you can use an array if you already learn about it in class). To find which one of the four rooms are empty, we need to ask a room if it is empty; so we need another method for that (say isEmpty()). To find an empty room, ask room 1, if room1 is empty return room1, otherwise ask room 2, if room2 is empty return room2, otherwise ask room 3, if room3 is empty, return room3, otherwise ask room 4, if room4 is empty, return room4. Otherwise, all rooms are occupied. [note: you will need to figure out what to do if all rooms are occupied]. An array will make this process much easier, you just need to loop through the array, and ask if it's empty and return the room if it's empty, otherwise continue checking next room.
Next, we break checkIn(). So, what do checkIn() need to know to fulfill the checking in process? It needs to know about two information: the room and the guest. Since checkIn() is an instance method of a room class, room is implied, so we only need one parameter, i.e. guest. So void checkIn(Guest guest) should set the guest member variable of the room.
Next, we break isEmpty(). How do we know if a room is empty? If a room has a guest inside it, then it's occupied, otherwise it's empty. So, isEmpty() checks if the guest member variable refers to a valid guest, returns true if it is valid guest, otherwise returns false. In Java, null is often used to mark that something is not a valid object, so you can check whether the guest member variable is null (obviously, you'll need to set the guest member variable to null when a guest checked out).
Do the same thing for the other processes. Break the problem down into smaller pieces until you're confident that you can work with a piece.
UPDATE2:
The error message you've got:
Java:28: checkIn(Gast) in Hotel cannot be applied to (java.lang.String)
hotel.checkIn("Guido");
is because you're passing a String ("Guido") to a function that takes a Gast argument (void checkIn(Gast nieuweGast) {...}). You should instead, pass a Gast object:
// declare a variable called g, with type Gast
Gast g;
// create a new Gast object, passing "Guido" as parameter
// to Gast's constructor, and assign the new object to g
g = new Gast("Guido");
// call hotel.checkIn(Gast) function with g as the argument,
// i.e. the Gast object we just created in the previous line
hotel.checkIn(g);
or more simply:
hotel.checkIn(new Gast("Guido"));
Short of solving this thing for you, here is what I would suggest you do.
Think of each operation that needs to be performed, and then diagram the logic that needs to take place to accomplish that operation. Then, with a clear list of logical operations that need to take place to accomplish that task, think carefully about what components you can break that operation up into and where each of those sub-tasks should go. This will give you a good idea of what the class diagram should look like. Once you've diagrammed the logic, classes/methods, and implemented one part, move on to the next part. At this point, you'll probably find that something wasn't structured in a way that allows it to be reused for the next task, so refactor it (modify the structure of the code) to make it more reusable.
For example, tackle the check-in operation. What do you need to do first? Well, first you need determine if there are any rooms available. How do you do that? You need to ask all of the rooms (by your design, if I remember correctly) if they are available. So, we can now identify that we need a method on the rooms to tell us that they are available. Maybe tackle that first. Then the checking for a free room part. That probably goes on the hotel class. You'll need to iterate over the rooms and ask them if they are free. After this, you need to decide what to do if there is a free room(s), or if there aren't any. I guess you display an error message if there aren't? If there are free rooms, you need form the check-in operation. Write that. Rinse and repeat. Before long, you'll have a functioning program.
The important thing to remember here is that there are MANY different ways to accomplish this in an object-oriented manner. When you are just learning OO design and the basics of programming, it is important to not get too hung up on the perfect design and to dive in. Then when you are in the middle of implementing it, I'm sure you'll say 'hey, if I do it this way it will come up much better'. I like to think of this as an iterative learning process.
Start with the objects, the domain
Try giving the classes useful members. What is a 'hotel' for this problem? When they have useful members, what are useful operation on them, the methods?
Fields: A room should have a room number. A room should know what guest is staying in that room. A guest should have a name. A hotel should have a collection of rooms, a name, an address? Maybe the address is useless here.
Methods: A room should have a method to see if it's empty and what guest is staying there if it is not empty. A hotel should have a method that tells you how many rooms are available, or give you the actual rooms. All the guests that stay in the hotel? Etc, etc, etc.
You'll notice you will have to keep refining, changing, fixing your model to be able to meet the requirements. For example 'checking in' might need a new method in room you did not yet have.
//Guest.java
package hotelcheckinsystem;
class Guest {
private String name;
public String getName() {
return name;
}
public Guest(String name) {
super();
this.name = name;
}
public void setName(String name) {
this.name = name;
}
}
//Room.java
package hotelcheckinsystem;
class Room {
private Guest guest;
public Guest getGuest() {
return guest;
}
public void setGuest(Guest guest) {
this.guest = guest;
}
public void removeGuest() {
guest.setName("");
}
public boolean isEmpty() {
if (this.getGuest().getName().equalsIgnoreCase("")) {
return true;
}
return false;
}
}
//Hotel.java
package hotelcheckinsystem;
public class Hotel {
private Room[] rooms = new Room[4];
public Hotel() {
for (int i = 0; i < 4; ++i) {
rooms[i] = new Room();
Guest guest = new Guest("");
rooms[i].setGuest(guest);
}
}
public void assignRoomToGuest(String name) {
int i;
for (i = 0; i < 4; ++i) {
if (rooms[i].isEmpty()) {
Guest guest = new Guest(name);
rooms[i].setGuest(guest);
System.out.println("Room number " + i + " assigned to " + name);
return;
}
}
if (i == 4) {
System.out.println("No empty Rooms to assign to " + name);
}
}
public void emptyRoom(int roomNo) {
if (roomNo > 3) {
System.out.println("please enter number between 0 to 3");
return;
}
rooms[roomNo].removeGuest();
System.out.println("Room number " + roomNo + " is empty now!.");
}
}
//Main.java
import java.util.Scanner;
import hotelcheckinsystem.*;
public class Main {
public static void main(String[] args) {
Hotel hotel = new Hotel();
while (true) {
System.out.println("Enter the Option: ");
System.out.println("1. Check in. \n2. Check out. \n3. Exit");
Scanner sc = new Scanner(System.in);
int option = sc.nextInt();
switch (option) {
case 1:
System.out.println("Enter the guests name");
hotel.assignRoomToGuest(sc.next());
break;
case 2:
System.out.println("Enter the Room number");
hotel.emptyRoom(sc.nextInt());
break;
case 3:
System.exit(0);
}
}
}
}
Sample Run:-
Enter the Option:
Check in.
Check out.
Exit
1
Enter the guests name
empty
Room number 0 assigned to empty
Enter the Option:
Check in.
Check out.
Exit
1
Enter the guests name
A
Room number 1 assigned to A
Enter the Option:
Check in.
Check out.
Exit
1
Enter the guests name
B
Room number 2 assigned to B
Enter the Option:
Check in.
Check out.
Exit
1
Enter the guests name
C
Room number 3 assigned to C
Enter the Option:
Check in.
Check out.
Exit
1
Enter the guests name
D
No empty Rooms to assign to D
Enter the Option:
Check in.
Check out.
Exit
2
Enter the Room number
1
Room number 1 is empty now!.
Enter the Option:
Check in.
Check out.
Exit
1
Enter the guests name
D
Room number 1 assigned to D
Enter the Option:
Check in.
Check out.
Exit
3
I can see you already made some code but for your next similar assignment I would code the classes in the following way.
Guest
Room
Hotel
Assignment5 <-- This is the most complicated one and is something you can do at last.
Why?
Guest is really easy to code. When that is done go on to Room. Room is gonna need the class Guest. After you finished Room go code Hotel. Hotel is gonna use Room.
From what I can understand from your (dutch)code you store a guest as a String. This is not the purpose of the assignment. It is supposed to store a Guest object.
I hope this will help you.
I'm joining this a little late, but the reason for the error is you are attempting to check in a String called Guido instead of a Guest (Gast) with the name field of Guido. You would need some lines like the following:
Gast guido = new Gast("Guido");
hotel.checkIn(guido); // this was previously hotel.checkIn("Guido")
Related
"If a player's playerID ends in 00 to 49, this person is on the “lucky list”; however if the playID ends in 50 to 99, this person is on the “normal list”."
//Players ID, is what i have so far
System.out.println("Please enter the player’s ID (8 digits): ");
int playerId = input.nextInt();
//When i use if else statements i can select for certain cases. for example
if (playerId % 50)
normalList;
if (playerId % 3)
luckyList;
These are two example that i can think of. I assume there is a shorter and more logical way to do this but i dont have a clue how.
Be more clear on what normal list; and what luckyList; are. You say lists, so I am assuming you are using array or java.util.ArrayList to store your objects.
What I would do is to the effect of this:
`//Players ID, is what you have so far.
int playerID;
System.out.println("Please enter the player’s ID (8 digits): ");
playerID=input.nextInt();
System.out.println("Please re-enter the player's ID(8 digits):");
String IDword=input.next(); //can also use nextLine here
//now what happens is I have made a string version and an int version of your ID. Now, using charAt, I can access the last two values.
if(IDword.charAt(IDword.length()-2)=='4')
{
LuckyList.add(playerID);
}
if(IDword.charAt(IDword.length()-2)=='3')
{
LuckyList.add(playerID);
}
if(IDword.charAt(IDword.length()-2)=='2')
{
LuckyList.add(playerID);
}
if(IDword.charAt(IDword.length()-2)=='1')
{
LuckyList.add(playerID);
}
if(IDword.charAt(IDword.length()-2)=='0')
{
LuckyList.add(playerID);
}
else
normalList.add(playerID);`
This detects the second to last digit- if it is 4 or less, it adds to luckyList. Else, it will add to normalList.
You may just want to add something indicating that the data is always 8 digits (that just being if(IDword.size()==8) do the following.
I have an assignment in Java to make a vending machine that displays items and prices using the printf tool and requests the user to enter the money they have. It then asks the user to make a selection with a character, exiting if they type x and prompting for another try if they type in an invalid character. It also keeps a running total of the money they have left and doesn't allow them to buy something they don't have the money for. After user 1 is done, it is then open for the next user to enter the amount of money they have and choose an item but with the items the first user chose absent. This cycle repeats until nothing is left in the machine or a user ends the program. Each user should be able to buy as many of each item as they want (one by one) until there is no more of that item.
I'd use a class to indicate a type of item.
public class Item { // or without public
private String name;
private char choice;
private double price;
private int amount; // or name it *quant*-what I can't spell that word
// Constructors, getters, setters, etc.
}
And you can use a list to handle them. This initializes items in the vender:
List<Item> items = new ArrayList<>();
items.add(new Item("Milk", 'a', 2.00, 5));
// Add other items
And this prints all items:
for(Item item : items)
System.out.printf(/* format string */, item.getName(), /* other arguments */);
And this handles actual purchase:
boolean foundItem = false;
for(Item item : items) {
if(item.getChoice() == choice) {
foundItem = true;
// Handle not enough money, not enough amount, etc. or sell it
}
}
if(!foundItem) {
// Invalid entry
}
And this it our main:
public static void main(String s) {
// Initialize items in the vender
// Initialize other things needed
while(/* has items to sell */) {
// Read a double as customer's money
// `break;` if is a program-exit request
while(true) {
// Print current items
// Read a character as customer choice, to lower case
// `break;` if is an customer-exit request
// Handle the actual purchase request
}
// Print customer exit message
}
// Print program exit message
}
Well, it's your responsibility to fill in the blank.
It seems like your while(choice==...) loop never ends.
The variable choice is never altered within the loop and so once you get in you'll never come out.
You should be prompting the user to enter a new choice inside the loop.
Alright so, i got my programm "finished" up to the point where i cant solve this simple problem.
i got this:
package hotel;
import java.util.Scanner;
/**
*
* #author Defalt
*/
public class Hotel {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Prices prices = new Prices();
Scanner user_in = new Scanner(System.in);
String method;
boolean taken = false;
boolean taken2 = false;
boolean taken3 = false;
boolean again = true;
//***********Introduction to the hotel*************
System.out.println("Welcome to Hotel Vermond's Heaven!");
System.out.println("We are happy to see that you would like to stay with us.");
//***********Creating of a Loop*************
do {
System.out.println("Please, type which room you would like to book: ");
System.out.println("Single Bed, Double Bed, President Suit");
method = user_in.nextLine();
//***********Choices of Rooms and Question to book another one*************
if ("Single Bed".equals(method)) {
System.out.println(prices.describe1());
if ("y".equals(user_in.nextLine())){
if(taken=true){
System.out.println("We are sorry, this room is already booked. Please choose another one");
}else again=true;
} else again=true;
} else if ("Double Bed".equals(method)){
System.out.println(prices.describe2());
if ("y".equals(user_in.nextLine())){
taken2 = true;
again=true;
} else again=true;
} else if ("President Suit".equals(method)){
System.out.println(prices.describe3());
if ("y".equals(user_in.nextLine())){
taken3 = true;
again=true;
} else again=true;
} else {
System.out.println("Please choose one of the rooms above.");
}
System.out.println("Would you like to book another Room(y/n)\n");
//***********Outbreak if User declines another booking*************
//***********Otherwise redo the whole process*************
if ("y".equals(user_in.nextLine())){
again=true;
} else{
break;
}
}while(again);
System.out.println("Thank you and goodbye!");
}
}
and my other class is this:
package hotel;
import java.util.Scanner;
/**
*
* #author Defalt
*/
public class Prices {
Scanner user_in = new Scanner(System.in);
int price1 = 300;
int price2 = 800;
int price3 = 2500;
//***********Information of the Room including Validation Question*************
public String describe1() {
return
"You Choose the Single Bed.\n\tThis room Contains 1 Bed, 1 Fridge a Bathroom but no View on the Ocean.\n\tThis room will cost CHF " + price1 + ".-.\n\tWould you like to book this room?(y/n)";
}
public String describe2() {
return
"You Choose the Double Bed.\n\tThis room Contains 1 Queen-size Bed, 1 Fridge a bathroom, an Icemaker but no View on the Ocean.\n\tThis room will cost CHF " + price2 + ".-.\n\tWould you like to book this room?(y/n)";
}
public String describe3() {
return
"You Choose the President Suit.\n\tThis room Contains 1 King-size Bed, 1 Fridge, XXL Bathroom, Private Entertainment-System, 65inch Flatscreen and a Balcony with View on the Ocean.\n\tThis room will cost CHF " + price3 + ".-.\n\tWould you like to book this room?(y/n)";
}
}
As you can see i tried to make the Single Bed a reserved room after it has been taken. Yet the System.out.println("We are sorry, this room is already booked. Please choose another one"); is shown even on the first booking.
why is that eventhough the initial value of my taken boolean is false?
what other option are there that i could take to make my idea work?
PS!: I think i got it working the first time BUT it kept the taken value on true no matter how often i close the program and restart it. (just a side note)
You need to change
if(taken=true){
System.out.println("We are sorry, this room is already booked. Please choose another one");
to
if(taken==true){
System.out.println("We are sorry, this room is already booked. Please choose another one");
Note the double equals in the if condition. This will check the value of taken instead of just always setting it to true, which is what is currently happening.
Alternatively, since taken is already a boolean value, you can simplify it to this:
if(taken){
System.out.println("We are sorry, this room is already booked. Please choose another one");
It would probably help you to catch these problems if you fixed your formatting and indentation. I tend to avoid omitting the curly braces at any time. This is generally java standard formatting:
if(condition) {
if(condition) {
// do the thing
}
else {
// do the other thing
}
}
else if(condition) {
// do another thing
}
else {
// do the third thing
}
Notice how ifs/else ifs/elses at the same nesting level are also kept at the same indentation level. This helps a lot with keeping them straight.
Edit:
Another thing I would like to point out is that you have the again boolean but you aren't using it. Instead of breaking out of the loop if the user is finished, use the again boolean you have like you should be and set it false. That will cause the loop condition to fail and exit the loop nicely. Using break is ugly.
I'm writing a simple riddle program that reads a text file of riddles, stores that into an ArrayList, then reads a text file of answers to the riddles, and stores that into an ArrayList. Well, so far so good. My problem is, whenever the user answers one of the riddles, it's supposed to give a point for the correct answer, and nothing for the wrong answer. I tested it out, answered the riddle correctly, however the program instead said it was wrong. Now, some of the answers should accept more than 1 as the correct answer, for instance:
Q: What has eyes but cannot see?
A: A potatoe, a storm, a needle.
Let's say the user didn't think of 3 but just 1, and it was storm. I want the program to read storm and since it is contained in the answer for that, then it's correct. Here's my code.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/* Array of riddles with a separate array of answers */
class RiddlesProgram
{
public RiddlesProgram(){} //Empty constructor
public void riddleGame() throws FileNotFoundException
{
String ridFile = "Riddles.txt";
String ansFile = "Answers.txt";
Scanner ridReader = new Scanner(new File(ridFile));
Scanner ansReader = new Scanner(new File(ansFile));
/** ArrayLists for riddles and answers */
ArrayList<String> riddles = new ArrayList<String>();
ArrayList<String> answers = new ArrayList<String>();
/** Reading the Riddles & storing them in their array */
while(ridReader.hasNext())
{
riddles.add(ridReader.nextLine());
}ridReader.close();
/** Reading the Answers & storing them in their array */
while(ansReader.hasNext())
{
answers.add(ansReader.nextLine());
}ansReader.close();
Scanner in = new Scanner(System.in);
String answer = "";
System.out.println("Welcome to the Riddler!");
System.out.println("Let's start answering riddles..."); System.out.println();
System.out.println("Each riddle will require either a one word answer, or multiple word answer.");
System.out.println("Example: \nQ: How much would could a wood chuck, chuck?\n"
+ "A: As much wood as a wood chuck could chuck if a wood chuck would chuck wood.");
int count = 1;
int points = 0;
while(count!=16)
{
System.out.println("Riddle # " + count);
System.out.println(riddles.get(count));
System.out.println("\nAnswer? ");
answer = in.nextLine();
if(answers.contains(answer.toLowerCase()))
{
System.out.println("Correct! + 1 point.");
points += 1;
}//End if
else
{
System.out.println("Wrong! No points!");
}//End else
count++;
}//End while
}//End riddlegame
}//End class
Sample riddle text
I am lighter than a feather, yet no man can hold me for very long. What am I?
Three guys run into a bar, the fourth man ducks. Why does he duck?
How do you put a giraffe in a refrigerator?
How do you put an elephant in a refrigerator?
All of the animals go to a meeting for the Lion King. One animal doesnt show up. Which animal doesn't come?
You come to a river that aligators live in. There is no boat, raft, bridge, nor material to make them. How do you get accross?
A fifteen foot rope is tied to a horse. The horse is 25 feet from a stack of hay. How can the horse get to the hay?
From what number can you take half and leave nothing?
How can you drop an egg 3 feet without breaking it?
How can you make a fire with only one stick?
How can you tell the difference between a can of chicken soup and a can of tomato soup?
Can giraffes have babies?
What has four wheels and flies?
Feed me and I live, give me something to drink and I'll die. What am I?
What has eyes but cannot see?
When is a door not a door?
Sample answer text
Breath
He didn't want to hit the bar
Open the door, put him in, close the door
Open the door, take the giraffe out, put him in, close the door
The elephant, he's in the refrigerator
Jump in, swim accross, get out. The aligators are at the meeting
The rope isn't tied to anything but the horse
8. Take the top half away and the "o" is left
Drop it 4 feet, the first 3 feet the egg won't hit anything
Make sure it's a matchstick
Read the label
No, they have giraffes
A dumpster
Fire
A needle, a potatoe, a storm, or true lovers
When it's ajar
answers does not contain the string "storm". It contains a string which contains "storm". Retrieve the corresponding string from answers before checking String#contains(String) (rather than List#contains(Object), as you're doing now).
String correctAnswer = answers.get(count).toLowerCase();
if(correctAnswer.contains(answer.toLowerCase()))
{
System.out.println("Correct! + 1 point.");
points += 1;
}
I keep getting the following errors:
Cannot find symbol
Variable find
cannot find symbol
method getdata(int)
I am sure I am making this way more difficult than it is, but I am not sure how make this work so that the return from searching through the array, can be seen and evaluated by the if statement.
//assigns manager identification
manID = keyboard.nextInt();
//Fibonacci binary array for passwords
int[] passWArray = {00000000,00000001,00000001,00000010,00000011,00000101,00001000,00001101};
//item = find.getdata(manID);
if (getdata(manID) != -1)
{
//Do work here
dblPayRate = 10.85;
dblGrossPay = (intHours * dblPayRate) + (15.00);
dblTaxes = dblGrossPay * 0.19;
dblGrossPay -= dblTaxes;
//Print information to user
System.out.print("\n\n$" + df2.format(dblTaxes) +
" was withheld from this paycheck in taxes after working "+ intHours + " hours.\n\n");
System.out.print("The amount \"Employer Here\" owes you is $" + df2.format(dblGrossPay) + "\n");
}
else
{
// Dialog box for incorrect password
JOptionPane.showMessageDialog(null, "Invalid Entry! Contact the BOFH!");
//exits program (Note: needed for any JOptionPane programs)
System.exit(0);
}
}// end of long if statement for >50 hours
}//end of main method
public int find(int[] passWArray, int manID)
{
//search for manID in passWArray array
for (int index = 0; index < passWArray.length; index++)
if ( passWArray[index] == manID )
return manID;
//-1 indicates the value was not found
return -1;
}// end of find method
Change
if (getdata(manID) != -1)
into
if (find(passWArray , manID) != -1)
BTW those numbers don't magically become binary because they only contain 0's and 1's. Here's a hint:
int thirteen = Integer.parseInt("00001101", 2)
EDIT: in response to your next error
For now make the method static:
public static int find(int[] passWArray, int manID)
Eventually you might want to think about your 'Object-Oriented design' and just use the main() method as an entry point. Within main you create an instance of a class and let it do its work. In this way you can use the powers of O-O like encapsulation and inheritance and don't have to make everything static.
EDIT2: Afterthought
Your program seems to have the following 'actions':
user interaction
authentication
calculation
And there seem to be the following 'things' in your domain:
user
password
keyboard
display (command line and screen)
calculation
A good rule of thumb for an O-O design is to convert some of the 'things' and 'actions' already present in your domain into classes. A good class has a single responsibility and shares as little as possible of its data and methods with other classes (this is called information hiding).
Here's a class diagram that comes to mind:
User (represents a user, contains a single field 'password')
Authenticator (authenticates a user, contains the list of allowed passwords)
Console (all user interaction, either use System.out/in or Swing, but don't mix them)
Calculator (it calculates shit)