# Design of classes

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-04-2010, 07:19 PM
cselic
Design of classes
Hi.
I want to write small program Theater.

The Theater consists of Balcony and Parterre.
Parterre has 20 rows, and 20 seats.
Balcony has 10 rows and 18 seats.

Price for ticket for seats on balcony is with 10% discount.

Theater need to have functions:
drawTheater, priceForTicket, isSeatReserved,...

What is the best way to write a class Theater, or maybe write
3 classes Theater, Parterre and Balcony?

I designed this classes to over 10 different ways, and it is still not good.
• 06-04-2010, 08:30 PM
StormyWaters
You could have as many designs as there are designers and they all could be "correct" by performing the same tasks with the same results, just in a different manner.

Personally I would start with the bottom and work up just because that's how I usually do things. From my perspective, I see 5 classes, Seat, Row, Parterre, Balcony and Theatre.
• 06-04-2010, 10:34 PM
xcallmejudasx
You could do it with just a Theater class. Use a List of Lists for the rows and seats(think of the seats as a column) and that gives you a grid like system.

Then you can just have a boolean value to say if the seat is a Parterre seat and if so take 10% off the price.

StormyWaters way works just as well, probably better since it's more OOP. A lot of your decisions will be based on how much time you have to write this, how long you need to keep it, if it's going to be updated later, etc etc etc. Just see what works best for you and what you're more comfortable doing.
• 06-04-2010, 11:11 PM
cselic
Well, variables numberOfRows and numberOfSeats are not very significant. They just tell me if seat is reserved or not. I'll probably implement them as a arrays of boolean.

My problem is this:
Let's say Theater consists of parterre and balcony.
The Balcony has almost every method, and almost every state same as Theater.
The Parterre also has almost every method, and almost every state same as Theater.

So I can do it with abstract class Theater, and class Balcony extends Theater, and class Parterre extends Balcony. But problem is, that I need to have constuctor for Theatre because Theatre consists of one Balcony and one Parterre.
• 06-04-2010, 11:17 PM
cselic
Problem with writing just one class Theatre for me is that when I want to write some functions for Theatre I must ask in every function if that is for Parterre or for Balcony, or write every function twice, first for Parterre and second for Balcony.

I think if I write something with inheritance it will be much better. For example
father is Theatre, and children are Balcony and Parterre.
• 06-04-2010, 11:27 PM
Norm
Quote:

Balcony extends Theater
Doesn't make sense to me. A balcony is a part of a theater not a type of theater.
• 06-04-2010, 11:37 PM
cselic
Quote:

Quote:
Balcony extends Theater
Doesn't make sense to me. A balcony is a part of a theater not a type of theater.
Balcony has almost every method, and almost every variable same as Theater. So it's obvious that I must to have some sort of inheritance.

So it has almost everything same with Theater but it is only part of Theater. Please give me some advice or opinion.
• 06-05-2010, 12:23 AM
Fubarable
Doesn't matter as you don't think of Balcony as a specific subtype of Theater. Balcony may share some behaviors, but it simply doesn't pass the "is-a" test. It simply isn't a theater.

For instance Dog "is-a" animal, and Beagle "is-a" dog, so it would be reasonable to have a Dog class extend from an Animal class and a Beagle class extend from Dog as these pass the "is-a" test.

For balcony and theater, there is more of "has-a" relationship: a Theater "has-a" Balcony. So a Theater class may contain 1 or more Balcony objects but neither class should extend from the other.
• 06-05-2010, 12:28 AM
iluxa
I'm gonna be thorough with this one, so brace yourself for a long read.

Step 1. Make sure your story is complete.

Quote:

The Theater consists of Balcony and Parterre.
Parterre has 20 rows, and 20 seats.
Balcony has 10 rows and 18 seats.

The Theater has a default price per ticket. Price for ticket for seats on balcony is with 10% discount.

Theater need to have functions:
drawTheater, priceForTicket, isSeatReserved,...
leaves a bunch of questions open. I'm going to complete it, making some assumptions along the way.

The Theater consists of two Sections: Balcony and Parterre.
Parterre has 20 rows, and 20 seats.
Balcony has 10 rows and 18 seats.

Price for ticket for seats on balcony is with 10% discount.
Seats are identified by the Section to which they belong, row number, and seat number

Theater need to have functions:
drawTheater, priceForTicket, isSeatReserved,..

Step 2. Highlight all the nouns in your story.

In our case, this is going to be Theater, Section, Balcony, Parterre, Row, Seat, Price, Default Price, Discount, Seat Identifier

90% of the time a noun will correspond to either a class or a field in a class.

Step 3. Let's think in terms of "is-a" and "has-a" relationships. Has-a will become aggregation; is-a will become inheritance.

Theater has 0..n Sections
Balcony is a Section
Parterre is a Section
Section has 0..n Rows
Row has 0..n Seats
Seat has "Is Occupied"
Theater has Default Price
Section has Discount

Step 4. Classes. No methods yet.

Code:

```enum SectionType {   BALCONY, PARTERRE } class Seat {   int number;   boolean isOccupied; } class Row {   List<Seat> seats; } class Section {   SectionType type;   List<Row> rows;   double discount; } class Balcony extends Section { } class Parterre extends Section { } class Theater {   List<Section> sections;   double defaultTicketPrice; }```

5. Let's figure out constructors...

Code:

```enum SectionType {   BALCONY, PARTERRE } class Seat {   int number;   boolean isOccupied;   Seat(int n) {     number = n; } class Row {   List<Seat> seats;   Row(int numSeats) {     seats = new ArrayList<Seat>();     for(int i = 0; o < numSeats; i++) {       seats.add (new Seat(i));     } } class Section {   SectionType type;   List<Row> rows;   double discount;   Section(SectionType type, double discount, int numRows, int seatsPerRow) {     this.type = type;     this.discount = discount;     rows = new ArrayList<Row>();     for (int i = 0; i < numRows; i ++) {       rows.add (new Row (seatsPerRow));     }   } } class Balcony extends Section {   Balcony() {     super(SectionType.BALCONY, 0.1, 10, 18);   } } class Parterre extends Section {   Parterre() {     super(SectionType.PARTERRE, 0, 20, 20);   } } class Theater {   List<Section> sections;   double defaultTicketPrice;   Theater(double defaultPrice) {     defaultTicketPrice = defaultPrice;     sections = new ArrayList<Section>();     sections.add (new Parterre());     sections.add (new Balcony()); }```
6. implement your methods... have fun with that one :)
• 06-05-2010, 02:01 AM
Norm

@OP Going back to the original program specs. If you want to go to a performance, you go to the Theater and ask if they have any seats in the balcony and how much they are.
So one of the things you pass to the ticket window is where you want to sit. On that basis, the methods for Theater would be different, you specify a location.

Also You wouldn't go to a Balcony and ask to buy a ticket.
• 06-05-2010, 10:14 AM
cselic
Quote:

'm gonna be thorough with this one, so brace yourself for a long read.
Thank you so much for your detailed reply. This explanation was very nice. Thanks.

Your pattern is great. I'll begin to use it.
• 06-05-2010, 11:18 AM
cselic
Quote:

Step 1. Make sure your story is complete.

Quote:
The Theater consists of Balcony and Parterre.
Parterre has 20 rows, and 20 seats.
Balcony has 10 rows and 18 seats.

The Theater has a default price per ticket. Price for ticket for seats on balcony is with 10% discount.

Theater need to have functions:
drawTheater, priceForTicket, isSeatReserved,...
leaves a bunch of questions open. I'm going to complete it, making some assumptions along the way.
I apologize that I initially hid a good part of the story.

Full story:
Quote:

Exam. Time: 120 min.

Write java application for the sale of tickets at the theater. The file "performances.txt" contains the names of performances. Performances may be ordinary, child and hit. While we working with the application it is possible to change the type of performance. Theatre has a parterre and a balcony. Parterre has 20 rows and 20 seats, and balcony has 10 rows and 18 seats. Ticket for parter's seat costs 5\$, 4\$, 10\$ (5\$ for ordinary performance, 4\$ for kids performance, 10\$ for hit performance). Ticket for seat on balcony has 10% discount.

Salesperson run application. He can choose performance name from ComboBox. When the name of performance is selected, on the screen will be drawn current state of reserved seats in the theatre (reserved seat is drawn with red color, and not reserved seat is drawn with green color). When salesperson click on not reserved seat it opens the form for selling the ticket. Saleperson fills password of buyer. If a person has never bought a ticket, saleperson can fill new password and to enter information about the customer (firste name, last name, phone number). Information about customers is stored in a file "customers.txt". The customer can be an ordinary customer or VIP customer. VIP customers have 20% discount. after-sale ticket seat is marked as reserved. Using the menu the seller can see the table showing current revenue for each theater's performance.
Take care of object-oriented application design.
• 06-05-2010, 12:04 PM
curmudgeon
Isn't this the time where you show us that you've tried to incorporate the ideas that have been shown to you with great effort above, and that you have used these ideas to attempt to come up with a solution of your own, and then post your code? Or are you just going to show us the full assignment and wait for someone to dump more code in your lap? Just curious.
• 06-05-2010, 12:19 PM
cselic
I do not like people who spread negativity! I do not tolerate such insults!
It is bad practice waiting for the opportunity to accused someone for something!

I do not ask anyone to solve this all. I wrote this up in order to further explain the problem, because they gave me the objection that I hid some parts of the story. :cool:

I wrote 80% of the code of this problem, but due to poor design class
I'll have to write much of the program again.

Quote:

Or are you just going to show us the full assignment and wait for someone to dump more code in your lap?

p.s. In the future I will not answer on this kind of nonsense.
• 06-05-2010, 12:23 PM
curmudgeon
I'm sorry you didn't like my post, but I call them as I see them. If you really want to prove me wrong, to show me up, then just post the code and show us your effort. I'll gladly take back my comments then.
• 06-05-2010, 12:30 PM
cselic
Why are you so nervous and impatient?!

Maybe you're superman or some other super hero and all you'll need 5 minutes to do everything.
I'm slower I need more time for designing and writing a code.
As you saw above, the program is not easy at all.

Quote:

If you really want to prove me wrong,
Who cares to prove something to you. Those who know me know that I will post code when I finish.
• 06-05-2010, 01:33 PM
JosAH
Quote:

Originally Posted by cselic
I do not like people who spread negativity! I do not tolerate such insults!
It is bad practice waiting for the opportunity to accused someone for something!

If you want to participate in forums you should grow a thicker skin. You should also help people who are willing to help you by being clear and by providing them all the information they might need. We are not mind readers (well maybe I am but I fell on my head when I was a baby).

You, nor anyone else (except moderators) are not in a position to tolerate anything, this is a free forum and everybody can reply here. If those replies happen to be utter nonsense all the time other posters will jump in; that's how it works.

kind regards,

Jos
• 06-08-2010, 10:58 PM
cselic
Applying Ilyxa's 6 steps pattern I solved Theater's design of classes like this:

1. abstract class Section
2. class Parterre extends Section
3. class Balcony extends Section
4. enum SectionType (PARTERRE, BALCONY)
5. enum PerformanceType (ORDINARY, CHILD, HIT)
6. class Performance
7. class Ticket
8. class Seat
9. class Audience
10. class Theater
11. class Salesman (with main method, and Graphics user interface)

That's much better than my earlier poor design with
3-4 classes, without abstract class Section, with rows and seats implemented with 2d arrays, without ticket and seat classes,...

I will post here what I've written so far. New design is better.
• 06-08-2010, 11:03 PM
cselic
class Section.java:

Code:

```import java.awt.Graphics; import java.util.Vector; public abstract class Section {         SectionType sectionType;         int numberOfRows;         int numberOfSeats;         Seat seat;         Vector<Seat> seats = new Vector<Seat>();         protected double discount = 0;                 public Section(SectionType sectionType, int numberOfRows, int numberOfSeats) {                 this.sectionType = sectionType;                 this.numberOfRows = numberOfRows;                 this.numberOfSeats = numberOfSeats;                 for(int i = 0; i < this.numberOfRows; i++)                         for(int j = 0; j < this.numberOfSeats; j++) {                                 seat = new Seat(this.sectionType, i, j);                                 seats.add(seat);                         }                        }                 public abstract void drawSections(Graphics g);         public abstract int getRowNumber();         public abstract int getSeatsNumber(); }```

class Parterre.java:

Code:

```import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; public class Parterre extends Section {                 public static final int PARTERRE_ROW_NUMBER = 20;         public static final int PARTERRE_SEATS_NUMBER = 20;                 public Parterre() {                 super(SectionType.PARTERRE, PARTERRE_ROW_NUMBER, PARTERRE_SEATS_NUMBER);         }                 public int getRowNumber() {                 return PARTERRE_ROW_NUMBER;         }         public int getSeatsNumber() {                 return PARTERRE_SEATS_NUMBER;         }         @Override         public void drawSections(Graphics g) {                                 Graphics2D g2 = (Graphics2D) g;                 g2.setColor(Color.RED);                                 for(int i = 0; i < 20; i++)                         for(int j = 0; j < 20; j++)                                 g2.fillRect(20 + (15 * i), 20 + (15 * j), 14, 14);         } }```

class Balcony.java:

Code:

```import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; public class Balcony extends Section {                 public static final int BALCONY_ROW_NUMBER = 10;         public static final int BALCONY_SEATS_NUMBER = 20;                 public Balcony() {                 super(SectionType.BALCONY, BALCONY_ROW_NUMBER, BALCONY_SEATS_NUMBER);         }                 public int getRowNumber() {                 return BALCONY_ROW_NUMBER;         }                 public int getSeatsNumber() {                 return BALCONY_SEATS_NUMBER;         }                 @Override         public void drawSections(Graphics g) {                 Graphics2D g2 = (Graphics2D) g;                 g2.setColor(Color.RED);                                 for(int i = 0; i < 10; i++)                         for(int j = 0; j < 18; j++)                                 g2.fillRect(340 + (15 * i), 20 + (15 * j), 14, 14);         } }```

enum SectionType.java:

Code:

```public enum SectionType {         BALCONY, PARTERRE }```
• 06-08-2010, 11:10 PM
cselic
enum PerformanceType.java:

Code:

```public enum PerformanceType {         ORDINARY, CHILD, HIT }```

class Performance.java:

Code:

```import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Vector; public class Performance {         public static final String PERFORMANCES_FILE = "performances.txt";         String nameOfPerformance;         PerformanceType typeOfPerformance;         Vector<Performance> performances = new Vector<Performance>();                 public Performance() {                 getPerformancesFromFile();         }                 public Performance(String nameOfPerformance, PerformanceType typeOfPerformance) {                 this.nameOfPerformance = nameOfPerformance;                 this.typeOfPerformance = typeOfPerformance;         }                 public void getPerformancesFromFile() {                 performances = new Vector<Performance>();                 BufferedReader reader = null;                 try {                         reader = new BufferedReader(new FileReader(PERFORMANCES_FILE));                         String currentLine = "";                         String performanceType;                         int positionOfTab;                                                 while((currentLine = reader.readLine()) != null) {                                 positionOfTab = currentLine.indexOf('\t');                                 nameOfPerformance = currentLine.substring(0, positionOfTab);                                                                                                 performanceType = currentLine.substring(positionOfTab + 1);                                 if(performanceType.equals("CHILD"))                                         typeOfPerformance = PerformanceType.CHILD;                                 else if(performanceType.equals("HIT"))                                         typeOfPerformance = PerformanceType.HIT;                                 else                                         typeOfPerformance = PerformanceType.ORDINARY;                                                                 performances.add(new Performance(nameOfPerformance, typeOfPerformance));                         }                                         } catch (FileNotFoundException e) {                         e.printStackTrace();                 } catch(IOException e) {                         e.printStackTrace();                 } finally {                         try {                                 reader.close();                         } catch (IOException e) {                                 e.printStackTrace();                         }                 }         }                public Vector<Performance> getPerformances() {                 return performances;         }                 public void changeATypeOfPerformance(String nameOfPerformance, PerformanceType newPerformanceType) {                         }         public Vector<String> getNamesOfPerformances() {                 Vector<String> namesOfPerformances = new Vector<String>();                 for(int i = 0; i < performances.size(); i++)                         namesOfPerformances.add(performances.get(i).getNameOfPerform());                                 return namesOfPerformances;         }                 public String getNameOfPerform() {                 return nameOfPerformance;         }         public PerformanceType getTypeOfPerformance() {                 return typeOfPerformance;         }                 public int numberOfPerfomances() {                 return performances.size();         } }```

class Ticket.java:

Code:

```public class Ticket {         Audience audience;         Seat seat;         private double ticketPrice;         private double balconyDiscount = 0;         private double vipDiscount = 0;                 public Ticket(Seat seat, Audience audience) {                 this.seat = seat;                 this.audience = audience;         }                 public double getTicketPrice(PerformanceType performanceType, SectionType sectionType) {                                 if(performanceType == PerformanceType.CHILD)                         ticketPrice = 4;                 else if(performanceType == PerformanceType.HIT)                         ticketPrice = 10;                 else                         ticketPrice = 5;                                 if(audience.isVIP())                         vipDiscount = 20;                                 if(sectionType == SectionType.BALCONY)                         balconyDiscount = 10;                                 ticketPrice = ((ticketPrice * (100 - vipDiscount) / 100) * (100 - balconyDiscount)) / 100;                 return ticketPrice;         } }```

class Seat.java:

Code:

```public class Seat {         SectionType sectionType;         int row;         int seat;         boolean reserved = false;                 public Seat(SectionType sectionType, int row, int seat) {                 this.sectionType = sectionType;                 this.row = row;                 this.seat = seat;         }                 public boolean isReserved() {                 return reserved;         }         public void setReserved(boolean reserved) {                 this.reserved = reserved;         }                 public SectionType getSectionType() {                 return sectionType;         } }```

class Audience.java:

Code:

```import java.util.HashSet; import java.util.Set; import java.util.Vector; public class Audience {         boolean VIP;         String password;         String firstName;         String lastName;         String phoneNumber;         Set<String> passwords = new HashSet<String>();         Vector<Audience> audience = new Vector<Audience>();                 // new customer         public Audience(String password, String firstName, String lastName, String phoneNumber, boolean VIP) {                 this.password = password;                 this.firstName = firstName;                 this.lastName = lastName;                 this.phoneNumber = phoneNumber;                 this.VIP = VIP;         }                 public Audience(String password) { }                 public void addAudience(Audience aud) {                 audience.add(aud);                 passwords.add(aud.password);         }         public boolean isVIP() {                 return VIP;         }         public void setVIP(boolean vIP) {                 VIP = vIP;         }                        public Set<String> getPasswords() {                 return passwords;         } }```

class Theater.java:

Code:

```import java.awt.Graphics; import java.util.Vector; public class Theater {         Section balcony;         Section parterre;         String nameOfPerformance;                 Theater theater;         Vector<Theater> theaters = new Vector<Theater>();                 public Theater() {                 parterre = new Parterre();                 balcony = new Balcony();         }                 public Theater(String nameOfPerformance) {                 this.nameOfPerformance = nameOfPerformance;                 parterre = new Parterre();                 balcony = new Balcony();         }                 public Theater(Theater theater) {                 this.theater = theater;         }                 public Vector<Theater> getTheaters(Vector<String> namesOfPerformances) {                 for(int i = 0; i < namesOfPerformances.size(); i++) {                         theater = new Theater(namesOfPerformances.get(i));                         theaters.add(theater);                 }                 return theaters;         }         public Section getBalcony() {                 return balcony;         }         public Section getParterre() {                 return parterre;         }                 public void drawTheater(Graphics g) {                         theater.getParterre().drawSections(g);                         theater.getBalcony().drawSections(g);         }         public Vector<Theater> getTheaters() {                 return theaters;         } }```
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last