简体   繁体   中英

How do I add strings together with different names

I've already created the ticketAgent and ticketType class and I'm having a tough time trying to add the strings. I've tried using a wrapper class but not sure if that's even the right approach. I've pasted my code and then the prompt respectively. Thanks for the help. I'm a first time poster, so I hope I've followed the posting guidelines.

Prompt:

Write a program called TicketBooth that instantiates at 3 different ticket agents. The TicketBooth program should prompt the user for the ticket agent they wish to purchase a ticket from and then display the list of ticket types and prices that are available. The TicketBooth program should allow the user to make a selection for the ticket to purchase. After a ticket type is selected, the TicketBooth program should display the:

• total sales for that ticket agent • total number of tickets sold by the ticket agent • total number of tickets sold by all ticket agents

The TicketBooth program should continue to prompt the user to purchase tickets from one of the ticket agents until the user types quit.

 package Exercises;
    import java.util.Scanner;

    public class TicketBooth {

        private static Scanner input;

        public static void main(String[] args) {

            input = new Scanner(System.in);

                String ticketAgent = "sam", "nicole" , "alex";
                String ticketType = "one";
                int t = Integer.parseInt(ticketAgent);
                int s = Integer.parseInt(ticketType);
                int sum;

                System.out.println("Please select your ticket agent: ");
                ticketAgent = input.next();

                System.out.println("Please select your ticket type or press -1 to quit: ");
                ticketType = input.next();  

                sum = t +s;
                System.out.println("The total tickets that were sold were: " + sum);



        }
    }






package exercises;

public class TicketAgent {

private static int numOfTicksPerAgent;

    private String agentName;
    private int numSold = 0;
    private double totalSales = 0.0;

    public TicketAgent(String agent) {
        agentName = agent;

        numSold = 0;
        totalSales = 0.0;
        numOfTicksPerAgent += 1;
    }

    public void sellTicket(TicketType tt) {
        numSold++;
        totalSales = totalSales + tt.getticketPrice();
        numOfTicksPerAgent++;
    }

    public double gettotalSales() {
        return totalSales;
    }

    public double getnumSold() {
        return numSold;
    }

    public double getnumOfTicksPerAgent() {
        return numOfTicksPerAgent;

    }
}








package exercises;




public enum TicketType 
{
    CHILD (6.50),
    ADULT (9.50),
    SENIOR (6.50);

private double tikPrice;
TicketType(double price) 
{
    tikPrice = price;
}
public double getticketPrice()
{   
return tikPrice;

    }
}

The problem is, that it seems that this is your homework and nobody wants to make your homeworks for you.

edit: I saw that you added your classes just now. All you have to do is to add the do while loop and instantiate your Agents in the right way. Where, exactly, is your problem? So we can help you better.

My first approach would be, to make it simple, to write a TicketAgent class and a TicketType class .

Write the TicketAgent in a way, you can put the name of the agent and the types into the constructor .

TicketAgent agentSam = new TicketAgent("Sam", new ExpensiveTicket());

Add setter and getter methods .

And the TicketType needs a float value for the price and maybe a String description or something like that.

After you are done with this, go back to your main class. Open a do while loop and set as the loop argument a boolean value for the user abort, which is false at the beginning and changes if the user types in the quit command.

Before the loop, instantiate the Agents with the tickets.

Display names and prices. Display tickets. Count together. Ask for abort.

Hope this helps a little. And sorry for my english, it is not my mother-tongue.

    additional:



private boolean userWantsToQuit = false;
private List<TicketAgent> avaibleAgents = new ArrayList<TicketAgent>();
private List<TicketAgent> usedAgents= new ArrayList<TicketAgent>();
private List<TickeType> boughtTickets= new ArrayList<TicketType>();

main(...){

   agents.add(new TicketAgent("Sam"));
   agents.add(new TicketAgent("wise"));
   agents.add(new TicketAgent("gamgee"));

   do{

   usedAgents.add(askUserForAgent()); //extra static method
   boughtTickets.add(askUserForType(agent)); //extra static method
   userWantsToQuit = askUserToQuit();//extra static method
   }while(!userWantsToQuit);

   displayAgentSummary(usedAgents);
   displayTicketSummary(boughtTickets);
}

[...]

public static displayAgentSummary(List<TicketAgent> agents)
{
    for(TicketAgent agent : agents)
    {
       System.out.println("Served by: " + agent.getName());
    }
}

public static displayTicketSummary(List<TicketType> tickets)
{
    float totalPrice = 0;
    for(TicketType ticket : tickets)
    {
       totalPrice =+ ticket.getPrice();
       System.out.println("You bought a " + ticket.getName() + " for " + ticket.getPrice()";
    }
    System.out.println("You bought tickets in total for " + totalPrice + " dollar".
}

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