A
A
Amir Kenesbay2021-10-31 20:54:46
Java
Amir Kenesbay, 2021-10-31 20:54:46

How to make code in MVC style?

I have a study assignment. The question arises how to do all this in the MVC architectural style? Below I have also indicated not implemented classes and interface. I can’t understand what and how to implement certain classes, what and where to call, and so on. Description:

Knight. Determine the hierarchy of the knight's ammunition. Operations with a knight:

Display the characteristics of the knight
Display a list of the knight's ammunition
Equip the knight, which should be reflected in his characteristics (for example, weight, attack power, etc.)
Sorting the ammunition based on one of the parameters
Find the elements of ammunition that correspond to the specified range of parameters.

I/O Example

Main menu:
1. Print knight stats
2. Show ammunition
3. Equip ammunition
4. Sort ammunition
5. Search ammunition
6. Exit
Choose option:
2
Sword{damage=10, weight=20, cost=30}
Helmet{protection=30, weight=10, cost=20}
Main menu:
1. Print knight stats
2. Show ammunition
3. Equip ammunition
4. Sort ammunition
5. Search ammunition
6. Exit
Choose option:
3
What kind of ammunition do you want to equip?
1. Sword
2. Helmet
Choose option:
1
Input sword weight:
100
Input sword cost:
2
Input sword damage:
30
Main menu:
1. Print knight stats
2. Show ammunition
3. Equip ammunition
4. Sort ammunition
5. Search ammunition
6. Exit
Choose option:
2
Sword{damage=10, weight=20, cost=30}
Helmet{protection=30, weight=10, cost=20}
Sword{damage=30, weight=100, cost=2}
Main menu:
1. Print knight stats
2. Show ammunition
3. Equip ammunition
4. Sort ammunition
5. Search ammunition
6. Exit
Choose option:
1
Ammunition cost: 52
Ammunition weight: 130
Ammunition damage: 40
Ammunition protection: 30
Main menu:
1. Print knight stats
2. Show ammunition
3. Equip ammunition
4. Sort ammunition
5. Search ammunition
6. Exit
Choose option:
4
Choose sort type:
1. Cost
2. Weight
Choose option:
2
Helmet{protection=30, weight=10, cost=20}
Sword{damage=10, weight=20, cost=30}
Sword{damage=30, weight=100, cost=2}
Main menu:
1. Print knight stats
2. Show ammunition
3. Equip ammunition
4. Sort ammunition
5. Search ammunition
6. Exit
Choose option:
5
Choose search field:
1. Cost
2. Weight
Choose option:
1
Input minimum cost: 
0
Input maximum cost: 
5
Sword{damage=30, weight=100, cost=2}
Main menu:
1. Print knight stats
2. Show ammunition
3. Equip ammunition
4. Sort ammunition
5. Search ammunition
6. Exit
Choose option:
6
Bye!


public class KnightApplication {

    public static void main(String[] args) {
        KnightApplication application = new KnightApplication();
        application.start();
    }

    private void start() {
        throw new UnsupportedOperationException("You need to implement this method");
    }

}


public class Knight {

    private Ammunition[] ammunition;

    public Ammunition[] getAmmunition() {
        throw new UnsupportedOperationException("You need to implement this method");
    }

    /**
     * Add new ammunition element to knight
     * @param element that should be equipped to the knight
     */
    public void equip(Ammunition element) {
        throw new UnsupportedOperationException("You need to implement this method");
    }

    public int calculateAmmunitionWeight() {
        throw new UnsupportedOperationException("You need to implement this method");
    }

    public int calculateAmmunitionCost() {
        throw new UnsupportedOperationException("You need to implement this method");
    }

    public int calculateAmmunitionDamage() {
        throw new UnsupportedOperationException("You need to implement this method");
    }

    public int calculateAmmunitionProtection() {
        throw new UnsupportedOperationException("You need to implement this method");
    }

}


public class KnightGenerator {

    /**
     * Use it to quickly generate knight
     * @return knight
     */
    public static Knight generateKnight() {
        throw new UnsupportedOperationException("You need to implement this method");
    }


public interface Ammunition {

    int getWeight();

    int getCost();

}


public class ConsoleView {
    
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-11-01
Hasanly @azerphoenix

Good afternoon.

public class Knight {
    private Ammunition[] ammunition;

Probably, instead of an array, it makes sense to use something from jcf. For example, List.
What is MVC - model, view, controller
Controller
Create some controllers. For example. MenuController, inside which there will be methods for displaying menus and other methods for working with menus
throw new UnsupportedOperationException("You need to implement this method");

If you want the user to implement this method, then instead of throwing an exception, you can use an abstract class, and leave the method body empty. Well, or use the interface.
Model
In this case, you have the necessary models (entities). For example, Knight, Ammunition
View
Well, here you can, for example, connect AsciiTable for beauty and display menu items in the form of a beautiful table or, for example, add a progress bar to a console application, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question