为什么不能更改其他实例的属性值?

时间:2019-06-01 19:48:18

标签: c#

这是我最常解决的家庭作业问题。

我被分配去创建一个Blockbuster程序的属性和功能,其中有两类:VHSTape和Blockbuster。该程序的工作方式类似于您从百视达商店租借电影的方式以及VHSTape的工作方式。商店的功能包括租借电影,退回电影,查明商店是否有电影以及检查是否可以租借电影。磁带的功能是您所期望的:播放,倒带,还有一个用于设置其出租可用性的功能。

运行程序时,我键入命令以将电影实例添加到Blockbuster的列表中,然后键入将其 Rented 属性设置为true( true ,在这种情况下,表示“不可用”)。问题就在这里。在添加到百视达列表中的第一个实例上,我只能将已出租的属性值更改为 false 。这是一些示例输入/输出:

传奇

add objName length将VHSTape实例添加到Blockbuster的列表中

find objName检查商店是否拥有电影

available objName检查商店是否有电影并且可以租借电影

rent objName将VHSTape实例的 Rented 属性设置为 true

return objName将VHSTape实例的 Rented 属性设置为 false

play objName floatNumrewind objName floatNum仅适用于租借的电影

INPUT
add The Shawshank Redemption 142
add The Godfather 175
add The Dark Knight 152
add Fight Club 139
add Se7en 127
add The Prestige 130
add Gladiator 155
find The Dark Knight        
available The Dark Knight   
rent The Dark Knight        
find The Dark Knight        
available The Dark Knight   
play The Dark Knight 100    
play The Dark Knight 100    
rewind The Dark Knight 100  
rewind The Dark Knight 100  
return The Dark Knight      
find The Dark Knight        
available The Dark Knight   
exit

OUTPUT
Store has The Dark Knight   
The Dark Knight is available    
The Dark Knight: rented     
Store has The Dark Knight   
The Dark Knight is rented   
The Dark Knight: 100        
The Dark Knight: 152        
The Dark Knight: 52     
The Dark Knight: 0      
The Dark Knight: available  
Store has The Dark Knight   
The Dark Knight is available    

所有功能都起作用,只是我不知道为什么我只能在添加到Blockbuster列表中的第一个实例上将Rented属性值更改为false。

我将自己的解决方案与同行进行了比较,我们的解决方案完全相同。

讲师提供的主程序。我编写了TODO下的代码行。

using System;
using System.Collections.Generic;
using System.Linq;

namespace Activity_4
{
    // TODO Create VHSTape class
    public class VHSTape
    {
        //To Do define properties for fields
        public string Name { set; get; }
        public float Length { set; get; }
        public bool Rented { set; get; }
        public float Progress { set; get; }

        public VHSTape(string name, float length)
        {
            //initialize constructor function for VHSTape objects
            Name = name;
            Length = length;
            Rented = false;
            Progress = 0;
        }

        public void Play(float duration)
        {
            //define method to play a movie, if duration is longer than movie length, stop at the Length of the movie.
            if (duration > Length)
                Progress = Length;
            else
                Progress = duration;
        }

        public void Rewind(float duration)
        {
            //define method for rewinding. If rewind results play time negative, stop at start of the movie, i.e. stop at "0.0" position. 
            if (Progress - duration < 0)
                Progress = 0;
            else
                Progress -= duration;
        }

        public void SetRented(bool rented)
        {
            //set a movie as rented by changing a boolean field value.
            Rented = rented;
        }
    }

    // TODO Create Blockbuster class
    public class Blockbuster
    {
        //define necessary properties for fields and the lists to track movies is already defined for you.
        public string Address { set; get; }
        public List<VHSTape> Movies = new List<VHSTape>();

        public Blockbuster(string address)
        {
            //initialize store address
            Address = address;
        }

        public void AddMovie(VHSTape movie)
        {
            //Store adds a 'movie' type. So add the move to a list. (Store's movie list)
            Movies.Add(movie);
        }

        public bool HasMovie(string name)
        {
            //Look for a movie name in the list of moves of the store.
            foreach (var movie in Movies)
            {
                if (movie.Name == name)
                    return true;
            }

            return false;
        }

        public bool IsMovieAvailable(string name)
        {
            //check if movie is available. A movie is available if store have the mivie in it's list and it is not rented yet.
            foreach (var movie in Movies)
            {
                if (movie.Name == name)
                    if (movie.Rented == false) 
                    return true;

            }

            return false;
        }

        public VHSTape Rent(string name)
        {
            //In order to rent a move, find the movie in store's movie list. If movie is there and not rented yet, set rent status True.
            //Otherwise, return null.
            foreach (var movie in Movies)
            {
                if (movie.Name == name)
                    if (movie.Rented == false)
                        movie.SetRented(true);
                        return movie;
            }

            return null;
        }

        public void Return(string name)
        {
            //In order to return a move, find the movie in store's movie list. If movie is there and rent status is true, set rent status False.
            foreach (var movie in Movies)
            {
                if (movie.Name == name)
                    if (movie.Rented == true)
                        movie.SetRented(false);
            }
        }

        public VHSTape GetRented(string name)
        {
            //This method returns already rented movie for playing or helping the Reurn method. 

            foreach (var movie in Movies)
            {
                //give logic to check if 'name' is in the list 'Movies' and the movie is already rented. 
                if (movie.Name == name)
                    if (movie.Rented == true)
                        return movie;
            }
            return null;
        }
    }

static void Main(string[] args)
        {
            var store = new Blockbuster("Calgary, Alberta, Canada.");
            string command = "";
            while (command != "exit")
            {
                command = Console.ReadLine();
                var cmdArgs = command.Split();
                if (cmdArgs.Length == 0)
                    continue;
                if (cmdArgs[0] == "add")
                { 
                    var name = string.Join(" ", cmdArgs.Skip(1).Take(cmdArgs.Length - 2));
                    var length = float.Parse(cmdArgs.Last());
                    // TODO Make new VHSTape object and add it to store
                    var movie = new VHSTape(name, length);

                    store.AddMovie(movie);
                }
                else if (cmdArgs[0] == "find")
                {
                    var name = string.Join(" ", cmdArgs.Skip(1).Take(cmdArgs.Length - 1));
                    bool hasMovie;
                    // TODO Set hasMovie to indicate if store has the given movie
                    hasMovie = store.HasMovie(name);

                    if (hasMovie)
                        Console.WriteLine("Store has " + name);
                    else
                        Console.WriteLine("Store does not have " + name);
                }
                else if (cmdArgs[0] == "available")
                {
                    var name = string.Join(" ", cmdArgs.Skip(1).Take(cmdArgs.Length - 1));
                    bool available;
                    // TODO Set available to indicate if store has the movie available to rent
                    available = store.IsMovieAvailable(name);

                    if (available)
                        Console.WriteLine(name + " is available");
                    else
                        Console.WriteLine(name + " is rented");
                }
                else if (cmdArgs[0] == "rent")
                {
                    var name = string.Join(" ", cmdArgs.Skip(1).Take(cmdArgs.Length - 1));
                    VHSTape movie;
                    // TODO Call your store's rent function and save the object in the movie variable
                    movie = store.Rent(name);

                    Console.WriteLine(name + ": " + (movie.Rented ? "rented" : "available"));
                }
                else if (cmdArgs[0] == "play")
                {
                    var name = string.Join(" ", cmdArgs.Skip(1).Take(cmdArgs.Length - 2));
                    var duration = float.Parse(cmdArgs.Last());
                    var movie = store.GetRented(name);
                    // TODO Call your VHSTape's play function
                    movie.Play(duration);

                    Console.WriteLine(movie.Name + ": " + movie.Progress);
                }
                else if (cmdArgs[0] == "rewind")
                {
                    var name = string.Join(" ", cmdArgs.Skip(1).Take(cmdArgs.Length - 2));
                    var duration = float.Parse(cmdArgs.Last());
                    var movie = store.GetRented(name);
                    // TODO Call your VHSTape's rewind function
                    movie.Rewind(duration);

                    Console.WriteLine(movie.Name + ": " + movie.Progress);
                }
                else if (cmdArgs[0] == "return")
                {
                    var name = string.Join(" ", cmdArgs.Skip(1).Take(cmdArgs.Length - 1));
                    var movie = store.GetRented(name);
                    // TODO Call your store's return function with the given movie name
                    store.Return(name);

                    Console.WriteLine(name + ": " + (movie.Rented ? "rented" : "available"));
                }
            }
        }

预期结果:

add Inception 145
add The Godfather 160
add Gladiator 130
rent The Godfather
The Godfather: rented
rent Gladiator
Gladiator: rented
rent Inception
Inception: rented

实际结果:

add Inception 145
add The Godfather 160
add Gladiator 130
rent The Godfather
The Godfather: available
rent Gladiator
Gladiator: available
rent Inception
Inception: rented

我只能租用我创建的第一个实例。

0 个答案:

没有答案