Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods:
Write a GameDriver program that uses Game objects to track the sales of games sold at a variety of stores.
import java.util.Scanner;
public class Game {
/*
* Instance variable in Our Game class
*/
private String gameName;
private int numSold;
private double priceEach;
/*
* Getters and setters for each field
*/
public String getGameName() {
return gameName;
}
public void setGameName(String gameName) {
this.gameName = gameName;
}
public int getNumSold() {
return numSold;
}
public void setNumSold(int numSold) {
this.numSold = numSold;
}
public double getPriceEach() {
return priceEach;
}
public void setPriceEach(double priceEach) {
this.priceEach = priceEach;
}
/*
* Default Constructor
*/
public Game() {
}
/*
* Parameterized Constructor contains two variables as a parameter
*i.e. GameName and Price of Each Game.
*
*/
public Game(String gameName, double priceEach) {
this.gameName=gameName;
this.priceEach=priceEach;
numSold=0;
}
/*
* This method basically counts the number of particular game sold from all stores
*/
public void updateSales(int additional) {
numSold=numSold+additional;
}
/*
* Total Earning from the every sales market for a particular game
*/
public double totalValue() {
return (priceEach*numSold);
}
/*
* toString method to print output
*/
@Override
public String toString() {
String result=gameName+"\t"+numSold+"\t$"+priceEach+"\t$"+totalValue();
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
/*
* Total games and stores available in market
*/
System.out.println("How many different types of games are being sold");
int totalGame=sc.nextInt();
System.out.println("How many stores");
int stores=sc.nextInt();
/*
*For each game we used this for loop.
*and get the name,price of each game and use our contructors in it.
*We also create an object of particular class.
*
*/
for (int i = 0; i < totalGame; i++) {
System.out.println("Game name");
/*
* if you String contains whitespace then we have to scan complete line.
*/
sc.nextLine();
String gN=sc.nextLine();
System.out.println("price");
double price=sc.nextDouble();
Game game = new Game(gN,price);
/*
* This loop is used to count the total number of sales for a particular product
*/
for (int j = 0; j < stores; j++) {
System.out.println("Enter sale from this store");
int sales=sc.nextInt();
game.updateSales(sales);
}
System.out.println(game.toString());
}
}
}
The explaination of code is with the program. I hope you like the solution .Support us by pressing that upvote button.
Get Answers For Free
Most questions answered within 1 hours.