Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Collections;
- using System.Diagnostics;
- // -----------------------------------------------------------------------------
- // A Drug object holds information about one fee-for-service outpatient drug
- // reimbursed by Medi-Cal (California's Medicaid program) to pharmacies.
- class Drug : IComparable
- {
- string name; // brand name, strength, dosage form
- string id; // national drug code number
- double size; // package size
- string unit; // unit of measurement
- double quantity; // number of units dispensed
- double lowest; // price Medi-Cal is willing to pay
- double ingredientCost; // estimated ingredient cost
- int numTar; // number of claims with a treatment auth. request
- double totalPaid; // total amount paid
- double averagePaid; // average paid per prescription
- int daysSupply; // total days supply
- int claimLines; // total number of claim lines
- // Properties providing read-only access to every field.
- public string Name { get { return name; } }
- public string Id { get { return id; } }
- public double Size { get { return size; } }
- public string Unit { get { return unit; } }
- public double Quantity { get { return quantity; } }
- public double Lowest { get { return lowest; } }
- public double IngredientCost { get { return ingredientCost; } }
- public int NumTar { get { return numTar; } }
- public double TotalPaid { get { return totalPaid; } }
- public double AveragePaid { get { return averagePaid; } }
- public int DaysSupply { get { return daysSupply; } }
- public int ClaimLines { get { return claimLines; } }
- public Drug ( string name, string id, double size, string unit,
- double quantity, double lowest, double ingredientCost, int numTar,
- double totalPaid, double averagePaid, int daysSupply, int claimLines )
- {
- this.name = name;
- this.id = id;
- this.size = size;
- this.unit = unit;
- this.quantity = quantity;
- this.lowest = lowest;
- this.ingredientCost = ingredientCost;
- this.numTar = numTar;
- this.totalPaid = totalPaid;
- this.averagePaid = averagePaid;
- this.daysSupply = daysSupply;
- this.claimLines = claimLines;
- }
- // Simple string for debugging purposes, showing only selected fields.
- public override string ToString( )
- {
- return string.Format(
- "{0}: {1}, {2}", id, name, size );
- }
- public int CompareTo( object obj )
- {
- // TODO : Implement this method, replacing the line below
- if (obj is Drug)
- {
- Drug drug = (Drug) obj;
- if (String.Compare(drug.Name, this.Name) > 0)
- return 1;
- else if (String.Compare(drug.Name, this.Name) < 0)
- return -1;
- else
- return 0;
- }
- else
- {
- throw new ArgumentException("no comparison is possible");
- }
- }
- }
- // -----------------------------------------------------------------------------
- // Linked list of Drugs. A list object holds references to its head and tail
- // Nodes and a count of the number of Nodes.
- class DrugList
- {
- // Nodes form the singly linked list. Each node holds one Drug item.
- class Node : IComparable
- {
- Node next;
- Drug data;
- public Node( Drug data ) { next = null; this.data = data; }
- public Node Next{ get { return next; } set { next = value; } }
- public Drug Data{ get { return data; } }
- public int CompareTo( object obj )
- {
- // TODO 1: Implement this method, replacing the line below
- if (obj is Node)
- {
- Node node = (Node) obj;
- if (String.Compare(node.Data.Name, this.Data.Name) > 0)
- return 1;
- else if (String.Compare(node.Data.Name, this.Data.Name) < 0)
- return -1;
- else
- return 0;
- }
- else
- {
- throw new ArgumentException("no comparison is possible");
- }
- }
- }
- Node tail;
- Node head;
- int count;
- public int Count { get { return count; } }
- // Constructors:
- public DrugList( ) { tail = null; head = null; count = 0; }
- public DrugList( string drugFile ) : this( ) { AppendFromFile( drugFile ); }
- // Methods which add elements:
- // Build this list from a specified drug file.
- public void AppendFromFile( string drugFile )
- {
- using( StreamReader sr = new StreamReader( drugFile ) )
- {
- while( ! sr.EndOfStream )
- {
- string line = sr.ReadLine( );
- // Extract drug information
- string name = line.Substring( 7, 30 ).Trim( );
- string id = line.Substring( 37, 13 ).Trim( );
- string temp = line.Substring( 50, 14 ).Trim( );
- double size
- = double.Parse( temp.Substring( 0 , temp.Length - 2 ) );
- string unit = temp.Substring( temp.Length - 2, 2 );
- double quantity = double.Parse( line.Substring( 64, 16 ) );
- double lowest = double.Parse( line.Substring( 80, 10 ) );
- double ingredientCost
- = double.Parse( line.Substring( 90, 12 ) );
- int numTar = int.Parse( line.Substring( 102, 8 ) );
- double totalPaid = double.Parse( line.Substring( 110, 14 ) );
- double averagePaid = double.Parse( line.Substring( 124, 10 ) );
- int daysSupply
- = ( int ) double.Parse( line.Substring( 134, 14 ) );
- int claimLines = int.Parse( line.Substring( 148 ) );
- // Put drug onto this list of drugs.
- Append( new Drug( name, id, size, unit, quantity, lowest,
- ingredientCost, numTar, totalPaid, averagePaid,
- daysSupply, claimLines ) );
- }
- }
- }
- // Add a new Drug item to the end of this linked list.
- public void Append( Drug data )
- {
- if (data == null )
- {
- return;
- }
- if (head == null)
- {
- head = tail = new Node(data);
- count++;
- }
- else
- {
- Node newDrug = new Node(data);
- tail.Next = newDrug;
- tail = newDrug;
- count++;
- }
- }
- // Remove the minimal Drug
- public Drug RemoveMin( )
- {
- // TODO 3: Complete this method, replacing the following statement.
- Node current = head;
- Node minimum = head;
- if (head == null)
- {
- return null;
- }
- while (current != null)
- {
- if (current.CompareTo(minimum) > 0)
- {
- minimum = current;
- current = current.Next;
- }
- else
- current = current.Next;
- }
- current = head;
- if (head == minimum)
- {
- count--;
- head = head.Next;
- return minimum.Data;
- }
- while (current.Next != minimum)
- {
- current = current.Next;
- }
- if (tail == minimum)
- {
- count--;
- current.Next = null;
- tail = current;
- return minimum.Data;
- }
- else
- {
- count--;
- current.Next = (current.Next).Next;
- return minimum.Data;
- }
- }
- // Methods which sort the list:
- // Sort this list by selection sort.
- public void SelectSort( )
- {
- int length = count;
- DrugList sorted = new DrugList();
- if (head == null)
- {
- return;
- }
- int i = 0;
- while(i < length)
- {
- sorted.Append(RemoveMin());
- i++;
- }
- head = sorted.head;
- tail = sorted.tail;
- count = sorted.count;
- }
- // Methods which extract the Drugs:
- // Return, as an array, references to all the Drug objects on the list.
- public Drug[ ] ToArray( )
- {
- Drug[ ] result = new Drug[ count ];
- int nextIndex = 0;
- Node current = head;
- while( current != null )
- {
- result[ nextIndex ] = current.Data;
- nextIndex ++;
- current = current.Next;
- }
- return result;
- }
- // Return a collection of references to the Drug items on this list which
- // can be used in a foreach loop. Understanding enumerations and the
- // 'yield return' statement is beyond the scope of the course.
- public IEnumerable< Drug > Enumeration
- {
- get
- {
- Node current = head;
- while( current != null )
- {
- yield return current.Data;
- current = current.Next;
- }
- }
- }
- }
- // -----------------------------------------------------------------------------
- // Test the linked list of Drugs.
- static class Program
- {
- static void Main( )
- {
- Stopwatch timer = new Stopwatch();
- timer.Start();
- DrugList drugs = new DrugList( "RXQT1402.txt" );
- drugs.SelectSort( );
- foreach( Drug d in drugs.ToArray( ) ) Console.WriteLine( d );
- timer.Stop();
- Console.WriteLine(timer.Elapsed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement