简体   繁体   中英

Polymorphism and boxes

I am trying to get round a rather annoying issue in my homework.

Basically the task is to create a fake ordering UI where the user puts in some variables about the type of box they need to order and then presses a button. Behind the scenes the app should be validating which pipe fits the users needs and then instantiate a new box object and execute the methods inside the new object.

I am basically struggling to find a way that isn't stupid which allows for the UI to validate a choice before creating a new object, a method is given to us to begin with and is referred to a 'brute force method' which has a massive if statement inside the button click which does the checking there, now I am 100% sure there is an easier way to do it although the only thing I can come up with is holding constants or statics in a class and checking each class before creating one.

Scenario: The idea is that each box the company sells has certain features (thickness, laminated, colour and other things) while others boxes don't, I need to be able to figure out when the order button is clicked what box the order fits once I know the box type that the order fits I should create a new object of that box and run the cost() method, if it doesn't fit any box the company sells then I should prompt the user. The program must use abstraction.

The class isn't at a high level at the moment so I can't use enums and lookup tables which is causing me problems.

Thanks for any help in advance.

在我看来,他们只想让您展示几个类(每种类型的盒子一个),它们都继承或实现了一个通用的接口,例如“ getWidth”,“ getHeight”等方法。您可以编写一个简单的循环来遍历箱型实例的集合,在返回兼容箱型列表之前评估其适用性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM