55502f40dc8b7c769880b10874abc9d0

hi i need help in urgent to refactor this code i have done some part of it as in the below code they are many classes i have removed one of them as credit card the rest i am not able to understand i would thankful if i coud get a response.
below i have added all the code which needs to be refactored i have done the credit card part by taking it out from the customer class the code is about a theatre which needs to be divided into sections that is it needs to be refactored

this is customer class

using System;
using System.Collections.Generic;
using System.Text;

namespace Assignment1MH_Base
{
    class Customer
    {
        public const int NonMember = 0;
        public const int Bronze = 1;
        public const int Silver = 2;
        public const int Gold = 3;
        private int memberType;
        private String lastName;
        private String firstName;
        private CreditCard cc;
        

        public Customer(int memberType, String lastName, String firstName, String creditNumber, String creditType, String expiry)
        {
            this.memberType = memberType;
            this.lastName = lastName;
            this.firstName = firstName;
            cc = new CreditCard(creditNumber, creditType, expiry);
            
        }

        public Customer(String lastName, String firstName, String creditNumber, String creditType, String expiry)
        {
            this.lastName = lastName;
            this.firstName = firstName;
            cc = new CreditCard(creditNumber, creditType, expiry);
        }

        public String getLastName()
        {
            return lastName;
        }

        public String getFirstName()
        {
            return firstName;
        }

        public int getMemberType()
        {
            return memberType;
        }

        public void setLastName(String lastName)
        {
            this.lastName = lastName;
        }

        public void setFirstName(String firstName)
        {
            this.firstName = firstName;
        }

        public void setMemberType(int memberType)
        {
            this.memberType = memberType;
        }
    }
}

this is credit card class which i createed

using System;
using System.Collections.Generic;
using System.Text;

namespace Assignment1MH_Base
{
    class CreditCard
    {
        private String creditNumber;
        private String creditType;
        private String expiry;

        public CreditCard(String creditNumber, String creditType, String expiry)
        {
            this.creditNumber = creditNumber;
            this.creditType = creditType;
            this.expiry = expiry;
        }
        public String getCreditNumber()
        {
            return creditNumber;
        }
        public String getCreditType()
        {
            return creditType;
        }
        public String getExpiry()
        {
            return expiry;
        }
        public void setCreditNumber(String creditNumber)
        {
            this.creditNumber = creditNumber;
        }
        public void setCreditType(String creditType)
        {
            this.creditType = creditType;
        }
        public void setExpiry(String expiry)
        {
            this.expiry = expiry;
        }
    }
}

this is receipt class

using System;
using System.Collections.Generic;
using System.Text;

namespace Assignment1MH_Base
{
    class Receipt
    {
        private int priceCode;
        private int numberOfSeats;
        private Customer theCust;
        private int rowNum;
        private int startSeatNum;

        public String toString () 
        {
            return "Price Code =" + priceCode + ", seats booked =" + numberOfSeats + ", sitting at row " + rowNum;
        }

        public Receipt(int priceCode, Customer theCust, int rowNum, int startSeatNum, int seatsBooked)
        {
            this.priceCode = priceCode;
            this.theCust = theCust;
            this.rowNum = rowNum;
            this.startSeatNum = startSeatNum;
            this.numberOfSeats = seatsBooked;
        }

        public int getNumberOfSeats()
        {
            return numberOfSeats;
        }

        public int getPriceCode()
        {
            return priceCode;
        }

        public Customer getTheCust()
        {
            return theCust;
        }

        public int getStartSeatNum()
        {
            return startSeatNum;
        }

        public int getRowNum()
        {
            return rowNum;
        }

        public void setNumberOfSeats(int numberOfSeats)
        {
            this.numberOfSeats = numberOfSeats;
        }

        public void setPriceCode(int priceCode)
        {
            this.priceCode = priceCode;
        }

        public void setTheCust(Customer theCust)
        {
            this.theCust = theCust;
        }

        public void setRowNum(int rowNum)
        {
            this.rowNum = rowNum;
        }

        public void setStartSeatNum(int startSeatNum)
        {
            this.startSeatNum = startSeatNum;
        }
    }
}

this is row class

using System;
using System.Collections.Generic;
using System.Text;

namespace Assignment1MH_Base
{
    class Row
    {
        public const int Stalls = 0;
        public const int DressCircle = 1;
        public const int Balcony = 2;
        private const double StallCharge = 37.25;
        private const double DressCircleCharge = 48.80;
        private const double BalconyCharge = 89.00;
        private const double BronzeDiscount = 0.90;
        private const double SilverDiscount = 0.80;
        private const double GoldDiscount = 0.75;

        private int priceCode;
        private int numberAvailable;
        private int currentSeat;
        private int lastBooked;

        public Row(int numAvail, int code)
        {
            this.priceCode = code;
            this.numberAvailable = numAvail;
            currentSeat = 1;
            lastBooked = 1;
        }

        public Boolean bookSeats(int num)
        {
            if (numberAvailable >= num)
            {
                lastBooked = currentSeat;
                currentSeat += num;
                numberAvailable -= num;
                return true;
            }
            return false;
        }

        public int getPriceCode()
        {
            return priceCode;
        }

        public int getLastBooked ()
        {
            return lastBooked;
        }

        public int getCurrentSeat()
        {
            return currentSeat;
        }
    }
}

this is hapening class

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace Assignment1MH_Base
{
    class Happening
    {
        private Hashtable custBooks = new Hashtable();
        private const double StallCharge = 37.25;
        private const double DressCircleCharge = 48.80;
        private const double BalconyCharge = 89.00;
        private const double BronzeDiscount = 0.90;
        private const double SilverDiscount = 0.80;
        private const double GoldDiscount = 0.75;
        private Theatre theTheatre;

        public Happening(Theatre theTheatre)
        {
            this.theTheatre = theTheatre;
        }

        public Boolean bookSeats(int priceCode, int number, Customer theCust)
        {
            if (priceCode == Row.Stalls)
            {
                ArrayList theStalls = theTheatre.getStalls();
                IEnumerator theEnum = theStalls.GetEnumerator();
                while (theEnum.MoveNext())
                {
                    Row theRow = (Row)theEnum.Current;
                    if (theRow.bookSeats(number))
                    {
                        Receipt newBooking = new Receipt(priceCode, theCust, theStalls.IndexOf(theRow) + 1, theRow.getLastBooked(), number);
                        custBooks.Add(theCust, newBooking);
                        return true;
                    }
                }
                return false;
            }
            else if (priceCode == Row.DressCircle)
            {
                ArrayList theDC = theTheatre.getDressCircle();
                IEnumerator theEnum = theDC.GetEnumerator();
                while (theEnum.MoveNext())
                {
                    Row theRow = (Row)theEnum.Current;
                    if (theRow.bookSeats(number))
                    {
                        Receipt newBooking = new Receipt(priceCode, theCust, theDC.IndexOf(theRow) + 1, theRow.getLastBooked(), number);
                        custBooks.Add(theCust, newBooking);
                        return true;
                    }
                }
                return false;
            }
            else if (priceCode == Row.Balcony)
            {
                ArrayList theBalcony = theTheatre.getBalcony();
                IEnumerator theEnum = theBalcony.GetEnumerator();
                while (theEnum.MoveNext())
                {
                    Row theRow = (Row)theEnum.Current;
                    if (theRow.bookSeats(number))
                    {
                        Receipt newBooking = new Receipt(priceCode, theCust, theBalcony.IndexOf(theRow) + 1, theRow.getLastBooked(), number);
                        custBooks.Add(theCust, newBooking);
                        return true;
                    }
                }
                return false;
            }
            return false;
        }

        public double getCharge(Customer theCust)
        {
            Receipt theBooking = (Receipt)custBooks[theCust];
            double theCharge = theTheatre.getDiscount(theCust.getMemberType()) * theTheatre.getCharge(theBooking.getPriceCode()) * theBooking.getNumberOfSeats();
            return theCharge;
        }

        public Receipt getCustomerBooking(Customer cust)
        {
            return (Receipt)custBooks[cust];
        }
    }
}

this is theatr class

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace Assignment1MH_Base
{
    class Theatre
    {
        private const double StallCharge = 37.25;
        private const double DressCircleCharge = 48.80;
        private const double BalconyCharge = 89.00;
        private const double BronzeDiscount = 0.90;
        private const double SilverDiscount = 0.80;
        private const double GoldDiscount = 0.75;

        private ArrayList stalls = new ArrayList();
        private ArrayList dressCircle = new ArrayList();
        private ArrayList balcony = new ArrayList();

        public double getDiscount(int memberCode)
        {
            if (memberCode == Customer.Bronze)
                return BronzeDiscount;
            if (memberCode == Customer.Silver)
                return SilverDiscount;
            if (memberCode == Customer.Gold)
                return GoldDiscount;
            return 1;
        }

        public double getCharge(int priceCode)
        {
            if (priceCode == Row.Stalls)
                return StallCharge;
            if (priceCode == Row.DressCircle)
                return DressCircleCharge;
            if (priceCode == Row.Balcony)
                return BalconyCharge;
            return -1;
        }

        public ArrayList getBalcony()
        {
            return balcony;
        }

        public ArrayList getDressCircle()
        {
            return dressCircle;
        }

        public ArrayList getStalls()
        {
            return stalls;
        }

        public void setStalls(ArrayList stalls)
        {
            this.stalls = stalls;
        }

        public void setBalcony(ArrayList balcony)
        {
            this.balcony = balcony;
        }

        public void setDressCircle(ArrayList dressCircle)
        {
            this.dressCircle = dressCircle;
        }
    }
}

this is test run

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace Assignment1MH_Base
{
    public class TestRun
    {
        // the theatre will have 5 rows in the stalls each with 6 seats
        private const int StallNo = 5;
        private const int StallSeats = 6;
        // the theatre will have 4 rows in the DC each with 6 seats
        private const int DCNo = 4;
        private const int DCSeats = 6;
        // the theatre will have 2 rows in the Balcony each with 6 seats
        private const int BalconyNo = 2;
        private const int BalconySeats = 6;

        static void Main(string[] args)
        {
            //create a new theatre
            Theatre theTheatre = new Theatre();
            populate(theTheatre);

            //create some customers
            Customer normal = new Customer (Customer.NonMember, "Smith", "Joel", "2345678", "Visa", "12/04");
            Customer bronze = new Customer(Customer.Bronze, "Jones", "Bruce", "6876543", "MasterCard", "03/06");
            Customer silver = new Customer(Customer.Silver, "Dell", "George", "9976543", "Visa", "02/06");
            Customer gold = new Customer(Customer.Gold, "Dell", "Bruce", "9876543", "MasterCard", "03/08");

            //make some bookings - print the charges
            Happening bookings = new Happening(theTheatre);

            //book five seats in the stalls for a nonmember
            bookings.bookSeats(Row.Stalls, 5, normal);
            Console.WriteLine("Five seats in the stalls for non-member = {0:C}", bookings.getCharge(normal));
            Console.WriteLine("You are seated in row {0} seat {1} to seat {2}", bookings.getCustomerBooking(normal).getRowNum(), bookings.getCustomerBooking(normal).getStartSeatNum(), bookings.getCustomerBooking(normal).getStartSeatNum() + bookings.getCustomerBooking(normal).getNumberOfSeats()-1);

            //book five seats in the stalls for a bronze member
            bookings.bookSeats(Row.Stalls, 5, bronze);
            Console.WriteLine("Five seats in the stalls for bronze member = {0:C}", bookings.getCharge(bronze));
            Console.WriteLine("You are seated in row {0} seat {1} to seat {2}", bookings.getCustomerBooking(bronze).getRowNum(), bookings.getCustomerBooking(bronze).getStartSeatNum(), bookings.getCustomerBooking(bronze).getStartSeatNum() + bookings.getCustomerBooking(bronze).getNumberOfSeats() - 1);

            //book one seat in the stalls for a silver member
            bookings.bookSeats(Row.Stalls, 1, silver);
            Console.WriteLine("One seat in the stalls for silver member = {0:C}", bookings.getCharge(silver));
            Console.WriteLine("You are seated in row {0} seat {1} to seat {2}", bookings.getCustomerBooking(silver).getRowNum(), bookings.getCustomerBooking(silver).getStartSeatNum(), bookings.getCustomerBooking(silver).getStartSeatNum() + bookings.getCustomerBooking(silver).getNumberOfSeats() - 1);

            //book two seats in the balcony for a gold member
            bookings.bookSeats(Row.Stalls, 1, gold);
            Console.WriteLine("One seat in the stalls for gold member = {0:C}", bookings.getCharge(gold));
            Console.WriteLine("You are seated in row {0} seat {1} to seat {2}", bookings.getCustomerBooking(gold).getRowNum(), bookings.getCustomerBooking(gold).getStartSeatNum(), bookings.getCustomerBooking(gold).getStartSeatNum() + bookings.getCustomerBooking(gold).getNumberOfSeats() - 1);


            Console.ReadLine();
        }

        private static void populate(Theatre theTheatre)
        {
            //initialise the stalls
            ArrayList theStalls = theTheatre.getStalls();
            for (int x = 0; x < StallNo; x++)
            {
                Row theRow = new Row(StallSeats, Row.Stalls);
                theStalls.Add(theRow);
            }
            //initialise the DC
            ArrayList theDC = theTheatre.getDressCircle();
            for (int x = 0; x < DCNo; x++)
            {
                Row theRow = new Row(DCSeats, Row.DressCircle);
                theDC.Add(theRow);
            }
            //initialise the Balcony
            ArrayList theBalcony = theTheatre.getBalcony();
            for (int x = 0; x < BalconyNo; x++)
            {
                Row theRow = new Row(BalconySeats, Row.Balcony);
                theBalcony.Add(theRow);
            }
        }
    }
}

Refactorings

No refactoring yet !

F9a9ba6663645458aa8630157ed5e71e

Ants

August 23, 2010, August 23, 2010 10:53, permalink

No rating. Login to rate!

Why don't you copy the answers from one of your other classmates that asked the same question here about 3-4 months ago. See: http://refactormycode.com/codes/1282-firts-time-refactoring

Your refactoring





Format Copy from initial code

or Cancel