Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Working PS3 Jailbreak 3.65 And 3.66
ExtaBit Premium Accounts and Cookies
Steam Wallet Hack - Money Adder & Hack v3
Empires & Allies Hack Cheat Trainer v5.4.1
Eve Onnline 60 Days Time Card Generator v2
Xbox Lve Generator v3
Better way to get content via jQuery $.get()
Free CarTown Blue Points Generator and CarTown Templates
Steam Wallet Hack 2012
Diablo 3 error 37 & error 3006 fix
Popular
XBOX POINTS GENERATOR - MICROSOFT POINTS GENERATOR v1.2012
11 may 2012 premium uploading accounts 100% working
Free Microsoft Points
Free Microsoft Points - Microsoft Points Generator - Xbox Live Codes 2012
Car Town Free Blue Points Hack
Free CarTown Blue Points Generator and CarTown Templates
Free Microsoft Points
Simple Days Purger
Sharecash Downloader Bypass Surveys New 05/2012
PAYPAL REMOVE ACCESS LIMITED ACCOUNT 100% Working
Pastable version of
help on refactoring this code
<pre class='prettyprint' language='cs'>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); } } } }</pre> <a href="http://www.refactormycode.com/codes/1413-help-on-refactoring-this-code" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>