bellow are the questions
(a) Write a complete Java class definition named PrepaidCard to model a game arcade centre‘s prepaid token card. The class information is as described below:
Class name: PrepaidCard
Attributes: private String cardID
private int tokenBalance
Constructors:public PrepaidCard(String id)
Purpose: This constructor has a parameter, which is id (type String). This constructor will instantiate this class object by assigning the parameter value to the object’s cardID attribute.
public PrepaidCard(String id, int token)
Purpose: This constructor has two parameters; id (type String) and token (type int). This constructor will instantiate this class object by assigning the parameter values to the object’s cardID and tokenBalance attributes respectively.
Member
Methods: public void addToken(int token)
Purpose: To add the value of the method parameter to the object’s tokenBalance attribute. This method will display the number of tokens successfully added.
public void deductToken(int token)
Purpose: To deduct the value of the object’s tokenBalance attribute based on the parameter value. This method should have a checking mechanism. If the value of the object’s tokenBalance is lower than the token value (parameter value) to be deducted, notify the user that the PrepaidCard object has insufficient token, else deduct the token value from the object’s tokenBalance and display the total token successfully deducted.
public String getCardID()
Purpose: To return the value of the object’s cardID.
public int getTokenBalance()
Purpose: To return the value of the object’s tokenBalance.
so this is what i did
import java.util.Scanner; public class PrepaidCard { private String cardID; private int tokenBalance = 0; Scanner addToken = new Scanner(System.in); Scanner delToken = new Scanner(System.in); public PrepaidCard(String id){ //initiate class object by assigning value to id id = cardID; } public PrepaidCard(String id,int token){ //initiate class object by assigning value to id and token id = cardID; token = tokenBalance; } public void addToken(int token){ //add token to balance System.out.println("Enter Token Addition Amount : "); token = addToken.nextInt(); tokenBalance = tokenBalance + token; System.out.println("Total of : " + addToken + " have been deducted"); System.out.println("Card Token Balance : " + tokenBalance); } public void deductToken(int token) { //deduct token from balance System.out.println("Enter Token Deduction Amount : "); token = delToken.nextInt(); if (token < tokenBalance) { tokenBalance = tokenBalance - token; System.out.println("Total of : " + delToken + " have been deducted"); System.out.println("Card Token Balance : " + tokenBalance); } else { System.out.println("Insufficient Token Amount"); } } public String getcardID(){ //return cardID value System.out.println("Identification: " + cardID); return cardID; } public int gettokenBalance(){//return tokenBalance value System.out.println("Card Token Balance : " + tokenBalance); return tokenBalance; } }
(b) Write a Java application to test the PrepaidCard class. In your application, create two PrepaidCard objects named card1 and card2 using the PrepaidCard(String id) and the PrepaidCard(String id, int token) constructors respectively. Display both objects’ cardID and tokenBalance. Next, ask the user to add token for card2 and display card2 new tokenBalance value. Deduct some token values from card2 and display the card2 new tokenBalance. Finally, deduct a much higher token value from the card2 current tokenBalance to test the checking mechanism of the tokenBalance value. You may use the Scanner class to read the input from user. A sample of the program execution is given below for your reference:
Enter Card1 cardID: A123
Enter Card2 cardID: B456
Enter Card2 initial token: 100
Card1 ID: A123
Card1 token balance: 0
Card2 ID: B456
Card2 token balance: 100
Enter total token to be added into Card2: 50
Total of 50 tokens added
Card2 token balance: 150
Enter total token to be deducted from Card2: 80
Total of 80 tokens deducted
Card2 token balance: 70
Enter total token to be deducted from Card2: 100
Insufficient token. Please top-up
Card2 token balance: 70
This is what i did
import java.util.Scanner; import java.io.*; public class ApplicationPrepaidCard { public static void main(String[] args) throws IOException { Scanner initialToken = new Scanner(System.in); Scanner readID = new Scanner(System.in); System.out.println("Enter Card1 Identification : ");//enter card1 String id = readID.nextLine(); PrepaidCard card1 = new PrepaidCard(id); System.out.println("Enter Card2 Identification : ");//enter card2 String id = readID.nextLine(); System.out.println("Enter Card2 Initial Token : ");//enter initial int token = initialToken.nextInt(); PrepaidCard card2 = new PrepaidCard(id,token); card1.getcardID(); card1.gettokenBalance(); card2.getcardID(); card2.gettokenBalance(); card2.addToken(); card2.delToken(); card2.delToken(); } }
i have errors at
1) Duplicate local variable id ()
String id = readID.nextLine();
2) The method addToken(int) in the type PrepaidCard is not applicable for the arguments ()
card2.addToken(); card2.delToken(); card2.delToken();
i am stuck and i don't know what to do next...