ASSIGNMENT CODE FORME

assignment3:

public class Account
{
    private const int TRANSIT_NUMBER = 314;
    private static int NEXT_ACCOUNT_NUMBER;
    public readonly string Number ;
    public double Balance { get; private set; }
    public List<string> Names { get;  private set; }
    static Account()
    {
        NEXT_ACCOUNT_NUMBER = 200_000;
    }


    private Account(string number, string name, double balance)
    {
        Number = number;
        Balance = balance;
        Names = new List<string>();
        Names.Add(name);
    }

    public static Account CreateAccount(string name, double balance = 500)
    {
        string accountNumber = string.Format("AC-{0}-{1}", TRANSIT_NUMBER, NEXT_ACCOUNT_NUMBER);
        NEXT_ACCOUNT_NUMBER++;
        return new Account(accountNumber, name, balance);
    }

    public void AddName(string name)
    {
        Names.Add(name);
    }

    public void Deposit(double amount)
    {
        Balance += amount;
    }

    public void Withdraw(double amount)
    {
        if (amount > Balance)
        {
            Console.WriteLine("Insufficient balance.");
            return;
        }
        Balance -= amount;
    }

    public override string ToString()
    {
        string namesString = string.Join(", ", Names);
        return $"Account Number: {Number}\nBalance: {Balance:C}\nNames: {namesString}\n";
    }
}

B=================================================================

using System;
using System.Collections.Generic;
using System.IO;
[Flags]
public enum SongGenre
{
    Unclassified = 0,
    Pop = 0b1,
    Rock = 0b10,
    Blues = 0b100,
    Country = 0b1_000,
    Metal = 0b10_000,
    Soul = 0b100_000
}

public class Song
{
    public string Artist { get; }

    public string Title { get; }

    public double Length { get; }

    public SongGenre Genre { get; }

    public Song(string title, string artist, double length, SongGenre genre)
    {
        Title = title;

        Artist = artist;

        Length = length;

        Genre = genre;
    }

    public override string ToString()
    {
        return $"Title: {Title}\nArtist: {Artist}\nLength: {Length}\nGenre: {Genre}";
    }
}

public static class Library
{
    private static List<Song> songs;
    public static void LoadSongs(string fileName)
    {
        songs = new List<Song>();
        using (StreamReader reader = new StreamReader(fileName))
        {
            while (!reader.EndOfStream)
            {
                string title = reader.ReadLine();
                string artist = reader.ReadLine();
                double length = Convert.ToDouble(reader.ReadLine());
                SongGenre genre = (SongGenre)Enum.Parse(typeof(SongGenre), reader.ReadLine());
                Song song = new Song(title, artist, length, genre);
                songs.Add(song);
            }
        }
    }

    public static void DisplaySongs()
    {
        foreach (Song song in songs)
        {
            Console.WriteLine(song);
        }
    }

    public static void DisplaySongs(double longerThan)
    {
        foreach (Song song in songs)
        {
            if (song.Length > longerThan)

                Console.WriteLine(song);
        }
    }

    public static void DisplaySongs(SongGenre genre)
    {
        foreach (Song song in songs)
        {
            if (song.Genre.HasFlag(genre))
                Console.WriteLine(song);
        }
    }

    public static void DisplaySongs(string artist)
    {
        foreach (Song song in songs)
        {
            if (song.Artist.Equals(artist, StringComparison.OrdinalIgnoreCase))
                Console.WriteLine(song);
        }
    }
}

SAMPLE1===========================================================

public class Prescription
{
    //field:
    private static int CURRENT_NUMBER = 100;
    public readonly string ID;

    //properties:
    public bool ForRepeat { get; private set; }
    public string Doctor { get; private set; }
    public string Name { get; private set; }
    public int Length { get; private set; }

    //constructor:
    public Prescription(string doctor, string name, int length, bool forRepeat = false)
    {
        //前面是名字后面是形式参数
        Doctor = doctor;
        Name = name;
        Length = length;
        ForRepeat = forRepeat;

        ID = CURRENT_NUMBER.ToString();
        CURRENT_NUMBER++;

        // Additional logic if needed
    }

    public override string ToString()
    {
        string repeatStatus = ForRepeat ? "Repeat" : "Not Repeat";

        return $"#{ID} {Name}  prescribed by{ Doctor} for{Length} days ({repeatStatus})";
    }
}

第二题
public class Patient
{
    //field
    private List<Prescription> prescriptions;

    //properties:
    public string Name { get; private set; }
    public int Yob { get; private set; }

    public Patient(string name, int yob)
    {
        Name = name;
        Yob = yob;
        prescriptions = new List<Prescription>();
    }

    public void AddPrescription(Prescription prescription)
    {
        prescriptions.Add(prescription);
    }

    public void RemovePrescription(string id)
    {
        bool removed = false;
        for (int i = 0; i < prescriptions.Count; i++)
        {
            if (prescriptions[i].ID == id)
            {
                prescriptions.RemoveAt(i);
                removed = true;
                break;
            }
        }

        if (!removed)
        {
            throw new Exception("Prescription not found.");
        }
    }

    private string GetPrescriptions()
    {
        string result = "";
        foreach (Prescription prescription in prescriptions)
        {
            result += prescription.ToString() + "\n";
        }
        return result;
    }

    public override string ToString()
    {
        return $"{Name} {Yob}\nList of Prescriptions:\n{GetPrescriptions()}";
    }

    public void SaveAsText(string filename)
    {
        using (StreamWriter writer = new StreamWriter(filename))
        {
            foreach (Prescription prescription in prescriptions)
            {
                writer.WriteLine(prescription.ToString());
                writer.WriteLine();
            }
        }
    }

SAMPLE2=============================================================

第一题
using System;

public enum PassType
{
    LifetimePass,
    DayPass,
    SeasonPass
}

public class Reservation
{
    public int DriverID { get; }
    public string Name { get; }
    public PassType Type { get; }
    public bool PassStatus { get; private set; }
    public double PassPrice
    {
        get
        {
            switch (Type)
            {
                case PassType.LifetimePass:
                    return 750;
                case PassType.DayPass:
                    return 50;
                case PassType.SeasonPass:
                    return 20;
                default:
                    throw new InvalidOperationException("Invalid pass type.");
            }
        }
    }

    public Reservation(int id, string name, PassType type)
    {
        DriverID = id;
        Name = name;
        Type = type;
        PassStatus = true;
    }

    public void UpdatePassStatus(bool activationStatus)
    {
        PassStatus = activationStatus;
        Console.WriteLine("Reservation information updated:");
        Console.WriteLine(ToString());
    }

    public override string ToString()
    {
        return $"Reservation ID: {DriverID}\nName: {Name}\nType: {Type}\nPass Status: {PassStatus}\nPass Price: {PassPrice}";
    }
}


第二题
using System;
using System.Collections.Generic;
using System.IO;

public class ReservationManager
{
    public int ID { get; }
    public List<Reservation> ReservationList { get; }

    public ReservationManager(int id)
    {
        ID = id;
        ReservationList = new List<Reservation>();

        try
        {
            string filePath = "Manager_Reservation.txt";
            string[] lines = File.ReadAllLines(filePath);

            foreach (string line in lines)
            {
                string[] parts = line.Split(',');

                if (parts.Length == 4 && int.Parse(parts[0]) == ID)
                {
                    int reservationId = int.Parse(parts[1]);
                    string name = parts[2];
                    PassType type = Enum.Parse<PassType>(parts[3]);

                    Reservation reservation = new Reservation(reservationId, name, type);
                    ReservationList.Add(reservation);
                }
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Manager_Reservation.txt file not found.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }

    public void AddReservation(int id, string name, PassType type)
    {
        Reservation reservation = new Reservation(id, name, type);
        ReservationList.Add(reservation);

        Console.WriteLine("New reservation added:");
        Console.WriteLine(reservation.ToString());
    }

    public void ShowAll()
    {
        Console.WriteLine($"Reservation details for Manager ID {ID}:");
        foreach (Reservation reservation in ReservationList)
        {
            Console.WriteLine(reservation.ToString());
            Console.WriteLine();
        }
    }

    public void ShowAll(PassType type)
    {
        Console.WriteLine($"Reservation details for Manager ID {ID} (Pass Type: {type}):");
        foreach (Reservation reservation in ReservationList)
        {
            if (reservation.Type == type)
            {
                Console.WriteLine(reservation.ToString());
                Console.WriteLine();
            }
        }
    }
}

ReservationManager manager = new ReservationManager(498);

manager.AddReservation(123, "John Doe", PassType.LifetimePass);
manager.AddReservation(456, "Jane Smith", PassType.DayPass);

manager.ShowAll();
manager.ShowAll(PassType.LifetimePass);


第三题
static void TestReservation()
{
    Console.WriteLine($"{new string('-', 26)} Reservation Application {new string('-', 26)}");

    ReservationManager branch1 = new ReservationManager(729);
    branch1.ReservationList.Add(new Reservation(546, "Alex Du", PassType.DayPass));
    branch1.ShowAll();
    branch1.AddReservation(921, "Dolly Lively", PassType.SeasonPass);
    branch1.ReservationList[0].UpdatePassStatus(false);
    branch1.ShowAll();
    branch1.ShowAll(PassType.SeasonPass);

    Console.WriteLine();

    ReservationManager branch2 = new ReservationManager(498);
    branch2.ReservationList.Add(new Reservation(576, "Dale", PassType.SeasonPass));
    branch2.ShowAll();
    branch2.AddReservation(847, "Jack Gibbs", PassType.DayPass);
    branch2.ReservationList[1].UpdatePassStatus(false);
    branch2.ShowAll();
    branch2.ShowAll(PassType.DayPass);

    Console.WriteLine($"{new string('-', 75)}");
    Console.ReadKey();
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值