Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Need to refractor this piece of shit

Name: OP hates his boss 2012-04-26 3:21

Been given this absolute piece of shit to attempt to refractor
-have i missed anything?

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

namespace Assignment1
{
    class Program
    {
        static void recieptHeader()
        {
            Console.WriteLine("\nAUTOMATED FUEL DELIVERY SYSTEM");
            Console.WriteLine("\nRECEIPT");
        }

        private static void RegisteredUsers(ArrayList registeredUsers)
        {
            registeredUsers.Add(new CommunicationSystem("476541", "Visa", 50));
            registeredUsers.Add(new CommunicationSystem("302354", "MasterCard", 400));
            registeredUsers.Add(new CommunicationSystem("470614", "Amex", 200));
            registeredUsers.Add(new CommunicationSystem("711251", "Diners", 90));
        }

        static void Main(string[] args)
        {
            SystemController aSystem = new SystemController();

            ArrayList registeredUsers = new ArrayList();

            // populate registered users
            RegisteredUsers(registeredUsers);

            aSystem.setRegisteredUsers(registeredUsers);


            // Add fuel to tank
            FuelTank aTank = aSystem.getFuelTank();
            aTank.addFuel(1000); // litres

            //validate transaction

            CardReader customer1 = aSystem.getCardReader();
            customer1.readCard("476541", "Visa");

            aSystem.getCardStatus(customer1);


            // Scenario 1 - Enter cash limit
            if (aSystem.getCardReader().getCardSatus() == SystemController.CardStatus_Valid)
            {
                Pump aPump = customer1.getPump();
                // Fuel selection
                aPump.setFuelType(SystemController.Unleaded);
                // set cash
                aPump.setPrice(40);      
                // check credit limit
                // check fuel price with customers selection
                if (customer1.getCreditLimit() > aPump.getPrice())
                {
                    double fuelDelivery = aPump.getPrice() / SystemController.UnleadedPrice;
                  
                    aPump.activate();
                    if (aTank.getTotalLevel() > fuelDelivery)
                    {
                        aPump.setFuelDispensed(fuelDelivery);
                    }
                    aPump.deactivate();
                    aTank.removeFuel(fuelDelivery);
                    customer1.setCreditLimit(customer1.getCreditLimit() - aPump.getPrice());
                    DateTime currTime = DateTime.Now;

                    // Print receipt
                    recieptHeader();
                    Console.WriteLine("\nDate : {0:d}", currTime);
                    Console.Write("\nPaid ${0} for {1:#.##} litres of ", aPump.getPrice(), fuelDelivery);
                    if (aPump.getFuelType() == SystemController.Unleaded)
                        Console.Write("Unleaded");
                    else if (aPump.getFuelType() == SystemController.PremiumUnleaded)
                        Console.Write("Premium Unleaded");
                    else if (aPump.getFuelType() == SystemController.LPG)
                        Console.Write("LPG");
                    else if (aPump.getFuelType() == SystemController.Diesel)
                        Console.Write("Diesel");
                    Console.WriteLine("\n\nPress any key to continue...");
                    string garbage = Console.ReadLine();
                    Console.Clear();
                  
                   
                }
         

                // max amount of fuel
                CardReader customer2 = new CardReader();
                customer2.readCard("302354", "MasterCard");
                aSystem.getCardStatus(customer2);

                // Scenario 2 - Customer enters max fuel
                if (aSystem.getCardReader().getCardSatus() == SystemController.CardStatus_Valid)
                {
                    // check fuel price with customers selection
                   
                    double maxDelivery = customer2.getCreditLimit() / SystemController.PremiumUnleadedPrice;
                    aSystem.setMaxDelivery(maxDelivery);
                    Pump bPump = customer2.getPump();
                    bPump.setFuelType(SystemController.PremiumUnleaded);
                    bPump.activate();
                    double fuelDispensed = 0;
                    double charge = 0;
                    bool filled = false;
                    Console.WriteLine("\n\nDISPENSING FUEL");
                    Console.WriteLine("\nPress y to start dispensing or ..\n\npress any other key to quit");
                    string a = Console.ReadLine().ToLower();
                    if (a.Equals("y"))
                    {
                        Console.Clear();
                        Console.WriteLine("\n\nDispensing fuel");
                        Console.WriteLine("\nPress <CR> to dispence in 0.1 litre increment\n\nPress q to terminate dispensing");
                        string req = Console.ReadLine().ToLower();
                        while ((aTank.getTotalLevel() >= maxDelivery) & (!req.Equals("q")) & fuelDispensed <= maxDelivery & customer2.getCreditLimit() >= charge)
                        {
                            filled = true;
                            charge += 0.1 * SystemController.PremiumUnleadedPrice;
                            fuelDispensed += 0.1;
                            aTank.removeFuel(0.1);
                                                      
                            Console.Clear();
                            Console.WriteLine("\n\nDispensing fuel");
                            Console.WriteLine("\nFuel dispensed {0:F2} litre(s) and Cost {1:C}", fuelDispensed, charge);
                            Console.WriteLine("\nPress <CR> to despence in 0.1 litre increment\n\nPress q to terminate dispensing");
                            string ch = Console.ReadLine().ToLower();
                            if (ch.Equals("q"))
                            {
                                bPump.setFuelDispensed(fuelDispensed);
                                bPump.setPrice(charge);
                                break;
                            }
                        }
                        if (filled)
                        {
                            customer2.setCreditLimit(customer2.getCreditLimit() - charge);
                            DateTime currTime = DateTime.Now;
                            // Print receipt
                            recieptHeader();
                            Console.WriteLine("\nDate : {0:d}", currTime);
                            Console.Write("\nPaid ${0} for {1:#.##} litres of ", bPump.getPrice(), bPump.getFuelDispensed());
                            if (bPump.getFuelType() == SystemController.Unleaded)
                                Console.Write("Unleaded");
                            else if (bPump.getFuelType() == SystemController.PremiumUnleaded)
                                Console.Write("Premium Unleaded");
                            else if (bPump.getFuelType() == SystemController.LPG)
                                Console.Write("LPG");
                            else if (bPump.getFuelType() == SystemController.Diesel)
                                Console.Write("Diesel");
                            Console.WriteLine("\n\nPress any key to continue...");
                            string garbage = Console.ReadLine().ToLower();
                            Console.Clear();

                        }
                    }                                       
                }
            }
        }
    }
}

Name: Anonymous 2012-04-26 4:07

the if(a.Equals("y")) statement should be put into a new method called StartDispenser(), as that is possibly something you may wish to call if the program got bigger and you needed to call it at times other than when the user presses "y".

The idea of refactoring is to create more reusable code.

Look for anything that you think might need to be called on its own.

A good idea is to create a flow chart of what is happening and try to pick out possible verbs which would denote potential methods or even classes (if you want to create new classes).

For example, just a quick look tells me you could have:
StartDispenser()
ChargeCustomer()
as potentially reusable methods

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List