Why cant i increase a value with 1?
Hi
I am trying to generate a userID by increasing the value by one with ++ for each id that is created.
But it does not work.
If you have time look over my code, it would make me happy :)
put everything in a zip file
mmmm.rar - Speedy Share - upload your files here
Re: Why cant i increase a value with 1?
Could you perhaps instead post the part of the code that doesn't work? I know I'm not too keen on downloading an unknown zip-file, and I wouldn't be surprised if I'm not the only one...
Re: Why cant i increase a value with 1?
true... only thing is that i guess the error could be somewhere els in the code.
but here goes.
here is my user file
Code:
import java.util.Calendar;
public class User {
// here are the stuff needed to state a person
protected String fname;
protected String lname;
protected static int birthYear;
protected int currentAge;
protected static int userID;
// each variable is private so no one can tamper with them.
// now we create constructor of each variable.
public User(String fname, String lname, int birthYear, int userID) {
this.fname = fname;
this.lname = lname;
User.birthYear = birthYear;
User.userID = userID;
}
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public static int getBirthYear() {
return birthYear;
}
public int currentAge() {
Calendar toDay = Calendar.getInstance();
currentAge = toDay.get(Calendar.YEAR);
return currentAge - birthYear;
}
public static int getUserID(){
return userID++;
}
}
and here is my main file that print the info.
Code:
public class Bank_System {
public static void main(String[] args) {
// these are the classes the bank system will use/need.
Customer customer1 = new Customer("Oskar", "Kuus", 1983, 1000, "Customer");
Address address1 = new Address("Helgdagsvägen", "15", "123 60",
"Farsta");
Account account1 = new Account(200);
// person1, customer1, account1
// print out the data from above classes
System.out.println(customer1);
System.out.println();
System.out.println(address1);
System.out.println();
System.out.println(account1);
// information is printed out, about person1. This can be duplicated by
// just copy above code
// and change the details at the top where person1 is stated.
// and of course change person1 to person2 and so on.
// ---------------------------------------------
System.out.println("\n----------------------\n");
// ---------------------------------------------
Teller teller1 = new Teller("Theodor", "Kuus", 2009, 1000, "Tellar");
Address address2 = new Address("Kungsgatan", "32b", "111 82", "Umeå");
Account account2 = new Account(100);
System.out.println(teller1);
System.out.println();
System.out.println(address2);
System.out.println();
System.out.println(account2);
// ---------------------------------------------
System.out.println("\n----------------------\n");
// ---------------------------------------------
Teller teller2 = new Teller("Olle", "Adolphsson", 1973, 1000, "Tellar");
Address address3 = new Address("Asksvängen", "1", "186 78", "Fruängen");
Account account3 = new Account(100);
System.out.println(teller2);
System.out.println();
System.out.println(address3);
System.out.println();
System.out.println(account3);
// ---------------------------------------------
System.out.println("\n----------------------\n");
// ---------------------------------------------
}
}
so what i want is my userID to increase by 1 for each user.... but it stays the same for all...
u might want to see the sub-class aswell. so here is the one for person.
Code:
public class Customer extends User{
private String role;
public Customer(String fname, String lname, int birthYear, int userID, String role){
super(fname, lname, birthYear, userID);
this.role = role;
}
public String getRole() {
return role;
}
public String toString(){
return "Hello " + fname + " " + lname + "\n" + "You where born "
+ birthYear + " and that means you are " + currentAge()
+ " year.\n" + "Here is your randomly generated userID: "
+ "0001" + birthYear + userID + "\n" + "You are a: " + role + "\n" + "The Bank is here to serve you!";
}
}
Re: Why cant i increase a value with 1?
Well... For one, I don't see User actually being used in any code. Does Teller and Customer extend User or something?
Anyway, problem 2: userID is static. It should not be if you want a different ID for each user.
Re: Why cant i increase a value with 1?
oh im sorry
i gave u the wrong part...
person class is not part of this
user is superclass of teller and customer
here is the customer code that i replaced in the other topic with the person code.
Code:
public class Customer extends User{
private String role;
public Customer(String fname, String lname, int birthYear, int userID, String role){
super(fname, lname, birthYear, userID);
this.role = role;
}
public String getRole() {
return role;
}
public String toString(){
return "Hello " + fname + " " + lname + "\n" + "You where born "
+ birthYear + " and that means you are " + currentAge()
+ " year.\n" + "Here is your randomly generated userID: "
+ "0001" + birthYear + userID + "\n" + "You are a: " + role + "\n" + "The Bank is here to serve you!";
}
}
Re: Why cant i increase a value with 1?
and now i have removed the static parts of userID
line 21 is now this.userID = userID;
in user.java
and line 46 is now public int getUserID(){
return userID++;
in user.java
and still it does not work
Re: Why cant i increase a value with 1?
Where do you specify which userID they will have? All I see of it is in super(fname, lname, birthYear, userID); which would set it to the default value (and it would thus be the same for everyone). Add the userID to the constructor in Customer and Teller (I assume you want them to have different userIDs) and add a counter in the Bank_System class to keep track of which userID to use next.
Re: Why cant i increase a value with 1?
oh i see...
aded them to the constructor... in customer and teller
but how do i do that ocunter thing in bank_system?
not sure what you mean that would be :)
Re: Why cant i increase a value with 1?
Make a variable in Bank_System that keeps track of the last userID. When a new teller or customer is made, use that to get the next userID.
Re: Why cant i increase a value with 1?
sry but i need an example of the code to even have a chance to get close to it :)
the print out is stated in the toString in teller or customer
and all i understand is that i enter the value in bank_system of each variable.
so if i where to make a variable called id in bank_system
int id;
then i would guess make a new method for this...
but im randomly guessing lol
Re: Why cant i increase a value with 1?
Okay... Reading your code again, I noticed that you actually _had_ a userID in there (unless you edited your previous posts?). Which means the real question becomes... why are you using the same userID when you create the tellers and customers?
If this is part of a school assignment (which I'm starting to think it is), could you at least post what, more precisely, you're supposed to do with it?
Re: Why cant i increase a value with 1?
it is part of a school assignment but im just trying to learn it by doing it on my own...
so there is no real description of the assignment.
i have not edited the post above
except what i said when i aded the code form the customer file.
when the info is printed
it gives the userid a 0001 or 0002 value depending if you are a teller or customer.
then it gives the birthYear of the user (teller and customer)
then i want a individual number for each user, so i tried to make 1000 go +1 for each user.
so now i am stuck.
i cant get the increase of 1 to work
and i guess it is more a big error in how i have made the code rather than a specific code error somewhere.
thats why i posted the .rar file with everything first.
Re: Why cant i increase a value with 1?
Look over where you create the tellers and customers. You specify the same userID to everyone there. Just change those numbers and they'll get different userIDs?
Re: Why cant i increase a value with 1?
sure i could
that would work just add a new number for each user.
but that takes away the idea of having automatically generated userID´s from the user information and random number.
but i see why that would work since i need to enter a new users info
i might aswell give him a new number
but i dont know if i give a duplicate number if i create many users.
so i would rather be able to make sure there are no users that are alike
even if they have the same name and same address.
Re: Why cant i increase a value with 1?
Then use a variable to create them. Make a new int at the start of your main method that contains your starting userID, and everytime you add a user, you add one to that variable. No need to create other methods or anything; just a single variable added.
Re: Why cant i increase a value with 1?
oke so i create a new int
int realuserID;
realuserID = 1000;
realuserid++;
all this at the start of the main class in bank_system.
then i just type realuserID instead of the value 1000 in the code.
but part of what i was trying to make is work with other classes and private/protected variables.
now this variable is unprotected.
Re: Why cant i increase a value with 1?
Protected and private doesn't matter there, since it's in a method. No other class or method can even see it.
Re: Why cant i increase a value with 1?
thanks, this solved it.
got this code now in main
Code:
public class Bank_System {
public static void main(String[] args) {
int id = 1000;
// these are the classes the bank system will use/need.
Customer customer1 = new Customer("Oskar", "Kuus", 1983, id++, "Customer");
Address address1 = new Address("Helgdagsvägen", "15", "123 60",
"Farsta");
Account account1 = new Account(200);
// person1, customer1, account1
// print out the data from above classes
System.out.println(customer1);
System.out.println();
System.out.println(address1);
System.out.println();
System.out.println(account1);
// information is printed out, about person1. This can be duplicated by
// just copy above code
// and change the details at the top where person1 is stated.
// and of course change person1 to person2 and so on.
// ---------------------------------------------
System.out.println("\n----------------------\n");
// ---------------------------------------------
Teller teller1 = new Teller("Theodor", "Kuus", 2009, id++, "Tellar");
Address address2 = new Address("Kungsgatan", "32b", "111 82", "Umeå");
Account account2 = new Account(100);
System.out.println(teller1);
System.out.println();
System.out.println(address2);
System.out.println();
System.out.println(account2);
// ---------------------------------------------
System.out.println("\n----------------------\n");
// ---------------------------------------------
Teller teller2 = new Teller("Olle", "Adolphsson", 1973, id++, "Tellar");
Address address3 = new Address("Asksvängen", "1", "186 78", "Fruängen");
Account account3 = new Account(100);
System.out.println(teller2);
System.out.println();
System.out.println(address3);
System.out.println();
System.out.println(account3);
// ---------------------------------------------
System.out.println("\n----------------------\n");
// ---------------------------------------------
}
}
tho i think my teacher will have something to say about it