2019-10-3 Javase例题

图书管理系统(面向过程)

import java.util.Scanner;

public class Test5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] bno = new int[100];
		String [] bname = new String[100];
		double [] bprice = new double[100];
		Scanner input = new Scanner(System.in);
		System.out.println("****************菜单****************");
		System.out.println(" 1.添加;2修改;3.删除;4.查询;5.退出;");
		System.out.println("**********************************");
		int no;
		int index = 0;
		do{
			System.out.print("--请输入选择:");
			no = input.nextInt();
			switch(no){
			case 1:
				
				for(int i = 0;i < bno.length; i++){
					if(bno[i] == 0){
						index = i;
						break;
					}
				}
				System.out.print("输入图书编号:");
				bno[index] = input.nextInt();
				System.out.print("输入图书名称:");
				bname[index] = input.next();
				System.out.print("输入图书价格:");
				bprice[index] = input.nextDouble();
				System.out.println("添加成功!");
				break;
			case 2:
				System.out.print("输入要修改图书编号:");
				int bno1 = input.nextInt();
				for(int i = 0;i < bno.length; i++){
					if(bno[i] == bno1){
						index = i;
						break;
					}
				}
				System.out.print("输入新的图书名称:");
				bname[index] = input.next();
				System.out.print("输入新的图书价格:");
				bprice[index] = input.nextDouble();
				System.out.println("修改成功!");
				break;
			case 3:
				System.out.print("输入要删除图书编号:");
				int bno2 = input.nextInt();
				for(int i = 0;i < bno.length; i++){
					if(bno[i] == bno2){
						index = i;
						break;
					}
				}
				for(int i = index+1;i<bno.length;i++){
					bno[i-1] = bno[i];
					bname[i-1] = bname[i];
					bprice[i-1] = bprice[i];
				}
				System.out.println("删除成功");
				break;
			case 4:
				System.out.println("编号\t名称\t单价");
				for(int i = 0; i<bno.length; i++){
					if(bno[i] == 0)
						break;
					System.out.println(bno[i]+"\t"+bname[i]+"\t"+bprice[i]);
				}
				break;
			case 5:
				return;
			}
		}while(true);
	}

}

图书管理系统(面向对象)

import java.util.Scanner;

class Book{
	private int no;
	private String name;
	private double price;
	
	public Book() {
	}
	public Book(int no, String name, double price) {

		this.no = no;
		this.name = name;
		this.price = price;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String show() {
		return no + "\t" + name + "\t" + price;
	}
}
class ManagerBook{
	private Book[] books = new Book[100];
	public void add(Book book) {
		for(int i = 0; i < books.length; i++) {
			if(books[i] == null) {
				books[i] = book;
				System.out.println("添加成功");
				break;
			}
		}
	}
	public void update(int no,double price) {
		for(int i = 0; i < books.length; i++) {
			if(books[i].getNo() == no) {
				books[i].setPrice(price);
				System.out.println("修改成功");
				break;
			}
		}
	}
	public void delete(int no) {
		for(int i = 0; i < books.length; i++) {
			if(books[i].getNo() == no) {
				for(int j = i; j < books.length-1; j ++) {
					books[j] = books[j+1];
				}
				if(books[books.length-1] != null) {
					books[books.length-1] = null;
				}
				System.out.println("修删除成功");
				break;
			}
		}
	}
	public void query() {
		System.out.println("图书编号\t图书名称\t图书价格");
		for(Book b : books) {
			if(b == null)
				break;
			System.out.println(b.show());
		}
	}
}
public class TestBook {

	public void startMenu() {
		Scanner input = new Scanner(System.in);
		ManagerBook mgr = new ManagerBook();
		System.out.println("\t\t-菜单-");
		System.out.println("1.添加;2.修改;3.删除;4.查询;5.退出\n");
		int menuNo;
		int no;
		String name;
		double price;
		while(true) {
			System.out.println("-- 输入选择菜单:");
			menuNo = input.nextInt();
			switch(menuNo) {
			case 1:
				System.out.println("-- 输入图书编号:");
				no = input.nextInt();
				System.out.println("-- 输入名称:");
				name = input.next();
				System.out.println("-- 输入价格:");
				price = input.nextDouble();
				Book book = new Book(no, name, price);
				mgr.add(book);
				break;
			case 2:
				System.out.println("-- 输入要修改的图书编号:");
				no = input.nextInt();
				System.out.println("-- 输入新的价格:");
				price = input.nextDouble();
				mgr.update(no, price);
				break;
			case 3:
				System.out.println("-- 输入要删除的图书编号:");
				no = input.nextInt();
				mgr.delete(no);
				break;
			case 4:
				mgr.query();
				break;
			case 5:
				System.out.println("退出图书系统");
				System.exit(0);
			}
		}
	}
	public static void main(String[] args) {
		new TestBook().startMenu();

	}

}

猜拳游戏

/**机器类*/
public class Computer {
	/**属性*/
	private String name = "匿名";
	private int score = 0;;
	/**封装*/
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	/**出拳*/
	public int showFist() {
		int show = (int) (Math.random() * 3 + 1); // 产生随机数,表示电脑出拳
		switch (show) {
		case 1:
			System.out.println("电脑出拳: 剪刀");
			break;
		case 2:
			System.out.println("电脑出拳: 石头");
			break;
		case 3:
			System.out.println("电脑出拳: 布");
			break;
		}
		return show;
	}
	/**选角色*/
	public void showRole() {
		int role = (int) (Math.random() * 3 + 1);
		if (role == 1) {
			name = "吸血鬼";
		} else if (role == 2) {
			name = "狼人";
		} else if (role == 3) {
			name = "变形金刚";
		}

	}
}
import java.util.Scanner;
/**人类*/
public class Person {
	/**属性*/
	private String name = "匿名";
	private int score = 0;
    /**封装*/
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}
	/**出拳*/
	public int showFist() {
		Scanner input = new Scanner(System.in);
		System.out.print("\n请出拳:1.剪刀 2.石头 3.布 (输入相应数字) :");
		int show = input.nextInt();
		switch (show) {
		case 1:
			System.out.println("你出拳: 剪刀");
			break;
		case 2:
			System.out.println("你出拳: 石头");
			break;
		case 3:
			System.out.println("你出拳: 布");
			break;
		}
		return show;
	}
	/**选角色*/
	public void showRole() {
		Scanner input = new Scanner(System.in);
		int role = input.nextInt();
		if (role == 1) {
			name = "吸血鬼";
		} else if (role == 2) {
			name = "狼人";
		} else if (role == 3) {
			name = "变形金刚";
		}
	}
}
import java.util.Scanner;
/**游戏类*/
public class Game {
	private Person person;       //甲方
	private Computer computer;   //乙方
	private int count;           //对战轮数
	
    /**初始化*/
    public Game(){
    	person = new Person();
    	computer = new Computer();
    	count = 0;
    }
		
    /**开始游戏*/
	public void startGame() {
		Scanner input = new Scanner(System.in);
		System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------\n");
		System.out.println("\n\t\t******************");
		System.out.println  ("\t\t**  猜拳, 开始       **");
		System.out.println  ("\t\t******************");
		
		System.out.println("\n\n出拳规则:1.剪刀 2.石头 3.布\n");
		/*选择双方角色*/
		System.out.print("请为自己选择角色(1:吸血鬼 2:狼人 3:变形金刚): ");
		person.showRole();
		System.out.print("我选择的角色是:"+person.getName());
		computer.showRole();
		System.out.println("\n\n计算机随机选择的角色是:"+computer.getName());
		System.out.println();
		
		System.out.print("\n要开始吗?(y/n) ");
		String con = input.next();
		int perFist;   //用户出的拳
		int compFist;  //计算机出的拳
		while(con.equals("y")){
	        /*出拳*/
			perFist = person.showFist();
			compFist = computer.showFist();
			/*裁决*/
			if(perFist == compFist){
				System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧 !\n");  //平局
			}else if((perFist == 1 && compFist == 3) || (perFist == 2  && compFist == 1) || (perFist == 3 && compFist == 2)){
				System.out.println("结果(人): "+person.getName()+"恭喜, 你赢了!");  //用户赢
				person.setScore(person.getScore()+1);//给人加分
			
			}else{
				System.out.println("结果(机器):"+computer.getName()+"你赢了!\n");  //计算机赢
				computer.setScore(computer.getScore()+1);//给机器加分
			}
			count++;//计算轮数 
			System.out.print("\n是否开始下一轮(y/n):  ");
			con = input.next();
		}
		/*显示结果*/
		showResult();
	}
	
	/** 显示比赛结果*/
	public void showResult(){
		/*显示最后结果*/
		System.out.println("---------------------------------------------------");
		System.out.println("(机器)"+computer.getName() + "  VS  (人)" + person.getName());
		System.out.println("对战次数:"+ count);
		int result = calcResult();
		if(result == 1){
			System.out.println("结果:打成平手,下次再和你一分高下!");
		}else if(result == 2){
			System.out.println("结果:恭喜恭喜!(人)"+person.getName()+"获胜");   //用户获胜
		}else{ 
			System.out.println("结果:(机器)"+computer.getName()+"获胜");   //计算机获胜
		}
		System.out.println("---------------------------------------------------");
	}
	
	/** 计算比赛结果*/
    public int calcResult(){
   
    	if(person.getScore() == computer.getScore()){
    		  return 1; 
    	}else if(person.getScore() > computer.getScore()){
    		  return 2;
    	}else{
    		  return 3;
    	}
    	
    }
}
public class StartGuess {
	public static void main(String[] args) {
		Game game = new Game();
    	game.startGame();
	}
}

集合list购物车

public class Goods {
	private int no;
	private String name;
	private double price;
	private int count;
	
	public Goods() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Goods(int no, String name, double price, int count) {
		super();
		this.no = no;
		this.name = name;
		this.price = price;
		this.count = count;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return no + "\t" + name + "\t" + price + "\t" + count;
	}
	
}
import java.util.ArrayList;
import java.util.List;

/**购物车*/
public class CartOperator {
	//车
	private List<Goods> list = new ArrayList<>();
	//添加
	public void add(Goods g) {
		list.add(g);
		System.out.println("添加成功");
	}
	//修改
	public void update(int no,int newcount) {
		for(Goods g : list) {
			if(g.getNo() == no) {
				g.setCount(newcount);
				System.out.println("修改成功");
				break;
			}
		}
	}
	//删除
	public void delete(int no) {
		for(Goods g : list) {
			if(g.getNo() == no) {
				list.remove(g);
				System.out.println("删除成功");
				break;
			}
		}
	}
	//查询
	public void select() {
		System.out.println("商品编号\t商品名称\t商品价格\t商品个数");
		//遍历集合
		list.forEach(System.out::println);
	}
	
}
import java.util.Scanner;

public class TestCart {
	//车
	CartOperator cart = new CartOperator();
	public void startMenu() {
		System.out.println("\t-菜单-");
		System.out.println("  1.添加;2.修改;3.删除;4.查询;5.退出\n");
		Scanner input = new Scanner(System.in);
		int menuNo;
		int no;
		String name;
		double price;
		int count;
		
		while(true) {
			System.out.println("-- 选择菜单项:");
			menuNo = input.nextInt();
			switch(menuNo) {
			case 1:
				//add
				System.out.println("--输入编号:");
				no = input.nextInt();
				System.out.println("--输入名称:");
				name = input.next();
				System.out.println("--输入价格");
				price = input.nextDouble();
				System.out.println("--输入个数");
				count = input.nextInt();
				Goods g = new Goods(no, name, price, count);
				cart.add(g);//add
				break;
			case 2:
				//update
				System.out.println("--输入编号:");
				no = input.nextInt();
				System.out.println("--输入新的个数");
				count = input.nextInt();
				cart.update(no,count);//update
				break;
			case 3:
				//delete
				System.out.println("--输入编号:");
				no = input.nextInt();
				cart.delete(no);//delete
				break;
			case 4:
				//select
				cart.select();
				break;
			case 5:
				System.out.println(" 退出应用程序");
				System.exit(0);
			}
		}
		
	}
	public static void main(String[] args) {
		new TestCart().startMenu();

	}

}

Map购物车

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

class Goods{
	private int no;
	private String name;
	private double price;

	public Goods() {
	}
	public Goods(int no, String name, double price) {
		this.no = no;
		this.name = name;
		this.price = price;

	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}	
}
class Database{
	private static Goods g1 = new Goods(1,"aa",11.1);
	private static Goods g2 = new Goods(2,"bb",22.2);
	private static Goods g3 = new Goods(3,"cc",33.3);
	private static Goods g4 = new Goods(4,"dd",44.4);
	private static Goods g5 = new Goods(5,"ee",55.5);
	public static Goods [] gs = {g1,g2,g3,g4,g5};
	
}
class OperatorCar{
	Map<Goods,Integer> car = new HashMap<>();
	public void add(Goods g,Integer count) {
		car.put(g, count);
	}
	public void modify(Goods g,Integer countnew) {
		if(car.containsKey(g)) {
			car.put(g, countnew);
		}else {
			System.out.println("商品不存在");
		}
	}
	public void delete(Goods g) {
		if(car.containsKey(g)) {
			car.remove(g);
		}else {
			System.out.println("商品不存在");
		}
	}
	public void query() {
		System.out.println("编号\t名称\t单价\t个数");
		Set<Entry<Goods,Integer>> set = car.entrySet();
		Iterator<Entry<Goods,Integer>> i = set.iterator();
		while(i.hasNext()) {
			Entry<Goods,Integer> e = i.next();
			Goods g = e.getKey();
			Integer c = e.getValue();
			System.out.println(g.getNo()+"\t"+g.getName()+"\t"+g.getPrice()+"\t"+c);
		}
	}
	public void menu() {
		Scanner input = new Scanner(System.in);
		int gno,gcount;
		int n ;
		Goods[] gs = Database.gs;
		System.out.println("**************菜单****************** ");
		System.out.println("  1.加;2.改;3.删;4.查;5;退");
		System.out.println("***********************************");
		System.out.println("\t编号\\t名称\\t单价");
		for(Goods g:gs) {
			System.out.println("\t"+g.getNo()+"\t"+g.getName()+"\t"+g.getPrice());
		}
		System.out.println("*************************************");
		Goods g;
		do{
			System.out.print("* 输入菜单选择:") ;
			n = input.nextInt();
			switch(n){
			case 1:
				System.out.print("* 输入选择的商品编号:");
				gno = input.nextInt();
				System.out.print("* 输入购买的个数:"); 
				gcount = input.nextInt();
				g = Database.gs[gno-1];
				this.add(g, gcount);
				break;
			case 2:
				System.out.print("* 输入修改的商品编号:");
				gno = input.nextInt();
				System.out.print("* 输入新的个数:"); 
				gcount = input.nextInt();
				g = Database.gs[gno-1];
				this.modify(g, gcount);
				break;
			case 3:
				System.out.print("* 输入删除的商品号:");
				gno = input.nextInt();
				g = Database.gs[gno-1];
				this.delete(g);
				break;
			case 4:
				this.query();
				break;
			case 5:
				System.out.println("退出");
				return;
			}
		}while(true);
	}
}

public class TestMapCar {

	public static void main(String[] args) {
		new OperatorCar().menu();

	}

}

多线程聊天

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
class SocketR implements Runnable{
	private Socket socket;
	SocketR(Socket socket){
		this.socket = socket;
	}
	public void run() {
		try {
			InputStream is= socket.getInputStream();
			Scanner in = new Scanner(is);
			String sr;
			while(in.hasNext()) {
				sr = in.nextLine();
				System.out.println("服务器:" + sr);
				if(sr.equals("end")) break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
class SocketW implements Runnable{
	private Socket socket;
	SocketW(Socket socket){
		this.socket = socket;
	}
	public void run(){
		try {
			OutputStream os = socket.getOutputStream();
			PrintWriter out = new PrintWriter(os,true);
			Scanner input = new Scanner(System.in);
			String sw;
			while(true) {
//				System.out.println("客户端:");//不提示
				sw = input.next();
				out.println(sw);
				if(sw.equals("end")) break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
public class Client {
	public static void main(String[] args) throws Exception {
		Socket socket = new Socket(InetAddress.getLocalHost(), 5678);
		System.out.println("------客户端-------");
		SocketR sr = new SocketR(socket);
		SocketW sw = new SocketW(socket);
		Thread tr = new Thread(sr);
		Thread tw = new Thread(sw);
		tr.start();
		tw.start();
		
//		socket.close();//不关
	}

}

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
class ServerR implements Runnable{
	private Socket socket;
	ServerR(Socket socket){
		this.socket = socket;
	}
	public void run() {
		try {
			InputStream is= socket.getInputStream();
			Scanner in = new Scanner(is);
			String sr;
			while(in.hasNext()) {
				sr = in.nextLine();
				System.out.println("客户端:" + sr);

				if(sr.equals("end")) break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
class ServerW implements Runnable{
	private Socket socket;
	ServerW(Socket socket){
		this.socket = socket;
	}
	public void run(){
		try {
			OutputStream os = socket.getOutputStream();
			PrintWriter out = new PrintWriter(os,true);
			Scanner input = new Scanner(System.in);
			String sw;
			out.println("客户端连接服务器成功");//写
			while(true) {
//				System.out.println("服务器:");//不提示
				sw = input.next();
				out.println(sw);
				if(sw.equals("end")) break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
public class Server {

	public static void main(String[] args) throws Exception {
		ServerSocket server = new ServerSocket(5678);
		Socket socket = server.accept();
		System.out.println("------服务器-------");
		ServerR sr = new ServerR(socket);
		ServerW sw = new ServerW(socket);
		Thread tr = new Thread(sr);
		Thread tw = new Thread(sw);
		tr.start();
		tw.start();
		
//		socket.close();//不关
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值