景区门票java代码,java景区售票系统
求助java编程解答!!!!!!!!!!!!!!!!!!!!!!!!!
既然用JAVA,那就来个面向对象的分析
目前创新互联建站已为近1000家的企业提供了网站建设、域名、虚拟空间、绵阳服务器托管、企业网站设计、城关网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
关键处: 需要一个"选择器"来依据数值区间得到一个反馈值,
定义接口
public interface Responsor {
public Object response();
}
定义实现
final public class PriceResponsor implements Responsor{
private int ticketsCount;
public PriceResponsor(int ticketsCount) {
this.ticketsCount = ticketsCount;
}
/**
* business implement
* @return the unit price according as tickets total number
*/
public Integer response() {
if (ticketsCount 0)
throw new IllegalArgumentException("negative is invalid");
if (ticketsCount 11) return 20;
if (ticketsCount = 11 ticketsCount 21) return 15;
if (ticketsCount = 21 ticketsCount 31) return 10;
if (ticketsCount = 31) return 8;
return null;
}
public Integer getTotalPrice() {
Integer I = response();
if (I == null) return null;
return ticketsCount*I;
}
}
演示代码:(从命令行输入一个整数表示票的张数)
final public class TicketPriceDemo {
private static final String USAGE = "TicketPriceDemo [a integer represents the ticketsCount]";
private static final String PARAMETER_TYPE="Parameter should be a integer";
public static void main(String[] args) {
if (args.length != 1) {
System.out.println(USAGE);
System.exit(1);
}
int c = 0;
try {
c = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.out.println(PARAMETER_TYPE+"\r\n"+USAGE);
System.exit(1);
}
PriceResponsor responsor = new PriceResponsor(c);
System.out.println("unit price : " + responsor.response());
System.out.println("total price:" + responsor.getTotalPrice());
}
}
java编程实现景区售票规则:成人票每人10元,儿童票折半(如果儿童身高不超过1.2
这个规则就是设计一个方法,该方法最后值为一个票价,参数应该是身高,所以有如下代码
public class PriceTest {
public static void main(String[] args) {
int childPrice = getPrice(1.2);
int adultPrice = getPrice(1.5);
System.out.println("1.2米身高的票价为:"+childPrice);
System.out.println("1.5米身高的成人票价为:"+adultPrice);
}
private static int getPrice(double height) {
return height 1.2?10:5;
}
}
最后结果如下:
java多线程的卖票问题
首先,定义的锁(lock)不对,必须是同一个锁,像你这样用this,new多少个MyThread就有多少个锁,违反了线程的同步机制;
其次,你如果想要呈现多线程的竞争,不可以在run方法里让一个线程一直运行而不释放锁,应该使用wait()/notify();
以下我稍微修改下,会形成两个线程交替执行的情形:
public class ThreadTest {
public static Object obj = new Object();
public static int number = 10;// 用static共享票源,不知道可以不,按理说是可以的
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread my = new MyThread();
MyThread my2 = new MyThread();
Thread t1 = new Thread(my2, "2号窗口");
Thread t2 = new Thread(my, "1号窗口");
t1.start();
t2.start();
}
static class MyThread implements Runnable {
public void run() {
while (true)
synchronized (obj) {
if (number 0) {
System.out.println(Thread.currentThread().getName()
+ "正在卖票," + "还剩" + number + "张票");
number--;
obj.notifyAll();
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
break;
}
}
}
}
用java模拟四个售票点,不重复的卖出100张票(票的编号为1--100),售完为止。
public class ShowDemo{
public static void main(String[] rags)throws Exception{
MyThread mt= new MyThread();
Thread th1 = new Thread(mt,"售票一");
Thread th2 = new Thread(mt,"售票二");
Thread th3 = new Thread(mt,"售票三");
Thread th4 = new Thread(mt,"售票四");
th1.start();th2.start();th3.start();th4.start();
}
}
class MyThread implements Runnable{
int ticket=1;
public void run(){
while(ticket=100){
if("售票一".equals(Thread.currentThread().getName()) ticket%2!=0){
System.out.println("售票一售出:"+ticket++);
}
if("售票二".equals(Thread.currentThread().getName()) ticket%2!=0){
System.out.println("售票二售出:"+ticket++);
}
if("售票三".equals(Thread.currentThread().getName()) ticket%2==0){
System.out.println("售票三售出:"+ticket++);
}
if("售票四".equals(Thread.currentThread().getName()) ticket%4==0){
System.out.println("售票四售出:"+ticket++);
}
}
}
}
文章题目:景区门票java代码,java景区售票系统
当前地址:http://azwzsj.com/article/phhhoj.html