Skip to content

Instantly share code, notes, and snippets.

@ahmedengu
Last active October 12, 2016 10:32
Show Gist options
  • Save ahmedengu/b3d4eaf84bc919ff4f6be476b1eb03cb to your computer and use it in GitHub Desktop.
Save ahmedengu/b3d4eaf84bc919ff4f6be476b1eb03cb to your computer and use it in GitHub Desktop.
Hotel Rooms Factory Pattern
public class Main {
public static void main(String[] args) {
Fact fact = new Fact();
Rooms one = fact.getRoom(1);
Rooms two = fact.getRoom(2);
Rooms three = fact.getRoom(3);
System.out.println(one.getBed() + " " + one.getCost());
System.out.println(two.getBed() + " " + two.getCost());
System.out.println(three.getBed() + " " + three.getCost());
}
}
class Fact {
public Rooms getRoom(int x) {
if (x == 1)
return new OneBed();
if (x == 2)
return new TwoBed();
if (x == 3)
return new ThreeBed();
return null;
}
}
interface Rooms {
int bed = 0;
int cost = 0;
public int getBed();
public int getCost();
}
class OneBed implements Rooms {
int bed = 1;
int cost = 100;
public OneBed() {
System.out.println("one");
}
public int getBed() {
return bed;
}
public void setBed(int bed) {
this.bed = bed;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
}
class TwoBed implements Rooms {
int bed = 2;
int cost = 200;
public TwoBed() {
System.out.println("two");
}
public int getBed() {
return bed;
}
public void setBed(int bed) {
this.bed = bed;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
}
class ThreeBed implements Rooms {
int bed = 3;
int cost = 300;
public ThreeBed() {
System.out.println("three");
}
public int getBed() {
return bed;
}
public void setBed(int bed) {
this.bed = bed;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
}
one
two
three
1 100
2 200
3 300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment