I'm in one of my first of many Java classes and we've just started working with writing classes. I understand the concept more or less, but I'm having trouble understand how to actually write them. In particular, we're working on an in class mini-game that asks to create a Player class to store scores; a name, username, number of games played, wins and losses. All which must be written to a file per username and updated with each play of the game.
We're asked to them write methods for:
a)Login – this will test a player's Id against a file (whose name is passed into the method as a parameter). If the id is found in the file the player's logged status will be marked as "true"; otherwise it will be marked as "false".
b)Register – this will test a player's Id against a file (whose name is passed into the method as a parameter). If the Id is not found in the file it will be added to the end of the file and the player's logged status will be marked as "true"; otherwise it will be marked as "false".
c)Update his/her wins and update his/her losses (since the main program will not be able to access the instance variables directly) - This is where I'm iffy on the use of files within methods and classes. Do I just write a method that says hey, open this file, read in the previous number recorded for numWins and the like, increment and rewrite?
d)Return a formatted String version of the player's information for output - I think I've got this with the return methods
e)Write the player's information back to the appropriate file (based on the player's Id).
I'm honestly not sure how to get these classes working and was wondering if someone might be able to summarize classes in a way that better explains them than my textbook.
I have a variable in my main program called numWins, etc. Do I even need that there if I have a variable numWins and the like in the Player class? And I'm particularly having trouble writing score back to the file. I would open the user's file first, read in the previous values of the variables then use FileWriter to append, but can I do this all through a class method? I'm guessing I'd then need a method to set the scores at 0 for a new player as well.
Here's what I have so far, which is basically a skeleton of sorts...
public class Player
{
private boolean loginStatus;
private int numWins;
private int numLosses;
private int gamesPlayed;
private String firstName;
private String lastName;
private String userName;
public setStats(int numWins, int numLosses, int gamesPlayed)
{
//initializes stats for a new player to the first game
//not sure if I'm doing this part right
wins = numWins ;
losses = numLosses;
totalPlayed = gamesPlayed;
}
public void updateWins();
{
//if player wins, update and rewrites score
//open userName.txt
//I still need to keep the username and Full Name though, not append, but not really rewrite
PrintWriter outputFile = new PrintWriter(fwriter);
//read num of wins already
outputFile.println(numWins++);
outputFile.println(gamesPlayed++);
outputFile.close();
//increment by 1
//rewrite
//close file
}
public void updateLosses();
{
//if player losses, update and rewrites score
//open userName.txt
PrintWriter outputFile = new PrintWriter(fwriter);
//read num of losses already
outputFile.println(numLosses++);
outputFile.println(gamesPlayed++);
outputFile.close();
//increment by 1
//rewrite
}
public int getWins()
{
return numWins;
}
public int getLosses()
{
return numLosses;
}
public int getGamesPlayed()
{
return gamesPlayed;
}
public String getID()
{
return userName;
}
public String getName()
{
return firstName + lastName;
}
public void displayStats();
{
System.out.println("Username: " + userName"\nName: " firstName +" " lastName"\nTotal Games Played: " + gamesPlayed"\nGames Won: " +
numWins"\nGames Lost: " + numLosses);
}
}
I'm honestly just lost on writing classes and any help would be appreciated. Thanks guys.

New Topic/Question
Reply



MultiQuote






|