Created
May 8, 2019 02:28
-
-
Save jaohaohsuan/d9f1307445f16324c7d37da0a03a4146 to your computer and use it in GitHub Desktop.
week 10 recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package comp601.week10; | |
import java.util.Arrays; | |
import java.util.List; | |
public class Main { | |
public static void main(String[] args) { | |
Ticket t1 = new Ticket("t1", 155); | |
Ticket t2 = new Ticket("t2", 245); | |
Ticket t3 = new Ticket("t3", 355); | |
List<Ticket> tickets = Arrays.asList(t1,t2,t3); | |
System.out.printf("the total airpoints of tickets is %d.\n", getTotalAirpoints(tickets, 0)); | |
} | |
public static int getTotalAirpoints(List<Ticket> tickets, int accum) { | |
if(tickets == null || tickets.size() == 0) { | |
return accum; | |
} | |
List<Ticket> tail = tickets.subList(1, tickets.size()); | |
accum += tickets.get(0).getAirpoints(); | |
return getTotalAirpoints(tail, accum); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package comp601.week10; | |
public class Ticket { | |
private String tno; | |
private double price; | |
public Ticket(String tno, double price) { | |
this.setTno(tno); | |
this.setPrice(price); | |
} | |
public String getTno() { | |
return tno; | |
} | |
public void setTno(String tno) { | |
this.tno = tno; | |
} | |
public double getPrice() { | |
return price; | |
} | |
public void setPrice(double price) { | |
this.price = price; | |
} | |
public int getAirpoints() { | |
// 3 points for every $100 | |
int p3 = ((int)price / 100) * 3; | |
// 1 points for every %50 | |
int p1 = (((int)price % 100) / 50) * 1; | |
// no points for under $50 | |
return p3 + p1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment