So basically I am done with my project, I've passed to our instructor already, and I've achieved an above average grade, but one thing though I am still not satisfied, can someone check this code out for me? give me few tips? ? or tricks about this? here's my code :) rate it and help me improve it (I want to improve it so that I could be a good java Programmer, and again I alraedy passed this project)

import java.util.Random;

public class Game {

	Player player = new Player();

	Banker banker = new Banker();

	private int a = 0;

	private int b = 6;

	private double myAmount = 0;

	private double offer = 0;

	private int turn = 1;

	private int cases = 26;

	private double amounts[] = { 23, 1, 5, 10, 25, 50, 75, 100,
			200, 300, 400, 500, 750, 1000, 5000, 10000, 25000, 50000, 75000,
			100000, 300000, 400000, 500000, 750000, 1000000, 250000, 800 };

	private String models[] = { "Nayer", "Michelle", "Obama", "Rosey", "Miney",
			"Ashley", "Maria", "Ozawa", "Audrey", "Kristen", "Kim",
			"Kardashian", "Kourtney", "Ann", "Macy", "Tori", "Sam", "Monica",
			"Jin", "Koi", "jill", "Usher", "Justin Bieber", "Lindsay Lohan",
			"Hazell", "Buttercup", "Don Amalia", "Kojic!" };

	Briefcase briefcase[] = new Briefcase[27];

	Model lady[] = new Model[27];

	public void setladies() {
		for (int i = 0; i < lady.length; i++) {
			lady[i] = new Model();
			String name = models[i];
			lady[i].setName(name);
		}
	}

	public void Shuffle() {

		Random rgen = new Random();
		for (int i = 0; i < amounts.length - 1; i++) {
			int Position = rgen.nextInt(amounts.length);
			double temp = amounts[i];
			amounts[i] = amounts[Position];
			amounts[Position] = temp;
		}
	}

	public void casesSetup() {
		Shuffle();
		for (int i = 0; i < briefcase.length; i++) {
			if (i == 0) {
			} else {
			}
			briefcase[i] = new Briefcase();
			double value = amounts[i];
			briefcase[i].setAmount(value);
			briefcase[i].setFace(i);
		}
	}

	public void showCases() {
		for (int a = 0; a < briefcase.length; a++) {
			if (a == 0) {
			} else if (briefcase[a] == null) {
				System.out.print("\t[X]");
			} else {
				System.out.print("\t[" + briefcase[a].getFace() + "]");
			}
			if (a % 5 == 0) {
				System.out.println();
			}
		}

		System.out.println();
	}

	public void Welcome() {
		System.out.println("\t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
		System.out.println("\t~*               Welcome !          ~*");
		System.out.println("\t~*~*~*~*~* Hosted by Kyel David ~*~*~*~*~*~*");
		System.out.println("\t~* Please Select from the Following Cases!~*");
		System.out.println("\t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
	}

	public void starGame() {

		boolean gamestatus = true;
		casesSetup();
		Welcome();
		showCases();
		setladies();

		int choice = player.nUser();
		myAmount = briefcase[choice].getAmount();
		briefcase[choice] = null;
		cases--;

		while (gamestatus == true) {
			showCases();
			if (cases == 25 || cases == 19 || cases == 14 || cases == 10
					|| cases == 7) {
				for (a = b; a > 0; a--) {
					int r = player.Remove(a, briefcase, models);
					briefcase[r] = null;
					cases--;
				}
				b--;
				turn++;
				banker.setOffer(turn,briefcase,myAmount);
				offer = banker.getOffer(turn, briefcase, myAmount);
				gamestatus = player.gamestatus();
			} else if (cases == 1) {
				int r = player.Remove(1, briefcase, models);
				briefcase[r] = null;
				gamestatus = false;
			} else {
				int r = player.Remove(1, briefcase, models);
				briefcase[r] = null;
				cases--;
				banker.setOffer(turn,briefcase,myAmount);
				offer = banker.getOffer(turn, briefcase, myAmount);
				gamestatus = player.gamestatus();
			}
		}
		finishgame();
	}

	public void finishgame() {
		if (cases == 1) {
			System.out.println("\tYou Rejected the Offer of Banker");
			System.out
			.printf("\tYour case contains $%.2f and the bankers offer is $%.2f\n",
					myAmount, offer);
			System.out.printf("\tYou've won your case with an amount of: %.2f",
					myAmount);
		} else {
			System.out.println("\tYou Accepted the offer of Banker");
			System.out
			.printf("\tYour case contains $%.2f and the bankers offer is $%.2f\n",
					myAmount, offer);
			System.out.printf("\tYou've won the offer of Banker: $%.2f", offer);
		}
	}
}

The Player class

import java.util.Scanner;

public class Player {

	Scanner input = new Scanner(System.in);
	Banker banker = new Banker();

	public boolean gamestatus() {
		System.out.print("\tAccept or Reject! [1]Accept [2]reject: ");
		int temp = input.nextInt();
		System.out.println();
		if (temp == 1) {
			return false;
		} else {
			return true;
		}
	}

	public int nUser() {

		boolean isOkay = false;
		int nUser = 0;
		while (isOkay == false) {
			System.out.print("\n\tPlease Select Your Case!: ");
			nUser = input.nextInt();
			if (nUser < 0 || nUser >= 27) {
				System.out.println("\tInvalid input Try again");
			} else {
				isOkay = true;
			}
		}
		return nUser;
	}

	public int Remove(int i, Briefcase c[], String[] m) {

		int nChoice = 0;
		boolean inputisok = false;

		while (inputisok == false) {
			System.out.print("\tPlease remove " + i + " case/s: ");	
			nChoice = input.nextInt();
			if (nChoice < 0 || nChoice >= c.length || c[nChoice] == null) {
				System.out.println();
				System.out.println("\tInvalid Input please Try again\n");
			} else {
				System.out.println("\tI'm " + m[nChoice]
						+ " You just removed case # " + nChoice);
				System.out.println("\t|" + nChoice + "| contains $"
						+ c[nChoice].getAmount() + "\n");
				inputisok = true;
			}

		}
		return nChoice;
	}
}

The banker class

public class Banker {
	
	private double total = 0;
	private int a = 0;
	private double amount =0;
	double Average = 0;
	
	public void setOffer(int turn, Briefcase[] cases, double myAmount) {

		for (int i = 0; i < cases.length; i++) {
			if (cases[i] == null) {
			} else {
				total = total + cases[i].getAmount();
				a++;
			}
		}
		Average = myAmount+total / a;
		amount = Average*turn/ 10;
	}

	public double getOffer(int turn, Briefcase[] cases, double myAmount) {
		setOffer(turn, cases, myAmount);
		System.out.printf("\tThe Bankers Offer is: %.2f \n\n", amount);
		return amount;
	}
}

Mode Class

public class Model {

	private String mName;

	public void setName(String mName) {

		this.mName = mName;
	}

	public String getName() {
		return mName;
	}

}

The briefcase class

public class Briefcase {

	private double amount;
	private int face;

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public double getAmount() {

		return this.amount;
	}

	public void setFace(int face) {
		this.face = face;
	}

	public int getFace() {
		return face;
	}

}

and the main class

public class Play {

	public static void main(String[] args) {
		Game dnd = new Game();
		dnd.starGame();
	}
}

Recommended Answers

All 9 Replies

i am also a beginner in Java so this code is tough for me but then also i would like to advice you that why are you increasing your LOC(Line of Code) by doing

private int a = 0;
 
	private int b = 6;
 
	private double myAmount = 0;
 
	private double offer = 0;
 
	private int turn = 1;
 
	private int cases = 26;

When it can be done in 2 lines and with clarity i.e.

private int a = 0, b = 6,turn = 1, cases = 26;

and similarly for double. And one more thing we know we can declare array in 2 ways i.e.

int[] a;

or

int a[];

but you know the first one is always preferred by most of the programmers.

^ yes.
int[] a is also documented in the official tutorials

You can also place the square brackets after the array's name:
float anArrayOfFloats[]; // this form is discouraged
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.

http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

ps Calling a variable "a" is a bad idea. Now you need a comment to explain what it's for. Names like myAmount or turn are far better.

^ yes.
int[] a is also documented in the official tutorials

http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

ps Calling a variable "a" is a bad idea. Now you need a comment to explain what it's for. Names like myAmount or turn are far better.

Regarding the declarations, I did that because, I am having hard time looking at them going to the right, I have a poor eye sight, it's more of a own thing. Hope nothing personal there. next is a? a is a counter variable, you see the value of a = b , which has a value of 6, thus each time it loops. the value of a will decrease after that block of code, variable b will be decrease thus a will to, I am having hard time explaing this, here's the code where I implemented it

and also I did that because, for each round, the user will selec 6 cases, 5 , 4, 3,2. so that's why I made it a = b , which b has a value of 6, sorry for my bad english I am having a hard time explaining it but here it is

while (gamestatus == true) {
			showCases();
			if (cases == 25 || cases == 19 || cases == 14 || cases == 10
					|| cases == 7) {
				for (a = b; a > 0; a--) {
					int r = player.Remove(a, briefcase, models);
					briefcase[r] = null;
					cases--;
				}
				b--;

If I understand that correctly then you could make it a lot clearer by renaming "b" as (eg) casesToRemove or something more descriptive. "a" just seems to be a local/temporary variable, which would be clearer if you just declared it where it was needed. eg

...
int casesToRemove = 6; // reduces each time used
...
    for (int count= casesToRemove, count>0; count++) { ...
    ...
casesToRemove--;

If I understand that correctly then you could make it a lot clearer by renaming "b" as (eg) casesToRemove or something more descriptive. "a" just seems to be a local/temporary variable, which would be clearer if you just declared it where it was needed. eg

...
int casesToRemove = 6; // reduces each time used
...
    for (int count= casesToRemove, count>0; count++) { ...
    ...
casesToRemove--;

Oh Good call, Okay done, I've edited it, what more do I need? to edit >? how about the codes in the while loop?

I don't know the game this is modelling, so I don't understand the code, but nothing jumps out at me as being silly.
But while we're talking about variable names, good names for booleans describe the "assertion" that can be true or false, which normally begin with "is", eg isVisible, isValidData. So how can I understand if (gameStatus == true) ?
Maybe what this means is if (gameIsFinished == false) ?

IMHO chosing the names for variables and methods is an essential skill - meaningful and explanatory names make the code readable, and eliminate the need for lots of detailed comments. They can also make it obvious when there's a logic error, eg if (gameStatus == false) ... is this wrong? Who knows? if (gameIsFinished == true) ... looks funny - why do stuff if the game is finished?

I don't know the game this is modelling, so I don't understand the code, but nothing jumps out at me as being silly.
But while we're talking about variable names, good names for booleans describe the "assertion" that can be true or false, which normally begin with "is", eg isVisible, isValidData. So how can I understand if (gameStatus == true) ?
Maybe what this means is if (gameIsFinished == false) ?

IMHO chosing the names for variables and methods is an essential skill - meaningful and explanatory names make the code readable, and eliminate the need for lots of detailed comments. They can also make it obvious when there's a logic error, eg if (gameStatus == false) ... is this wrong? Who knows? if (gameIsFinished == true) ... looks funny - why do stuff if the game is finished?

How about gameContinue? I'll change it to if(gameContinue == true) the whole loop will still run if its false it will sotp how about that? any more corrections to be made?

gamestatus = player.gamestatus();
			} else if (cases == 1) {
				int r = player.Remove(1, briefcase, models);
				briefcase[r] = null;
				gamestatus = false;
			} else {
				int r = player.Remove(1, briefcase, models);
				briefcase[r] = null;
				cases--;
				banker.setOffer(turn,briefcase,myAmount);
				offer = banker.getOffer(turn, briefcase, myAmount);
				gamestatus = player.gamestatus();
			}

how about these?

How about gameContinue? I'll change it to if(gameContinue == true) the whole loop will still run if its false it will sotp how about that? any more corrections to be made?

It's up to you. That certainly makes more sense to me that gameStatus.
nb if (someBoolean == true) is just a long way to write if (someBoolean) As for the final code snippet - I don't know what its supposed to do so I can't offer any real insight. It looks like reasonable code to me.
If you want to stick to normal coding conventions then gamestatus = player.gamestatus(); should be gamestatus = player.getGamestatus(); I'm all done with comments now. Maybe someone else has some suggestions.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.