You are on page 1of 9

CSE-3002

Programming in Java

Challenging task 1 -
Problem Statement

Requirement specifications

UML Diagram

Code

Test Output Screenshots

Problem Statement -

Designing an application that can be used to either vote for a dish to be made by multiple
family members, or can be used by an admin ( here known as ‘mom’) to either monitor
all the votes being entered or change the dishes available for voting.

Requirement specifications -

6 Dishes are required.


8 Family Members have been accommodated in the program.
UML Diagram -

Code -
package pp;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import javax.swing.*;

class Dish{
public String[] TypeOfDish= { "Chhole Bhature","Samosa","Matar
Paneer","Pizza","Lasagna","Pasta"};
public int NoOfDishes = TypeOfDish.length;
public void select(List<Integer> list, Integer[] array) {
int n = array.length;
final HashMap<Integer, Integer> mapCount = new HashMap<Integer, Integer>();
final HashMap<Integer, Integer> mapIndex = new HashMap<Integer, Integer>();
for (int i = 0; i < n; i++) {
if (mapCount.containsKey(array[i])) {
mapCount.put(array[i],mapCount.get(array[i]) + 1);
}
else {
mapCount.put(array[i],1);
mapIndex.put(array[i],i);
}
}

Collections.sort(list, new Comparator<Integer>() {


public int compare(Integer n1, Integer n2)
{
int freq1 = mapCount.get(n1);
int freq2 = mapCount.get(n2);
if (freq1 != freq2) {
return freq2 - freq1;
}
else {
return mapIndex.get(n1) - mapIndex.get(n2);
}
}
});
System.out.println(Arrays.toString(array));
String s1 = String.format("The chosen dish is %s",TypeOfDish[array[0]-1]);
JOptionPane.showMessageDialog(null,s1);
}
}

class FamilyMember{
private String NameOfFamilyMember;
public int choice;
FamilyMember(String NameOfFamilyMember){
this.NameOfFamilyMember = NameOfFamilyMember;
this.choice = 1;
}
public String Get_name() {
return NameOfFamilyMember;
}
public void Set_Choice(int choice) {
this.choice = choice;
}
public void Vote(Dish ds) {
String z = String.format("Hello %s \nEnter Number To Select your Dish \n 1 for %s \n 2
for %s \n 3 for %s \n 4 for %s \n 5 for %s \n 6 for
%s",NameOfFamilyMember,ds.TypeOfDish[0],ds.TypeOfDish[1],ds.TypeOfDish[2],ds.TypeOfDish[3],d
s.TypeOfDish[4],ds.TypeOfDish[5]);
String choice = JOptionPane.showInputDialog(z);
int x = Integer.parseInt(choice);
Set_Choice(x);
}
}

class Mom{
FamilyMember a[];
Dish d;
Mom(Dish d,FamilyMember ... a){
this.a = a;
this.d = d;
}
public void ShowVote() {
String z = String.format("The Votes are as follows \n %s has voted for %s \n %s has
voted for %s \n %s has voted for %s \n %s has voted for %s \n %s has voted for %s \n %s has voted for
%s \n %s has voted for %s \n %s has voted for %s",
a[0].Get_name(),d.TypeOfDish[a[0].choice-1],a[1].Get_name(),d.TypeOfDish[a[1].choice-1],a[2].Get_na
me(),d.TypeOfDish[a[2].choice-1],a[3].Get_name(),d.TypeOfDish[a[3].choice-1],a[4].Get_name(),d.Type
OfDish[a[4].choice-1],a[5].Get_name(),d.TypeOfDish[a[5].choice-1],a[6].Get_name(),d.TypeOfDish[a[6]
.choice-1],a[7].Get_name(),d.TypeOfDish[a[7].choice-1]);
JOptionPane.showMessageDialog(null,z);
}
public void ChangeDishes() {
for(int i = 0; i<d.NoOfDishes; i++)
d.TypeOfDish[i] = JOptionPane.showInputDialog("Enter Dish no. " +(i+1));
}
}
public class DishSelector {
public static void main(String args[]) {
FamilyMember a = new FamilyMember("Mom"),b = new FamilyMember("Dad"),c =
new FamilyMember("Son"),d = new FamilyMember("Daughter"),e = new
FamilyMember("Grandmother"),f = new FamilyMember("Grandfather"),g = new
FamilyMember("Uncle"),h= new FamilyMember("Aunt");
Dish ds = new Dish();
Mom m = new Mom(ds,a,b,c,d,e,f,g,h);
int k1=1,x;
String z;
Integer[] array;
do {
z = JOptionPane.showInputDialog("Enter 0 to Enter Voting Mode, or press 1 to
enter Mom mode");
x = Integer.parseInt(z);
int k=1;
if(x==0) {
do {
switch(k) {
case 1: a.Vote(ds);
break;
case 2: b.Vote(ds);
break;
case 3: c.Vote(ds);
break;
case 4: d.Vote(ds);
break;
case 5: e.Vote(ds);
break;
case 6: f.Vote(ds);
break;
case 7: g.Vote(ds);
break;
case 8: h.Vote(ds);
break;

}
k++;

}while(k<9);
array = new Integer[]
{a.choice,b.choice,c.choice,d.choice,e.choice,f.choice,g.choice,h.choice};
List<Integer> list = Arrays.asList(array);
ds.select(list, array);
}
else {

String z4 = JOptionPane.showInputDialog("Enter 0 to Show Votes, or


press 1 to Change Dishes");
int x4 = Integer.parseInt(z4);
if(x4==0) {
m.ShowVote();
}
else {
m.ChangeDishes();
}
}
String z1 = JOptionPane.showInputDialog("Do you want to Continue\n 1 to
continue or 0 to exit");
k1 = Integer.parseInt(z1);

}while(k1==1);
}
}

Test Output Screenshots-

1. Running the program initially will give us this


a. If press 0 at this screen, it will take us to Voting Mode

b. After Voting is done, It will show Selected Dish

c. Then we can continue and move back to Step(1).


2. If we press 1 at the initial screen on Step(1) then we get a screen to either check
all votes or change dishes

a. Pressing 0 here will show us this screen


b. Then we will go back to Step(1) through

3. Pressing 1 will allow us to change all the dishes in the voting process

4. Then we can go back to Step(1) or Exit the program

By- Abhyuday Chauhan


20BCG10090
VIT Bhopal

You might also like