. University of Harda W ork Raffle Objective: Review Jana by working uit h iff y
ID: 3854298 • Letter: #
Question
Explanation / Answer
Below is your code: -
Ticket.java
public class Ticket {
private String ticketNo;
private String name;
public Ticket() {
}
public Ticket(String ticketNo, String name) {
this.ticketNo = ticketNo;
this.name = name;
}
public String getTicketNo() {
return ticketNo;
}
public void setTicketNo(String ticketNo) {
this.ticketNo = ticketNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Raffle.java
import java.util.Scanner;
public class Raffle {
public static void main(String[] args) {
int firstwinners = 0, secondwinners = 0, thirdwinners = 0;
System.out.println(" Welcome to the Raffle Game ... !! ");
System.out.print(" Please enter the winning ticket number: ");
Scanner sc = new Scanner(System.in);
String winTickNo = sc.next();
boolean done = false;
String name, ticketNo;
Ticket tic;
while (!done) {
System.out.print(" Enter the name of holder: ");
name = sc.next();
System.out.print("Enter 6 digit ticket number: ");
ticketNo = sc.next();
tic = new Ticket(ticketNo, name);
if (tic.getTicketNo().equals(winTickNo)) {
firstwinners++;
System.out.println(tic.getName() + " has won first prize.");
} else if (new StringBuffer(tic.getTicketNo()).reverse().toString().equals(winTickNo)) {
secondwinners++;
System.out.println(tic.getName() + " has won second prize.");
} else if (tic.getTicketNo().substring(0, 3).equals(winTickNo.substring(0, 3))
|| tic.getTicketNo().substring(1, 4).equals(winTickNo.substring(1, 4))
|| tic.getTicketNo().substring(2, 5).equals(winTickNo.substring(2, 5))
|| tic.getTicketNo().substring(3, 6).equals(winTickNo.substring(3, 6))) {
thirdwinners++;
System.out.println(tic.getName() + " has won third prize.");
}
System.out.print(" Do you have more tickets to check: (y/n): ");
String op = sc.next();
if(!op.equals("y") && !op.equals("Y")) {
done = true;
}
}
System.out.println(" Total number of first prize winners: "+firstwinners);
System.out.println("Total number of second prize winners: "+secondwinners);
System.out.println("Total number of third prize winners: "+thirdwinners);
sc.close();
}
}
Sample run: -
Welcome to the Raffle Game ... !!
Please enter the winning ticket number: 123456
Enter the name of holder: sal
Enter 6 digit ticket number: 123456
sal has won first prize.
Do you have more tickets to check: (y/n): y
Enter the name of holder: ili
Enter 6 digit ticket number: 654321
ili has won second prize.
Do you have more tickets to check: (y/n): y
Enter the name of holder: ban
Enter 6 digit ticket number: 123789
ban has won third prize.
Do you have more tickets to check: (y/n): y
Enter the name of holder: sal
Enter 6 digit ticket number: 456987
Do you have more tickets to check: (y/n): y
Enter the name of holder: sasa
Enter 6 digit ticket number: 823456
sasa has won third prize.
Do you have more tickets to check: (y/n): n
Total number of first prize winners: 1
Total number of second prize winners: 1
Total number of third prize winners: 2
Let me know if you need anything else in this in comments
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.